blob: 749fbde4fd54fcb270097fc3c86686d1bc619c71 [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>
Holger Schurige93156e2009-10-22 15:30:58 +02009
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include "decl.h"
Kiran Divekare86dc1c2010-06-14 22:01:26 +053011#include "cfg.h"
Dan Williams6e66f032007-12-11 12:42:16 -050012#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013
Holger Schurige93156e2009-10-22 15:30:58 +020014
David Woodhouse2fd6cfe2007-12-11 17:44:10 -050015static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
Holger Schurig0d61d042007-12-05 17:58:06 +010016
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020017/**
Holger Schurig8db4a2b2008-03-19 10:11:00 +010018 * @brief Simple callback that copies response back into command
19 *
20 * @param priv A pointer to struct lbs_private structure
21 * @param extra A pointer to the original command structure for which
22 * 'resp' is a response
23 * @param resp A pointer to the command response
24 *
25 * @return 0 on success, error on failure
26 */
27int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
28 struct cmd_header *resp)
29{
30 struct cmd_header *buf = (void *)extra;
31 uint16_t copy_len;
32
33 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
34 memcpy(buf, resp, copy_len);
35 return 0;
36}
37EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
38
39/**
40 * @brief Simple callback that ignores the result. Use this if
41 * you just want to send a command to the hardware, but don't
42 * care for the result.
43 *
44 * @param priv ignored
45 * @param extra ignored
46 * @param resp ignored
47 *
48 * @return 0 for success
49 */
50static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
51 struct cmd_header *resp)
52{
53 return 0;
54}
55
56
57/**
Dan Williams852e1f22007-12-10 15:24:47 -050058 * @brief Checks whether a command is allowed in Power Save mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020059 *
60 * @param command the command ID
Dan Williams852e1f22007-12-10 15:24:47 -050061 * @return 1 if allowed, 0 if not allowed
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020062 */
Dan Williams852e1f22007-12-10 15:24:47 -050063static u8 is_command_allowed_in_ps(u16 cmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020064{
Dan Williams852e1f22007-12-10 15:24:47 -050065 switch (cmd) {
66 case CMD_802_11_RSSI:
67 return 1;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -070068 case CMD_802_11_HOST_SLEEP_CFG:
69 return 1;
Dan Williams852e1f22007-12-10 15:24:47 -050070 default:
71 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020072 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073 return 0;
74}
75
Dan Williams6e66f032007-12-11 12:42:16 -050076/**
Amitkumar Karwar63f275d2009-10-06 19:20:28 -070077 * @brief This function checks if the command is allowed.
78 *
79 * @param priv A pointer to lbs_private structure
80 * @return allowed or not allowed.
81 */
82
83static int lbs_is_cmd_allowed(struct lbs_private *priv)
84{
85 int ret = 1;
86
87 lbs_deb_enter(LBS_DEB_CMD);
88
89 if (!priv->is_auto_deep_sleep_enabled) {
90 if (priv->is_deep_sleep) {
91 lbs_deb_cmd("command not allowed in deep sleep\n");
92 ret = 0;
93 }
94 }
95
96 lbs_deb_leave(LBS_DEB_CMD);
97 return ret;
98}
99
100/**
Dan Williams6e66f032007-12-11 12:42:16 -0500101 * @brief Updates the hardware details like MAC address and regulatory region
102 *
103 * @param priv A pointer to struct lbs_private structure
104 *
105 * @return 0 on success, error on failure
106 */
107int lbs_update_hw_spec(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200108{
Dan Williams6e66f032007-12-11 12:42:16 -0500109 struct cmd_ds_get_hw_spec cmd;
110 int ret = -1;
111 u32 i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200112
Holger Schurig9012b282007-05-25 11:27:16 -0400113 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200114
Dan Williams6e66f032007-12-11 12:42:16 -0500115 memset(&cmd, 0, sizeof(cmd));
116 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
117 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
David Woodhouse689442d2007-12-12 16:00:42 -0500118 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
Dan Williams6e66f032007-12-11 12:42:16 -0500119 if (ret)
120 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200121
Dan Williams6e66f032007-12-11 12:42:16 -0500122 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500123
Holger Schurigdac10a92008-01-16 15:55:22 +0100124 /* The firmware release is in an interesting format: the patch
125 * level is in the most significant nibble ... so fix that: */
126 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
127 priv->fwrelease = (priv->fwrelease << 8) |
128 (priv->fwrelease >> 24 & 0xff);
129
130 /* Some firmware capabilities:
131 * CF card firmware 5.0.16p0: cap 0x00000303
132 * USB dongle firmware 5.110.17p2: cap 0x00000303
133 */
Johannes Berge1749612008-10-27 15:59:26 -0700134 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
135 cmd.permanentaddr,
Holger Schurigdac10a92008-01-16 15:55:22 +0100136 priv->fwrelease >> 24 & 0xff,
137 priv->fwrelease >> 16 & 0xff,
138 priv->fwrelease >> 8 & 0xff,
139 priv->fwrelease & 0xff,
140 priv->fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500141 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
142 cmd.hwifversion, cmd.version);
143
144 /* Clamp region code to 8-bit since FW spec indicates that it should
145 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
146 * returns non-zero high 8 bits here.
Marek Vasut15483992009-07-16 19:19:53 +0200147 *
148 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
149 * need to check for this problem and handle it properly.
Dan Williams6e66f032007-12-11 12:42:16 -0500150 */
Marek Vasut15483992009-07-16 19:19:53 +0200151 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
152 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
153 else
154 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
Dan Williams6e66f032007-12-11 12:42:16 -0500155
156 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
157 /* use the region code to search for the index */
158 if (priv->regioncode == lbs_region_code_to_index[i])
159 break;
160 }
161
162 /* if it's unidentified region code, use the default (USA) */
163 if (i >= MRVDRV_MAX_REGION_CODE) {
164 priv->regioncode = 0x10;
165 lbs_pr_info("unidentified region code; using the default (USA)\n");
166 }
167
168 if (priv->current_addr[0] == 0xff)
169 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
170
171 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
172 if (priv->mesh_dev)
173 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
174
Dan Williams6e66f032007-12-11 12:42:16 -0500175out:
Holger Schurig9012b282007-05-25 11:27:16 -0400176 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams6e66f032007-12-11 12:42:16 -0500177 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178}
179
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700180static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
181 struct cmd_header *resp)
182{
183 lbs_deb_enter(LBS_DEB_CMD);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530184 if (priv->is_host_sleep_activated) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700185 priv->is_host_sleep_configured = 0;
186 if (priv->psstate == PS_STATE_FULL_POWER) {
187 priv->is_host_sleep_activated = 0;
188 wake_up_interruptible(&priv->host_sleep_q);
189 }
190 } else {
191 priv->is_host_sleep_configured = 1;
192 }
193 lbs_deb_leave(LBS_DEB_CMD);
194 return 0;
195}
196
Anna Neal582c1b52008-10-20 16:46:56 -0700197int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
198 struct wol_config *p_wol_config)
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500199{
200 struct cmd_ds_host_sleep cmd_config;
201 int ret;
202
David Woodhouse9fae8992007-12-15 03:46:44 -0500203 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500204 cmd_config.criteria = cpu_to_le32(criteria);
David Woodhouse506e9022007-12-12 20:06:06 -0500205 cmd_config.gpio = priv->wol_gpio;
206 cmd_config.gap = priv->wol_gap;
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500207
Anna Neal582c1b52008-10-20 16:46:56 -0700208 if (p_wol_config != NULL)
209 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
210 sizeof(struct wol_config));
211 else
212 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
213
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700214 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
215 le16_to_cpu(cmd_config.hdr.size),
216 lbs_ret_host_sleep_cfg, 0);
David Woodhouse506e9022007-12-12 20:06:06 -0500217 if (!ret) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700218 if (p_wol_config)
Anna Neal582c1b52008-10-20 16:46:56 -0700219 memcpy((uint8_t *) p_wol_config,
220 (uint8_t *)&cmd_config.wol_conf,
221 sizeof(struct wol_config));
David Woodhouse506e9022007-12-12 20:06:06 -0500222 } else {
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500223 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500224 }
David Woodhouse506e9022007-12-12 20:06:06 -0500225
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500226 return ret;
227}
228EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
229
Holger Schurige98a88d2008-03-19 14:25:58 +0100230static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200231 u16 cmd_action)
232{
233 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200234
Holger Schurig9012b282007-05-25 11:27:16 -0400235 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236
Dan Williams0aef64d2007-08-02 11:31:18 -0400237 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
David Woodhouse981f1872007-05-25 23:36:54 -0400238 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200239 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200240 psm->action = cpu_to_le16(cmd_action);
241 psm->multipledtim = 0;
David Woodhouse981f1872007-05-25 23:36:54 -0400242 switch (cmd_action) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400243 case CMD_SUBCMD_ENTER_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400244 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245
Holger Schurig252cf0d2007-08-02 13:09:34 -0400246 psm->locallisteninterval = 0;
Holger Schurig97605c32007-08-02 13:09:15 -0400247 psm->nullpktinterval = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200248 psm->multipledtim =
Holger Schurig56c46562007-08-02 13:09:49 -0400249 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200250 break;
251
Dan Williams0aef64d2007-08-02 11:31:18 -0400252 case CMD_SUBCMD_EXIT_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400253 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254 break;
255
Dan Williams0aef64d2007-08-02 11:31:18 -0400256 case CMD_SUBCMD_SLEEP_CONFIRMED:
Holger Schurig9012b282007-05-25 11:27:16 -0400257 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200258 break;
259
260 default:
261 break;
262 }
263
Holger Schurig9012b282007-05-25 11:27:16 -0400264 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265 return 0;
266}
267
David Woodhouse3fbe1042007-12-17 23:48:31 -0500268int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
269 struct sleep_params *sp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200270{
David Woodhouse3fbe1042007-12-17 23:48:31 -0500271 struct cmd_ds_802_11_sleep_params cmd;
272 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273
Holger Schurig9012b282007-05-25 11:27:16 -0400274 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200275
Dan Williams0aef64d2007-08-02 11:31:18 -0400276 if (cmd_action == CMD_ACT_GET) {
David Woodhouse3fbe1042007-12-17 23:48:31 -0500277 memset(&cmd, 0, sizeof(cmd));
278 } else {
279 cmd.error = cpu_to_le16(sp->sp_error);
280 cmd.offset = cpu_to_le16(sp->sp_offset);
281 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
282 cmd.calcontrol = sp->sp_calcontrol;
283 cmd.externalsleepclk = sp->sp_extsleepclk;
284 cmd.reserved = cpu_to_le16(sp->sp_reserved);
285 }
286 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
287 cmd.action = cpu_to_le16(cmd_action);
288
289 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
290
291 if (!ret) {
292 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
293 "calcontrol 0x%x extsleepclk 0x%x\n",
294 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
295 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
296 cmd.externalsleepclk);
297
298 sp->sp_error = le16_to_cpu(cmd.error);
299 sp->sp_offset = le16_to_cpu(cmd.offset);
300 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
301 sp->sp_calcontrol = cmd.calcontrol;
302 sp->sp_extsleepclk = cmd.externalsleepclk;
303 sp->sp_reserved = le16_to_cpu(cmd.reserved);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 }
305
David Woodhouse3fbe1042007-12-17 23:48:31 -0500306 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307 return 0;
308}
309
Amitkumar Karwar49125452009-09-30 20:04:38 -0700310static int lbs_wait_for_ds_awake(struct lbs_private *priv)
311{
312 int ret = 0;
313
314 lbs_deb_enter(LBS_DEB_CMD);
315
316 if (priv->is_deep_sleep) {
317 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
318 !priv->is_deep_sleep, (10 * HZ))) {
319 lbs_pr_err("ds_awake_q: timer expired\n");
320 ret = -1;
321 }
322 }
323
324 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
325 return ret;
326}
327
328int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
329{
330 int ret = 0;
331
332 lbs_deb_enter(LBS_DEB_CMD);
333
334 if (deep_sleep) {
335 if (priv->is_deep_sleep != 1) {
336 lbs_deb_cmd("deep sleep: sleep\n");
337 BUG_ON(!priv->enter_deep_sleep);
338 ret = priv->enter_deep_sleep(priv);
339 if (!ret) {
340 netif_stop_queue(priv->dev);
341 netif_carrier_off(priv->dev);
342 }
343 } else {
344 lbs_pr_err("deep sleep: already enabled\n");
345 }
346 } else {
347 if (priv->is_deep_sleep) {
348 lbs_deb_cmd("deep sleep: wakeup\n");
349 BUG_ON(!priv->exit_deep_sleep);
350 ret = priv->exit_deep_sleep(priv);
351 if (!ret) {
352 ret = lbs_wait_for_ds_awake(priv);
353 if (ret)
354 lbs_pr_err("deep sleep: wakeup"
355 "failed\n");
356 }
357 }
358 }
359
360 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
361 return ret;
362}
363
Amitkumar Karwar13118432010-07-08 06:43:48 +0530364static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
365 unsigned long dummy,
366 struct cmd_header *cmd)
367{
368 lbs_deb_enter(LBS_DEB_FW);
369 priv->is_host_sleep_activated = 1;
370 wake_up_interruptible(&priv->host_sleep_q);
371 lbs_deb_leave(LBS_DEB_FW);
372 return 0;
373}
374
375int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
376{
377 struct cmd_header cmd;
378 int ret = 0;
379 uint32_t criteria = EHS_REMOVE_WAKEUP;
380
381 lbs_deb_enter(LBS_DEB_CMD);
382
383 if (host_sleep) {
384 if (priv->is_host_sleep_activated != 1) {
385 memset(&cmd, 0, sizeof(cmd));
386 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
387 (struct wol_config *)NULL);
388 if (ret) {
389 lbs_pr_info("Host sleep configuration failed: "
390 "%d\n", ret);
391 return ret;
392 }
393 if (priv->psstate == PS_STATE_FULL_POWER) {
394 ret = __lbs_cmd(priv,
395 CMD_802_11_HOST_SLEEP_ACTIVATE,
396 &cmd,
397 sizeof(cmd),
398 lbs_ret_host_sleep_activate, 0);
399 if (ret)
400 lbs_pr_info("HOST_SLEEP_ACTIVATE "
401 "failed: %d\n", ret);
402 }
403
404 if (!wait_event_interruptible_timeout(
405 priv->host_sleep_q,
406 priv->is_host_sleep_activated,
407 (10 * HZ))) {
408 lbs_pr_err("host_sleep_q: timer expired\n");
409 ret = -1;
410 }
411 } else {
412 lbs_pr_err("host sleep: already enabled\n");
413 }
414 } else {
415 if (priv->is_host_sleep_activated)
416 ret = lbs_host_sleep_cfg(priv, criteria,
417 (struct wol_config *)NULL);
418 }
419
420 return ret;
421}
422
Dan Williams39fcf7a2008-09-10 12:49:00 -0400423/**
424 * @brief Set an SNMP MIB value
425 *
426 * @param priv A pointer to struct lbs_private structure
427 * @param oid The OID to set in the firmware
428 * @param val Value to set the OID to
429 *
430 * @return 0 on success, error on failure
431 */
432int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200433{
Dan Williams39fcf7a2008-09-10 12:49:00 -0400434 struct cmd_ds_802_11_snmp_mib cmd;
435 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436
Holger Schurig9012b282007-05-25 11:27:16 -0400437 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438
Dan Williams39fcf7a2008-09-10 12:49:00 -0400439 memset(&cmd, 0, sizeof (cmd));
440 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
441 cmd.action = cpu_to_le16(CMD_ACT_SET);
442 cmd.oid = cpu_to_le16((u16) oid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200443
Dan Williams39fcf7a2008-09-10 12:49:00 -0400444 switch (oid) {
445 case SNMP_MIB_OID_BSS_TYPE:
446 cmd.bufsize = cpu_to_le16(sizeof(u8));
Holger Schurigfef06402009-10-22 15:30:59 +0200447 cmd.value[0] = val;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448 break;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400449 case SNMP_MIB_OID_11D_ENABLE:
450 case SNMP_MIB_OID_FRAG_THRESHOLD:
451 case SNMP_MIB_OID_RTS_THRESHOLD:
452 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
453 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
454 cmd.bufsize = cpu_to_le16(sizeof(u16));
455 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 break;
457 default:
Dan Williams39fcf7a2008-09-10 12:49:00 -0400458 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
459 ret = -EINVAL;
460 goto out;
461 }
462
463 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
464 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
465
466 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
467
468out:
469 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
470 return ret;
471}
472
473/**
474 * @brief Get an SNMP MIB value
475 *
476 * @param priv A pointer to struct lbs_private structure
477 * @param oid The OID to retrieve from the firmware
478 * @param out_val Location for the returned value
479 *
480 * @return 0 on success, error on failure
481 */
482int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
483{
484 struct cmd_ds_802_11_snmp_mib cmd;
485 int ret;
486
487 lbs_deb_enter(LBS_DEB_CMD);
488
489 memset(&cmd, 0, sizeof (cmd));
490 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
491 cmd.action = cpu_to_le16(CMD_ACT_GET);
492 cmd.oid = cpu_to_le16(oid);
493
494 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
495 if (ret)
496 goto out;
497
498 switch (le16_to_cpu(cmd.bufsize)) {
499 case sizeof(u8):
Holger Schurigfef06402009-10-22 15:30:59 +0200500 *out_val = cmd.value[0];
Dan Williams39fcf7a2008-09-10 12:49:00 -0400501 break;
502 case sizeof(u16):
503 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
504 break;
505 default:
506 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
507 oid, le16_to_cpu(cmd.bufsize));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508 break;
509 }
510
Dan Williams39fcf7a2008-09-10 12:49:00 -0400511out:
512 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
513 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514}
515
Dan Williams87c8c722008-08-19 15:15:35 -0400516/**
517 * @brief Get the min, max, and current TX power
518 *
519 * @param priv A pointer to struct lbs_private structure
520 * @param curlevel Current power level in dBm
521 * @param minlevel Minimum supported power level in dBm (optional)
522 * @param maxlevel Maximum supported power level in dBm (optional)
523 *
524 * @return 0 on success, error on failure
525 */
526int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
527 s16 *maxlevel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528{
Dan Williams87c8c722008-08-19 15:15:35 -0400529 struct cmd_ds_802_11_rf_tx_power cmd;
530 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531
Holger Schurig9012b282007-05-25 11:27:16 -0400532 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533
Dan Williams87c8c722008-08-19 15:15:35 -0400534 memset(&cmd, 0, sizeof(cmd));
535 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
536 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537
Dan Williams87c8c722008-08-19 15:15:35 -0400538 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
539 if (ret == 0) {
540 *curlevel = le16_to_cpu(cmd.curlevel);
541 if (minlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100542 *minlevel = cmd.minlevel;
Dan Williams87c8c722008-08-19 15:15:35 -0400543 if (maxlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100544 *maxlevel = cmd.maxlevel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200545 }
Holger Schurig9012b282007-05-25 11:27:16 -0400546
547 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams87c8c722008-08-19 15:15:35 -0400548 return ret;
549}
550
551/**
552 * @brief Set the TX power
553 *
554 * @param priv A pointer to struct lbs_private structure
555 * @param dbm The desired power level in dBm
556 *
557 * @return 0 on success, error on failure
558 */
559int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
560{
561 struct cmd_ds_802_11_rf_tx_power cmd;
562 int ret;
563
564 lbs_deb_enter(LBS_DEB_CMD);
565
566 memset(&cmd, 0, sizeof(cmd));
567 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
568 cmd.action = cpu_to_le16(CMD_ACT_SET);
569 cmd.curlevel = cpu_to_le16(dbm);
570
571 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
572
573 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
574
575 lbs_deb_leave(LBS_DEB_CMD);
576 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577}
578
Holger Schurige98a88d2008-03-19 14:25:58 +0100579static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400580 u16 cmd_action, void *pdata_buf)
581{
582 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
583
584 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
585 cmd->size =
586 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200587 sizeof(struct cmd_header));
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400588
589 monitor->action = cpu_to_le16(cmd_action);
590 if (cmd_action == CMD_ACT_SET) {
591 monitor->mode =
592 cpu_to_le16((u16) (*(u32 *) pdata_buf));
593 }
594
595 return 0;
596}
597
Dan Williams2dd4b262007-12-11 16:54:15 -0500598/**
599 * @brief Get the radio channel
600 *
601 * @param priv A pointer to struct lbs_private structure
602 *
603 * @return The channel on success, error on failure
604 */
Holger Schuriga3cbfb02009-10-16 17:33:56 +0200605static int lbs_get_channel(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606{
Dan Williams2dd4b262007-12-11 16:54:15 -0500607 struct cmd_ds_802_11_rf_channel cmd;
608 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609
Holger Schurig8ff12da2007-08-02 11:54:31 -0400610 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200612 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500613 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
614 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615
David Woodhouse689442d2007-12-12 16:00:42 -0500616 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500617 if (ret)
618 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619
Dan Williamscb182a62007-12-11 17:35:51 -0500620 ret = le16_to_cpu(cmd.channel);
621 lbs_deb_cmd("current radio channel is %d\n", ret);
Dan Williams2dd4b262007-12-11 16:54:15 -0500622
623out:
624 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
625 return ret;
626}
627
Holger Schurig73ab1f22008-04-02 16:52:19 +0200628int lbs_update_channel(struct lbs_private *priv)
629{
630 int ret;
631
632 /* the channel in f/w could be out of sync; get the current channel */
633 lbs_deb_enter(LBS_DEB_ASSOC);
634
635 ret = lbs_get_channel(priv);
636 if (ret > 0) {
Holger Schurigc14951f2009-10-22 15:30:50 +0200637 priv->channel = ret;
Holger Schurig73ab1f22008-04-02 16:52:19 +0200638 ret = 0;
639 }
640 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
641 return ret;
642}
643
Dan Williams2dd4b262007-12-11 16:54:15 -0500644/**
645 * @brief Set the radio channel
646 *
647 * @param priv A pointer to struct lbs_private structure
648 * @param channel The desired channel, or 0 to clear a locked channel
649 *
650 * @return 0 on success, error on failure
651 */
652int lbs_set_channel(struct lbs_private *priv, u8 channel)
653{
654 struct cmd_ds_802_11_rf_channel cmd;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530655#ifdef DEBUG
Holger Schurigc14951f2009-10-22 15:30:50 +0200656 u8 old_channel = priv->channel;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530657#endif
Dan Williams2dd4b262007-12-11 16:54:15 -0500658 int ret = 0;
659
660 lbs_deb_enter(LBS_DEB_CMD);
661
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200662 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500663 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
664 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
665 cmd.channel = cpu_to_le16(channel);
666
David Woodhouse689442d2007-12-12 16:00:42 -0500667 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500668 if (ret)
669 goto out;
670
Holger Schurigc14951f2009-10-22 15:30:50 +0200671 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
Dan Williamscb182a62007-12-11 17:35:51 -0500672 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
Holger Schurigc14951f2009-10-22 15:30:50 +0200673 priv->channel);
Dan Williams2dd4b262007-12-11 16:54:15 -0500674
675out:
676 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
677 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678}
679
Holger Schurige98a88d2008-03-19 14:25:58 +0100680static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 u8 cmd_action, void *pdata_buf)
682{
Holger Schurig10078322007-11-15 18:05:47 -0500683 struct lbs_offset_value *offval;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200684
Holger Schurig9012b282007-05-25 11:27:16 -0400685 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686
Holger Schurig10078322007-11-15 18:05:47 -0500687 offval = (struct lbs_offset_value *)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000689 switch (le16_to_cpu(cmdptr->command)) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400690 case CMD_MAC_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 {
692 struct cmd_ds_mac_reg_access *macreg;
693
694 cmdptr->size =
David Woodhouse981f1872007-05-25 23:36:54 -0400695 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200696 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 macreg =
698 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
699 macreg;
700
701 macreg->action = cpu_to_le16(cmd_action);
702 macreg->offset = cpu_to_le16((u16) offval->offset);
703 macreg->value = cpu_to_le32(offval->value);
704
705 break;
706 }
707
Dan Williams0aef64d2007-08-02 11:31:18 -0400708 case CMD_BBP_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709 {
710 struct cmd_ds_bbp_reg_access *bbpreg;
711
712 cmdptr->size =
713 cpu_to_le16(sizeof
714 (struct cmd_ds_bbp_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200715 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716 bbpreg =
717 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
718 bbpreg;
719
720 bbpreg->action = cpu_to_le16(cmd_action);
721 bbpreg->offset = cpu_to_le16((u16) offval->offset);
722 bbpreg->value = (u8) offval->value;
723
724 break;
725 }
726
Dan Williams0aef64d2007-08-02 11:31:18 -0400727 case CMD_RF_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728 {
729 struct cmd_ds_rf_reg_access *rfreg;
730
731 cmdptr->size =
732 cpu_to_le16(sizeof
733 (struct cmd_ds_rf_reg_access) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200734 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200735 rfreg =
736 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
737 rfreg;
738
739 rfreg->action = cpu_to_le16(cmd_action);
740 rfreg->offset = cpu_to_le16((u16) offval->offset);
741 rfreg->value = (u8) offval->value;
742
743 break;
744 }
745
746 default:
747 break;
748 }
749
Holger Schurig9012b282007-05-25 11:27:16 -0400750 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200751 return 0;
752}
753
David Woodhouse681ffbb2007-12-15 20:04:54 -0500754static void lbs_queue_cmd(struct lbs_private *priv,
755 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756{
757 unsigned long flags;
David Woodhouse681ffbb2007-12-15 20:04:54 -0500758 int addtail = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759
Holger Schurig8ff12da2007-08-02 11:54:31 -0400760 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761
David Woodhousec4ab4122007-12-15 00:41:51 -0500762 if (!cmdnode) {
763 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764 goto done;
765 }
David Woodhoused9896ee2007-12-15 00:09:25 -0500766 if (!cmdnode->cmdbuf->size) {
767 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
768 goto done;
769 }
David Woodhouseae125bf2007-12-15 04:22:52 -0500770 cmdnode->result = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771
772 /* Exit_PS command needs to be queued in the header always. */
Dan Williamsddac4522007-12-11 13:49:39 -0500773 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
David Woodhouse38bfab12007-12-16 23:26:54 -0500774 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
Dan Williamsddac4522007-12-11 13:49:39 -0500775
Dan Williams0aef64d2007-08-02 11:31:18 -0400776 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000777 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200778 addtail = 0;
779 }
780 }
781
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700782 if (le16_to_cpu(cmdnode->cmdbuf->command) ==
783 CMD_802_11_WAKEUP_CONFIRM)
784 addtail = 0;
785
David Woodhouseaa21c002007-12-08 20:04:36 +0000786 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200787
David Woodhouseac472462007-12-08 00:35:00 +0000788 if (addtail)
David Woodhouseaa21c002007-12-08 20:04:36 +0000789 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
David Woodhouseac472462007-12-08 00:35:00 +0000790 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000791 list_add(&cmdnode->list, &priv->cmdpendingq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792
David Woodhouseaa21c002007-12-08 20:04:36 +0000793 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794
Holger Schurig8ff12da2007-08-02 11:54:31 -0400795 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
David Woodhousec4ab4122007-12-15 00:41:51 -0500796 le16_to_cpu(cmdnode->cmdbuf->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797
798done:
Holger Schurig8ff12da2007-08-02 11:54:31 -0400799 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200800}
801
David Woodhouse18c52e72007-12-17 16:03:58 -0500802static void lbs_submit_command(struct lbs_private *priv,
803 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200804{
805 unsigned long flags;
Dan Williamsddac4522007-12-11 13:49:39 -0500806 struct cmd_header *cmd;
David Woodhouse18c52e72007-12-17 16:03:58 -0500807 uint16_t cmdsize;
808 uint16_t command;
Holger Schurig57962f02008-05-14 16:30:28 +0200809 int timeo = 3 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500810 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811
Holger Schurig8ff12da2007-08-02 11:54:31 -0400812 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813
Dan Williamsddac4522007-12-11 13:49:39 -0500814 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200815
David Woodhouseaa21c002007-12-08 20:04:36 +0000816 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +0000817 priv->cur_cmd = cmdnode;
818 priv->cur_cmd_retcode = 0;
819 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820
Dan Williamsddac4522007-12-11 13:49:39 -0500821 cmdsize = le16_to_cpu(cmd->size);
822 command = le16_to_cpu(cmd->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200823
David Woodhouse18c52e72007-12-17 16:03:58 -0500824 /* These commands take longer */
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400825 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
Holger Schurig57962f02008-05-14 16:30:28 +0200826 timeo = 5 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500827
Holger Schurige5225b32008-03-26 10:04:44 +0100828 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
829 command, le16_to_cpu(cmd->seqnum), cmdsize);
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100830 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
Holger Schurig8ff12da2007-08-02 11:54:31 -0400831
Dan Williamsddac4522007-12-11 13:49:39 -0500832 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
David Woodhouse18c52e72007-12-17 16:03:58 -0500833
David Woodhoused9896ee2007-12-15 00:09:25 -0500834 if (ret) {
835 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
David Woodhouse18c52e72007-12-17 16:03:58 -0500836 /* Let the timer kick in and retry, and potentially reset
837 the whole thing if the condition persists */
Holger Schurig57962f02008-05-14 16:30:28 +0200838 timeo = HZ/4;
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100839 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840
Amitkumar Karwar49125452009-09-30 20:04:38 -0700841 if (command == CMD_802_11_DEEP_SLEEP) {
842 if (priv->is_auto_deep_sleep_enabled) {
843 priv->wakeup_dev_required = 1;
844 priv->dnld_sent = 0;
845 }
846 priv->is_deep_sleep = 1;
847 lbs_complete_command(priv, cmdnode, 0);
848 } else {
849 /* Setup the timer after transmit command */
850 mod_timer(&priv->command_timer, jiffies + timeo);
851 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200852
David Woodhouse18c52e72007-12-17 16:03:58 -0500853 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200854}
855
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200856/**
857 * This function inserts command node to cmdfreeq
David Woodhouseaa21c002007-12-08 20:04:36 +0000858 * after cleans it. Requires priv->driver_lock held.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859 */
David Woodhouse183aeac2007-12-15 01:52:54 -0500860static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500861 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200862{
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500863 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200864
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500865 if (!cmdnode)
866 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200867
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500868 cmdnode->callback = NULL;
869 cmdnode->callback_arg = 0;
870
871 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
872
873 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
874 out:
875 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200876}
877
Holger Schurig69f90322007-11-23 15:43:44 +0100878static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
879 struct cmd_ctrl_node *ptempcmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200880{
881 unsigned long flags;
882
David Woodhouseaa21c002007-12-08 20:04:36 +0000883 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig10078322007-11-15 18:05:47 -0500884 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
David Woodhouseaa21c002007-12-08 20:04:36 +0000885 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200886}
887
David Woodhouse183aeac2007-12-15 01:52:54 -0500888void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
889 int result)
890{
891 if (cmd == priv->cur_cmd)
892 priv->cur_cmd_retcode = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500893
David Woodhouseae125bf2007-12-15 04:22:52 -0500894 cmd->result = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500895 cmd->cmdwaitqwoken = 1;
896 wake_up_interruptible(&cmd->cmdwait_q);
897
Holger Schurig8db4a2b2008-03-19 10:11:00 +0100898 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
David Woodhousead12d0f2007-12-15 02:06:16 -0500899 __lbs_cleanup_and_insert_cmd(priv, cmd);
David Woodhouse183aeac2007-12-15 01:52:54 -0500900 priv->cur_cmd = NULL;
901}
902
Dan Williamsd5db2df2008-08-21 17:51:07 -0400903int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200904{
David Woodhousea7c45892007-12-17 22:43:48 -0500905 struct cmd_ds_802_11_radio_control cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400906 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907
Holger Schurig9012b282007-05-25 11:27:16 -0400908 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200909
David Woodhousea7c45892007-12-17 22:43:48 -0500910 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
911 cmd.action = cpu_to_le16(CMD_ACT_SET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200912
Dan Williamsd5db2df2008-08-21 17:51:07 -0400913 /* Only v8 and below support setting the preamble */
914 if (priv->fwrelease < 0x09000000) {
915 switch (preamble) {
916 case RADIO_PREAMBLE_SHORT:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400917 case RADIO_PREAMBLE_AUTO:
918 case RADIO_PREAMBLE_LONG:
919 cmd.control = cpu_to_le16(preamble);
920 break;
921 default:
922 goto out;
923 }
David Woodhousea7c45892007-12-17 22:43:48 -0500924 }
925
Dan Williamsd5db2df2008-08-21 17:51:07 -0400926 if (radio_on)
927 cmd.control |= cpu_to_le16(0x1);
928 else {
929 cmd.control &= cpu_to_le16(~0x1);
930 priv->txpower_cur = 0;
931 }
David Woodhousea7c45892007-12-17 22:43:48 -0500932
Dan Williamsd5db2df2008-08-21 17:51:07 -0400933 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
934 radio_on ? "ON" : "OFF", preamble);
935
936 priv->radio_on = radio_on;
David Woodhousea7c45892007-12-17 22:43:48 -0500937
938 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200939
Dan Williamsd5db2df2008-08-21 17:51:07 -0400940out:
Holger Schurig9012b282007-05-25 11:27:16 -0400941 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200942 return ret;
943}
944
Holger Schurigc97329e2008-03-18 11:20:21 +0100945void lbs_set_mac_control(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946{
Holger Schurig835d3ac2008-03-12 16:05:40 +0100947 struct cmd_ds_mac_control cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200948
Holger Schurig9012b282007-05-25 11:27:16 -0400949 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950
Holger Schurig835d3ac2008-03-12 16:05:40 +0100951 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurigd9e97782008-03-12 16:06:43 +0100952 cmd.action = cpu_to_le16(priv->mac_control);
Holger Schurig835d3ac2008-03-12 16:05:40 +0100953 cmd.reserved = 0;
954
David Woodhouse75bf45a2008-05-20 13:32:45 +0100955 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200956
Holger Schurigc97329e2008-03-18 11:20:21 +0100957 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200958}
959
960/**
Kiran Divekar1047d5e2010-06-04 23:20:42 -0700961 * @brief This function implements command CMD_802_11D_DOMAIN_INFO
962 * @param priv pointer to struct lbs_private
963 * @param cmd pointer to cmd buffer
964 * @param cmdno cmd ID
965 * @param cmdOption cmd action
966 * @return 0
967*/
968int lbs_cmd_802_11d_domain_info(struct lbs_private *priv,
969 struct cmd_ds_command *cmd,
970 u16 cmdoption)
971{
972 struct cmd_ds_802_11d_domain_info *pdomaininfo =
973 &cmd->params.domaininfo;
974 struct mrvl_ie_domain_param_set *domain = &pdomaininfo->domain;
975 u8 nr_triplet = priv->domain_reg.no_triplet;
976
977 lbs_deb_enter(LBS_DEB_11D);
978
979 lbs_deb_11d("nr_triplet=%x\n", nr_triplet);
980
981 pdomaininfo->action = cpu_to_le16(cmdoption);
982 if (cmdoption == CMD_ACT_GET) {
983 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
984 sizeof(struct cmd_header));
985 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
986 le16_to_cpu(cmd->size));
987 goto done;
988 }
989
990 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
991 memcpy(domain->countrycode, priv->domain_reg.country_code,
992 sizeof(domain->countrycode));
993
994 domain->header.len = cpu_to_le16(nr_triplet
995 * sizeof(struct ieee80211_country_ie_triplet)
996 + sizeof(domain->countrycode));
997
998 if (nr_triplet) {
999 memcpy(domain->triplet, priv->domain_reg.triplet,
1000 nr_triplet *
1001 sizeof(struct ieee80211_country_ie_triplet));
1002
1003 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1004 le16_to_cpu(domain->header.len) +
1005 sizeof(struct mrvl_ie_header) +
1006 sizeof(struct cmd_header));
1007 } else {
1008 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1009 sizeof(struct cmd_header));
1010 }
1011
1012 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
1013 le16_to_cpu(cmd->size));
1014
1015done:
1016 lbs_deb_enter(LBS_DEB_11D);
1017 return 0;
1018}
1019
1020/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001021 * @brief This function prepare the command before send to firmware.
1022 *
Holger Schurig69f90322007-11-23 15:43:44 +01001023 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024 * @param cmd_no command number
1025 * @param cmd_action command action: GET or SET
1026 * @param wait_option wait option: wait response or not
1027 * @param cmd_oid cmd oid: treated as sub command
1028 * @param pdata_buf A pointer to informaion buffer
1029 * @return 0 or -1
1030 */
Holger Schurig69f90322007-11-23 15:43:44 +01001031int lbs_prepare_and_send_command(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001032 u16 cmd_no,
1033 u16 cmd_action,
1034 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1035{
1036 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001037 struct cmd_ctrl_node *cmdnode;
1038 struct cmd_ds_command *cmdptr;
1039 unsigned long flags;
1040
Holger Schurig8ff12da2007-08-02 11:54:31 -04001041 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042
David Woodhouseaa21c002007-12-08 20:04:36 +00001043 if (!priv) {
1044 lbs_deb_host("PREP_CMD: priv is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045 ret = -1;
1046 goto done;
1047 }
1048
David Woodhouseaa21c002007-12-08 20:04:36 +00001049 if (priv->surpriseremoved) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001050 lbs_deb_host("PREP_CMD: card removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 ret = -1;
1052 goto done;
1053 }
1054
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001055 if (!lbs_is_cmd_allowed(priv)) {
1056 ret = -EBUSY;
1057 goto done;
1058 }
1059
Holger Schurig0d61d042007-12-05 17:58:06 +01001060 cmdnode = lbs_get_cmd_ctrl_node(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001061
1062 if (cmdnode == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001063 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064
1065 /* Wake up main thread to execute next command */
Dan Williamsfe336152007-08-02 11:32:25 -04001066 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001067 ret = -1;
1068 goto done;
1069 }
1070
Holger Schurige98a88d2008-03-19 14:25:58 +01001071 cmdnode->callback = NULL;
1072 cmdnode->callback_arg = (unsigned long)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073
Dan Williamsddac4522007-12-11 13:49:39 -05001074 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001075
Holger Schurig8ff12da2007-08-02 11:54:31 -04001076 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001077
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001078 /* Set sequence number, command and INT option */
David Woodhouseaa21c002007-12-08 20:04:36 +00001079 priv->seqnum++;
1080 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001081
David Woodhouse981f1872007-05-25 23:36:54 -04001082 cmdptr->command = cpu_to_le16(cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083 cmdptr->result = 0;
1084
1085 switch (cmd_no) {
Dan Williams0aef64d2007-08-02 11:31:18 -04001086 case CMD_802_11_PS_MODE:
Holger Schurige98a88d2008-03-19 14:25:58 +01001087 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001088 break;
1089
Dan Williams0aef64d2007-08-02 11:31:18 -04001090 case CMD_MAC_REG_ACCESS:
1091 case CMD_BBP_REG_ACCESS:
1092 case CMD_RF_REG_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001093 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001094 break;
1095
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001096 case CMD_802_11_MONITOR_MODE:
Holger Schurige98a88d2008-03-19 14:25:58 +01001097 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -04001098 cmd_action, pdata_buf);
1099 break;
1100
Dan Williams0aef64d2007-08-02 11:31:18 -04001101 case CMD_802_11_RSSI:
Holger Schurig10078322007-11-15 18:05:47 -05001102 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 break;
1104
Dan Williams0aef64d2007-08-02 11:31:18 -04001105 case CMD_802_11_SET_AFC:
1106 case CMD_802_11_GET_AFC:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001107
1108 cmdptr->command = cpu_to_le16(cmd_no);
David Woodhouse981f1872007-05-25 23:36:54 -04001109 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001110 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111
1112 memmove(&cmdptr->params.afc,
1113 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1114
1115 ret = 0;
1116 goto done;
1117
Kiran Divekar1047d5e2010-06-04 23:20:42 -07001118 case CMD_802_11D_DOMAIN_INFO:
1119 cmdptr->command = cpu_to_le16(cmd_no);
1120 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr, cmd_action);
1121 break;
1122
Dan Williams0aef64d2007-08-02 11:31:18 -04001123 case CMD_802_11_TPC_CFG:
1124 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001125 cmdptr->size =
1126 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001127 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128
1129 memmove(&cmdptr->params.tpccfg,
1130 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1131
1132 ret = 0;
1133 break;
David Woodhouse5844d122007-12-18 02:01:37 -05001134
Holger Schurig4143a232009-12-02 15:26:02 +01001135#ifdef CONFIG_LIBERTAS_MESH
1136
Dan Williams0aef64d2007-08-02 11:31:18 -04001137 case CMD_BT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001138 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001139 break;
1140
Dan Williams0aef64d2007-08-02 11:31:18 -04001141 case CMD_FWT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001142 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001143 break;
1144
Holger Schurig4143a232009-12-02 15:26:02 +01001145#endif
1146
Brajesh Dave96287ac2007-11-20 17:44:28 -05001147 case CMD_802_11_BEACON_CTRL:
1148 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1149 break;
Amitkumar Karwar49125452009-09-30 20:04:38 -07001150 case CMD_802_11_DEEP_SLEEP:
1151 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001152 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
Amitkumar Karwar49125452009-09-30 20:04:38 -07001153 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001154 default:
David Woodhousee37fc6e2008-05-20 11:47:16 +01001155 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156 ret = -1;
1157 break;
1158 }
1159
1160 /* return error, since the command preparation failed */
1161 if (ret != 0) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001162 lbs_deb_host("PREP_CMD: command preparation failed\n");
Holger Schurig10078322007-11-15 18:05:47 -05001163 lbs_cleanup_and_insert_cmd(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 ret = -1;
1165 goto done;
1166 }
1167
1168 cmdnode->cmdwaitqwoken = 0;
1169
David Woodhouse681ffbb2007-12-15 20:04:54 -05001170 lbs_queue_cmd(priv, cmdnode);
Dan Williamsfe336152007-08-02 11:32:25 -04001171 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001172
Dan Williams0aef64d2007-08-02 11:31:18 -04001173 if (wait_option & CMD_OPTION_WAITFORRSP) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001174 lbs_deb_host("PREP_CMD: wait for response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175 might_sleep();
1176 wait_event_interruptible(cmdnode->cmdwait_q,
1177 cmdnode->cmdwaitqwoken);
1178 }
1179
David Woodhouseaa21c002007-12-08 20:04:36 +00001180 spin_lock_irqsave(&priv->driver_lock, flags);
1181 if (priv->cur_cmd_retcode) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001182 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
David Woodhouseaa21c002007-12-08 20:04:36 +00001183 priv->cur_cmd_retcode);
1184 priv->cur_cmd_retcode = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001185 ret = -1;
1186 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001187 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188
1189done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001190 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001191 return ret;
1192}
1193
1194/**
1195 * @brief This function allocates the command buffer and link
1196 * it to command free queue.
1197 *
Holger Schurig69f90322007-11-23 15:43:44 +01001198 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001199 * @return 0 or -1
1200 */
Holger Schurig69f90322007-11-23 15:43:44 +01001201int lbs_allocate_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001202{
1203 int ret = 0;
Dan Williamsddac4522007-12-11 13:49:39 -05001204 u32 bufsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001205 u32 i;
Dan Williamsddac4522007-12-11 13:49:39 -05001206 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001207
Holger Schurig8ff12da2007-08-02 11:54:31 -04001208 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001209
Dan Williamsddac4522007-12-11 13:49:39 -05001210 /* Allocate and initialize the command array */
1211 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1212 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001213 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001214 ret = -1;
1215 goto done;
1216 }
Dan Williamsddac4522007-12-11 13:49:39 -05001217 priv->cmd_array = cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001218
Dan Williamsddac4522007-12-11 13:49:39 -05001219 /* Allocate and initialize each command buffer in the command array */
1220 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1221 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1222 if (!cmdarray[i].cmdbuf) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001223 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001224 ret = -1;
1225 goto done;
1226 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001227 }
1228
Dan Williamsddac4522007-12-11 13:49:39 -05001229 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1230 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1231 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001232 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001233 ret = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001234
1235done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001236 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001237 return ret;
1238}
1239
1240/**
1241 * @brief This function frees the command buffer.
1242 *
Holger Schurig69f90322007-11-23 15:43:44 +01001243 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001244 * @return 0 or -1
1245 */
Holger Schurig69f90322007-11-23 15:43:44 +01001246int lbs_free_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001247{
Dan Williamsddac4522007-12-11 13:49:39 -05001248 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001249 unsigned int i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001250
Holger Schurig8ff12da2007-08-02 11:54:31 -04001251 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001252
1253 /* need to check if cmd array is allocated or not */
David Woodhouseaa21c002007-12-08 20:04:36 +00001254 if (priv->cmd_array == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001255 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001256 goto done;
1257 }
1258
Dan Williamsddac4522007-12-11 13:49:39 -05001259 cmdarray = priv->cmd_array;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001260
1261 /* Release shared memory buffers */
Dan Williamsddac4522007-12-11 13:49:39 -05001262 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1263 if (cmdarray[i].cmdbuf) {
1264 kfree(cmdarray[i].cmdbuf);
1265 cmdarray[i].cmdbuf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266 }
1267 }
1268
1269 /* Release cmd_ctrl_node */
David Woodhouseaa21c002007-12-08 20:04:36 +00001270 if (priv->cmd_array) {
1271 kfree(priv->cmd_array);
1272 priv->cmd_array = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001273 }
1274
1275done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001276 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001277 return 0;
1278}
1279
1280/**
1281 * @brief This function gets a free command node if available in
1282 * command free queue.
1283 *
Holger Schurig69f90322007-11-23 15:43:44 +01001284 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001285 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1286 */
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001287static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288{
1289 struct cmd_ctrl_node *tempnode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290 unsigned long flags;
1291
Holger Schurig8ff12da2007-08-02 11:54:31 -04001292 lbs_deb_enter(LBS_DEB_HOST);
1293
David Woodhouseaa21c002007-12-08 20:04:36 +00001294 if (!priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295 return NULL;
1296
David Woodhouseaa21c002007-12-08 20:04:36 +00001297 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001298
David Woodhouseaa21c002007-12-08 20:04:36 +00001299 if (!list_empty(&priv->cmdfreeq)) {
1300 tempnode = list_first_entry(&priv->cmdfreeq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001301 struct cmd_ctrl_node, list);
1302 list_del(&tempnode->list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001304 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001305 tempnode = NULL;
1306 }
1307
David Woodhouseaa21c002007-12-08 20:04:36 +00001308 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001309
Holger Schurig8ff12da2007-08-02 11:54:31 -04001310 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311 return tempnode;
1312}
1313
1314/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001315 * @brief This function executes next command in command
Nick Andrew877d0312009-01-26 11:06:57 +01001316 * pending queue. It will put firmware back to PS mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001317 * if applicable.
1318 *
Holger Schurig69f90322007-11-23 15:43:44 +01001319 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001320 * @return 0 or -1
1321 */
Holger Schurig69f90322007-11-23 15:43:44 +01001322int lbs_execute_next_command(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001323{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001324 struct cmd_ctrl_node *cmdnode = NULL;
Dan Williamsddac4522007-12-11 13:49:39 -05001325 struct cmd_header *cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001326 unsigned long flags;
1327 int ret = 0;
1328
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001329 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1330 * only caller to us is lbs_thread() and we get even when a
1331 * data packet is received */
Holger Schurig8ff12da2007-08-02 11:54:31 -04001332 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001333
David Woodhouseaa21c002007-12-08 20:04:36 +00001334 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001335
David Woodhouseaa21c002007-12-08 20:04:36 +00001336 if (priv->cur_cmd) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001337 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001338 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001339 ret = -1;
1340 goto done;
1341 }
1342
David Woodhouseaa21c002007-12-08 20:04:36 +00001343 if (!list_empty(&priv->cmdpendingq)) {
1344 cmdnode = list_first_entry(&priv->cmdpendingq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001345 struct cmd_ctrl_node, list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346 }
1347
David Woodhouseaa21c002007-12-08 20:04:36 +00001348 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001349
1350 if (cmdnode) {
Dan Williamsddac4522007-12-11 13:49:39 -05001351 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001352
Dan Williamsddac4522007-12-11 13:49:39 -05001353 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001354 if ((priv->psstate == PS_STATE_SLEEP) ||
1355 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001356 lbs_deb_host(
1357 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001358 le16_to_cpu(cmd->command),
David Woodhouseaa21c002007-12-08 20:04:36 +00001359 priv->psstate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001360 ret = -1;
1361 goto done;
1362 }
Holger Schurig8ff12da2007-08-02 11:54:31 -04001363 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
Dan Williamsddac4522007-12-11 13:49:39 -05001364 "0x%04x in psstate %d\n",
1365 le16_to_cpu(cmd->command), priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +00001366 } else if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001367 /*
1368 * 1. Non-PS command:
1369 * Queue it. set needtowakeup to TRUE if current state
Holger Schurig10078322007-11-15 18:05:47 -05001370 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001371 * 2. PS command but not Exit_PS:
1372 * Ignore it.
1373 * 3. PS command Exit_PS:
1374 * Set needtowakeup to TRUE if current state is SLEEP,
1375 * otherwise send this command down to firmware
1376 * immediately.
1377 */
Dan Williamsddac4522007-12-11 13:49:39 -05001378 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001379 /* Prepare to send Exit PS,
1380 * this non PS command will be sent later */
David Woodhouseaa21c002007-12-08 20:04:36 +00001381 if ((priv->psstate == PS_STATE_SLEEP)
1382 || (priv->psstate == PS_STATE_PRE_SLEEP)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001383 ) {
1384 /* w/ new scheme, it will not reach here.
1385 since it is blocked in main_thread. */
David Woodhouseaa21c002007-12-08 20:04:36 +00001386 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001387 } else
Holger Schurig10078322007-11-15 18:05:47 -05001388 lbs_ps_wakeup(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001389
1390 ret = 0;
1391 goto done;
1392 } else {
1393 /*
1394 * PS command. Ignore it if it is not Exit_PS.
1395 * otherwise send it down immediately.
1396 */
David Woodhouse38bfab12007-12-16 23:26:54 -05001397 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001398
Holger Schurig8ff12da2007-08-02 11:54:31 -04001399 lbs_deb_host(
1400 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001401 psm->action);
1402 if (psm->action !=
Dan Williams0aef64d2007-08-02 11:31:18 -04001403 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001404 lbs_deb_host(
1405 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001406 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001407 spin_lock_irqsave(&priv->driver_lock, flags);
1408 lbs_complete_command(priv, cmdnode, 0);
1409 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410
1411 ret = 0;
1412 goto done;
1413 }
1414
David Woodhouseaa21c002007-12-08 20:04:36 +00001415 if ((priv->psstate == PS_STATE_SLEEP) ||
1416 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001417 lbs_deb_host(
1418 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001419 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001420 spin_lock_irqsave(&priv->driver_lock, flags);
1421 lbs_complete_command(priv, cmdnode, 0);
1422 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +00001423 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424
1425 ret = 0;
1426 goto done;
1427 }
1428
Holger Schurig8ff12da2007-08-02 11:54:31 -04001429 lbs_deb_host(
1430 "EXEC_NEXT_CMD: sending EXIT_PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001431 }
1432 }
Li Zefanabe3ed12007-12-06 13:01:21 +01001433 list_del(&cmdnode->list);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001434 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001435 le16_to_cpu(cmd->command));
David Woodhoused9896ee2007-12-15 00:09:25 -05001436 lbs_submit_command(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437 } else {
1438 /*
1439 * check if in power save mode, if yes, put the device back
1440 * to PS mode
1441 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301442#ifdef TODO
1443 /*
1444 * This was the old code for libertas+wext. Someone that
1445 * understands this beast should re-code it in a sane way.
1446 *
1447 * I actually don't understand why this is related to WPA
1448 * and to connection status, shouldn't powering should be
1449 * independ of such things?
1450 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001451 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1452 (priv->psstate == PS_STATE_FULL_POWER) &&
1453 ((priv->connect_status == LBS_CONNECTED) ||
Holger Schurig602114a2009-12-02 15:26:01 +01001454 lbs_mesh_connected(priv))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001455 if (priv->secinfo.WPAenabled ||
1456 priv->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001457 /* check for valid WPA group keys */
David Woodhouseaa21c002007-12-08 20:04:36 +00001458 if (priv->wpa_mcast_key.len ||
1459 priv->wpa_unicast_key.len) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001460 lbs_deb_host(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001461 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1462 " go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001463 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001464 }
1465 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001466 lbs_deb_host(
1467 "EXEC_NEXT_CMD: cmdpendingq empty, "
1468 "go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001469 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470 }
1471 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301472#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001473 }
1474
1475 ret = 0;
1476done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001477 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001478 return ret;
1479}
1480
Holger Schurigf539f2e2008-03-26 13:22:11 +01001481static void lbs_send_confirmsleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001482{
1483 unsigned long flags;
Holger Schurigf539f2e2008-03-26 13:22:11 +01001484 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001485
Holger Schurig8ff12da2007-08-02 11:54:31 -04001486 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001487 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1488 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001489
Holger Schurigf539f2e2008-03-26 13:22:11 +01001490 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1491 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001492 if (ret) {
Holger Schurigf539f2e2008-03-26 13:22:11 +01001493 lbs_pr_alert("confirm_sleep failed\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001494 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001495 }
Holger Schurig7919b892008-04-01 14:50:43 +02001496
1497 spin_lock_irqsave(&priv->driver_lock, flags);
1498
Holger Schuriga01f5452008-06-04 11:10:40 +02001499 /* We don't get a response on the sleep-confirmation */
1500 priv->dnld_sent = DNLD_RES_RECEIVED;
1501
Amitkumar Karwar66fceb62010-05-19 03:24:38 -07001502 if (priv->is_host_sleep_configured) {
1503 priv->is_host_sleep_activated = 1;
1504 wake_up_interruptible(&priv->host_sleep_q);
1505 }
1506
Holger Schurig7919b892008-04-01 14:50:43 +02001507 /* If nothing to do, go back to sleep (?) */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001508 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
Holger Schurig7919b892008-04-01 14:50:43 +02001509 priv->psstate = PS_STATE_SLEEP;
1510
1511 spin_unlock_irqrestore(&priv->driver_lock, flags);
1512
1513out:
Holger Schurigf539f2e2008-03-26 13:22:11 +01001514 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001515}
1516
Holger Schurig69f90322007-11-23 15:43:44 +01001517void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001518{
Holger Schurig8ff12da2007-08-02 11:54:31 -04001519 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001520
1521 /*
1522 * PS is currently supported only in Infrastructure mode
1523 * Remove this check if it is to be supported in IBSS mode also
1524 */
1525
Holger Schurig10078322007-11-15 18:05:47 -05001526 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001527 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001528
Holger Schurig8ff12da2007-08-02 11:54:31 -04001529 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001530}
1531
1532/**
Holger Schurig8ff12da2007-08-02 11:54:31 -04001533 * @brief This function sends Exit_PS command to firmware.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001534 *
Holger Schurig69f90322007-11-23 15:43:44 +01001535 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001536 * @param wait_option wait response or not
1537 * @return n/a
1538 */
Holger Schurig69f90322007-11-23 15:43:44 +01001539void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001540{
David Woodhouse981f1872007-05-25 23:36:54 -04001541 __le32 Localpsmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001542
Holger Schurig8ff12da2007-08-02 11:54:31 -04001543 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001544
Holger Schurig10078322007-11-15 18:05:47 -05001545 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001546
Holger Schurig10078322007-11-15 18:05:47 -05001547 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001548 CMD_SUBCMD_EXIT_PS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001549 wait_option, 0, &Localpsmode);
1550
Holger Schurig8ff12da2007-08-02 11:54:31 -04001551 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001552}
1553
1554/**
1555 * @brief This function checks condition and prepares to
1556 * send sleep confirm command to firmware if ok.
1557 *
Holger Schurig69f90322007-11-23 15:43:44 +01001558 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001559 * @param psmode Power Saving mode
1560 * @return n/a
1561 */
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001562void lbs_ps_confirm_sleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001563{
1564 unsigned long flags =0;
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001565 int allowed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001566
Holger Schurig8ff12da2007-08-02 11:54:31 -04001567 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001568
Holger Schuriga01f5452008-06-04 11:10:40 +02001569 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig634b8f42007-05-25 13:05:16 -04001570 if (priv->dnld_sent) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001571 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001572 lbs_deb_host("dnld_sent was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001573 }
1574
Holger Schurig7919b892008-04-01 14:50:43 +02001575 /* In-progress command? */
David Woodhouseaa21c002007-12-08 20:04:36 +00001576 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001577 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001578 lbs_deb_host("cur_cmd was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001579 }
Holger Schurig7919b892008-04-01 14:50:43 +02001580
1581 /* Pending events or command responses? */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001582 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001583 allowed = 0;
Holger Schurig7919b892008-04-01 14:50:43 +02001584 lbs_deb_host("pending events or command responses\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001585 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001586 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001587
1588 if (allowed) {
Holger Schurig10078322007-11-15 18:05:47 -05001589 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
Holger Schurigf539f2e2008-03-26 13:22:11 +01001590 lbs_send_confirmsleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001591 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001592 lbs_deb_host("sleep confirm has been delayed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001593 }
1594
Holger Schurig8ff12da2007-08-02 11:54:31 -04001595 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001596}
Holger Schurig675787e2007-12-05 17:58:11 +01001597
1598
Anna Neal0112c9e2008-09-11 11:17:25 -07001599/**
1600 * @brief Configures the transmission power control functionality.
1601 *
1602 * @param priv A pointer to struct lbs_private structure
1603 * @param enable Transmission power control enable
1604 * @param p0 Power level when link quality is good (dBm).
1605 * @param p1 Power level when link quality is fair (dBm).
1606 * @param p2 Power level when link quality is poor (dBm).
1607 * @param usesnr Use Signal to Noise Ratio in TPC
1608 *
1609 * @return 0 on success
1610 */
1611int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1612 int8_t p2, int usesnr)
1613{
1614 struct cmd_ds_802_11_tpc_cfg cmd;
1615 int ret;
1616
1617 memset(&cmd, 0, sizeof(cmd));
1618 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1619 cmd.action = cpu_to_le16(CMD_ACT_SET);
1620 cmd.enable = !!enable;
Anna Neal3ed6e082008-09-26 11:34:35 -04001621 cmd.usesnr = !!usesnr;
Anna Neal0112c9e2008-09-11 11:17:25 -07001622 cmd.P0 = p0;
1623 cmd.P1 = p1;
1624 cmd.P2 = p2;
1625
1626 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1627
1628 return ret;
1629}
1630
1631/**
1632 * @brief Configures the power adaptation settings.
1633 *
1634 * @param priv A pointer to struct lbs_private structure
1635 * @param enable Power adaptation enable
1636 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1637 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1638 * @param p2 Power level for 48 and 54 Mbps (dBm).
1639 *
1640 * @return 0 on Success
1641 */
1642
1643int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1644 int8_t p1, int8_t p2)
1645{
1646 struct cmd_ds_802_11_pa_cfg cmd;
1647 int ret;
1648
1649 memset(&cmd, 0, sizeof(cmd));
1650 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1651 cmd.action = cpu_to_le16(CMD_ACT_SET);
1652 cmd.enable = !!enable;
1653 cmd.P0 = p0;
1654 cmd.P1 = p1;
1655 cmd.P2 = p2;
1656
1657 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1658
1659 return ret;
1660}
1661
1662
Holger Schurig6d898b12009-10-14 16:49:53 +02001663struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001664 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1665 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1666 unsigned long callback_arg)
Holger Schurig675787e2007-12-05 17:58:11 +01001667{
Holger Schurig675787e2007-12-05 17:58:11 +01001668 struct cmd_ctrl_node *cmdnode;
Holger Schurig675787e2007-12-05 17:58:11 +01001669
1670 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurig675787e2007-12-05 17:58:11 +01001671
David Woodhouseaa21c002007-12-08 20:04:36 +00001672 if (priv->surpriseremoved) {
Holger Schurig675787e2007-12-05 17:58:11 +01001673 lbs_deb_host("PREP_CMD: card removed\n");
David Woodhouse3399ea52007-12-15 03:09:33 -05001674 cmdnode = ERR_PTR(-ENOENT);
Holger Schurig675787e2007-12-05 17:58:11 +01001675 goto done;
1676 }
1677
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001678 if (!lbs_is_cmd_allowed(priv)) {
1679 cmdnode = ERR_PTR(-EBUSY);
1680 goto done;
1681 }
1682
Holger Schurig675787e2007-12-05 17:58:11 +01001683 cmdnode = lbs_get_cmd_ctrl_node(priv);
Holger Schurig675787e2007-12-05 17:58:11 +01001684 if (cmdnode == NULL) {
1685 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1686
1687 /* Wake up main thread to execute next command */
1688 wake_up_interruptible(&priv->waitq);
David Woodhouse3399ea52007-12-15 03:09:33 -05001689 cmdnode = ERR_PTR(-ENOBUFS);
Holger Schurig675787e2007-12-05 17:58:11 +01001690 goto done;
1691 }
1692
David Woodhouse448a51a2007-12-08 00:59:54 +00001693 cmdnode->callback = callback;
David Woodhouse1309b552007-12-10 13:36:10 -05001694 cmdnode->callback_arg = callback_arg;
Holger Schurig675787e2007-12-05 17:58:11 +01001695
Dan Williams7ad994d2007-12-11 12:33:30 -05001696 /* Copy the incoming command to the buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001697 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
Dan Williams7ad994d2007-12-11 12:33:30 -05001698
Holger Schurig675787e2007-12-05 17:58:11 +01001699 /* Set sequence number, clean result, move to buffer */
David Woodhouseaa21c002007-12-08 20:04:36 +00001700 priv->seqnum++;
Dan Williamsddac4522007-12-11 13:49:39 -05001701 cmdnode->cmdbuf->command = cpu_to_le16(command);
1702 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1703 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1704 cmdnode->cmdbuf->result = 0;
Holger Schurig675787e2007-12-05 17:58:11 +01001705
1706 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1707
Holger Schurig675787e2007-12-05 17:58:11 +01001708 cmdnode->cmdwaitqwoken = 0;
David Woodhouse681ffbb2007-12-15 20:04:54 -05001709 lbs_queue_cmd(priv, cmdnode);
Holger Schurig675787e2007-12-05 17:58:11 +01001710 wake_up_interruptible(&priv->waitq);
1711
David Woodhouse3399ea52007-12-15 03:09:33 -05001712 done:
1713 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1714 return cmdnode;
1715}
1716
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001717void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1718 struct cmd_header *in_cmd, int in_cmd_size)
1719{
1720 lbs_deb_enter(LBS_DEB_CMD);
1721 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1722 lbs_cmd_async_callback, 0);
1723 lbs_deb_leave(LBS_DEB_CMD);
1724}
1725
David Woodhouse3399ea52007-12-15 03:09:33 -05001726int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1727 struct cmd_header *in_cmd, int in_cmd_size,
1728 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1729 unsigned long callback_arg)
1730{
1731 struct cmd_ctrl_node *cmdnode;
1732 unsigned long flags;
1733 int ret = 0;
1734
1735 lbs_deb_enter(LBS_DEB_HOST);
1736
1737 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1738 callback, callback_arg);
1739 if (IS_ERR(cmdnode)) {
1740 ret = PTR_ERR(cmdnode);
1741 goto done;
1742 }
1743
Holger Schurig675787e2007-12-05 17:58:11 +01001744 might_sleep();
1745 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1746
David Woodhouseaa21c002007-12-08 20:04:36 +00001747 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseae125bf2007-12-15 04:22:52 -05001748 ret = cmdnode->result;
1749 if (ret)
1750 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1751 command, ret);
David Woodhouse3399ea52007-12-15 03:09:33 -05001752
David Woodhousead12d0f2007-12-15 02:06:16 -05001753 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
David Woodhouseaa21c002007-12-08 20:04:36 +00001754 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig675787e2007-12-05 17:58:11 +01001755
1756done:
1757 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1758 return ret;
1759}
Dan Williams14e865b2007-12-10 15:11:23 -05001760EXPORT_SYMBOL_GPL(__lbs_cmd);