blob: 4a0d8f4188ca354eea1d7f6ea68127a1303a686c [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>
8
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include "decl.h"
11#include "defs.h"
12#include "dev.h"
Holger Schurig697900a2008-04-02 16:27:10 +020013#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014#include "wext.h"
Holger Schurig2d465022009-10-22 15:30:48 +020015#include "scan.h"
Dan Williams6e66f032007-12-11 12:42:16 -050016#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020017
Holger Schurige93156e2009-10-22 15:30:58 +020018
David Woodhouse2fd6cfe2007-12-11 17:44:10 -050019static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
Holger Schurig0d61d042007-12-05 17:58:06 +010020
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020021/**
Holger Schurig8db4a2b2008-03-19 10:11:00 +010022 * @brief Simple callback that copies response back into command
23 *
24 * @param priv A pointer to struct lbs_private structure
25 * @param extra A pointer to the original command structure for which
26 * 'resp' is a response
27 * @param resp A pointer to the command response
28 *
29 * @return 0 on success, error on failure
30 */
31int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
32 struct cmd_header *resp)
33{
34 struct cmd_header *buf = (void *)extra;
35 uint16_t copy_len;
36
37 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
38 memcpy(buf, resp, copy_len);
39 return 0;
40}
41EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
42
43/**
44 * @brief Simple callback that ignores the result. Use this if
45 * you just want to send a command to the hardware, but don't
46 * care for the result.
47 *
48 * @param priv ignored
49 * @param extra ignored
50 * @param resp ignored
51 *
52 * @return 0 for success
53 */
54static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
55 struct cmd_header *resp)
56{
57 return 0;
58}
59
60
61/**
Dan Williams852e1f22007-12-10 15:24:47 -050062 * @brief Checks whether a command is allowed in Power Save mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063 *
64 * @param command the command ID
Dan Williams852e1f22007-12-10 15:24:47 -050065 * @return 1 if allowed, 0 if not allowed
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020066 */
Dan Williams852e1f22007-12-10 15:24:47 -050067static u8 is_command_allowed_in_ps(u16 cmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020068{
Dan Williams852e1f22007-12-10 15:24:47 -050069 switch (cmd) {
70 case CMD_802_11_RSSI:
71 return 1;
72 default:
73 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075 return 0;
76}
77
Dan Williams6e66f032007-12-11 12:42:16 -050078/**
Amitkumar Karwar63f275d2009-10-06 19:20:28 -070079 * @brief This function checks if the command is allowed.
80 *
81 * @param priv A pointer to lbs_private structure
82 * @return allowed or not allowed.
83 */
84
85static int lbs_is_cmd_allowed(struct lbs_private *priv)
86{
87 int ret = 1;
88
89 lbs_deb_enter(LBS_DEB_CMD);
90
91 if (!priv->is_auto_deep_sleep_enabled) {
92 if (priv->is_deep_sleep) {
93 lbs_deb_cmd("command not allowed in deep sleep\n");
94 ret = 0;
95 }
96 }
97
98 lbs_deb_leave(LBS_DEB_CMD);
99 return ret;
100}
101
102/**
Dan Williams6e66f032007-12-11 12:42:16 -0500103 * @brief Updates the hardware details like MAC address and regulatory region
104 *
105 * @param priv A pointer to struct lbs_private structure
106 *
107 * @return 0 on success, error on failure
108 */
109int lbs_update_hw_spec(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200110{
Dan Williams6e66f032007-12-11 12:42:16 -0500111 struct cmd_ds_get_hw_spec cmd;
112 int ret = -1;
113 u32 i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200114
Holger Schurig9012b282007-05-25 11:27:16 -0400115 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200116
Dan Williams6e66f032007-12-11 12:42:16 -0500117 memset(&cmd, 0, sizeof(cmd));
118 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
119 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
David Woodhouse689442d2007-12-12 16:00:42 -0500120 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
Dan Williams6e66f032007-12-11 12:42:16 -0500121 if (ret)
122 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200123
Dan Williams6e66f032007-12-11 12:42:16 -0500124 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500125
Holger Schurigdac10a92008-01-16 15:55:22 +0100126 /* The firmware release is in an interesting format: the patch
127 * level is in the most significant nibble ... so fix that: */
128 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
129 priv->fwrelease = (priv->fwrelease << 8) |
130 (priv->fwrelease >> 24 & 0xff);
131
132 /* Some firmware capabilities:
133 * CF card firmware 5.0.16p0: cap 0x00000303
134 * USB dongle firmware 5.110.17p2: cap 0x00000303
135 */
Johannes Berge1749612008-10-27 15:59:26 -0700136 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
137 cmd.permanentaddr,
Holger Schurigdac10a92008-01-16 15:55:22 +0100138 priv->fwrelease >> 24 & 0xff,
139 priv->fwrelease >> 16 & 0xff,
140 priv->fwrelease >> 8 & 0xff,
141 priv->fwrelease & 0xff,
142 priv->fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500143 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
144 cmd.hwifversion, cmd.version);
145
146 /* Clamp region code to 8-bit since FW spec indicates that it should
147 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
148 * returns non-zero high 8 bits here.
Marek Vasut15483992009-07-16 19:19:53 +0200149 *
150 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
151 * need to check for this problem and handle it properly.
Dan Williams6e66f032007-12-11 12:42:16 -0500152 */
Marek Vasut15483992009-07-16 19:19:53 +0200153 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
154 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
155 else
156 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
Dan Williams6e66f032007-12-11 12:42:16 -0500157
158 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
159 /* use the region code to search for the index */
160 if (priv->regioncode == lbs_region_code_to_index[i])
161 break;
162 }
163
164 /* if it's unidentified region code, use the default (USA) */
165 if (i >= MRVDRV_MAX_REGION_CODE) {
166 priv->regioncode = 0x10;
167 lbs_pr_info("unidentified region code; using the default (USA)\n");
168 }
169
170 if (priv->current_addr[0] == 0xff)
171 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
172
173 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
174 if (priv->mesh_dev)
175 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
176
177 if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
178 ret = -1;
179 goto out;
180 }
181
Dan Williams6e66f032007-12-11 12:42:16 -0500182out:
Holger Schurig9012b282007-05-25 11:27:16 -0400183 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams6e66f032007-12-11 12:42:16 -0500184 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185}
186
Anna Neal582c1b52008-10-20 16:46:56 -0700187int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
188 struct wol_config *p_wol_config)
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500189{
190 struct cmd_ds_host_sleep cmd_config;
191 int ret;
192
David Woodhouse9fae8992007-12-15 03:46:44 -0500193 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500194 cmd_config.criteria = cpu_to_le32(criteria);
David Woodhouse506e9022007-12-12 20:06:06 -0500195 cmd_config.gpio = priv->wol_gpio;
196 cmd_config.gap = priv->wol_gap;
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500197
Anna Neal582c1b52008-10-20 16:46:56 -0700198 if (p_wol_config != NULL)
199 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
200 sizeof(struct wol_config));
201 else
202 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
203
David Woodhouse689442d2007-12-12 16:00:42 -0500204 ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
David Woodhouse506e9022007-12-12 20:06:06 -0500205 if (!ret) {
Anna Neal582c1b52008-10-20 16:46:56 -0700206 if (criteria) {
207 lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
208 priv->wol_criteria = criteria;
209 } else
210 memcpy((uint8_t *) p_wol_config,
211 (uint8_t *)&cmd_config.wol_conf,
212 sizeof(struct wol_config));
David Woodhouse506e9022007-12-12 20:06:06 -0500213 } else {
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500214 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500215 }
David Woodhouse506e9022007-12-12 20:06:06 -0500216
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500217 return ret;
218}
219EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
220
Holger Schurige98a88d2008-03-19 14:25:58 +0100221static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200222 u16 cmd_action)
223{
224 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200225
Holger Schurig9012b282007-05-25 11:27:16 -0400226 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200227
Dan Williams0aef64d2007-08-02 11:31:18 -0400228 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
David Woodhouse981f1872007-05-25 23:36:54 -0400229 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200230 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200231 psm->action = cpu_to_le16(cmd_action);
232 psm->multipledtim = 0;
David Woodhouse981f1872007-05-25 23:36:54 -0400233 switch (cmd_action) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400234 case CMD_SUBCMD_ENTER_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400235 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236
Holger Schurig252cf0d12007-08-02 13:09:34 -0400237 psm->locallisteninterval = 0;
Holger Schurig97605c32007-08-02 13:09:15 -0400238 psm->nullpktinterval = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200239 psm->multipledtim =
Holger Schurig56c46562007-08-02 13:09:49 -0400240 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241 break;
242
Dan Williams0aef64d2007-08-02 11:31:18 -0400243 case CMD_SUBCMD_EXIT_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400244 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245 break;
246
Dan Williams0aef64d2007-08-02 11:31:18 -0400247 case CMD_SUBCMD_SLEEP_CONFIRMED:
Holger Schurig9012b282007-05-25 11:27:16 -0400248 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 break;
250
251 default:
252 break;
253 }
254
Holger Schurig9012b282007-05-25 11:27:16 -0400255 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200256 return 0;
257}
258
David Woodhouse3fbe1042007-12-17 23:48:31 -0500259int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
260 struct sleep_params *sp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200261{
David Woodhouse3fbe1042007-12-17 23:48:31 -0500262 struct cmd_ds_802_11_sleep_params cmd;
263 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200264
Holger Schurig9012b282007-05-25 11:27:16 -0400265 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266
Dan Williams0aef64d2007-08-02 11:31:18 -0400267 if (cmd_action == CMD_ACT_GET) {
David Woodhouse3fbe1042007-12-17 23:48:31 -0500268 memset(&cmd, 0, sizeof(cmd));
269 } else {
270 cmd.error = cpu_to_le16(sp->sp_error);
271 cmd.offset = cpu_to_le16(sp->sp_offset);
272 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
273 cmd.calcontrol = sp->sp_calcontrol;
274 cmd.externalsleepclk = sp->sp_extsleepclk;
275 cmd.reserved = cpu_to_le16(sp->sp_reserved);
276 }
277 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
278 cmd.action = cpu_to_le16(cmd_action);
279
280 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
281
282 if (!ret) {
283 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
284 "calcontrol 0x%x extsleepclk 0x%x\n",
285 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
286 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
287 cmd.externalsleepclk);
288
289 sp->sp_error = le16_to_cpu(cmd.error);
290 sp->sp_offset = le16_to_cpu(cmd.offset);
291 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
292 sp->sp_calcontrol = cmd.calcontrol;
293 sp->sp_extsleepclk = cmd.externalsleepclk;
294 sp->sp_reserved = le16_to_cpu(cmd.reserved);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200295 }
296
David Woodhouse3fbe1042007-12-17 23:48:31 -0500297 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200298 return 0;
299}
300
Amitkumar Karwar49125452009-09-30 20:04:38 -0700301static int lbs_wait_for_ds_awake(struct lbs_private *priv)
302{
303 int ret = 0;
304
305 lbs_deb_enter(LBS_DEB_CMD);
306
307 if (priv->is_deep_sleep) {
308 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
309 !priv->is_deep_sleep, (10 * HZ))) {
310 lbs_pr_err("ds_awake_q: timer expired\n");
311 ret = -1;
312 }
313 }
314
315 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
316 return ret;
317}
318
319int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
320{
321 int ret = 0;
322
323 lbs_deb_enter(LBS_DEB_CMD);
324
325 if (deep_sleep) {
326 if (priv->is_deep_sleep != 1) {
327 lbs_deb_cmd("deep sleep: sleep\n");
328 BUG_ON(!priv->enter_deep_sleep);
329 ret = priv->enter_deep_sleep(priv);
330 if (!ret) {
331 netif_stop_queue(priv->dev);
332 netif_carrier_off(priv->dev);
333 }
334 } else {
335 lbs_pr_err("deep sleep: already enabled\n");
336 }
337 } else {
338 if (priv->is_deep_sleep) {
339 lbs_deb_cmd("deep sleep: wakeup\n");
340 BUG_ON(!priv->exit_deep_sleep);
341 ret = priv->exit_deep_sleep(priv);
342 if (!ret) {
343 ret = lbs_wait_for_ds_awake(priv);
344 if (ret)
345 lbs_pr_err("deep sleep: wakeup"
346 "failed\n");
347 }
348 }
349 }
350
351 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
352 return ret;
353}
354
Dan Williams39fcf7a2008-09-10 12:49:00 -0400355/**
356 * @brief Set an SNMP MIB value
357 *
358 * @param priv A pointer to struct lbs_private structure
359 * @param oid The OID to set in the firmware
360 * @param val Value to set the OID to
361 *
362 * @return 0 on success, error on failure
363 */
364int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200365{
Dan Williams39fcf7a2008-09-10 12:49:00 -0400366 struct cmd_ds_802_11_snmp_mib cmd;
367 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368
Holger Schurig9012b282007-05-25 11:27:16 -0400369 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370
Dan Williams39fcf7a2008-09-10 12:49:00 -0400371 memset(&cmd, 0, sizeof (cmd));
372 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
373 cmd.action = cpu_to_le16(CMD_ACT_SET);
374 cmd.oid = cpu_to_le16((u16) oid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200375
Dan Williams39fcf7a2008-09-10 12:49:00 -0400376 switch (oid) {
377 case SNMP_MIB_OID_BSS_TYPE:
378 cmd.bufsize = cpu_to_le16(sizeof(u8));
Holger Schurigfef06402009-10-22 15:30:59 +0200379 cmd.value[0] = val;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380 break;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400381 case SNMP_MIB_OID_11D_ENABLE:
382 case SNMP_MIB_OID_FRAG_THRESHOLD:
383 case SNMP_MIB_OID_RTS_THRESHOLD:
384 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
385 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
386 cmd.bufsize = cpu_to_le16(sizeof(u16));
387 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388 break;
389 default:
Dan Williams39fcf7a2008-09-10 12:49:00 -0400390 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
391 ret = -EINVAL;
392 goto out;
393 }
394
395 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
396 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
397
398 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
399
400out:
401 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
402 return ret;
403}
404
405/**
406 * @brief Get an SNMP MIB value
407 *
408 * @param priv A pointer to struct lbs_private structure
409 * @param oid The OID to retrieve from the firmware
410 * @param out_val Location for the returned value
411 *
412 * @return 0 on success, error on failure
413 */
414int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
415{
416 struct cmd_ds_802_11_snmp_mib cmd;
417 int ret;
418
419 lbs_deb_enter(LBS_DEB_CMD);
420
421 memset(&cmd, 0, sizeof (cmd));
422 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
423 cmd.action = cpu_to_le16(CMD_ACT_GET);
424 cmd.oid = cpu_to_le16(oid);
425
426 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
427 if (ret)
428 goto out;
429
430 switch (le16_to_cpu(cmd.bufsize)) {
431 case sizeof(u8):
Holger Schurigfef06402009-10-22 15:30:59 +0200432 *out_val = cmd.value[0];
Dan Williams39fcf7a2008-09-10 12:49:00 -0400433 break;
434 case sizeof(u16):
435 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
436 break;
437 default:
438 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
439 oid, le16_to_cpu(cmd.bufsize));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440 break;
441 }
442
Dan Williams39fcf7a2008-09-10 12:49:00 -0400443out:
444 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
445 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446}
447
Dan Williams87c8c722008-08-19 15:15:35 -0400448/**
449 * @brief Get the min, max, and current TX power
450 *
451 * @param priv A pointer to struct lbs_private structure
452 * @param curlevel Current power level in dBm
453 * @param minlevel Minimum supported power level in dBm (optional)
454 * @param maxlevel Maximum supported power level in dBm (optional)
455 *
456 * @return 0 on success, error on failure
457 */
458int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
459 s16 *maxlevel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460{
Dan Williams87c8c722008-08-19 15:15:35 -0400461 struct cmd_ds_802_11_rf_tx_power cmd;
462 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463
Holger Schurig9012b282007-05-25 11:27:16 -0400464 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200465
Dan Williams87c8c722008-08-19 15:15:35 -0400466 memset(&cmd, 0, sizeof(cmd));
467 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
468 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469
Dan Williams87c8c722008-08-19 15:15:35 -0400470 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
471 if (ret == 0) {
472 *curlevel = le16_to_cpu(cmd.curlevel);
473 if (minlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100474 *minlevel = cmd.minlevel;
Dan Williams87c8c722008-08-19 15:15:35 -0400475 if (maxlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100476 *maxlevel = cmd.maxlevel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477 }
Holger Schurig9012b282007-05-25 11:27:16 -0400478
479 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams87c8c722008-08-19 15:15:35 -0400480 return ret;
481}
482
483/**
484 * @brief Set the TX power
485 *
486 * @param priv A pointer to struct lbs_private structure
487 * @param dbm The desired power level in dBm
488 *
489 * @return 0 on success, error on failure
490 */
491int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
492{
493 struct cmd_ds_802_11_rf_tx_power cmd;
494 int ret;
495
496 lbs_deb_enter(LBS_DEB_CMD);
497
498 memset(&cmd, 0, sizeof(cmd));
499 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
500 cmd.action = cpu_to_le16(CMD_ACT_SET);
501 cmd.curlevel = cpu_to_le16(dbm);
502
503 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
504
505 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
506
507 lbs_deb_leave(LBS_DEB_CMD);
508 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509}
510
Holger Schurige98a88d2008-03-19 14:25:58 +0100511static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400512 u16 cmd_action, void *pdata_buf)
513{
514 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
515
516 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
517 cmd->size =
518 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200519 sizeof(struct cmd_header));
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400520
521 monitor->action = cpu_to_le16(cmd_action);
522 if (cmd_action == CMD_ACT_SET) {
523 monitor->mode =
524 cpu_to_le16((u16) (*(u32 *) pdata_buf));
525 }
526
527 return 0;
528}
529
Dan Williams2dd4b262007-12-11 16:54:15 -0500530/**
531 * @brief Get the radio channel
532 *
533 * @param priv A pointer to struct lbs_private structure
534 *
535 * @return The channel on success, error on failure
536 */
Holger Schuriga3cbfb02009-10-16 17:33:56 +0200537static int lbs_get_channel(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538{
Dan Williams2dd4b262007-12-11 16:54:15 -0500539 struct cmd_ds_802_11_rf_channel cmd;
540 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541
Holger Schurig8ff12da2007-08-02 11:54:31 -0400542 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200543
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200544 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500545 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
546 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547
David Woodhouse689442d2007-12-12 16:00:42 -0500548 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500549 if (ret)
550 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551
Dan Williamscb182a62007-12-11 17:35:51 -0500552 ret = le16_to_cpu(cmd.channel);
553 lbs_deb_cmd("current radio channel is %d\n", ret);
Dan Williams2dd4b262007-12-11 16:54:15 -0500554
555out:
556 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
557 return ret;
558}
559
Holger Schurig73ab1f22008-04-02 16:52:19 +0200560int lbs_update_channel(struct lbs_private *priv)
561{
562 int ret;
563
564 /* the channel in f/w could be out of sync; get the current channel */
565 lbs_deb_enter(LBS_DEB_ASSOC);
566
567 ret = lbs_get_channel(priv);
568 if (ret > 0) {
Holger Schurigc14951f2009-10-22 15:30:50 +0200569 priv->channel = ret;
Holger Schurig73ab1f22008-04-02 16:52:19 +0200570 ret = 0;
571 }
572 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
573 return ret;
574}
575
Dan Williams2dd4b262007-12-11 16:54:15 -0500576/**
577 * @brief Set the radio channel
578 *
579 * @param priv A pointer to struct lbs_private structure
580 * @param channel The desired channel, or 0 to clear a locked channel
581 *
582 * @return 0 on success, error on failure
583 */
584int lbs_set_channel(struct lbs_private *priv, u8 channel)
585{
586 struct cmd_ds_802_11_rf_channel cmd;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530587#ifdef DEBUG
Holger Schurigc14951f2009-10-22 15:30:50 +0200588 u8 old_channel = priv->channel;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530589#endif
Dan Williams2dd4b262007-12-11 16:54:15 -0500590 int ret = 0;
591
592 lbs_deb_enter(LBS_DEB_CMD);
593
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200594 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500595 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
596 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
597 cmd.channel = cpu_to_le16(channel);
598
David Woodhouse689442d2007-12-12 16:00:42 -0500599 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500600 if (ret)
601 goto out;
602
Holger Schurigc14951f2009-10-22 15:30:50 +0200603 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
Dan Williamscb182a62007-12-11 17:35:51 -0500604 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
Holger Schurigc14951f2009-10-22 15:30:50 +0200605 priv->channel);
Dan Williams2dd4b262007-12-11 16:54:15 -0500606
607out:
608 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
609 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610}
611
Holger Schurige98a88d2008-03-19 14:25:58 +0100612static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 u8 cmd_action, void *pdata_buf)
614{
Holger Schurig10078322007-11-15 18:05:47 -0500615 struct lbs_offset_value *offval;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616
Holger Schurig9012b282007-05-25 11:27:16 -0400617 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618
Holger Schurig10078322007-11-15 18:05:47 -0500619 offval = (struct lbs_offset_value *)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000621 switch (le16_to_cpu(cmdptr->command)) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400622 case CMD_MAC_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623 {
624 struct cmd_ds_mac_reg_access *macreg;
625
626 cmdptr->size =
David Woodhouse981f1872007-05-25 23:36:54 -0400627 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200628 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629 macreg =
630 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
631 macreg;
632
633 macreg->action = cpu_to_le16(cmd_action);
634 macreg->offset = cpu_to_le16((u16) offval->offset);
635 macreg->value = cpu_to_le32(offval->value);
636
637 break;
638 }
639
Dan Williams0aef64d2007-08-02 11:31:18 -0400640 case CMD_BBP_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641 {
642 struct cmd_ds_bbp_reg_access *bbpreg;
643
644 cmdptr->size =
645 cpu_to_le16(sizeof
646 (struct cmd_ds_bbp_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200647 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648 bbpreg =
649 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
650 bbpreg;
651
652 bbpreg->action = cpu_to_le16(cmd_action);
653 bbpreg->offset = cpu_to_le16((u16) offval->offset);
654 bbpreg->value = (u8) offval->value;
655
656 break;
657 }
658
Dan Williams0aef64d2007-08-02 11:31:18 -0400659 case CMD_RF_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200660 {
661 struct cmd_ds_rf_reg_access *rfreg;
662
663 cmdptr->size =
664 cpu_to_le16(sizeof
665 (struct cmd_ds_rf_reg_access) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200666 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667 rfreg =
668 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
669 rfreg;
670
671 rfreg->action = cpu_to_le16(cmd_action);
672 rfreg->offset = cpu_to_le16((u16) offval->offset);
673 rfreg->value = (u8) offval->value;
674
675 break;
676 }
677
678 default:
679 break;
680 }
681
Holger Schurig9012b282007-05-25 11:27:16 -0400682 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 return 0;
684}
685
David Woodhouse681ffbb2007-12-15 20:04:54 -0500686static void lbs_queue_cmd(struct lbs_private *priv,
687 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688{
689 unsigned long flags;
David Woodhouse681ffbb2007-12-15 20:04:54 -0500690 int addtail = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691
Holger Schurig8ff12da2007-08-02 11:54:31 -0400692 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693
David Woodhousec4ab4122007-12-15 00:41:51 -0500694 if (!cmdnode) {
695 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200696 goto done;
697 }
David Woodhoused9896ee2007-12-15 00:09:25 -0500698 if (!cmdnode->cmdbuf->size) {
699 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
700 goto done;
701 }
David Woodhouseae125bf2007-12-15 04:22:52 -0500702 cmdnode->result = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703
704 /* Exit_PS command needs to be queued in the header always. */
Dan Williamsddac4522007-12-11 13:49:39 -0500705 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
David Woodhouse38bfab12007-12-16 23:26:54 -0500706 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
Dan Williamsddac4522007-12-11 13:49:39 -0500707
Dan Williams0aef64d2007-08-02 11:31:18 -0400708 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000709 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 addtail = 0;
711 }
712 }
713
David Woodhouseaa21c002007-12-08 20:04:36 +0000714 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715
David Woodhouseac472462007-12-08 00:35:00 +0000716 if (addtail)
David Woodhouseaa21c002007-12-08 20:04:36 +0000717 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
David Woodhouseac472462007-12-08 00:35:00 +0000718 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000719 list_add(&cmdnode->list, &priv->cmdpendingq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720
David Woodhouseaa21c002007-12-08 20:04:36 +0000721 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722
Holger Schurig8ff12da2007-08-02 11:54:31 -0400723 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
David Woodhousec4ab4122007-12-15 00:41:51 -0500724 le16_to_cpu(cmdnode->cmdbuf->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725
726done:
Holger Schurig8ff12da2007-08-02 11:54:31 -0400727 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728}
729
David Woodhouse18c52e72007-12-17 16:03:58 -0500730static void lbs_submit_command(struct lbs_private *priv,
731 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732{
733 unsigned long flags;
Dan Williamsddac4522007-12-11 13:49:39 -0500734 struct cmd_header *cmd;
David Woodhouse18c52e72007-12-17 16:03:58 -0500735 uint16_t cmdsize;
736 uint16_t command;
Holger Schurig57962f02008-05-14 16:30:28 +0200737 int timeo = 3 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500738 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739
Holger Schurig8ff12da2007-08-02 11:54:31 -0400740 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741
Dan Williamsddac4522007-12-11 13:49:39 -0500742 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
David Woodhouseaa21c002007-12-08 20:04:36 +0000744 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +0000745 priv->cur_cmd = cmdnode;
746 priv->cur_cmd_retcode = 0;
747 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200748
Dan Williamsddac4522007-12-11 13:49:39 -0500749 cmdsize = le16_to_cpu(cmd->size);
750 command = le16_to_cpu(cmd->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200751
David Woodhouse18c52e72007-12-17 16:03:58 -0500752 /* These commands take longer */
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400753 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
Holger Schurig57962f02008-05-14 16:30:28 +0200754 timeo = 5 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500755
Holger Schurige5225b32008-03-26 10:04:44 +0100756 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
757 command, le16_to_cpu(cmd->seqnum), cmdsize);
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100758 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
Holger Schurig8ff12da2007-08-02 11:54:31 -0400759
Dan Williamsddac4522007-12-11 13:49:39 -0500760 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
David Woodhouse18c52e72007-12-17 16:03:58 -0500761
David Woodhoused9896ee2007-12-15 00:09:25 -0500762 if (ret) {
763 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
David Woodhouse18c52e72007-12-17 16:03:58 -0500764 /* Let the timer kick in and retry, and potentially reset
765 the whole thing if the condition persists */
Holger Schurig57962f02008-05-14 16:30:28 +0200766 timeo = HZ/4;
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100767 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768
Amitkumar Karwar49125452009-09-30 20:04:38 -0700769 if (command == CMD_802_11_DEEP_SLEEP) {
770 if (priv->is_auto_deep_sleep_enabled) {
771 priv->wakeup_dev_required = 1;
772 priv->dnld_sent = 0;
773 }
774 priv->is_deep_sleep = 1;
775 lbs_complete_command(priv, cmdnode, 0);
776 } else {
777 /* Setup the timer after transmit command */
778 mod_timer(&priv->command_timer, jiffies + timeo);
779 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200780
David Woodhouse18c52e72007-12-17 16:03:58 -0500781 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200782}
783
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784/**
785 * This function inserts command node to cmdfreeq
David Woodhouseaa21c002007-12-08 20:04:36 +0000786 * after cleans it. Requires priv->driver_lock held.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200787 */
David Woodhouse183aeac2007-12-15 01:52:54 -0500788static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500789 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790{
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500791 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500793 if (!cmdnode)
794 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500796 cmdnode->callback = NULL;
797 cmdnode->callback_arg = 0;
798
799 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
800
801 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
802 out:
803 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200804}
805
Holger Schurig69f90322007-11-23 15:43:44 +0100806static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
807 struct cmd_ctrl_node *ptempcmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200808{
809 unsigned long flags;
810
David Woodhouseaa21c002007-12-08 20:04:36 +0000811 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig10078322007-11-15 18:05:47 -0500812 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
David Woodhouseaa21c002007-12-08 20:04:36 +0000813 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200814}
815
David Woodhouse183aeac2007-12-15 01:52:54 -0500816void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
817 int result)
818{
819 if (cmd == priv->cur_cmd)
820 priv->cur_cmd_retcode = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500821
David Woodhouseae125bf2007-12-15 04:22:52 -0500822 cmd->result = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500823 cmd->cmdwaitqwoken = 1;
824 wake_up_interruptible(&cmd->cmdwait_q);
825
Holger Schurig8db4a2b2008-03-19 10:11:00 +0100826 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
David Woodhousead12d0f2007-12-15 02:06:16 -0500827 __lbs_cleanup_and_insert_cmd(priv, cmd);
David Woodhouse183aeac2007-12-15 01:52:54 -0500828 priv->cur_cmd = NULL;
829}
830
Dan Williamsd5db2df2008-08-21 17:51:07 -0400831int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832{
David Woodhousea7c45892007-12-17 22:43:48 -0500833 struct cmd_ds_802_11_radio_control cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400834 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200835
Holger Schurig9012b282007-05-25 11:27:16 -0400836 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200837
David Woodhousea7c45892007-12-17 22:43:48 -0500838 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
839 cmd.action = cpu_to_le16(CMD_ACT_SET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840
Dan Williamsd5db2df2008-08-21 17:51:07 -0400841 /* Only v8 and below support setting the preamble */
842 if (priv->fwrelease < 0x09000000) {
843 switch (preamble) {
844 case RADIO_PREAMBLE_SHORT:
845 if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
846 goto out;
847 /* Fall through */
848 case RADIO_PREAMBLE_AUTO:
849 case RADIO_PREAMBLE_LONG:
850 cmd.control = cpu_to_le16(preamble);
851 break;
852 default:
853 goto out;
854 }
David Woodhousea7c45892007-12-17 22:43:48 -0500855 }
856
Dan Williamsd5db2df2008-08-21 17:51:07 -0400857 if (radio_on)
858 cmd.control |= cpu_to_le16(0x1);
859 else {
860 cmd.control &= cpu_to_le16(~0x1);
861 priv->txpower_cur = 0;
862 }
David Woodhousea7c45892007-12-17 22:43:48 -0500863
Dan Williamsd5db2df2008-08-21 17:51:07 -0400864 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
865 radio_on ? "ON" : "OFF", preamble);
866
867 priv->radio_on = radio_on;
David Woodhousea7c45892007-12-17 22:43:48 -0500868
869 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200870
Dan Williamsd5db2df2008-08-21 17:51:07 -0400871out:
Holger Schurig9012b282007-05-25 11:27:16 -0400872 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200873 return ret;
874}
875
Holger Schurigc97329e2008-03-18 11:20:21 +0100876void lbs_set_mac_control(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877{
Holger Schurig835d3ac2008-03-12 16:05:40 +0100878 struct cmd_ds_mac_control cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200879
Holger Schurig9012b282007-05-25 11:27:16 -0400880 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200881
Holger Schurig835d3ac2008-03-12 16:05:40 +0100882 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurigd9e97782008-03-12 16:06:43 +0100883 cmd.action = cpu_to_le16(priv->mac_control);
Holger Schurig835d3ac2008-03-12 16:05:40 +0100884 cmd.reserved = 0;
885
David Woodhouse75bf45a2008-05-20 13:32:45 +0100886 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200887
Holger Schurigc97329e2008-03-18 11:20:21 +0100888 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200889}
890
891/**
892 * @brief This function prepare the command before send to firmware.
893 *
Holger Schurig69f90322007-11-23 15:43:44 +0100894 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200895 * @param cmd_no command number
896 * @param cmd_action command action: GET or SET
897 * @param wait_option wait option: wait response or not
898 * @param cmd_oid cmd oid: treated as sub command
899 * @param pdata_buf A pointer to informaion buffer
900 * @return 0 or -1
901 */
Holger Schurig69f90322007-11-23 15:43:44 +0100902int lbs_prepare_and_send_command(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200903 u16 cmd_no,
904 u16 cmd_action,
905 u16 wait_option, u32 cmd_oid, void *pdata_buf)
906{
907 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200908 struct cmd_ctrl_node *cmdnode;
909 struct cmd_ds_command *cmdptr;
910 unsigned long flags;
911
Holger Schurig8ff12da2007-08-02 11:54:31 -0400912 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200913
David Woodhouseaa21c002007-12-08 20:04:36 +0000914 if (!priv) {
915 lbs_deb_host("PREP_CMD: priv is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200916 ret = -1;
917 goto done;
918 }
919
David Woodhouseaa21c002007-12-08 20:04:36 +0000920 if (priv->surpriseremoved) {
Holger Schurig8ff12da2007-08-02 11:54:31 -0400921 lbs_deb_host("PREP_CMD: card removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200922 ret = -1;
923 goto done;
924 }
925
Amitkumar Karwar63f275d2009-10-06 19:20:28 -0700926 if (!lbs_is_cmd_allowed(priv)) {
927 ret = -EBUSY;
928 goto done;
929 }
930
Holger Schurig0d61d042007-12-05 17:58:06 +0100931 cmdnode = lbs_get_cmd_ctrl_node(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200932
933 if (cmdnode == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -0400934 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200935
936 /* Wake up main thread to execute next command */
Dan Williamsfe336152007-08-02 11:32:25 -0400937 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200938 ret = -1;
939 goto done;
940 }
941
Holger Schurige98a88d2008-03-19 14:25:58 +0100942 cmdnode->callback = NULL;
943 cmdnode->callback_arg = (unsigned long)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944
Dan Williamsddac4522007-12-11 13:49:39 -0500945 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946
Holger Schurig8ff12da2007-08-02 11:54:31 -0400947 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200948
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949 /* Set sequence number, command and INT option */
David Woodhouseaa21c002007-12-08 20:04:36 +0000950 priv->seqnum++;
951 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952
David Woodhouse981f1872007-05-25 23:36:54 -0400953 cmdptr->command = cpu_to_le16(cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954 cmdptr->result = 0;
955
956 switch (cmd_no) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400957 case CMD_802_11_PS_MODE:
Holger Schurige98a88d2008-03-19 14:25:58 +0100958 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959 break;
960
Dan Williams0aef64d2007-08-02 11:31:18 -0400961 case CMD_MAC_REG_ACCESS:
962 case CMD_BBP_REG_ACCESS:
963 case CMD_RF_REG_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +0100964 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200965 break;
966
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400967 case CMD_802_11_MONITOR_MODE:
Holger Schurige98a88d2008-03-19 14:25:58 +0100968 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400969 cmd_action, pdata_buf);
970 break;
971
Dan Williams0aef64d2007-08-02 11:31:18 -0400972 case CMD_802_11_RSSI:
Holger Schurig10078322007-11-15 18:05:47 -0500973 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974 break;
975
Dan Williams0aef64d2007-08-02 11:31:18 -0400976 case CMD_802_11_SET_AFC:
977 case CMD_802_11_GET_AFC:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200978
979 cmdptr->command = cpu_to_le16(cmd_no);
David Woodhouse981f1872007-05-25 23:36:54 -0400980 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200981 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982
983 memmove(&cmdptr->params.afc,
984 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
985
986 ret = 0;
987 goto done;
988
Dan Williams0aef64d2007-08-02 11:31:18 -0400989 case CMD_802_11_TPC_CFG:
990 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200991 cmdptr->size =
992 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200993 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200994
995 memmove(&cmdptr->params.tpccfg,
996 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
997
998 ret = 0;
999 break;
David Woodhouse5844d122007-12-18 02:01:37 -05001000
Holger Schurig4143a232009-12-02 15:26:02 +01001001#ifdef CONFIG_LIBERTAS_MESH
1002
Dan Williams0aef64d2007-08-02 11:31:18 -04001003 case CMD_BT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001004 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005 break;
1006
Dan Williams0aef64d2007-08-02 11:31:18 -04001007 case CMD_FWT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001008 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001009 break;
1010
Holger Schurig4143a232009-12-02 15:26:02 +01001011#endif
1012
Brajesh Dave96287ac2007-11-20 17:44:28 -05001013 case CMD_802_11_BEACON_CTRL:
1014 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1015 break;
Amitkumar Karwar49125452009-09-30 20:04:38 -07001016 case CMD_802_11_DEEP_SLEEP:
1017 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001018 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
Amitkumar Karwar49125452009-09-30 20:04:38 -07001019 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 default:
David Woodhousee37fc6e2008-05-20 11:47:16 +01001021 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022 ret = -1;
1023 break;
1024 }
1025
1026 /* return error, since the command preparation failed */
1027 if (ret != 0) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001028 lbs_deb_host("PREP_CMD: command preparation failed\n");
Holger Schurig10078322007-11-15 18:05:47 -05001029 lbs_cleanup_and_insert_cmd(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001030 ret = -1;
1031 goto done;
1032 }
1033
1034 cmdnode->cmdwaitqwoken = 0;
1035
David Woodhouse681ffbb2007-12-15 20:04:54 -05001036 lbs_queue_cmd(priv, cmdnode);
Dan Williamsfe336152007-08-02 11:32:25 -04001037 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001038
Dan Williams0aef64d2007-08-02 11:31:18 -04001039 if (wait_option & CMD_OPTION_WAITFORRSP) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001040 lbs_deb_host("PREP_CMD: wait for response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041 might_sleep();
1042 wait_event_interruptible(cmdnode->cmdwait_q,
1043 cmdnode->cmdwaitqwoken);
1044 }
1045
David Woodhouseaa21c002007-12-08 20:04:36 +00001046 spin_lock_irqsave(&priv->driver_lock, flags);
1047 if (priv->cur_cmd_retcode) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001048 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
David Woodhouseaa21c002007-12-08 20:04:36 +00001049 priv->cur_cmd_retcode);
1050 priv->cur_cmd_retcode = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 ret = -1;
1052 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001053 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054
1055done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001056 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001057 return ret;
1058}
1059
1060/**
1061 * @brief This function allocates the command buffer and link
1062 * it to command free queue.
1063 *
Holger Schurig69f90322007-11-23 15:43:44 +01001064 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001065 * @return 0 or -1
1066 */
Holger Schurig69f90322007-11-23 15:43:44 +01001067int lbs_allocate_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001068{
1069 int ret = 0;
Dan Williamsddac4522007-12-11 13:49:39 -05001070 u32 bufsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001071 u32 i;
Dan Williamsddac4522007-12-11 13:49:39 -05001072 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073
Holger Schurig8ff12da2007-08-02 11:54:31 -04001074 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001075
Dan Williamsddac4522007-12-11 13:49:39 -05001076 /* Allocate and initialize the command array */
1077 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1078 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001079 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080 ret = -1;
1081 goto done;
1082 }
Dan Williamsddac4522007-12-11 13:49:39 -05001083 priv->cmd_array = cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084
Dan Williamsddac4522007-12-11 13:49:39 -05001085 /* Allocate and initialize each command buffer in the command array */
1086 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1087 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1088 if (!cmdarray[i].cmdbuf) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001089 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090 ret = -1;
1091 goto done;
1092 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001093 }
1094
Dan Williamsddac4522007-12-11 13:49:39 -05001095 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1096 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1097 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001099 ret = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001100
1101done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001102 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 return ret;
1104}
1105
1106/**
1107 * @brief This function frees the command buffer.
1108 *
Holger Schurig69f90322007-11-23 15:43:44 +01001109 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001110 * @return 0 or -1
1111 */
Holger Schurig69f90322007-11-23 15:43:44 +01001112int lbs_free_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001113{
Dan Williamsddac4522007-12-11 13:49:39 -05001114 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115 unsigned int i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001116
Holger Schurig8ff12da2007-08-02 11:54:31 -04001117 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118
1119 /* need to check if cmd array is allocated or not */
David Woodhouseaa21c002007-12-08 20:04:36 +00001120 if (priv->cmd_array == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001121 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122 goto done;
1123 }
1124
Dan Williamsddac4522007-12-11 13:49:39 -05001125 cmdarray = priv->cmd_array;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001126
1127 /* Release shared memory buffers */
Dan Williamsddac4522007-12-11 13:49:39 -05001128 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1129 if (cmdarray[i].cmdbuf) {
1130 kfree(cmdarray[i].cmdbuf);
1131 cmdarray[i].cmdbuf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132 }
1133 }
1134
1135 /* Release cmd_ctrl_node */
David Woodhouseaa21c002007-12-08 20:04:36 +00001136 if (priv->cmd_array) {
1137 kfree(priv->cmd_array);
1138 priv->cmd_array = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001139 }
1140
1141done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001142 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001143 return 0;
1144}
1145
1146/**
1147 * @brief This function gets a free command node if available in
1148 * command free queue.
1149 *
Holger Schurig69f90322007-11-23 15:43:44 +01001150 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1152 */
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001153static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001154{
1155 struct cmd_ctrl_node *tempnode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156 unsigned long flags;
1157
Holger Schurig8ff12da2007-08-02 11:54:31 -04001158 lbs_deb_enter(LBS_DEB_HOST);
1159
David Woodhouseaa21c002007-12-08 20:04:36 +00001160 if (!priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001161 return NULL;
1162
David Woodhouseaa21c002007-12-08 20:04:36 +00001163 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164
David Woodhouseaa21c002007-12-08 20:04:36 +00001165 if (!list_empty(&priv->cmdfreeq)) {
1166 tempnode = list_first_entry(&priv->cmdfreeq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001167 struct cmd_ctrl_node, list);
1168 list_del(&tempnode->list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001169 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001170 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 tempnode = NULL;
1172 }
1173
David Woodhouseaa21c002007-12-08 20:04:36 +00001174 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175
Holger Schurig8ff12da2007-08-02 11:54:31 -04001176 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177 return tempnode;
1178}
1179
1180/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001181 * @brief This function executes next command in command
Nick Andrew877d0312009-01-26 11:06:57 +01001182 * pending queue. It will put firmware back to PS mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001183 * if applicable.
1184 *
Holger Schurig69f90322007-11-23 15:43:44 +01001185 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001186 * @return 0 or -1
1187 */
Holger Schurig69f90322007-11-23 15:43:44 +01001188int lbs_execute_next_command(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001189{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001190 struct cmd_ctrl_node *cmdnode = NULL;
Dan Williamsddac4522007-12-11 13:49:39 -05001191 struct cmd_header *cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192 unsigned long flags;
1193 int ret = 0;
1194
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001195 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1196 * only caller to us is lbs_thread() and we get even when a
1197 * data packet is received */
Holger Schurig8ff12da2007-08-02 11:54:31 -04001198 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001199
David Woodhouseaa21c002007-12-08 20:04:36 +00001200 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001201
David Woodhouseaa21c002007-12-08 20:04:36 +00001202 if (priv->cur_cmd) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001203 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001204 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001205 ret = -1;
1206 goto done;
1207 }
1208
David Woodhouseaa21c002007-12-08 20:04:36 +00001209 if (!list_empty(&priv->cmdpendingq)) {
1210 cmdnode = list_first_entry(&priv->cmdpendingq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001211 struct cmd_ctrl_node, list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001212 }
1213
David Woodhouseaa21c002007-12-08 20:04:36 +00001214 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001215
1216 if (cmdnode) {
Dan Williamsddac4522007-12-11 13:49:39 -05001217 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001218
Dan Williamsddac4522007-12-11 13:49:39 -05001219 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001220 if ((priv->psstate == PS_STATE_SLEEP) ||
1221 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001222 lbs_deb_host(
1223 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001224 le16_to_cpu(cmd->command),
David Woodhouseaa21c002007-12-08 20:04:36 +00001225 priv->psstate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001226 ret = -1;
1227 goto done;
1228 }
Holger Schurig8ff12da2007-08-02 11:54:31 -04001229 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
Dan Williamsddac4522007-12-11 13:49:39 -05001230 "0x%04x in psstate %d\n",
1231 le16_to_cpu(cmd->command), priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +00001232 } else if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001233 /*
1234 * 1. Non-PS command:
1235 * Queue it. set needtowakeup to TRUE if current state
Holger Schurig10078322007-11-15 18:05:47 -05001236 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001237 * 2. PS command but not Exit_PS:
1238 * Ignore it.
1239 * 3. PS command Exit_PS:
1240 * Set needtowakeup to TRUE if current state is SLEEP,
1241 * otherwise send this command down to firmware
1242 * immediately.
1243 */
Dan Williamsddac4522007-12-11 13:49:39 -05001244 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001245 /* Prepare to send Exit PS,
1246 * this non PS command will be sent later */
David Woodhouseaa21c002007-12-08 20:04:36 +00001247 if ((priv->psstate == PS_STATE_SLEEP)
1248 || (priv->psstate == PS_STATE_PRE_SLEEP)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001249 ) {
1250 /* w/ new scheme, it will not reach here.
1251 since it is blocked in main_thread. */
David Woodhouseaa21c002007-12-08 20:04:36 +00001252 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001253 } else
Holger Schurig10078322007-11-15 18:05:47 -05001254 lbs_ps_wakeup(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255
1256 ret = 0;
1257 goto done;
1258 } else {
1259 /*
1260 * PS command. Ignore it if it is not Exit_PS.
1261 * otherwise send it down immediately.
1262 */
David Woodhouse38bfab12007-12-16 23:26:54 -05001263 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001264
Holger Schurig8ff12da2007-08-02 11:54:31 -04001265 lbs_deb_host(
1266 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001267 psm->action);
1268 if (psm->action !=
Dan Williams0aef64d2007-08-02 11:31:18 -04001269 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001270 lbs_deb_host(
1271 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001272 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001273 spin_lock_irqsave(&priv->driver_lock, flags);
1274 lbs_complete_command(priv, cmdnode, 0);
1275 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001276
1277 ret = 0;
1278 goto done;
1279 }
1280
David Woodhouseaa21c002007-12-08 20:04:36 +00001281 if ((priv->psstate == PS_STATE_SLEEP) ||
1282 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001283 lbs_deb_host(
1284 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001285 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001286 spin_lock_irqsave(&priv->driver_lock, flags);
1287 lbs_complete_command(priv, cmdnode, 0);
1288 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +00001289 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290
1291 ret = 0;
1292 goto done;
1293 }
1294
Holger Schurig8ff12da2007-08-02 11:54:31 -04001295 lbs_deb_host(
1296 "EXEC_NEXT_CMD: sending EXIT_PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297 }
1298 }
Li Zefanabe3ed12007-12-06 13:01:21 +01001299 list_del(&cmdnode->list);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001300 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001301 le16_to_cpu(cmd->command));
David Woodhoused9896ee2007-12-15 00:09:25 -05001302 lbs_submit_command(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303 } else {
1304 /*
1305 * check if in power save mode, if yes, put the device back
1306 * to PS mode
1307 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001308 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1309 (priv->psstate == PS_STATE_FULL_POWER) &&
1310 ((priv->connect_status == LBS_CONNECTED) ||
Holger Schurig602114a2009-12-02 15:26:01 +01001311 lbs_mesh_connected(priv))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001312 if (priv->secinfo.WPAenabled ||
1313 priv->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001314 /* check for valid WPA group keys */
David Woodhouseaa21c002007-12-08 20:04:36 +00001315 if (priv->wpa_mcast_key.len ||
1316 priv->wpa_unicast_key.len) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001317 lbs_deb_host(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1319 " go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001320 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001321 }
1322 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001323 lbs_deb_host(
1324 "EXEC_NEXT_CMD: cmdpendingq empty, "
1325 "go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001326 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001327 }
1328 }
1329 }
1330
1331 ret = 0;
1332done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001333 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001334 return ret;
1335}
1336
Holger Schurigf539f2e2008-03-26 13:22:11 +01001337static void lbs_send_confirmsleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001338{
1339 unsigned long flags;
Holger Schurigf539f2e2008-03-26 13:22:11 +01001340 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001341
Holger Schurig8ff12da2007-08-02 11:54:31 -04001342 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001343 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1344 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001345
Holger Schurigf539f2e2008-03-26 13:22:11 +01001346 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1347 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001348 if (ret) {
Holger Schurigf539f2e2008-03-26 13:22:11 +01001349 lbs_pr_alert("confirm_sleep failed\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001350 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001351 }
Holger Schurig7919b892008-04-01 14:50:43 +02001352
1353 spin_lock_irqsave(&priv->driver_lock, flags);
1354
Holger Schuriga01f5452008-06-04 11:10:40 +02001355 /* We don't get a response on the sleep-confirmation */
1356 priv->dnld_sent = DNLD_RES_RECEIVED;
1357
Holger Schurig7919b892008-04-01 14:50:43 +02001358 /* If nothing to do, go back to sleep (?) */
1359 if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1360 priv->psstate = PS_STATE_SLEEP;
1361
1362 spin_unlock_irqrestore(&priv->driver_lock, flags);
1363
1364out:
Holger Schurigf539f2e2008-03-26 13:22:11 +01001365 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001366}
1367
Holger Schurig69f90322007-11-23 15:43:44 +01001368void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001369{
Holger Schurig8ff12da2007-08-02 11:54:31 -04001370 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001371
1372 /*
1373 * PS is currently supported only in Infrastructure mode
1374 * Remove this check if it is to be supported in IBSS mode also
1375 */
1376
Holger Schurig10078322007-11-15 18:05:47 -05001377 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001378 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001379
Holger Schurig8ff12da2007-08-02 11:54:31 -04001380 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001381}
1382
1383/**
Holger Schurig8ff12da2007-08-02 11:54:31 -04001384 * @brief This function sends Exit_PS command to firmware.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001385 *
Holger Schurig69f90322007-11-23 15:43:44 +01001386 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001387 * @param wait_option wait response or not
1388 * @return n/a
1389 */
Holger Schurig69f90322007-11-23 15:43:44 +01001390void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391{
David Woodhouse981f1872007-05-25 23:36:54 -04001392 __le32 Localpsmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001393
Holger Schurig8ff12da2007-08-02 11:54:31 -04001394 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001395
Holger Schurig10078322007-11-15 18:05:47 -05001396 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001397
Holger Schurig10078322007-11-15 18:05:47 -05001398 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001399 CMD_SUBCMD_EXIT_PS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 wait_option, 0, &Localpsmode);
1401
Holger Schurig8ff12da2007-08-02 11:54:31 -04001402 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001403}
1404
1405/**
1406 * @brief This function checks condition and prepares to
1407 * send sleep confirm command to firmware if ok.
1408 *
Holger Schurig69f90322007-11-23 15:43:44 +01001409 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410 * @param psmode Power Saving mode
1411 * @return n/a
1412 */
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001413void lbs_ps_confirm_sleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001414{
1415 unsigned long flags =0;
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001416 int allowed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001417
Holger Schurig8ff12da2007-08-02 11:54:31 -04001418 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001419
Holger Schuriga01f5452008-06-04 11:10:40 +02001420 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig634b8f42007-05-25 13:05:16 -04001421 if (priv->dnld_sent) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001422 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001423 lbs_deb_host("dnld_sent was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424 }
1425
Holger Schurig7919b892008-04-01 14:50:43 +02001426 /* In-progress command? */
David Woodhouseaa21c002007-12-08 20:04:36 +00001427 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001428 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001429 lbs_deb_host("cur_cmd was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001430 }
Holger Schurig7919b892008-04-01 14:50:43 +02001431
1432 /* Pending events or command responses? */
1433 if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001434 allowed = 0;
Holger Schurig7919b892008-04-01 14:50:43 +02001435 lbs_deb_host("pending events or command responses\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001436 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001437 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001438
1439 if (allowed) {
Holger Schurig10078322007-11-15 18:05:47 -05001440 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
Holger Schurigf539f2e2008-03-26 13:22:11 +01001441 lbs_send_confirmsleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001442 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001443 lbs_deb_host("sleep confirm has been delayed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001444 }
1445
Holger Schurig8ff12da2007-08-02 11:54:31 -04001446 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001447}
Holger Schurig675787e2007-12-05 17:58:11 +01001448
1449
Anna Neal0112c9e2008-09-11 11:17:25 -07001450/**
1451 * @brief Configures the transmission power control functionality.
1452 *
1453 * @param priv A pointer to struct lbs_private structure
1454 * @param enable Transmission power control enable
1455 * @param p0 Power level when link quality is good (dBm).
1456 * @param p1 Power level when link quality is fair (dBm).
1457 * @param p2 Power level when link quality is poor (dBm).
1458 * @param usesnr Use Signal to Noise Ratio in TPC
1459 *
1460 * @return 0 on success
1461 */
1462int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1463 int8_t p2, int usesnr)
1464{
1465 struct cmd_ds_802_11_tpc_cfg cmd;
1466 int ret;
1467
1468 memset(&cmd, 0, sizeof(cmd));
1469 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1470 cmd.action = cpu_to_le16(CMD_ACT_SET);
1471 cmd.enable = !!enable;
Anna Neal3ed6e082008-09-26 11:34:35 -04001472 cmd.usesnr = !!usesnr;
Anna Neal0112c9e2008-09-11 11:17:25 -07001473 cmd.P0 = p0;
1474 cmd.P1 = p1;
1475 cmd.P2 = p2;
1476
1477 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1478
1479 return ret;
1480}
1481
1482/**
1483 * @brief Configures the power adaptation settings.
1484 *
1485 * @param priv A pointer to struct lbs_private structure
1486 * @param enable Power adaptation enable
1487 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1488 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1489 * @param p2 Power level for 48 and 54 Mbps (dBm).
1490 *
1491 * @return 0 on Success
1492 */
1493
1494int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1495 int8_t p1, int8_t p2)
1496{
1497 struct cmd_ds_802_11_pa_cfg cmd;
1498 int ret;
1499
1500 memset(&cmd, 0, sizeof(cmd));
1501 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1502 cmd.action = cpu_to_le16(CMD_ACT_SET);
1503 cmd.enable = !!enable;
1504 cmd.P0 = p0;
1505 cmd.P1 = p1;
1506 cmd.P2 = p2;
1507
1508 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1509
1510 return ret;
1511}
1512
1513
Holger Schurig6d898b12009-10-14 16:49:53 +02001514struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001515 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1516 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1517 unsigned long callback_arg)
Holger Schurig675787e2007-12-05 17:58:11 +01001518{
Holger Schurig675787e2007-12-05 17:58:11 +01001519 struct cmd_ctrl_node *cmdnode;
Holger Schurig675787e2007-12-05 17:58:11 +01001520
1521 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurig675787e2007-12-05 17:58:11 +01001522
David Woodhouseaa21c002007-12-08 20:04:36 +00001523 if (priv->surpriseremoved) {
Holger Schurig675787e2007-12-05 17:58:11 +01001524 lbs_deb_host("PREP_CMD: card removed\n");
David Woodhouse3399ea52007-12-15 03:09:33 -05001525 cmdnode = ERR_PTR(-ENOENT);
Holger Schurig675787e2007-12-05 17:58:11 +01001526 goto done;
1527 }
1528
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001529 if (!lbs_is_cmd_allowed(priv)) {
1530 cmdnode = ERR_PTR(-EBUSY);
1531 goto done;
1532 }
1533
Holger Schurig675787e2007-12-05 17:58:11 +01001534 cmdnode = lbs_get_cmd_ctrl_node(priv);
Holger Schurig675787e2007-12-05 17:58:11 +01001535 if (cmdnode == NULL) {
1536 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1537
1538 /* Wake up main thread to execute next command */
1539 wake_up_interruptible(&priv->waitq);
David Woodhouse3399ea52007-12-15 03:09:33 -05001540 cmdnode = ERR_PTR(-ENOBUFS);
Holger Schurig675787e2007-12-05 17:58:11 +01001541 goto done;
1542 }
1543
David Woodhouse448a51a2007-12-08 00:59:54 +00001544 cmdnode->callback = callback;
David Woodhouse1309b552007-12-10 13:36:10 -05001545 cmdnode->callback_arg = callback_arg;
Holger Schurig675787e2007-12-05 17:58:11 +01001546
Dan Williams7ad994d2007-12-11 12:33:30 -05001547 /* Copy the incoming command to the buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001548 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
Dan Williams7ad994d2007-12-11 12:33:30 -05001549
Holger Schurig675787e2007-12-05 17:58:11 +01001550 /* Set sequence number, clean result, move to buffer */
David Woodhouseaa21c002007-12-08 20:04:36 +00001551 priv->seqnum++;
Dan Williamsddac4522007-12-11 13:49:39 -05001552 cmdnode->cmdbuf->command = cpu_to_le16(command);
1553 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1554 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1555 cmdnode->cmdbuf->result = 0;
Holger Schurig675787e2007-12-05 17:58:11 +01001556
1557 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1558
Holger Schurig675787e2007-12-05 17:58:11 +01001559 cmdnode->cmdwaitqwoken = 0;
David Woodhouse681ffbb2007-12-15 20:04:54 -05001560 lbs_queue_cmd(priv, cmdnode);
Holger Schurig675787e2007-12-05 17:58:11 +01001561 wake_up_interruptible(&priv->waitq);
1562
David Woodhouse3399ea52007-12-15 03:09:33 -05001563 done:
1564 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1565 return cmdnode;
1566}
1567
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001568void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1569 struct cmd_header *in_cmd, int in_cmd_size)
1570{
1571 lbs_deb_enter(LBS_DEB_CMD);
1572 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1573 lbs_cmd_async_callback, 0);
1574 lbs_deb_leave(LBS_DEB_CMD);
1575}
1576
David Woodhouse3399ea52007-12-15 03:09:33 -05001577int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1578 struct cmd_header *in_cmd, int in_cmd_size,
1579 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1580 unsigned long callback_arg)
1581{
1582 struct cmd_ctrl_node *cmdnode;
1583 unsigned long flags;
1584 int ret = 0;
1585
1586 lbs_deb_enter(LBS_DEB_HOST);
1587
1588 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1589 callback, callback_arg);
1590 if (IS_ERR(cmdnode)) {
1591 ret = PTR_ERR(cmdnode);
1592 goto done;
1593 }
1594
Holger Schurig675787e2007-12-05 17:58:11 +01001595 might_sleep();
1596 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1597
David Woodhouseaa21c002007-12-08 20:04:36 +00001598 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseae125bf2007-12-15 04:22:52 -05001599 ret = cmdnode->result;
1600 if (ret)
1601 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1602 command, ret);
David Woodhouse3399ea52007-12-15 03:09:33 -05001603
David Woodhousead12d0f2007-12-15 02:06:16 -05001604 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
David Woodhouseaa21c002007-12-08 20:04:36 +00001605 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig675787e2007-12-05 17:58:11 +01001606
1607done:
1608 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1609 return ret;
1610}
Dan Williams14e865b2007-12-10 15:11:23 -05001611EXPORT_SYMBOL_GPL(__lbs_cmd);