blob: 4454988fc00b317d9c8422d9a3045a139e74189e [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
Holger Schurige93156e2009-10-22 15:30:58 +020015
David Woodhouse2fd6cfe2007-12-11 17:44:10 -050016static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
Holger Schurig0d61d042007-12-05 17:58:06 +010017
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018/**
Holger Schurig8db4a2b2008-03-19 10:11:00 +010019 * @brief Simple callback that copies response back into command
20 *
21 * @param priv A pointer to struct lbs_private structure
22 * @param extra A pointer to the original command structure for which
23 * 'resp' is a response
24 * @param resp A pointer to the command response
25 *
26 * @return 0 on success, error on failure
27 */
28int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
29 struct cmd_header *resp)
30{
31 struct cmd_header *buf = (void *)extra;
32 uint16_t copy_len;
33
34 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
35 memcpy(buf, resp, copy_len);
36 return 0;
37}
38EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
39
40/**
41 * @brief Simple callback that ignores the result. Use this if
42 * you just want to send a command to the hardware, but don't
43 * care for the result.
44 *
45 * @param priv ignored
46 * @param extra ignored
47 * @param resp ignored
48 *
49 * @return 0 for success
50 */
51static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
52 struct cmd_header *resp)
53{
54 return 0;
55}
56
57
58/**
Dan Williams852e1f22007-12-10 15:24:47 -050059 * @brief Checks whether a command is allowed in Power Save mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020060 *
61 * @param command the command ID
Dan Williams852e1f22007-12-10 15:24:47 -050062 * @return 1 if allowed, 0 if not allowed
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063 */
Dan Williams852e1f22007-12-10 15:24:47 -050064static u8 is_command_allowed_in_ps(u16 cmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020065{
Dan Williams852e1f22007-12-10 15:24:47 -050066 switch (cmd) {
67 case CMD_802_11_RSSI:
68 return 1;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -070069 case CMD_802_11_HOST_SLEEP_CFG:
70 return 1;
Dan Williams852e1f22007-12-10 15:24:47 -050071 default:
72 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074 return 0;
75}
76
Dan Williams6e66f032007-12-11 12:42:16 -050077/**
Amitkumar Karwar63f275d2009-10-06 19:20:28 -070078 * @brief This function checks if the command is allowed.
79 *
80 * @param priv A pointer to lbs_private structure
81 * @return allowed or not allowed.
82 */
83
84static int lbs_is_cmd_allowed(struct lbs_private *priv)
85{
86 int ret = 1;
87
88 lbs_deb_enter(LBS_DEB_CMD);
89
90 if (!priv->is_auto_deep_sleep_enabled) {
91 if (priv->is_deep_sleep) {
92 lbs_deb_cmd("command not allowed in deep sleep\n");
93 ret = 0;
94 }
95 }
96
97 lbs_deb_leave(LBS_DEB_CMD);
98 return ret;
99}
100
101/**
Dan Williams6e66f032007-12-11 12:42:16 -0500102 * @brief Updates the hardware details like MAC address and regulatory region
103 *
104 * @param priv A pointer to struct lbs_private structure
105 *
106 * @return 0 on success, error on failure
107 */
108int lbs_update_hw_spec(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200109{
Dan Williams6e66f032007-12-11 12:42:16 -0500110 struct cmd_ds_get_hw_spec cmd;
111 int ret = -1;
112 u32 i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200113
Holger Schurig9012b282007-05-25 11:27:16 -0400114 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200115
Dan Williams6e66f032007-12-11 12:42:16 -0500116 memset(&cmd, 0, sizeof(cmd));
117 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
118 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
David Woodhouse689442d2007-12-12 16:00:42 -0500119 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
Dan Williams6e66f032007-12-11 12:42:16 -0500120 if (ret)
121 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200122
Dan Williams6e66f032007-12-11 12:42:16 -0500123 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500124
Holger Schurigdac10a92008-01-16 15:55:22 +0100125 /* The firmware release is in an interesting format: the patch
126 * level is in the most significant nibble ... so fix that: */
127 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
128 priv->fwrelease = (priv->fwrelease << 8) |
129 (priv->fwrelease >> 24 & 0xff);
130
131 /* Some firmware capabilities:
132 * CF card firmware 5.0.16p0: cap 0x00000303
133 * USB dongle firmware 5.110.17p2: cap 0x00000303
134 */
Johannes Berge1749612008-10-27 15:59:26 -0700135 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
136 cmd.permanentaddr,
Holger Schurigdac10a92008-01-16 15:55:22 +0100137 priv->fwrelease >> 24 & 0xff,
138 priv->fwrelease >> 16 & 0xff,
139 priv->fwrelease >> 8 & 0xff,
140 priv->fwrelease & 0xff,
141 priv->fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500142 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
143 cmd.hwifversion, cmd.version);
144
145 /* Clamp region code to 8-bit since FW spec indicates that it should
146 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
147 * returns non-zero high 8 bits here.
Marek Vasut15483992009-07-16 19:19:53 +0200148 *
149 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
150 * need to check for this problem and handle it properly.
Dan Williams6e66f032007-12-11 12:42:16 -0500151 */
Marek Vasut15483992009-07-16 19:19:53 +0200152 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
153 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
154 else
155 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
Dan Williams6e66f032007-12-11 12:42:16 -0500156
157 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
158 /* use the region code to search for the index */
159 if (priv->regioncode == lbs_region_code_to_index[i])
160 break;
161 }
162
163 /* if it's unidentified region code, use the default (USA) */
164 if (i >= MRVDRV_MAX_REGION_CODE) {
165 priv->regioncode = 0x10;
166 lbs_pr_info("unidentified region code; using the default (USA)\n");
167 }
168
169 if (priv->current_addr[0] == 0xff)
170 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
171
172 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
173 if (priv->mesh_dev)
174 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
175
Dan Williams6e66f032007-12-11 12:42:16 -0500176out:
Holger Schurig9012b282007-05-25 11:27:16 -0400177 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams6e66f032007-12-11 12:42:16 -0500178 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179}
180
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700181static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
182 struct cmd_header *resp)
183{
184 lbs_deb_enter(LBS_DEB_CMD);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530185 if (priv->is_host_sleep_activated) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700186 priv->is_host_sleep_configured = 0;
187 if (priv->psstate == PS_STATE_FULL_POWER) {
188 priv->is_host_sleep_activated = 0;
189 wake_up_interruptible(&priv->host_sleep_q);
190 }
191 } else {
192 priv->is_host_sleep_configured = 1;
193 }
194 lbs_deb_leave(LBS_DEB_CMD);
195 return 0;
196}
197
Anna Neal582c1b52008-10-20 16:46:56 -0700198int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
199 struct wol_config *p_wol_config)
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500200{
201 struct cmd_ds_host_sleep cmd_config;
202 int ret;
203
David Woodhouse9fae8992007-12-15 03:46:44 -0500204 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500205 cmd_config.criteria = cpu_to_le32(criteria);
David Woodhouse506e9022007-12-12 20:06:06 -0500206 cmd_config.gpio = priv->wol_gpio;
207 cmd_config.gap = priv->wol_gap;
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500208
Anna Neal582c1b52008-10-20 16:46:56 -0700209 if (p_wol_config != NULL)
210 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
211 sizeof(struct wol_config));
212 else
213 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
214
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700215 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
216 le16_to_cpu(cmd_config.hdr.size),
217 lbs_ret_host_sleep_cfg, 0);
David Woodhouse506e9022007-12-12 20:06:06 -0500218 if (!ret) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700219 if (p_wol_config)
Anna Neal582c1b52008-10-20 16:46:56 -0700220 memcpy((uint8_t *) p_wol_config,
221 (uint8_t *)&cmd_config.wol_conf,
222 sizeof(struct wol_config));
David Woodhouse506e9022007-12-12 20:06:06 -0500223 } else {
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500224 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500225 }
David Woodhouse506e9022007-12-12 20:06:06 -0500226
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500227 return ret;
228}
229EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
230
Holger Schurige98a88d2008-03-19 14:25:58 +0100231static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200232 u16 cmd_action)
233{
234 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200235
Holger Schurig9012b282007-05-25 11:27:16 -0400236 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200237
Dan Williams0aef64d2007-08-02 11:31:18 -0400238 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
David Woodhouse981f1872007-05-25 23:36:54 -0400239 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200240 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241 psm->action = cpu_to_le16(cmd_action);
242 psm->multipledtim = 0;
David Woodhouse981f1872007-05-25 23:36:54 -0400243 switch (cmd_action) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400244 case CMD_SUBCMD_ENTER_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400245 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246
Holger Schurig252cf0d2007-08-02 13:09:34 -0400247 psm->locallisteninterval = 0;
Holger Schurig97605c32007-08-02 13:09:15 -0400248 psm->nullpktinterval = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 psm->multipledtim =
Holger Schurig56c46562007-08-02 13:09:49 -0400250 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251 break;
252
Dan Williams0aef64d2007-08-02 11:31:18 -0400253 case CMD_SUBCMD_EXIT_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400254 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255 break;
256
Dan Williams0aef64d2007-08-02 11:31:18 -0400257 case CMD_SUBCMD_SLEEP_CONFIRMED:
Holger Schurig9012b282007-05-25 11:27:16 -0400258 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259 break;
260
261 default:
262 break;
263 }
264
Holger Schurig9012b282007-05-25 11:27:16 -0400265 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266 return 0;
267}
268
David Woodhouse3fbe1042007-12-17 23:48:31 -0500269int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
270 struct sleep_params *sp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271{
David Woodhouse3fbe1042007-12-17 23:48:31 -0500272 struct cmd_ds_802_11_sleep_params cmd;
273 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274
Holger Schurig9012b282007-05-25 11:27:16 -0400275 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276
Dan Williams0aef64d2007-08-02 11:31:18 -0400277 if (cmd_action == CMD_ACT_GET) {
David Woodhouse3fbe1042007-12-17 23:48:31 -0500278 memset(&cmd, 0, sizeof(cmd));
279 } else {
280 cmd.error = cpu_to_le16(sp->sp_error);
281 cmd.offset = cpu_to_le16(sp->sp_offset);
282 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
283 cmd.calcontrol = sp->sp_calcontrol;
284 cmd.externalsleepclk = sp->sp_extsleepclk;
285 cmd.reserved = cpu_to_le16(sp->sp_reserved);
286 }
287 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
288 cmd.action = cpu_to_le16(cmd_action);
289
290 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
291
292 if (!ret) {
293 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
294 "calcontrol 0x%x extsleepclk 0x%x\n",
295 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
296 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
297 cmd.externalsleepclk);
298
299 sp->sp_error = le16_to_cpu(cmd.error);
300 sp->sp_offset = le16_to_cpu(cmd.offset);
301 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
302 sp->sp_calcontrol = cmd.calcontrol;
303 sp->sp_extsleepclk = cmd.externalsleepclk;
304 sp->sp_reserved = le16_to_cpu(cmd.reserved);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305 }
306
David Woodhouse3fbe1042007-12-17 23:48:31 -0500307 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200308 return 0;
309}
310
Amitkumar Karwar49125452009-09-30 20:04:38 -0700311static int lbs_wait_for_ds_awake(struct lbs_private *priv)
312{
313 int ret = 0;
314
315 lbs_deb_enter(LBS_DEB_CMD);
316
317 if (priv->is_deep_sleep) {
318 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
319 !priv->is_deep_sleep, (10 * HZ))) {
320 lbs_pr_err("ds_awake_q: timer expired\n");
321 ret = -1;
322 }
323 }
324
325 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
326 return ret;
327}
328
329int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
330{
331 int ret = 0;
332
333 lbs_deb_enter(LBS_DEB_CMD);
334
335 if (deep_sleep) {
336 if (priv->is_deep_sleep != 1) {
337 lbs_deb_cmd("deep sleep: sleep\n");
338 BUG_ON(!priv->enter_deep_sleep);
339 ret = priv->enter_deep_sleep(priv);
340 if (!ret) {
341 netif_stop_queue(priv->dev);
342 netif_carrier_off(priv->dev);
343 }
344 } else {
345 lbs_pr_err("deep sleep: already enabled\n");
346 }
347 } else {
348 if (priv->is_deep_sleep) {
349 lbs_deb_cmd("deep sleep: wakeup\n");
350 BUG_ON(!priv->exit_deep_sleep);
351 ret = priv->exit_deep_sleep(priv);
352 if (!ret) {
353 ret = lbs_wait_for_ds_awake(priv);
354 if (ret)
355 lbs_pr_err("deep sleep: wakeup"
356 "failed\n");
357 }
358 }
359 }
360
361 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
362 return ret;
363}
364
Amitkumar Karwar13118432010-07-08 06:43:48 +0530365static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
366 unsigned long dummy,
367 struct cmd_header *cmd)
368{
369 lbs_deb_enter(LBS_DEB_FW);
370 priv->is_host_sleep_activated = 1;
371 wake_up_interruptible(&priv->host_sleep_q);
372 lbs_deb_leave(LBS_DEB_FW);
373 return 0;
374}
375
376int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
377{
378 struct cmd_header cmd;
379 int ret = 0;
380 uint32_t criteria = EHS_REMOVE_WAKEUP;
381
382 lbs_deb_enter(LBS_DEB_CMD);
383
384 if (host_sleep) {
385 if (priv->is_host_sleep_activated != 1) {
386 memset(&cmd, 0, sizeof(cmd));
387 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
388 (struct wol_config *)NULL);
389 if (ret) {
390 lbs_pr_info("Host sleep configuration failed: "
391 "%d\n", ret);
392 return ret;
393 }
394 if (priv->psstate == PS_STATE_FULL_POWER) {
395 ret = __lbs_cmd(priv,
396 CMD_802_11_HOST_SLEEP_ACTIVATE,
397 &cmd,
398 sizeof(cmd),
399 lbs_ret_host_sleep_activate, 0);
400 if (ret)
401 lbs_pr_info("HOST_SLEEP_ACTIVATE "
402 "failed: %d\n", ret);
403 }
404
405 if (!wait_event_interruptible_timeout(
406 priv->host_sleep_q,
407 priv->is_host_sleep_activated,
408 (10 * HZ))) {
409 lbs_pr_err("host_sleep_q: timer expired\n");
410 ret = -1;
411 }
412 } else {
413 lbs_pr_err("host sleep: already enabled\n");
414 }
415 } else {
416 if (priv->is_host_sleep_activated)
417 ret = lbs_host_sleep_cfg(priv, criteria,
418 (struct wol_config *)NULL);
419 }
420
421 return ret;
422}
423
Dan Williams39fcf7a2008-09-10 12:49:00 -0400424/**
425 * @brief Set an SNMP MIB value
426 *
427 * @param priv A pointer to struct lbs_private structure
428 * @param oid The OID to set in the firmware
429 * @param val Value to set the OID to
430 *
431 * @return 0 on success, error on failure
432 */
433int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434{
Dan Williams39fcf7a2008-09-10 12:49:00 -0400435 struct cmd_ds_802_11_snmp_mib cmd;
436 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437
Holger Schurig9012b282007-05-25 11:27:16 -0400438 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200439
Dan Williams39fcf7a2008-09-10 12:49:00 -0400440 memset(&cmd, 0, sizeof (cmd));
441 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
442 cmd.action = cpu_to_le16(CMD_ACT_SET);
443 cmd.oid = cpu_to_le16((u16) oid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200444
Dan Williams39fcf7a2008-09-10 12:49:00 -0400445 switch (oid) {
446 case SNMP_MIB_OID_BSS_TYPE:
447 cmd.bufsize = cpu_to_le16(sizeof(u8));
Holger Schurigfef06402009-10-22 15:30:59 +0200448 cmd.value[0] = val;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449 break;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400450 case SNMP_MIB_OID_11D_ENABLE:
451 case SNMP_MIB_OID_FRAG_THRESHOLD:
452 case SNMP_MIB_OID_RTS_THRESHOLD:
453 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
454 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
455 cmd.bufsize = cpu_to_le16(sizeof(u16));
456 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200457 break;
458 default:
Dan Williams39fcf7a2008-09-10 12:49:00 -0400459 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
460 ret = -EINVAL;
461 goto out;
462 }
463
464 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
465 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
466
467 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
468
469out:
470 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
471 return ret;
472}
473
474/**
475 * @brief Get an SNMP MIB value
476 *
477 * @param priv A pointer to struct lbs_private structure
478 * @param oid The OID to retrieve from the firmware
479 * @param out_val Location for the returned value
480 *
481 * @return 0 on success, error on failure
482 */
483int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
484{
485 struct cmd_ds_802_11_snmp_mib cmd;
486 int ret;
487
488 lbs_deb_enter(LBS_DEB_CMD);
489
490 memset(&cmd, 0, sizeof (cmd));
491 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
492 cmd.action = cpu_to_le16(CMD_ACT_GET);
493 cmd.oid = cpu_to_le16(oid);
494
495 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
496 if (ret)
497 goto out;
498
499 switch (le16_to_cpu(cmd.bufsize)) {
500 case sizeof(u8):
Holger Schurigfef06402009-10-22 15:30:59 +0200501 *out_val = cmd.value[0];
Dan Williams39fcf7a2008-09-10 12:49:00 -0400502 break;
503 case sizeof(u16):
504 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
505 break;
506 default:
507 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
508 oid, le16_to_cpu(cmd.bufsize));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509 break;
510 }
511
Dan Williams39fcf7a2008-09-10 12:49:00 -0400512out:
513 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
514 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200515}
516
Dan Williams87c8c722008-08-19 15:15:35 -0400517/**
518 * @brief Get the min, max, and current TX power
519 *
520 * @param priv A pointer to struct lbs_private structure
521 * @param curlevel Current power level in dBm
522 * @param minlevel Minimum supported power level in dBm (optional)
523 * @param maxlevel Maximum supported power level in dBm (optional)
524 *
525 * @return 0 on success, error on failure
526 */
527int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
528 s16 *maxlevel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529{
Dan Williams87c8c722008-08-19 15:15:35 -0400530 struct cmd_ds_802_11_rf_tx_power cmd;
531 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532
Holger Schurig9012b282007-05-25 11:27:16 -0400533 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534
Dan Williams87c8c722008-08-19 15:15:35 -0400535 memset(&cmd, 0, sizeof(cmd));
536 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
537 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538
Dan Williams87c8c722008-08-19 15:15:35 -0400539 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
540 if (ret == 0) {
541 *curlevel = le16_to_cpu(cmd.curlevel);
542 if (minlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100543 *minlevel = cmd.minlevel;
Dan Williams87c8c722008-08-19 15:15:35 -0400544 if (maxlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100545 *maxlevel = cmd.maxlevel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546 }
Holger Schurig9012b282007-05-25 11:27:16 -0400547
548 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams87c8c722008-08-19 15:15:35 -0400549 return ret;
550}
551
552/**
553 * @brief Set the TX power
554 *
555 * @param priv A pointer to struct lbs_private structure
556 * @param dbm The desired power level in dBm
557 *
558 * @return 0 on success, error on failure
559 */
560int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
561{
562 struct cmd_ds_802_11_rf_tx_power cmd;
563 int ret;
564
565 lbs_deb_enter(LBS_DEB_CMD);
566
567 memset(&cmd, 0, sizeof(cmd));
568 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
569 cmd.action = cpu_to_le16(CMD_ACT_SET);
570 cmd.curlevel = cpu_to_le16(dbm);
571
572 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
573
574 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
575
576 lbs_deb_leave(LBS_DEB_CMD);
577 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578}
579
Dan Williamsa45b6f42010-07-27 12:54:34 -0700580/**
581 * @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
582 *
583 * @param priv A pointer to struct lbs_private structure
584 * @param enable 1 to enable monitor mode, 0 to disable
585 *
586 * @return 0 on success, error on failure
587 */
588int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400589{
Dan Williamsa45b6f42010-07-27 12:54:34 -0700590 struct cmd_ds_802_11_monitor_mode cmd;
591 int ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400592
Dan Williamsa45b6f42010-07-27 12:54:34 -0700593 memset(&cmd, 0, sizeof(cmd));
594 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
595 cmd.action = cpu_to_le16(CMD_ACT_SET);
596 if (enable)
597 cmd.mode = cpu_to_le16(0x1);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400598
Dan Williamsa45b6f42010-07-27 12:54:34 -0700599 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
600
601 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
602 if (ret == 0) {
603 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
604 ARPHRD_ETHER;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400605 }
606
Dan Williamsa45b6f42010-07-27 12:54:34 -0700607 lbs_deb_leave(LBS_DEB_CMD);
608 return ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400609}
610
Dan Williams2dd4b262007-12-11 16:54:15 -0500611/**
612 * @brief Get the radio channel
613 *
614 * @param priv A pointer to struct lbs_private structure
615 *
616 * @return The channel on success, error on failure
617 */
Holger Schuriga3cbfb02009-10-16 17:33:56 +0200618static int lbs_get_channel(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619{
Dan Williams2dd4b262007-12-11 16:54:15 -0500620 struct cmd_ds_802_11_rf_channel cmd;
621 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622
Holger Schurig8ff12da2007-08-02 11:54:31 -0400623 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200625 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500626 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
627 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628
David Woodhouse689442d2007-12-12 16:00:42 -0500629 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500630 if (ret)
631 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200632
Dan Williamscb182a62007-12-11 17:35:51 -0500633 ret = le16_to_cpu(cmd.channel);
634 lbs_deb_cmd("current radio channel is %d\n", ret);
Dan Williams2dd4b262007-12-11 16:54:15 -0500635
636out:
637 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
638 return ret;
639}
640
Holger Schurig73ab1f22008-04-02 16:52:19 +0200641int lbs_update_channel(struct lbs_private *priv)
642{
643 int ret;
644
645 /* the channel in f/w could be out of sync; get the current channel */
646 lbs_deb_enter(LBS_DEB_ASSOC);
647
648 ret = lbs_get_channel(priv);
649 if (ret > 0) {
Holger Schurigc14951f2009-10-22 15:30:50 +0200650 priv->channel = ret;
Holger Schurig73ab1f22008-04-02 16:52:19 +0200651 ret = 0;
652 }
653 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
654 return ret;
655}
656
Dan Williams2dd4b262007-12-11 16:54:15 -0500657/**
658 * @brief Set the radio channel
659 *
660 * @param priv A pointer to struct lbs_private structure
661 * @param channel The desired channel, or 0 to clear a locked channel
662 *
663 * @return 0 on success, error on failure
664 */
665int lbs_set_channel(struct lbs_private *priv, u8 channel)
666{
667 struct cmd_ds_802_11_rf_channel cmd;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530668#ifdef DEBUG
Holger Schurigc14951f2009-10-22 15:30:50 +0200669 u8 old_channel = priv->channel;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530670#endif
Dan Williams2dd4b262007-12-11 16:54:15 -0500671 int ret = 0;
672
673 lbs_deb_enter(LBS_DEB_CMD);
674
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200675 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500676 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
677 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
678 cmd.channel = cpu_to_le16(channel);
679
David Woodhouse689442d2007-12-12 16:00:42 -0500680 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500681 if (ret)
682 goto out;
683
Holger Schurigc14951f2009-10-22 15:30:50 +0200684 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
Dan Williamscb182a62007-12-11 17:35:51 -0500685 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
Holger Schurigc14951f2009-10-22 15:30:50 +0200686 priv->channel);
Dan Williams2dd4b262007-12-11 16:54:15 -0500687
688out:
689 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
690 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691}
692
Holger Schurige98a88d2008-03-19 14:25:58 +0100693static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 u8 cmd_action, void *pdata_buf)
695{
Holger Schurig10078322007-11-15 18:05:47 -0500696 struct lbs_offset_value *offval;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697
Holger Schurig9012b282007-05-25 11:27:16 -0400698 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699
Holger Schurig10078322007-11-15 18:05:47 -0500700 offval = (struct lbs_offset_value *)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000702 switch (le16_to_cpu(cmdptr->command)) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400703 case CMD_MAC_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704 {
705 struct cmd_ds_mac_reg_access *macreg;
706
707 cmdptr->size =
David Woodhouse981f1872007-05-25 23:36:54 -0400708 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200709 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 macreg =
711 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
712 macreg;
713
714 macreg->action = cpu_to_le16(cmd_action);
715 macreg->offset = cpu_to_le16((u16) offval->offset);
716 macreg->value = cpu_to_le32(offval->value);
717
718 break;
719 }
720
Dan Williams0aef64d2007-08-02 11:31:18 -0400721 case CMD_BBP_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722 {
723 struct cmd_ds_bbp_reg_access *bbpreg;
724
725 cmdptr->size =
726 cpu_to_le16(sizeof
727 (struct cmd_ds_bbp_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200728 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 bbpreg =
730 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
731 bbpreg;
732
733 bbpreg->action = cpu_to_le16(cmd_action);
734 bbpreg->offset = cpu_to_le16((u16) offval->offset);
735 bbpreg->value = (u8) offval->value;
736
737 break;
738 }
739
Dan Williams0aef64d2007-08-02 11:31:18 -0400740 case CMD_RF_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741 {
742 struct cmd_ds_rf_reg_access *rfreg;
743
744 cmdptr->size =
745 cpu_to_le16(sizeof
746 (struct cmd_ds_rf_reg_access) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200747 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748 rfreg =
749 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
750 rfreg;
751
752 rfreg->action = cpu_to_le16(cmd_action);
753 rfreg->offset = cpu_to_le16((u16) offval->offset);
754 rfreg->value = (u8) offval->value;
755
756 break;
757 }
758
759 default:
760 break;
761 }
762
Holger Schurig9012b282007-05-25 11:27:16 -0400763 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764 return 0;
765}
766
David Woodhouse681ffbb2007-12-15 20:04:54 -0500767static void lbs_queue_cmd(struct lbs_private *priv,
768 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769{
770 unsigned long flags;
David Woodhouse681ffbb2007-12-15 20:04:54 -0500771 int addtail = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200772
Holger Schurig8ff12da2007-08-02 11:54:31 -0400773 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200774
David Woodhousec4ab4122007-12-15 00:41:51 -0500775 if (!cmdnode) {
776 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200777 goto done;
778 }
David Woodhoused9896ee2007-12-15 00:09:25 -0500779 if (!cmdnode->cmdbuf->size) {
780 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
781 goto done;
782 }
David Woodhouseae125bf2007-12-15 04:22:52 -0500783 cmdnode->result = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784
785 /* Exit_PS command needs to be queued in the header always. */
Dan Williamsddac4522007-12-11 13:49:39 -0500786 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
David Woodhouse38bfab12007-12-16 23:26:54 -0500787 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
Dan Williamsddac4522007-12-11 13:49:39 -0500788
Dan Williams0aef64d2007-08-02 11:31:18 -0400789 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000790 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791 addtail = 0;
792 }
793 }
794
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700795 if (le16_to_cpu(cmdnode->cmdbuf->command) ==
796 CMD_802_11_WAKEUP_CONFIRM)
797 addtail = 0;
798
David Woodhouseaa21c002007-12-08 20:04:36 +0000799 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200800
David Woodhouseac472462007-12-08 00:35:00 +0000801 if (addtail)
David Woodhouseaa21c002007-12-08 20:04:36 +0000802 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
David Woodhouseac472462007-12-08 00:35:00 +0000803 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000804 list_add(&cmdnode->list, &priv->cmdpendingq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200805
David Woodhouseaa21c002007-12-08 20:04:36 +0000806 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807
Holger Schurig8ff12da2007-08-02 11:54:31 -0400808 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
David Woodhousec4ab4122007-12-15 00:41:51 -0500809 le16_to_cpu(cmdnode->cmdbuf->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200810
811done:
Holger Schurig8ff12da2007-08-02 11:54:31 -0400812 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813}
814
David Woodhouse18c52e72007-12-17 16:03:58 -0500815static void lbs_submit_command(struct lbs_private *priv,
816 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817{
818 unsigned long flags;
Dan Williamsddac4522007-12-11 13:49:39 -0500819 struct cmd_header *cmd;
David Woodhouse18c52e72007-12-17 16:03:58 -0500820 uint16_t cmdsize;
821 uint16_t command;
Holger Schurig57962f02008-05-14 16:30:28 +0200822 int timeo = 3 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500823 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824
Holger Schurig8ff12da2007-08-02 11:54:31 -0400825 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826
Dan Williamsddac4522007-12-11 13:49:39 -0500827 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200828
David Woodhouseaa21c002007-12-08 20:04:36 +0000829 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +0000830 priv->cur_cmd = cmdnode;
831 priv->cur_cmd_retcode = 0;
832 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200833
Dan Williamsddac4522007-12-11 13:49:39 -0500834 cmdsize = le16_to_cpu(cmd->size);
835 command = le16_to_cpu(cmd->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200836
David Woodhouse18c52e72007-12-17 16:03:58 -0500837 /* These commands take longer */
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400838 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
Holger Schurig57962f02008-05-14 16:30:28 +0200839 timeo = 5 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500840
Holger Schurige5225b32008-03-26 10:04:44 +0100841 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
842 command, le16_to_cpu(cmd->seqnum), cmdsize);
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100843 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
Holger Schurig8ff12da2007-08-02 11:54:31 -0400844
Dan Williamsddac4522007-12-11 13:49:39 -0500845 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
David Woodhouse18c52e72007-12-17 16:03:58 -0500846
David Woodhoused9896ee2007-12-15 00:09:25 -0500847 if (ret) {
848 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
David Woodhouse18c52e72007-12-17 16:03:58 -0500849 /* Let the timer kick in and retry, and potentially reset
850 the whole thing if the condition persists */
Holger Schurig57962f02008-05-14 16:30:28 +0200851 timeo = HZ/4;
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100852 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200853
Amitkumar Karwar49125452009-09-30 20:04:38 -0700854 if (command == CMD_802_11_DEEP_SLEEP) {
855 if (priv->is_auto_deep_sleep_enabled) {
856 priv->wakeup_dev_required = 1;
857 priv->dnld_sent = 0;
858 }
859 priv->is_deep_sleep = 1;
860 lbs_complete_command(priv, cmdnode, 0);
861 } else {
862 /* Setup the timer after transmit command */
863 mod_timer(&priv->command_timer, jiffies + timeo);
864 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200865
David Woodhouse18c52e72007-12-17 16:03:58 -0500866 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200867}
868
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200869/**
870 * This function inserts command node to cmdfreeq
David Woodhouseaa21c002007-12-08 20:04:36 +0000871 * after cleans it. Requires priv->driver_lock held.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872 */
David Woodhouse183aeac2007-12-15 01:52:54 -0500873static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500874 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200875{
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500876 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500878 if (!cmdnode)
879 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200880
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500881 cmdnode->callback = NULL;
882 cmdnode->callback_arg = 0;
883
884 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
885
886 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
887 out:
888 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200889}
890
Holger Schurig69f90322007-11-23 15:43:44 +0100891static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
892 struct cmd_ctrl_node *ptempcmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200893{
894 unsigned long flags;
895
David Woodhouseaa21c002007-12-08 20:04:36 +0000896 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig10078322007-11-15 18:05:47 -0500897 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
David Woodhouseaa21c002007-12-08 20:04:36 +0000898 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200899}
900
David Woodhouse183aeac2007-12-15 01:52:54 -0500901void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
902 int result)
903{
904 if (cmd == priv->cur_cmd)
905 priv->cur_cmd_retcode = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500906
David Woodhouseae125bf2007-12-15 04:22:52 -0500907 cmd->result = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500908 cmd->cmdwaitqwoken = 1;
909 wake_up_interruptible(&cmd->cmdwait_q);
910
Holger Schurig8db4a2b2008-03-19 10:11:00 +0100911 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
David Woodhousead12d0f2007-12-15 02:06:16 -0500912 __lbs_cleanup_and_insert_cmd(priv, cmd);
David Woodhouse183aeac2007-12-15 01:52:54 -0500913 priv->cur_cmd = NULL;
914}
915
Dan Williamsd5db2df2008-08-21 17:51:07 -0400916int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200917{
David Woodhousea7c45892007-12-17 22:43:48 -0500918 struct cmd_ds_802_11_radio_control cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400919 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200920
Holger Schurig9012b282007-05-25 11:27:16 -0400921 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200922
David Woodhousea7c45892007-12-17 22:43:48 -0500923 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
924 cmd.action = cpu_to_le16(CMD_ACT_SET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200925
Dan Williamsd5db2df2008-08-21 17:51:07 -0400926 /* Only v8 and below support setting the preamble */
927 if (priv->fwrelease < 0x09000000) {
928 switch (preamble) {
929 case RADIO_PREAMBLE_SHORT:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400930 case RADIO_PREAMBLE_AUTO:
931 case RADIO_PREAMBLE_LONG:
932 cmd.control = cpu_to_le16(preamble);
933 break;
934 default:
935 goto out;
936 }
David Woodhousea7c45892007-12-17 22:43:48 -0500937 }
938
Dan Williamsd5db2df2008-08-21 17:51:07 -0400939 if (radio_on)
940 cmd.control |= cpu_to_le16(0x1);
941 else {
942 cmd.control &= cpu_to_le16(~0x1);
943 priv->txpower_cur = 0;
944 }
David Woodhousea7c45892007-12-17 22:43:48 -0500945
Dan Williamsd5db2df2008-08-21 17:51:07 -0400946 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
947 radio_on ? "ON" : "OFF", preamble);
948
949 priv->radio_on = radio_on;
David Woodhousea7c45892007-12-17 22:43:48 -0500950
951 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952
Dan Williamsd5db2df2008-08-21 17:51:07 -0400953out:
Holger Schurig9012b282007-05-25 11:27:16 -0400954 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200955 return ret;
956}
957
Holger Schurigc97329e2008-03-18 11:20:21 +0100958void lbs_set_mac_control(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959{
Holger Schurig835d3ac2008-03-12 16:05:40 +0100960 struct cmd_ds_mac_control cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200961
Holger Schurig9012b282007-05-25 11:27:16 -0400962 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963
Holger Schurig835d3ac2008-03-12 16:05:40 +0100964 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurigd9e97782008-03-12 16:06:43 +0100965 cmd.action = cpu_to_le16(priv->mac_control);
Holger Schurig835d3ac2008-03-12 16:05:40 +0100966 cmd.reserved = 0;
967
David Woodhouse75bf45a2008-05-20 13:32:45 +0100968 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200969
Holger Schurigc97329e2008-03-18 11:20:21 +0100970 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971}
972
973/**
Kiran Divekar1047d5e2010-06-04 23:20:42 -0700974 * @brief This function implements command CMD_802_11D_DOMAIN_INFO
975 * @param priv pointer to struct lbs_private
976 * @param cmd pointer to cmd buffer
977 * @param cmdno cmd ID
978 * @param cmdOption cmd action
979 * @return 0
980*/
981int lbs_cmd_802_11d_domain_info(struct lbs_private *priv,
982 struct cmd_ds_command *cmd,
983 u16 cmdoption)
984{
985 struct cmd_ds_802_11d_domain_info *pdomaininfo =
986 &cmd->params.domaininfo;
987 struct mrvl_ie_domain_param_set *domain = &pdomaininfo->domain;
988 u8 nr_triplet = priv->domain_reg.no_triplet;
989
990 lbs_deb_enter(LBS_DEB_11D);
991
992 lbs_deb_11d("nr_triplet=%x\n", nr_triplet);
993
994 pdomaininfo->action = cpu_to_le16(cmdoption);
995 if (cmdoption == CMD_ACT_GET) {
996 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
997 sizeof(struct cmd_header));
998 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
999 le16_to_cpu(cmd->size));
1000 goto done;
1001 }
1002
1003 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
1004 memcpy(domain->countrycode, priv->domain_reg.country_code,
1005 sizeof(domain->countrycode));
1006
1007 domain->header.len = cpu_to_le16(nr_triplet
1008 * sizeof(struct ieee80211_country_ie_triplet)
1009 + sizeof(domain->countrycode));
1010
1011 if (nr_triplet) {
1012 memcpy(domain->triplet, priv->domain_reg.triplet,
1013 nr_triplet *
1014 sizeof(struct ieee80211_country_ie_triplet));
1015
1016 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1017 le16_to_cpu(domain->header.len) +
1018 sizeof(struct mrvl_ie_header) +
1019 sizeof(struct cmd_header));
1020 } else {
1021 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1022 sizeof(struct cmd_header));
1023 }
1024
1025 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
1026 le16_to_cpu(cmd->size));
1027
1028done:
1029 lbs_deb_enter(LBS_DEB_11D);
1030 return 0;
1031}
1032
1033/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034 * @brief This function prepare the command before send to firmware.
1035 *
Holger Schurig69f90322007-11-23 15:43:44 +01001036 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001037 * @param cmd_no command number
1038 * @param cmd_action command action: GET or SET
1039 * @param wait_option wait option: wait response or not
1040 * @param cmd_oid cmd oid: treated as sub command
1041 * @param pdata_buf A pointer to informaion buffer
1042 * @return 0 or -1
1043 */
Holger Schurig69f90322007-11-23 15:43:44 +01001044int lbs_prepare_and_send_command(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045 u16 cmd_no,
1046 u16 cmd_action,
1047 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1048{
1049 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050 struct cmd_ctrl_node *cmdnode;
1051 struct cmd_ds_command *cmdptr;
1052 unsigned long flags;
1053
Holger Schurig8ff12da2007-08-02 11:54:31 -04001054 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001055
David Woodhouseaa21c002007-12-08 20:04:36 +00001056 if (!priv) {
1057 lbs_deb_host("PREP_CMD: priv is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001058 ret = -1;
1059 goto done;
1060 }
1061
David Woodhouseaa21c002007-12-08 20:04:36 +00001062 if (priv->surpriseremoved) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001063 lbs_deb_host("PREP_CMD: card removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064 ret = -1;
1065 goto done;
1066 }
1067
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001068 if (!lbs_is_cmd_allowed(priv)) {
1069 ret = -EBUSY;
1070 goto done;
1071 }
1072
Holger Schurig0d61d042007-12-05 17:58:06 +01001073 cmdnode = lbs_get_cmd_ctrl_node(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001074
1075 if (cmdnode == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001076 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001077
1078 /* Wake up main thread to execute next command */
Dan Williamsfe336152007-08-02 11:32:25 -04001079 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080 ret = -1;
1081 goto done;
1082 }
1083
Holger Schurige98a88d2008-03-19 14:25:58 +01001084 cmdnode->callback = NULL;
1085 cmdnode->callback_arg = (unsigned long)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086
Dan Williamsddac4522007-12-11 13:49:39 -05001087 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001088
Holger Schurig8ff12da2007-08-02 11:54:31 -04001089 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001091 /* Set sequence number, command and INT option */
David Woodhouseaa21c002007-12-08 20:04:36 +00001092 priv->seqnum++;
1093 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001094
David Woodhouse981f1872007-05-25 23:36:54 -04001095 cmdptr->command = cpu_to_le16(cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001096 cmdptr->result = 0;
1097
1098 switch (cmd_no) {
Dan Williams0aef64d2007-08-02 11:31:18 -04001099 case CMD_802_11_PS_MODE:
Holger Schurige98a88d2008-03-19 14:25:58 +01001100 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001101 break;
1102
Dan Williams0aef64d2007-08-02 11:31:18 -04001103 case CMD_MAC_REG_ACCESS:
1104 case CMD_BBP_REG_ACCESS:
1105 case CMD_RF_REG_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001106 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001107 break;
1108
Dan Williams0aef64d2007-08-02 11:31:18 -04001109 case CMD_802_11_RSSI:
Holger Schurig10078322007-11-15 18:05:47 -05001110 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111 break;
1112
Dan Williams0aef64d2007-08-02 11:31:18 -04001113 case CMD_802_11_SET_AFC:
1114 case CMD_802_11_GET_AFC:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115
1116 cmdptr->command = cpu_to_le16(cmd_no);
David Woodhouse981f1872007-05-25 23:36:54 -04001117 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001118 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001119
1120 memmove(&cmdptr->params.afc,
1121 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1122
1123 ret = 0;
1124 goto done;
1125
Kiran Divekar1047d5e2010-06-04 23:20:42 -07001126 case CMD_802_11D_DOMAIN_INFO:
1127 cmdptr->command = cpu_to_le16(cmd_no);
1128 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr, cmd_action);
1129 break;
1130
Dan Williams0aef64d2007-08-02 11:31:18 -04001131 case CMD_802_11_TPC_CFG:
1132 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001133 cmdptr->size =
1134 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001135 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136
1137 memmove(&cmdptr->params.tpccfg,
1138 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1139
1140 ret = 0;
1141 break;
David Woodhouse5844d122007-12-18 02:01:37 -05001142
Holger Schurig4143a232009-12-02 15:26:02 +01001143#ifdef CONFIG_LIBERTAS_MESH
1144
Dan Williams0aef64d2007-08-02 11:31:18 -04001145 case CMD_BT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001146 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001147 break;
1148
Dan Williams0aef64d2007-08-02 11:31:18 -04001149 case CMD_FWT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001150 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151 break;
1152
Holger Schurig4143a232009-12-02 15:26:02 +01001153#endif
1154
Brajesh Dave96287ac2007-11-20 17:44:28 -05001155 case CMD_802_11_BEACON_CTRL:
1156 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1157 break;
Amitkumar Karwar49125452009-09-30 20:04:38 -07001158 case CMD_802_11_DEEP_SLEEP:
1159 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001160 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
Amitkumar Karwar49125452009-09-30 20:04:38 -07001161 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001162 default:
David Woodhousee37fc6e2008-05-20 11:47:16 +01001163 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 ret = -1;
1165 break;
1166 }
1167
1168 /* return error, since the command preparation failed */
1169 if (ret != 0) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001170 lbs_deb_host("PREP_CMD: command preparation failed\n");
Holger Schurig10078322007-11-15 18:05:47 -05001171 lbs_cleanup_and_insert_cmd(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001172 ret = -1;
1173 goto done;
1174 }
1175
1176 cmdnode->cmdwaitqwoken = 0;
1177
David Woodhouse681ffbb2007-12-15 20:04:54 -05001178 lbs_queue_cmd(priv, cmdnode);
Dan Williamsfe336152007-08-02 11:32:25 -04001179 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180
Dan Williams0aef64d2007-08-02 11:31:18 -04001181 if (wait_option & CMD_OPTION_WAITFORRSP) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001182 lbs_deb_host("PREP_CMD: wait for response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001183 might_sleep();
1184 wait_event_interruptible(cmdnode->cmdwait_q,
1185 cmdnode->cmdwaitqwoken);
1186 }
1187
David Woodhouseaa21c002007-12-08 20:04:36 +00001188 spin_lock_irqsave(&priv->driver_lock, flags);
1189 if (priv->cur_cmd_retcode) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001190 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
David Woodhouseaa21c002007-12-08 20:04:36 +00001191 priv->cur_cmd_retcode);
1192 priv->cur_cmd_retcode = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001193 ret = -1;
1194 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001195 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001196
1197done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001198 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001199 return ret;
1200}
1201
1202/**
1203 * @brief This function allocates the command buffer and link
1204 * it to command free queue.
1205 *
Holger Schurig69f90322007-11-23 15:43:44 +01001206 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001207 * @return 0 or -1
1208 */
Holger Schurig69f90322007-11-23 15:43:44 +01001209int lbs_allocate_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001210{
1211 int ret = 0;
Dan Williamsddac4522007-12-11 13:49:39 -05001212 u32 bufsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001213 u32 i;
Dan Williamsddac4522007-12-11 13:49:39 -05001214 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001215
Holger Schurig8ff12da2007-08-02 11:54:31 -04001216 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001217
Dan Williamsddac4522007-12-11 13:49:39 -05001218 /* Allocate and initialize the command array */
1219 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1220 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001221 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001222 ret = -1;
1223 goto done;
1224 }
Dan Williamsddac4522007-12-11 13:49:39 -05001225 priv->cmd_array = cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001226
Dan Williamsddac4522007-12-11 13:49:39 -05001227 /* Allocate and initialize each command buffer in the command array */
1228 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1229 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1230 if (!cmdarray[i].cmdbuf) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001231 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001232 ret = -1;
1233 goto done;
1234 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001235 }
1236
Dan Williamsddac4522007-12-11 13:49:39 -05001237 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1238 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1239 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001240 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001241 ret = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001242
1243done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001244 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001245 return ret;
1246}
1247
1248/**
1249 * @brief This function frees the command buffer.
1250 *
Holger Schurig69f90322007-11-23 15:43:44 +01001251 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001252 * @return 0 or -1
1253 */
Holger Schurig69f90322007-11-23 15:43:44 +01001254int lbs_free_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255{
Dan Williamsddac4522007-12-11 13:49:39 -05001256 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001257 unsigned int i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001258
Holger Schurig8ff12da2007-08-02 11:54:31 -04001259 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001260
1261 /* need to check if cmd array is allocated or not */
David Woodhouseaa21c002007-12-08 20:04:36 +00001262 if (priv->cmd_array == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001263 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001264 goto done;
1265 }
1266
Dan Williamsddac4522007-12-11 13:49:39 -05001267 cmdarray = priv->cmd_array;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001268
1269 /* Release shared memory buffers */
Dan Williamsddac4522007-12-11 13:49:39 -05001270 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1271 if (cmdarray[i].cmdbuf) {
1272 kfree(cmdarray[i].cmdbuf);
1273 cmdarray[i].cmdbuf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001274 }
1275 }
1276
1277 /* Release cmd_ctrl_node */
David Woodhouseaa21c002007-12-08 20:04:36 +00001278 if (priv->cmd_array) {
1279 kfree(priv->cmd_array);
1280 priv->cmd_array = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001281 }
1282
1283done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001284 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001285 return 0;
1286}
1287
1288/**
1289 * @brief This function gets a free command node if available in
1290 * command free queue.
1291 *
Holger Schurig69f90322007-11-23 15:43:44 +01001292 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1294 */
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001295static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001296{
1297 struct cmd_ctrl_node *tempnode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001298 unsigned long flags;
1299
Holger Schurig8ff12da2007-08-02 11:54:31 -04001300 lbs_deb_enter(LBS_DEB_HOST);
1301
David Woodhouseaa21c002007-12-08 20:04:36 +00001302 if (!priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303 return NULL;
1304
David Woodhouseaa21c002007-12-08 20:04:36 +00001305 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306
David Woodhouseaa21c002007-12-08 20:04:36 +00001307 if (!list_empty(&priv->cmdfreeq)) {
1308 tempnode = list_first_entry(&priv->cmdfreeq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001309 struct cmd_ctrl_node, list);
1310 list_del(&tempnode->list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001312 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001313 tempnode = NULL;
1314 }
1315
David Woodhouseaa21c002007-12-08 20:04:36 +00001316 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001317
Holger Schurig8ff12da2007-08-02 11:54:31 -04001318 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001319 return tempnode;
1320}
1321
1322/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001323 * @brief This function executes next command in command
Nick Andrew877d0312009-01-26 11:06:57 +01001324 * pending queue. It will put firmware back to PS mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001325 * if applicable.
1326 *
Holger Schurig69f90322007-11-23 15:43:44 +01001327 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001328 * @return 0 or -1
1329 */
Holger Schurig69f90322007-11-23 15:43:44 +01001330int lbs_execute_next_command(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001331{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001332 struct cmd_ctrl_node *cmdnode = NULL;
Dan Williamsddac4522007-12-11 13:49:39 -05001333 struct cmd_header *cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001334 unsigned long flags;
1335 int ret = 0;
1336
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001337 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1338 * only caller to us is lbs_thread() and we get even when a
1339 * data packet is received */
Holger Schurig8ff12da2007-08-02 11:54:31 -04001340 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001341
David Woodhouseaa21c002007-12-08 20:04:36 +00001342 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001343
David Woodhouseaa21c002007-12-08 20:04:36 +00001344 if (priv->cur_cmd) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001345 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001346 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 ret = -1;
1348 goto done;
1349 }
1350
David Woodhouseaa21c002007-12-08 20:04:36 +00001351 if (!list_empty(&priv->cmdpendingq)) {
1352 cmdnode = list_first_entry(&priv->cmdpendingq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001353 struct cmd_ctrl_node, list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001354 }
1355
David Woodhouseaa21c002007-12-08 20:04:36 +00001356 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001357
1358 if (cmdnode) {
Dan Williamsddac4522007-12-11 13:49:39 -05001359 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001360
Dan Williamsddac4522007-12-11 13:49:39 -05001361 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001362 if ((priv->psstate == PS_STATE_SLEEP) ||
1363 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001364 lbs_deb_host(
1365 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001366 le16_to_cpu(cmd->command),
David Woodhouseaa21c002007-12-08 20:04:36 +00001367 priv->psstate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001368 ret = -1;
1369 goto done;
1370 }
Holger Schurig8ff12da2007-08-02 11:54:31 -04001371 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
Dan Williamsddac4522007-12-11 13:49:39 -05001372 "0x%04x in psstate %d\n",
1373 le16_to_cpu(cmd->command), priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +00001374 } else if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001375 /*
1376 * 1. Non-PS command:
1377 * Queue it. set needtowakeup to TRUE if current state
Holger Schurig10078322007-11-15 18:05:47 -05001378 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001379 * 2. PS command but not Exit_PS:
1380 * Ignore it.
1381 * 3. PS command Exit_PS:
1382 * Set needtowakeup to TRUE if current state is SLEEP,
1383 * otherwise send this command down to firmware
1384 * immediately.
1385 */
Dan Williamsddac4522007-12-11 13:49:39 -05001386 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001387 /* Prepare to send Exit PS,
1388 * this non PS command will be sent later */
David Woodhouseaa21c002007-12-08 20:04:36 +00001389 if ((priv->psstate == PS_STATE_SLEEP)
1390 || (priv->psstate == PS_STATE_PRE_SLEEP)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391 ) {
1392 /* w/ new scheme, it will not reach here.
1393 since it is blocked in main_thread. */
David Woodhouseaa21c002007-12-08 20:04:36 +00001394 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001395 } else
Holger Schurig10078322007-11-15 18:05:47 -05001396 lbs_ps_wakeup(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001397
1398 ret = 0;
1399 goto done;
1400 } else {
1401 /*
1402 * PS command. Ignore it if it is not Exit_PS.
1403 * otherwise send it down immediately.
1404 */
David Woodhouse38bfab12007-12-16 23:26:54 -05001405 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406
Holger Schurig8ff12da2007-08-02 11:54:31 -04001407 lbs_deb_host(
1408 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001409 psm->action);
1410 if (psm->action !=
Dan Williams0aef64d2007-08-02 11:31:18 -04001411 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001412 lbs_deb_host(
1413 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001414 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001415 spin_lock_irqsave(&priv->driver_lock, flags);
1416 lbs_complete_command(priv, cmdnode, 0);
1417 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418
1419 ret = 0;
1420 goto done;
1421 }
1422
David Woodhouseaa21c002007-12-08 20:04:36 +00001423 if ((priv->psstate == PS_STATE_SLEEP) ||
1424 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001425 lbs_deb_host(
1426 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001427 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001428 spin_lock_irqsave(&priv->driver_lock, flags);
1429 lbs_complete_command(priv, cmdnode, 0);
1430 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +00001431 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001432
1433 ret = 0;
1434 goto done;
1435 }
1436
Holger Schurig8ff12da2007-08-02 11:54:31 -04001437 lbs_deb_host(
1438 "EXEC_NEXT_CMD: sending EXIT_PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001439 }
1440 }
Li Zefanabe3ed12007-12-06 13:01:21 +01001441 list_del(&cmdnode->list);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001442 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001443 le16_to_cpu(cmd->command));
David Woodhoused9896ee2007-12-15 00:09:25 -05001444 lbs_submit_command(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001445 } else {
1446 /*
1447 * check if in power save mode, if yes, put the device back
1448 * to PS mode
1449 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301450#ifdef TODO
1451 /*
1452 * This was the old code for libertas+wext. Someone that
1453 * understands this beast should re-code it in a sane way.
1454 *
1455 * I actually don't understand why this is related to WPA
1456 * and to connection status, shouldn't powering should be
1457 * independ of such things?
1458 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001459 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1460 (priv->psstate == PS_STATE_FULL_POWER) &&
1461 ((priv->connect_status == LBS_CONNECTED) ||
Holger Schurig602114a2009-12-02 15:26:01 +01001462 lbs_mesh_connected(priv))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001463 if (priv->secinfo.WPAenabled ||
1464 priv->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001465 /* check for valid WPA group keys */
David Woodhouseaa21c002007-12-08 20:04:36 +00001466 if (priv->wpa_mcast_key.len ||
1467 priv->wpa_unicast_key.len) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001468 lbs_deb_host(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001469 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1470 " go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001471 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001472 }
1473 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001474 lbs_deb_host(
1475 "EXEC_NEXT_CMD: cmdpendingq empty, "
1476 "go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001477 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001478 }
1479 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301480#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001481 }
1482
1483 ret = 0;
1484done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001485 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001486 return ret;
1487}
1488
Holger Schurigf539f2e2008-03-26 13:22:11 +01001489static void lbs_send_confirmsleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001490{
1491 unsigned long flags;
Holger Schurigf539f2e2008-03-26 13:22:11 +01001492 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001493
Holger Schurig8ff12da2007-08-02 11:54:31 -04001494 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001495 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1496 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001497
Holger Schurigf539f2e2008-03-26 13:22:11 +01001498 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1499 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001500 if (ret) {
Holger Schurigf539f2e2008-03-26 13:22:11 +01001501 lbs_pr_alert("confirm_sleep failed\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001502 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001503 }
Holger Schurig7919b892008-04-01 14:50:43 +02001504
1505 spin_lock_irqsave(&priv->driver_lock, flags);
1506
Holger Schuriga01f5452008-06-04 11:10:40 +02001507 /* We don't get a response on the sleep-confirmation */
1508 priv->dnld_sent = DNLD_RES_RECEIVED;
1509
Amitkumar Karwar66fceb62010-05-19 03:24:38 -07001510 if (priv->is_host_sleep_configured) {
1511 priv->is_host_sleep_activated = 1;
1512 wake_up_interruptible(&priv->host_sleep_q);
1513 }
1514
Holger Schurig7919b892008-04-01 14:50:43 +02001515 /* If nothing to do, go back to sleep (?) */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001516 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
Holger Schurig7919b892008-04-01 14:50:43 +02001517 priv->psstate = PS_STATE_SLEEP;
1518
1519 spin_unlock_irqrestore(&priv->driver_lock, flags);
1520
1521out:
Holger Schurigf539f2e2008-03-26 13:22:11 +01001522 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001523}
1524
Holger Schurig69f90322007-11-23 15:43:44 +01001525void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001526{
Holger Schurig8ff12da2007-08-02 11:54:31 -04001527 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001528
1529 /*
1530 * PS is currently supported only in Infrastructure mode
1531 * Remove this check if it is to be supported in IBSS mode also
1532 */
1533
Holger Schurig10078322007-11-15 18:05:47 -05001534 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001535 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001536
Holger Schurig8ff12da2007-08-02 11:54:31 -04001537 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001538}
1539
1540/**
Holger Schurig8ff12da2007-08-02 11:54:31 -04001541 * @brief This function sends Exit_PS command to firmware.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001542 *
Holger Schurig69f90322007-11-23 15:43:44 +01001543 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001544 * @param wait_option wait response or not
1545 * @return n/a
1546 */
Holger Schurig69f90322007-11-23 15:43:44 +01001547void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001548{
David Woodhouse981f1872007-05-25 23:36:54 -04001549 __le32 Localpsmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001550
Holger Schurig8ff12da2007-08-02 11:54:31 -04001551 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001552
Holger Schurig10078322007-11-15 18:05:47 -05001553 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001554
Holger Schurig10078322007-11-15 18:05:47 -05001555 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001556 CMD_SUBCMD_EXIT_PS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001557 wait_option, 0, &Localpsmode);
1558
Holger Schurig8ff12da2007-08-02 11:54:31 -04001559 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001560}
1561
1562/**
1563 * @brief This function checks condition and prepares to
1564 * send sleep confirm command to firmware if ok.
1565 *
Holger Schurig69f90322007-11-23 15:43:44 +01001566 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001567 * @param psmode Power Saving mode
1568 * @return n/a
1569 */
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001570void lbs_ps_confirm_sleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001571{
1572 unsigned long flags =0;
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001573 int allowed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001574
Holger Schurig8ff12da2007-08-02 11:54:31 -04001575 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001576
Holger Schuriga01f5452008-06-04 11:10:40 +02001577 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig634b8f42007-05-25 13:05:16 -04001578 if (priv->dnld_sent) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001579 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001580 lbs_deb_host("dnld_sent was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001581 }
1582
Holger Schurig7919b892008-04-01 14:50:43 +02001583 /* In-progress command? */
David Woodhouseaa21c002007-12-08 20:04:36 +00001584 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001585 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001586 lbs_deb_host("cur_cmd was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001587 }
Holger Schurig7919b892008-04-01 14:50:43 +02001588
1589 /* Pending events or command responses? */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001590 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001591 allowed = 0;
Holger Schurig7919b892008-04-01 14:50:43 +02001592 lbs_deb_host("pending events or command responses\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001593 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001594 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001595
1596 if (allowed) {
Holger Schurig10078322007-11-15 18:05:47 -05001597 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
Holger Schurigf539f2e2008-03-26 13:22:11 +01001598 lbs_send_confirmsleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001599 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001600 lbs_deb_host("sleep confirm has been delayed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001601 }
1602
Holger Schurig8ff12da2007-08-02 11:54:31 -04001603 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001604}
Holger Schurig675787e2007-12-05 17:58:11 +01001605
1606
Anna Neal0112c9e2008-09-11 11:17:25 -07001607/**
1608 * @brief Configures the transmission power control functionality.
1609 *
1610 * @param priv A pointer to struct lbs_private structure
1611 * @param enable Transmission power control enable
1612 * @param p0 Power level when link quality is good (dBm).
1613 * @param p1 Power level when link quality is fair (dBm).
1614 * @param p2 Power level when link quality is poor (dBm).
1615 * @param usesnr Use Signal to Noise Ratio in TPC
1616 *
1617 * @return 0 on success
1618 */
1619int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1620 int8_t p2, int usesnr)
1621{
1622 struct cmd_ds_802_11_tpc_cfg cmd;
1623 int ret;
1624
1625 memset(&cmd, 0, sizeof(cmd));
1626 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1627 cmd.action = cpu_to_le16(CMD_ACT_SET);
1628 cmd.enable = !!enable;
Anna Neal3ed6e082008-09-26 11:34:35 -04001629 cmd.usesnr = !!usesnr;
Anna Neal0112c9e2008-09-11 11:17:25 -07001630 cmd.P0 = p0;
1631 cmd.P1 = p1;
1632 cmd.P2 = p2;
1633
1634 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1635
1636 return ret;
1637}
1638
1639/**
1640 * @brief Configures the power adaptation settings.
1641 *
1642 * @param priv A pointer to struct lbs_private structure
1643 * @param enable Power adaptation enable
1644 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1645 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1646 * @param p2 Power level for 48 and 54 Mbps (dBm).
1647 *
1648 * @return 0 on Success
1649 */
1650
1651int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1652 int8_t p1, int8_t p2)
1653{
1654 struct cmd_ds_802_11_pa_cfg cmd;
1655 int ret;
1656
1657 memset(&cmd, 0, sizeof(cmd));
1658 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1659 cmd.action = cpu_to_le16(CMD_ACT_SET);
1660 cmd.enable = !!enable;
1661 cmd.P0 = p0;
1662 cmd.P1 = p1;
1663 cmd.P2 = p2;
1664
1665 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1666
1667 return ret;
1668}
1669
1670
Holger Schurig6d898b12009-10-14 16:49:53 +02001671struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001672 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1673 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1674 unsigned long callback_arg)
Holger Schurig675787e2007-12-05 17:58:11 +01001675{
Holger Schurig675787e2007-12-05 17:58:11 +01001676 struct cmd_ctrl_node *cmdnode;
Holger Schurig675787e2007-12-05 17:58:11 +01001677
1678 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurig675787e2007-12-05 17:58:11 +01001679
David Woodhouseaa21c002007-12-08 20:04:36 +00001680 if (priv->surpriseremoved) {
Holger Schurig675787e2007-12-05 17:58:11 +01001681 lbs_deb_host("PREP_CMD: card removed\n");
David Woodhouse3399ea52007-12-15 03:09:33 -05001682 cmdnode = ERR_PTR(-ENOENT);
Holger Schurig675787e2007-12-05 17:58:11 +01001683 goto done;
1684 }
1685
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001686 if (!lbs_is_cmd_allowed(priv)) {
1687 cmdnode = ERR_PTR(-EBUSY);
1688 goto done;
1689 }
1690
Holger Schurig675787e2007-12-05 17:58:11 +01001691 cmdnode = lbs_get_cmd_ctrl_node(priv);
Holger Schurig675787e2007-12-05 17:58:11 +01001692 if (cmdnode == NULL) {
1693 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1694
1695 /* Wake up main thread to execute next command */
1696 wake_up_interruptible(&priv->waitq);
David Woodhouse3399ea52007-12-15 03:09:33 -05001697 cmdnode = ERR_PTR(-ENOBUFS);
Holger Schurig675787e2007-12-05 17:58:11 +01001698 goto done;
1699 }
1700
David Woodhouse448a51a2007-12-08 00:59:54 +00001701 cmdnode->callback = callback;
David Woodhouse1309b552007-12-10 13:36:10 -05001702 cmdnode->callback_arg = callback_arg;
Holger Schurig675787e2007-12-05 17:58:11 +01001703
Dan Williams7ad994d2007-12-11 12:33:30 -05001704 /* Copy the incoming command to the buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001705 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
Dan Williams7ad994d2007-12-11 12:33:30 -05001706
Holger Schurig675787e2007-12-05 17:58:11 +01001707 /* Set sequence number, clean result, move to buffer */
David Woodhouseaa21c002007-12-08 20:04:36 +00001708 priv->seqnum++;
Dan Williamsddac4522007-12-11 13:49:39 -05001709 cmdnode->cmdbuf->command = cpu_to_le16(command);
1710 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1711 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1712 cmdnode->cmdbuf->result = 0;
Holger Schurig675787e2007-12-05 17:58:11 +01001713
1714 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1715
Holger Schurig675787e2007-12-05 17:58:11 +01001716 cmdnode->cmdwaitqwoken = 0;
David Woodhouse681ffbb2007-12-15 20:04:54 -05001717 lbs_queue_cmd(priv, cmdnode);
Holger Schurig675787e2007-12-05 17:58:11 +01001718 wake_up_interruptible(&priv->waitq);
1719
David Woodhouse3399ea52007-12-15 03:09:33 -05001720 done:
1721 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1722 return cmdnode;
1723}
1724
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001725void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1726 struct cmd_header *in_cmd, int in_cmd_size)
1727{
1728 lbs_deb_enter(LBS_DEB_CMD);
1729 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1730 lbs_cmd_async_callback, 0);
1731 lbs_deb_leave(LBS_DEB_CMD);
1732}
1733
David Woodhouse3399ea52007-12-15 03:09:33 -05001734int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1735 struct cmd_header *in_cmd, int in_cmd_size,
1736 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1737 unsigned long callback_arg)
1738{
1739 struct cmd_ctrl_node *cmdnode;
1740 unsigned long flags;
1741 int ret = 0;
1742
1743 lbs_deb_enter(LBS_DEB_HOST);
1744
1745 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1746 callback, callback_arg);
1747 if (IS_ERR(cmdnode)) {
1748 ret = PTR_ERR(cmdnode);
1749 goto done;
1750 }
1751
Holger Schurig675787e2007-12-05 17:58:11 +01001752 might_sleep();
1753 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1754
David Woodhouseaa21c002007-12-08 20:04:36 +00001755 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseae125bf2007-12-15 04:22:52 -05001756 ret = cmdnode->result;
1757 if (ret)
1758 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1759 command, ret);
David Woodhouse3399ea52007-12-15 03:09:33 -05001760
David Woodhousead12d0f2007-12-15 02:06:16 -05001761 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
David Woodhouseaa21c002007-12-08 20:04:36 +00001762 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig675787e2007-12-05 17:58:11 +01001763
1764done:
1765 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1766 return ret;
1767}
Dan Williams14e865b2007-12-10 15:11:23 -05001768EXPORT_SYMBOL_GPL(__lbs_cmd);