blob: 6a96fc9c1cea4d7fc9d9359d6c50c7f7c15219fe [file] [log] [blame]
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001/*
2 * This file contains the handling of command.
3 * It prepares command and sends it to firmware when it is ready.
4 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02005
Holger Schurig7919b892008-04-01 14:50:43 +02006#include <linux/kfifo.h>
Holger Schurige93156e2009-10-22 15:30:58 +02007#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Dan Williamsa45b6f42010-07-27 12:54:34 -07009#include <linux/if_arp.h>
Holger Schurige93156e2009-10-22 15:30:58 +020010
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "decl.h"
Kiran Divekare86dc1c2010-06-14 22:01:26 +053012#include "cfg.h"
Dan Williams6e66f032007-12-11 12:42:16 -050013#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
Dan Williams9fb76632010-07-27 12:55:21 -070015#define CAL_NF(nf) ((s32)(-(s32)(nf)))
16#define CAL_RSSI(snr, nf) ((s32)((s32)(snr) + CAL_NF(nf)))
Holger Schurige93156e2009-10-22 15:30:58 +020017
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070019 * lbs_cmd_copyback - Simple callback that copies response back into command
Holger Schurig8db4a2b2008-03-19 10:11:00 +010020 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070021 * @priv: A pointer to &struct lbs_private structure
22 * @extra: A pointer to the original command structure for which
23 * 'resp' is a response
24 * @resp: A pointer to the command response
Holger Schurig8db4a2b2008-03-19 10:11:00 +010025 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070026 * returns: 0 on success, error on failure
Holger Schurig8db4a2b2008-03-19 10:11:00 +010027 */
28int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
29 struct cmd_header *resp)
30{
31 struct cmd_header *buf = (void *)extra;
32 uint16_t copy_len;
33
34 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
35 memcpy(buf, resp, copy_len);
36 return 0;
37}
38EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
39
40/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070041 * lbs_cmd_async_callback - Simple callback that ignores the result.
42 * Use this if you just want to send a command to the hardware, but don't
Holger Schurig8db4a2b2008-03-19 10:11:00 +010043 * care for the result.
44 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070045 * @priv: ignored
46 * @extra: ignored
47 * @resp: ignored
Holger Schurig8db4a2b2008-03-19 10:11:00 +010048 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070049 * returns: 0 for success
Holger Schurig8db4a2b2008-03-19 10:11:00 +010050 */
51static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
52 struct cmd_header *resp)
53{
54 return 0;
55}
56
57
58/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070059 * is_command_allowed_in_ps - tests if a command is allowed in Power Save mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020060 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070061 * @cmd: the command ID
62 *
63 * returns: 1 if allowed, 0 if not allowed
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020064 */
Dan Williams852e1f22007-12-10 15:24:47 -050065static u8 is_command_allowed_in_ps(u16 cmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020066{
Dan Williams852e1f22007-12-10 15:24:47 -050067 switch (cmd) {
68 case CMD_802_11_RSSI:
69 return 1;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -070070 case CMD_802_11_HOST_SLEEP_CFG:
71 return 1;
Dan Williams852e1f22007-12-10 15:24:47 -050072 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/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070079 * lbs_update_hw_spec - Updates the hardware details like MAC address
80 * and regulatory region
Dan Williams6e66f032007-12-11 12:42:16 -050081 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070082 * @priv: A pointer to &struct lbs_private structure
Dan Williams6e66f032007-12-11 12:42:16 -050083 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070084 * returns: 0 on success, error on failure
Dan Williams6e66f032007-12-11 12:42:16 -050085 */
86int lbs_update_hw_spec(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020087{
Dan Williams6e66f032007-12-11 12:42:16 -050088 struct cmd_ds_get_hw_spec cmd;
89 int ret = -1;
90 u32 i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020091
Holger Schurig9012b282007-05-25 11:27:16 -040092 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020093
Dan Williams6e66f032007-12-11 12:42:16 -050094 memset(&cmd, 0, sizeof(cmd));
95 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
96 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
David Woodhouse689442d2007-12-12 16:00:42 -050097 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
Dan Williams6e66f032007-12-11 12:42:16 -050098 if (ret)
99 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200100
Dan Williams6e66f032007-12-11 12:42:16 -0500101 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500102
Holger Schurigdac10a92008-01-16 15:55:22 +0100103 /* The firmware release is in an interesting format: the patch
104 * level is in the most significant nibble ... so fix that: */
105 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
106 priv->fwrelease = (priv->fwrelease << 8) |
107 (priv->fwrelease >> 24 & 0xff);
108
109 /* Some firmware capabilities:
110 * CF card firmware 5.0.16p0: cap 0x00000303
111 * USB dongle firmware 5.110.17p2: cap 0x00000303
112 */
Johannes Berge1749612008-10-27 15:59:26 -0700113 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
114 cmd.permanentaddr,
Holger Schurigdac10a92008-01-16 15:55:22 +0100115 priv->fwrelease >> 24 & 0xff,
116 priv->fwrelease >> 16 & 0xff,
117 priv->fwrelease >> 8 & 0xff,
118 priv->fwrelease & 0xff,
119 priv->fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500120 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
121 cmd.hwifversion, cmd.version);
122
123 /* Clamp region code to 8-bit since FW spec indicates that it should
124 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
125 * returns non-zero high 8 bits here.
Marek Vasut15483992009-07-16 19:19:53 +0200126 *
127 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
128 * need to check for this problem and handle it properly.
Dan Williams6e66f032007-12-11 12:42:16 -0500129 */
Marek Vasut15483992009-07-16 19:19:53 +0200130 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
131 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
132 else
133 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
Dan Williams6e66f032007-12-11 12:42:16 -0500134
135 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
136 /* use the region code to search for the index */
137 if (priv->regioncode == lbs_region_code_to_index[i])
138 break;
139 }
140
141 /* if it's unidentified region code, use the default (USA) */
142 if (i >= MRVDRV_MAX_REGION_CODE) {
143 priv->regioncode = 0x10;
144 lbs_pr_info("unidentified region code; using the default (USA)\n");
145 }
146
147 if (priv->current_addr[0] == 0xff)
148 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
149
Vasily Khoruzhick75abde42011-01-21 22:44:49 +0200150 if (!priv->copied_hwaddr) {
151 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
152 if (priv->mesh_dev)
153 memcpy(priv->mesh_dev->dev_addr,
154 priv->current_addr, ETH_ALEN);
155 priv->copied_hwaddr = 1;
156 }
Dan Williams6e66f032007-12-11 12:42:16 -0500157
Dan Williams6e66f032007-12-11 12:42:16 -0500158out:
Holger Schurig9012b282007-05-25 11:27:16 -0400159 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams6e66f032007-12-11 12:42:16 -0500160 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161}
162
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700163static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
164 struct cmd_header *resp)
165{
166 lbs_deb_enter(LBS_DEB_CMD);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530167 if (priv->is_host_sleep_activated) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700168 priv->is_host_sleep_configured = 0;
169 if (priv->psstate == PS_STATE_FULL_POWER) {
170 priv->is_host_sleep_activated = 0;
171 wake_up_interruptible(&priv->host_sleep_q);
172 }
173 } else {
174 priv->is_host_sleep_configured = 1;
175 }
176 lbs_deb_leave(LBS_DEB_CMD);
177 return 0;
178}
179
Anna Neal582c1b52008-10-20 16:46:56 -0700180int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
181 struct wol_config *p_wol_config)
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500182{
183 struct cmd_ds_host_sleep cmd_config;
184 int ret;
185
Deepak Saxenaae63a332010-10-31 13:40:33 +0000186 /*
187 * Certain firmware versions do not support EHS_REMOVE_WAKEUP command
188 * and the card will return a failure. Since we need to be
189 * able to reset the mask, in those cases we set a 0 mask instead.
190 */
191 if (criteria == EHS_REMOVE_WAKEUP && !priv->ehs_remove_supported)
192 criteria = 0;
193
David Woodhouse9fae8992007-12-15 03:46:44 -0500194 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500195 cmd_config.criteria = cpu_to_le32(criteria);
David Woodhouse506e9022007-12-12 20:06:06 -0500196 cmd_config.gpio = priv->wol_gpio;
197 cmd_config.gap = priv->wol_gap;
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500198
Anna Neal582c1b52008-10-20 16:46:56 -0700199 if (p_wol_config != NULL)
200 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
201 sizeof(struct wol_config));
202 else
203 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
204
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700205 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
206 le16_to_cpu(cmd_config.hdr.size),
207 lbs_ret_host_sleep_cfg, 0);
David Woodhouse506e9022007-12-12 20:06:06 -0500208 if (!ret) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700209 if (p_wol_config)
Anna Neal582c1b52008-10-20 16:46:56 -0700210 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
Dan Williams0bb64082010-07-27 13:08:08 -0700221/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700222 * lbs_set_ps_mode - Sets the Power Save mode
Dan Williams0bb64082010-07-27 13:08:08 -0700223 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700224 * @priv: A pointer to &struct lbs_private structure
225 * @cmd_action: The Power Save operation (PS_MODE_ACTION_ENTER_PS or
Dan Williams0bb64082010-07-27 13:08:08 -0700226 * PS_MODE_ACTION_EXIT_PS)
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700227 * @block: Whether to block on a response or not
Dan Williams0bb64082010-07-27 13:08:08 -0700228 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700229 * returns: 0 on success, error on failure
Dan Williams0bb64082010-07-27 13:08:08 -0700230 */
231int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200232{
Dan Williams0bb64082010-07-27 13:08:08 -0700233 struct cmd_ds_802_11_ps_mode cmd;
234 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200235
Holger Schurig9012b282007-05-25 11:27:16 -0400236 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200237
Dan Williams0bb64082010-07-27 13:08:08 -0700238 memset(&cmd, 0, sizeof(cmd));
239 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
240 cmd.action = cpu_to_le16(cmd_action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200241
Dan Williams0bb64082010-07-27 13:08:08 -0700242 if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
243 lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
244 cmd.multipledtim = cpu_to_le16(1); /* Default DTIM multiple */
245 } else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
246 lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
247 } else {
248 /* We don't handle CONFIRM_SLEEP here because it needs to
249 * be fastpathed to the firmware.
250 */
251 lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
252 ret = -EOPNOTSUPP;
253 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254 }
255
Dan Williams0bb64082010-07-27 13:08:08 -0700256 if (block)
257 ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
258 else
259 lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
260
261out:
262 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
263 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200264}
265
David Woodhouse3fbe1042007-12-17 23:48:31 -0500266int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
267 struct sleep_params *sp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268{
David Woodhouse3fbe1042007-12-17 23:48:31 -0500269 struct cmd_ds_802_11_sleep_params cmd;
270 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271
Holger Schurig9012b282007-05-25 11:27:16 -0400272 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273
Dan Williams0aef64d2007-08-02 11:31:18 -0400274 if (cmd_action == CMD_ACT_GET) {
David Woodhouse3fbe1042007-12-17 23:48:31 -0500275 memset(&cmd, 0, sizeof(cmd));
276 } else {
277 cmd.error = cpu_to_le16(sp->sp_error);
278 cmd.offset = cpu_to_le16(sp->sp_offset);
279 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
280 cmd.calcontrol = sp->sp_calcontrol;
281 cmd.externalsleepclk = sp->sp_extsleepclk;
282 cmd.reserved = cpu_to_le16(sp->sp_reserved);
283 }
284 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
285 cmd.action = cpu_to_le16(cmd_action);
286
287 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
288
289 if (!ret) {
290 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
291 "calcontrol 0x%x extsleepclk 0x%x\n",
292 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
293 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
294 cmd.externalsleepclk);
295
296 sp->sp_error = le16_to_cpu(cmd.error);
297 sp->sp_offset = le16_to_cpu(cmd.offset);
298 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
299 sp->sp_calcontrol = cmd.calcontrol;
300 sp->sp_extsleepclk = cmd.externalsleepclk;
301 sp->sp_reserved = le16_to_cpu(cmd.reserved);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200302 }
303
David Woodhouse3fbe1042007-12-17 23:48:31 -0500304 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305 return 0;
306}
307
Amitkumar Karwar49125452009-09-30 20:04:38 -0700308static int lbs_wait_for_ds_awake(struct lbs_private *priv)
309{
310 int ret = 0;
311
312 lbs_deb_enter(LBS_DEB_CMD);
313
314 if (priv->is_deep_sleep) {
315 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
316 !priv->is_deep_sleep, (10 * HZ))) {
317 lbs_pr_err("ds_awake_q: timer expired\n");
318 ret = -1;
319 }
320 }
321
322 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
323 return ret;
324}
325
326int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
327{
328 int ret = 0;
329
330 lbs_deb_enter(LBS_DEB_CMD);
331
332 if (deep_sleep) {
333 if (priv->is_deep_sleep != 1) {
334 lbs_deb_cmd("deep sleep: sleep\n");
335 BUG_ON(!priv->enter_deep_sleep);
336 ret = priv->enter_deep_sleep(priv);
337 if (!ret) {
338 netif_stop_queue(priv->dev);
339 netif_carrier_off(priv->dev);
340 }
341 } else {
342 lbs_pr_err("deep sleep: already enabled\n");
343 }
344 } else {
345 if (priv->is_deep_sleep) {
346 lbs_deb_cmd("deep sleep: wakeup\n");
347 BUG_ON(!priv->exit_deep_sleep);
348 ret = priv->exit_deep_sleep(priv);
349 if (!ret) {
350 ret = lbs_wait_for_ds_awake(priv);
351 if (ret)
352 lbs_pr_err("deep sleep: wakeup"
353 "failed\n");
354 }
355 }
356 }
357
358 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
359 return ret;
360}
361
Amitkumar Karwar13118432010-07-08 06:43:48 +0530362static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
363 unsigned long dummy,
364 struct cmd_header *cmd)
365{
366 lbs_deb_enter(LBS_DEB_FW);
367 priv->is_host_sleep_activated = 1;
368 wake_up_interruptible(&priv->host_sleep_q);
369 lbs_deb_leave(LBS_DEB_FW);
370 return 0;
371}
372
373int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
374{
375 struct cmd_header cmd;
376 int ret = 0;
377 uint32_t criteria = EHS_REMOVE_WAKEUP;
378
379 lbs_deb_enter(LBS_DEB_CMD);
380
381 if (host_sleep) {
382 if (priv->is_host_sleep_activated != 1) {
383 memset(&cmd, 0, sizeof(cmd));
384 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
385 (struct wol_config *)NULL);
386 if (ret) {
387 lbs_pr_info("Host sleep configuration failed: "
388 "%d\n", ret);
389 return ret;
390 }
391 if (priv->psstate == PS_STATE_FULL_POWER) {
392 ret = __lbs_cmd(priv,
393 CMD_802_11_HOST_SLEEP_ACTIVATE,
394 &cmd,
395 sizeof(cmd),
396 lbs_ret_host_sleep_activate, 0);
397 if (ret)
398 lbs_pr_info("HOST_SLEEP_ACTIVATE "
399 "failed: %d\n", ret);
400 }
401
402 if (!wait_event_interruptible_timeout(
403 priv->host_sleep_q,
404 priv->is_host_sleep_activated,
405 (10 * HZ))) {
406 lbs_pr_err("host_sleep_q: timer expired\n");
407 ret = -1;
408 }
409 } else {
410 lbs_pr_err("host sleep: already enabled\n");
411 }
412 } else {
413 if (priv->is_host_sleep_activated)
414 ret = lbs_host_sleep_cfg(priv, criteria,
415 (struct wol_config *)NULL);
416 }
417
418 return ret;
419}
420
Dan Williams39fcf7a2008-09-10 12:49:00 -0400421/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700422 * lbs_set_snmp_mib - Set an SNMP MIB value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400423 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700424 * @priv: A pointer to &struct lbs_private structure
425 * @oid: The OID to set in the firmware
426 * @val: Value to set the OID to
Dan Williams39fcf7a2008-09-10 12:49:00 -0400427 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700428 * returns: 0 on success, error on failure
Dan Williams39fcf7a2008-09-10 12:49:00 -0400429 */
430int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200431{
Dan Williams39fcf7a2008-09-10 12:49:00 -0400432 struct cmd_ds_802_11_snmp_mib cmd;
433 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434
Holger Schurig9012b282007-05-25 11:27:16 -0400435 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436
Dan Williams39fcf7a2008-09-10 12:49:00 -0400437 memset(&cmd, 0, sizeof (cmd));
438 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
439 cmd.action = cpu_to_le16(CMD_ACT_SET);
440 cmd.oid = cpu_to_le16((u16) oid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200441
Dan Williams39fcf7a2008-09-10 12:49:00 -0400442 switch (oid) {
443 case SNMP_MIB_OID_BSS_TYPE:
444 cmd.bufsize = cpu_to_le16(sizeof(u8));
Holger Schurigfef06402009-10-22 15:30:59 +0200445 cmd.value[0] = val;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 break;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400447 case SNMP_MIB_OID_11D_ENABLE:
448 case SNMP_MIB_OID_FRAG_THRESHOLD:
449 case SNMP_MIB_OID_RTS_THRESHOLD:
450 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
451 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
452 cmd.bufsize = cpu_to_le16(sizeof(u16));
453 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 break;
455 default:
Dan Williams39fcf7a2008-09-10 12:49:00 -0400456 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
457 ret = -EINVAL;
458 goto out;
459 }
460
461 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
462 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
463
464 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
465
466out:
467 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
468 return ret;
469}
470
471/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700472 * lbs_get_snmp_mib - Get an SNMP MIB value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400473 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700474 * @priv: A pointer to &struct lbs_private structure
475 * @oid: The OID to retrieve from the firmware
476 * @out_val: Location for the returned value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400477 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700478 * returns: 0 on success, error on failure
Dan Williams39fcf7a2008-09-10 12:49:00 -0400479 */
480int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
481{
482 struct cmd_ds_802_11_snmp_mib cmd;
483 int ret;
484
485 lbs_deb_enter(LBS_DEB_CMD);
486
487 memset(&cmd, 0, sizeof (cmd));
488 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
489 cmd.action = cpu_to_le16(CMD_ACT_GET);
490 cmd.oid = cpu_to_le16(oid);
491
492 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
493 if (ret)
494 goto out;
495
496 switch (le16_to_cpu(cmd.bufsize)) {
497 case sizeof(u8):
Holger Schurigfef06402009-10-22 15:30:59 +0200498 *out_val = cmd.value[0];
Dan Williams39fcf7a2008-09-10 12:49:00 -0400499 break;
500 case sizeof(u16):
501 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
502 break;
503 default:
504 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
505 oid, le16_to_cpu(cmd.bufsize));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506 break;
507 }
508
Dan Williams39fcf7a2008-09-10 12:49:00 -0400509out:
510 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
511 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512}
513
Dan Williams87c8c722008-08-19 15:15:35 -0400514/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700515 * lbs_get_tx_power - Get the min, max, and current TX power
Dan Williams87c8c722008-08-19 15:15:35 -0400516 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700517 * @priv: A pointer to &struct lbs_private structure
518 * @curlevel: Current power level in dBm
519 * @minlevel: Minimum supported power level in dBm (optional)
520 * @maxlevel: Maximum supported power level in dBm (optional)
Dan Williams87c8c722008-08-19 15:15:35 -0400521 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700522 * returns: 0 on success, error on failure
Dan Williams87c8c722008-08-19 15:15:35 -0400523 */
524int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
525 s16 *maxlevel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200526{
Dan Williams87c8c722008-08-19 15:15:35 -0400527 struct cmd_ds_802_11_rf_tx_power cmd;
528 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529
Holger Schurig9012b282007-05-25 11:27:16 -0400530 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531
Dan Williams87c8c722008-08-19 15:15:35 -0400532 memset(&cmd, 0, sizeof(cmd));
533 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
534 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535
Dan Williams87c8c722008-08-19 15:15:35 -0400536 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
537 if (ret == 0) {
538 *curlevel = le16_to_cpu(cmd.curlevel);
539 if (minlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100540 *minlevel = cmd.minlevel;
Dan Williams87c8c722008-08-19 15:15:35 -0400541 if (maxlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100542 *maxlevel = cmd.maxlevel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200543 }
Holger Schurig9012b282007-05-25 11:27:16 -0400544
545 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams87c8c722008-08-19 15:15:35 -0400546 return ret;
547}
548
549/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700550 * lbs_set_tx_power - Set the TX power
Dan Williams87c8c722008-08-19 15:15:35 -0400551 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700552 * @priv: A pointer to &struct lbs_private structure
553 * @dbm: The desired power level in dBm
Dan Williams87c8c722008-08-19 15:15:35 -0400554 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700555 * returns: 0 on success, error on failure
Dan Williams87c8c722008-08-19 15:15:35 -0400556 */
557int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
558{
559 struct cmd_ds_802_11_rf_tx_power cmd;
560 int ret;
561
562 lbs_deb_enter(LBS_DEB_CMD);
563
564 memset(&cmd, 0, sizeof(cmd));
565 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
566 cmd.action = cpu_to_le16(CMD_ACT_SET);
567 cmd.curlevel = cpu_to_le16(dbm);
568
569 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
570
571 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
572
573 lbs_deb_leave(LBS_DEB_CMD);
574 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575}
576
Dan Williamsa45b6f42010-07-27 12:54:34 -0700577/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700578 * lbs_set_monitor_mode - Enable or disable monitor mode
579 * (only implemented on OLPC usb8388 FW)
Dan Williamsa45b6f42010-07-27 12:54:34 -0700580 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700581 * @priv: A pointer to &struct lbs_private structure
582 * @enable: 1 to enable monitor mode, 0 to disable
Dan Williamsa45b6f42010-07-27 12:54:34 -0700583 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700584 * returns: 0 on success, error on failure
Dan Williamsa45b6f42010-07-27 12:54:34 -0700585 */
586int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400587{
Dan Williamsa45b6f42010-07-27 12:54:34 -0700588 struct cmd_ds_802_11_monitor_mode cmd;
589 int ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400590
Dan Williamsa45b6f42010-07-27 12:54:34 -0700591 memset(&cmd, 0, sizeof(cmd));
592 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
593 cmd.action = cpu_to_le16(CMD_ACT_SET);
594 if (enable)
595 cmd.mode = cpu_to_le16(0x1);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400596
Dan Williamsa45b6f42010-07-27 12:54:34 -0700597 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
598
599 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
600 if (ret == 0) {
601 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
602 ARPHRD_ETHER;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400603 }
604
Dan Williamsa45b6f42010-07-27 12:54:34 -0700605 lbs_deb_leave(LBS_DEB_CMD);
606 return ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400607}
608
Dan Williams2dd4b262007-12-11 16:54:15 -0500609/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700610 * lbs_get_channel - Get the radio channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500611 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700612 * @priv: A pointer to &struct lbs_private structure
Dan Williams2dd4b262007-12-11 16:54:15 -0500613 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700614 * returns: The channel on success, error on failure
Dan Williams2dd4b262007-12-11 16:54:15 -0500615 */
Holger Schuriga3cbfb02009-10-16 17:33:56 +0200616static int lbs_get_channel(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617{
Dan Williams2dd4b262007-12-11 16:54:15 -0500618 struct cmd_ds_802_11_rf_channel cmd;
619 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620
Holger Schurig8ff12da2007-08-02 11:54:31 -0400621 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200623 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500624 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
625 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626
David Woodhouse689442d2007-12-12 16:00:42 -0500627 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500628 if (ret)
629 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630
Dan Williamscb182a62007-12-11 17:35:51 -0500631 ret = le16_to_cpu(cmd.channel);
632 lbs_deb_cmd("current radio channel is %d\n", ret);
Dan Williams2dd4b262007-12-11 16:54:15 -0500633
634out:
635 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
636 return ret;
637}
638
Holger Schurig73ab1f22008-04-02 16:52:19 +0200639int lbs_update_channel(struct lbs_private *priv)
640{
641 int ret;
642
643 /* the channel in f/w could be out of sync; get the current channel */
644 lbs_deb_enter(LBS_DEB_ASSOC);
645
646 ret = lbs_get_channel(priv);
647 if (ret > 0) {
Holger Schurigc14951f2009-10-22 15:30:50 +0200648 priv->channel = ret;
Holger Schurig73ab1f22008-04-02 16:52:19 +0200649 ret = 0;
650 }
651 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
652 return ret;
653}
654
Dan Williams2dd4b262007-12-11 16:54:15 -0500655/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700656 * lbs_set_channel - Set the radio channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500657 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700658 * @priv: A pointer to &struct lbs_private structure
659 * @channel: The desired channel, or 0 to clear a locked channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500660 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700661 * returns: 0 on success, error on failure
Dan Williams2dd4b262007-12-11 16:54:15 -0500662 */
663int lbs_set_channel(struct lbs_private *priv, u8 channel)
664{
665 struct cmd_ds_802_11_rf_channel cmd;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530666#ifdef DEBUG
Holger Schurigc14951f2009-10-22 15:30:50 +0200667 u8 old_channel = priv->channel;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530668#endif
Dan Williams2dd4b262007-12-11 16:54:15 -0500669 int ret = 0;
670
671 lbs_deb_enter(LBS_DEB_CMD);
672
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200673 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500674 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
675 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
676 cmd.channel = cpu_to_le16(channel);
677
David Woodhouse689442d2007-12-12 16:00:42 -0500678 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500679 if (ret)
680 goto out;
681
Holger Schurigc14951f2009-10-22 15:30:50 +0200682 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
Dan Williamscb182a62007-12-11 17:35:51 -0500683 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
Holger Schurigc14951f2009-10-22 15:30:50 +0200684 priv->channel);
Dan Williams2dd4b262007-12-11 16:54:15 -0500685
686out:
687 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
688 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689}
690
Dan Williams9fb76632010-07-27 12:55:21 -0700691/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700692 * lbs_get_rssi - Get current RSSI and noise floor
Dan Williams9fb76632010-07-27 12:55:21 -0700693 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700694 * @priv: A pointer to &struct lbs_private structure
695 * @rssi: On successful return, signal level in mBm
696 * @nf: On successful return, Noise floor
Dan Williams9fb76632010-07-27 12:55:21 -0700697 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700698 * returns: The channel on success, error on failure
Dan Williams9fb76632010-07-27 12:55:21 -0700699 */
700int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
701{
702 struct cmd_ds_802_11_rssi cmd;
703 int ret = 0;
704
705 lbs_deb_enter(LBS_DEB_CMD);
706
707 BUG_ON(rssi == NULL);
708 BUG_ON(nf == NULL);
709
710 memset(&cmd, 0, sizeof(cmd));
711 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
712 /* Average SNR over last 8 beacons */
713 cmd.n_or_snr = cpu_to_le16(8);
714
715 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
716 if (ret == 0) {
717 *nf = CAL_NF(le16_to_cpu(cmd.nf));
718 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
719 }
720
721 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
722 return ret;
723}
724
Dan Williamscc4b9d32010-07-27 12:56:05 -0700725/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700726 * lbs_set_11d_domain_info - Send regulatory and 802.11d domain information
727 * to the firmware
Dan Williamscc4b9d32010-07-27 12:56:05 -0700728 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700729 * @priv: pointer to &struct lbs_private
730 * @request: cfg80211 regulatory request structure
731 * @bands: the device's supported bands and channels
Dan Williamscc4b9d32010-07-27 12:56:05 -0700732 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700733 * returns: 0 on success, error code on failure
Dan Williamscc4b9d32010-07-27 12:56:05 -0700734*/
735int lbs_set_11d_domain_info(struct lbs_private *priv,
736 struct regulatory_request *request,
737 struct ieee80211_supported_band **bands)
738{
739 struct cmd_ds_802_11d_domain_info cmd;
740 struct mrvl_ie_domain_param_set *domain = &cmd.domain;
741 struct ieee80211_country_ie_triplet *t;
742 enum ieee80211_band band;
743 struct ieee80211_channel *ch;
744 u8 num_triplet = 0;
745 u8 num_parsed_chan = 0;
746 u8 first_channel = 0, next_chan = 0, max_pwr = 0;
747 u8 i, flag = 0;
748 size_t triplet_size;
749 int ret;
750
751 lbs_deb_enter(LBS_DEB_11D);
752
753 memset(&cmd, 0, sizeof(cmd));
754 cmd.action = cpu_to_le16(CMD_ACT_SET);
755
756 lbs_deb_11d("Setting country code '%c%c'\n",
757 request->alpha2[0], request->alpha2[1]);
758
759 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
760
761 /* Set country code */
762 domain->country_code[0] = request->alpha2[0];
763 domain->country_code[1] = request->alpha2[1];
764 domain->country_code[2] = ' ';
765
766 /* Now set up the channel triplets; firmware is somewhat picky here
767 * and doesn't validate channel numbers and spans; hence it would
768 * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
769 * the last 3 aren't valid channels, the driver is responsible for
770 * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
771 * etc.
772 */
773 for (band = 0;
774 (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
775 band++) {
776
777 if (!bands[band])
778 continue;
779
780 for (i = 0;
781 (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
782 i++) {
783 ch = &bands[band]->channels[i];
784 if (ch->flags & IEEE80211_CHAN_DISABLED)
785 continue;
786
787 if (!flag) {
788 flag = 1;
789 next_chan = first_channel = (u32) ch->hw_value;
790 max_pwr = ch->max_power;
791 num_parsed_chan = 1;
792 continue;
793 }
794
795 if ((ch->hw_value == next_chan + 1) &&
796 (ch->max_power == max_pwr)) {
797 /* Consolidate adjacent channels */
798 next_chan++;
799 num_parsed_chan++;
800 } else {
801 /* Add this triplet */
802 lbs_deb_11d("11D triplet (%d, %d, %d)\n",
803 first_channel, num_parsed_chan,
804 max_pwr);
805 t = &domain->triplet[num_triplet];
806 t->chans.first_channel = first_channel;
807 t->chans.num_channels = num_parsed_chan;
808 t->chans.max_power = max_pwr;
809 num_triplet++;
810 flag = 0;
811 }
812 }
813
814 if (flag) {
815 /* Add last triplet */
816 lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
817 num_parsed_chan, max_pwr);
818 t = &domain->triplet[num_triplet];
819 t->chans.first_channel = first_channel;
820 t->chans.num_channels = num_parsed_chan;
821 t->chans.max_power = max_pwr;
822 num_triplet++;
823 }
824 }
825
826 lbs_deb_11d("# triplets %d\n", num_triplet);
827
828 /* Set command header sizes */
829 triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
830 domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
831 triplet_size);
832
833 lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
834 (u8 *) &cmd.domain.country_code,
835 le16_to_cpu(domain->header.len));
836
837 cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
838 sizeof(cmd.action) +
839 sizeof(cmd.domain.header) +
840 sizeof(cmd.domain.country_code) +
841 triplet_size);
842
843 ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
844
845 lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
846 return ret;
847}
848
Dan Williams4c7c6e002010-07-27 13:01:07 -0700849/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700850 * lbs_get_reg - Read a MAC, Baseband, or RF register
Dan Williams4c7c6e002010-07-27 13:01:07 -0700851 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700852 * @priv: pointer to &struct lbs_private
853 * @reg: register command, one of CMD_MAC_REG_ACCESS,
854 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
855 * @offset: byte offset of the register to get
856 * @value: on success, the value of the register at 'offset'
Dan Williams4c7c6e002010-07-27 13:01:07 -0700857 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700858 * returns: 0 on success, error code on failure
Dan Williams4c7c6e002010-07-27 13:01:07 -0700859*/
860int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861{
Dan Williams4c7c6e002010-07-27 13:01:07 -0700862 struct cmd_ds_reg_access cmd;
863 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200864
Holger Schurig9012b282007-05-25 11:27:16 -0400865 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200866
Dan Williams4c7c6e002010-07-27 13:01:07 -0700867 BUG_ON(value == NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200868
Dan Williams4c7c6e002010-07-27 13:01:07 -0700869 memset(&cmd, 0, sizeof(cmd));
870 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
871 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872
Dan Williams4c7c6e002010-07-27 13:01:07 -0700873 if (reg != CMD_MAC_REG_ACCESS &&
874 reg != CMD_BBP_REG_ACCESS &&
875 reg != CMD_RF_REG_ACCESS) {
876 ret = -EINVAL;
877 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200878 }
879
Dan Williams4c7c6e002010-07-27 13:01:07 -0700880 ret = lbs_cmd_with_response(priv, reg, &cmd);
881 if (ret) {
882 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
883 *value = cmd.value.bbp_rf;
884 else if (reg == CMD_MAC_REG_ACCESS)
885 *value = le32_to_cpu(cmd.value.mac);
886 }
887
888out:
889 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
890 return ret;
891}
892
893/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700894 * lbs_set_reg - Write a MAC, Baseband, or RF register
Dan Williams4c7c6e002010-07-27 13:01:07 -0700895 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700896 * @priv: pointer to &struct lbs_private
897 * @reg: register command, one of CMD_MAC_REG_ACCESS,
898 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
899 * @offset: byte offset of the register to set
900 * @value: the value to write to the register at 'offset'
Dan Williams4c7c6e002010-07-27 13:01:07 -0700901 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700902 * returns: 0 on success, error code on failure
Dan Williams4c7c6e002010-07-27 13:01:07 -0700903*/
904int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
905{
906 struct cmd_ds_reg_access cmd;
907 int ret = 0;
908
909 lbs_deb_enter(LBS_DEB_CMD);
910
911 memset(&cmd, 0, sizeof(cmd));
912 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
913 cmd.action = cpu_to_le16(CMD_ACT_SET);
914
915 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
916 cmd.value.bbp_rf = (u8) (value & 0xFF);
917 else if (reg == CMD_MAC_REG_ACCESS)
918 cmd.value.mac = cpu_to_le32(value);
919 else {
920 ret = -EINVAL;
921 goto out;
922 }
923
924 ret = lbs_cmd_with_response(priv, reg, &cmd);
925
926out:
927 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
928 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200929}
930
David Woodhouse681ffbb2007-12-15 20:04:54 -0500931static void lbs_queue_cmd(struct lbs_private *priv,
932 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933{
934 unsigned long flags;
David Woodhouse681ffbb2007-12-15 20:04:54 -0500935 int addtail = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200936
Holger Schurig8ff12da2007-08-02 11:54:31 -0400937 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200938
David Woodhousec4ab4122007-12-15 00:41:51 -0500939 if (!cmdnode) {
940 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200941 goto done;
942 }
David Woodhoused9896ee2007-12-15 00:09:25 -0500943 if (!cmdnode->cmdbuf->size) {
944 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
945 goto done;
946 }
David Woodhouseae125bf2007-12-15 04:22:52 -0500947 cmdnode->result = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200948
949 /* Exit_PS command needs to be queued in the header always. */
Dan Williamsddac4522007-12-11 13:49:39 -0500950 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
Dan Williams0bb64082010-07-27 13:08:08 -0700951 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf;
Dan Williamsddac4522007-12-11 13:49:39 -0500952
Dan Williams0bb64082010-07-27 13:08:08 -0700953 if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000954 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200955 addtail = 0;
956 }
957 }
958
Dan Williams0bb64082010-07-27 13:08:08 -0700959 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700960 addtail = 0;
961
David Woodhouseaa21c002007-12-08 20:04:36 +0000962 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963
David Woodhouseac472462007-12-08 00:35:00 +0000964 if (addtail)
David Woodhouseaa21c002007-12-08 20:04:36 +0000965 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
David Woodhouseac472462007-12-08 00:35:00 +0000966 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000967 list_add(&cmdnode->list, &priv->cmdpendingq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200968
David Woodhouseaa21c002007-12-08 20:04:36 +0000969 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200970
Holger Schurig8ff12da2007-08-02 11:54:31 -0400971 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
David Woodhousec4ab4122007-12-15 00:41:51 -0500972 le16_to_cpu(cmdnode->cmdbuf->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200973
974done:
Holger Schurig8ff12da2007-08-02 11:54:31 -0400975 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976}
977
David Woodhouse18c52e72007-12-17 16:03:58 -0500978static void lbs_submit_command(struct lbs_private *priv,
979 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200980{
981 unsigned long flags;
Dan Williamsddac4522007-12-11 13:49:39 -0500982 struct cmd_header *cmd;
David Woodhouse18c52e72007-12-17 16:03:58 -0500983 uint16_t cmdsize;
984 uint16_t command;
Holger Schurig57962f02008-05-14 16:30:28 +0200985 int timeo = 3 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500986 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200987
Holger Schurig8ff12da2007-08-02 11:54:31 -0400988 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989
Dan Williamsddac4522007-12-11 13:49:39 -0500990 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200991
David Woodhouseaa21c002007-12-08 20:04:36 +0000992 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +0000993 priv->cur_cmd = cmdnode;
David Woodhouseaa21c002007-12-08 20:04:36 +0000994 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995
Dan Williamsddac4522007-12-11 13:49:39 -0500996 cmdsize = le16_to_cpu(cmd->size);
997 command = le16_to_cpu(cmd->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200998
David Woodhouse18c52e72007-12-17 16:03:58 -0500999 /* These commands take longer */
Dan Williamsbe0d76e2009-05-22 20:05:25 -04001000 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
Holger Schurig57962f02008-05-14 16:30:28 +02001001 timeo = 5 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -05001002
Holger Schurige5225b32008-03-26 10:04:44 +01001003 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1004 command, le16_to_cpu(cmd->seqnum), cmdsize);
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001005 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001006
Dan Williamsddac4522007-12-11 13:49:39 -05001007 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
David Woodhouse18c52e72007-12-17 16:03:58 -05001008
David Woodhoused9896ee2007-12-15 00:09:25 -05001009 if (ret) {
1010 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
David Woodhouse18c52e72007-12-17 16:03:58 -05001011 /* Let the timer kick in and retry, and potentially reset
1012 the whole thing if the condition persists */
Holger Schurig57962f02008-05-14 16:30:28 +02001013 timeo = HZ/4;
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001014 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015
Amitkumar Karwar49125452009-09-30 20:04:38 -07001016 if (command == CMD_802_11_DEEP_SLEEP) {
1017 if (priv->is_auto_deep_sleep_enabled) {
1018 priv->wakeup_dev_required = 1;
1019 priv->dnld_sent = 0;
1020 }
1021 priv->is_deep_sleep = 1;
1022 lbs_complete_command(priv, cmdnode, 0);
1023 } else {
1024 /* Setup the timer after transmit command */
1025 mod_timer(&priv->command_timer, jiffies + timeo);
1026 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027
David Woodhouse18c52e72007-12-17 16:03:58 -05001028 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029}
1030
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001031/*
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001032 * This function inserts command node to cmdfreeq
David Woodhouseaa21c002007-12-08 20:04:36 +00001033 * after cleans it. Requires priv->driver_lock held.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034 */
David Woodhouse183aeac2007-12-15 01:52:54 -05001035static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001036 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001037{
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001038 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001039
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001040 if (!cmdnode)
1041 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001043 cmdnode->callback = NULL;
1044 cmdnode->callback_arg = 0;
1045
1046 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1047
1048 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1049 out:
1050 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051}
1052
Holger Schurig69f90322007-11-23 15:43:44 +01001053static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1054 struct cmd_ctrl_node *ptempcmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001055{
1056 unsigned long flags;
1057
David Woodhouseaa21c002007-12-08 20:04:36 +00001058 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig10078322007-11-15 18:05:47 -05001059 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
David Woodhouseaa21c002007-12-08 20:04:36 +00001060 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001061}
1062
David Woodhouse183aeac2007-12-15 01:52:54 -05001063void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1064 int result)
1065{
David Woodhouseae125bf2007-12-15 04:22:52 -05001066 cmd->result = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001067 cmd->cmdwaitqwoken = 1;
1068 wake_up_interruptible(&cmd->cmdwait_q);
1069
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001070 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
David Woodhousead12d0f2007-12-15 02:06:16 -05001071 __lbs_cleanup_and_insert_cmd(priv, cmd);
David Woodhouse183aeac2007-12-15 01:52:54 -05001072 priv->cur_cmd = NULL;
1073}
1074
Dan Williamsd5db2df2008-08-21 17:51:07 -04001075int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076{
David Woodhousea7c45892007-12-17 22:43:48 -05001077 struct cmd_ds_802_11_radio_control cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -04001078 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001079
Holger Schurig9012b282007-05-25 11:27:16 -04001080 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001081
David Woodhousea7c45892007-12-17 22:43:48 -05001082 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1083 cmd.action = cpu_to_le16(CMD_ACT_SET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084
Dan Williamsd5db2df2008-08-21 17:51:07 -04001085 /* Only v8 and below support setting the preamble */
1086 if (priv->fwrelease < 0x09000000) {
1087 switch (preamble) {
1088 case RADIO_PREAMBLE_SHORT:
Dan Williamsd5db2df2008-08-21 17:51:07 -04001089 case RADIO_PREAMBLE_AUTO:
1090 case RADIO_PREAMBLE_LONG:
1091 cmd.control = cpu_to_le16(preamble);
1092 break;
1093 default:
1094 goto out;
1095 }
David Woodhousea7c45892007-12-17 22:43:48 -05001096 }
1097
Dan Williamsd5db2df2008-08-21 17:51:07 -04001098 if (radio_on)
1099 cmd.control |= cpu_to_le16(0x1);
1100 else {
1101 cmd.control &= cpu_to_le16(~0x1);
1102 priv->txpower_cur = 0;
1103 }
David Woodhousea7c45892007-12-17 22:43:48 -05001104
Dan Williamsd5db2df2008-08-21 17:51:07 -04001105 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1106 radio_on ? "ON" : "OFF", preamble);
1107
1108 priv->radio_on = radio_on;
David Woodhousea7c45892007-12-17 22:43:48 -05001109
1110 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111
Dan Williamsd5db2df2008-08-21 17:51:07 -04001112out:
Holger Schurig9012b282007-05-25 11:27:16 -04001113 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114 return ret;
1115}
1116
Holger Schurigc97329e2008-03-18 11:20:21 +01001117void lbs_set_mac_control(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118{
Holger Schurig835d3ac2008-03-12 16:05:40 +01001119 struct cmd_ds_mac_control cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001120
Holger Schurig9012b282007-05-25 11:27:16 -04001121 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122
Holger Schurig835d3ac2008-03-12 16:05:40 +01001123 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurigd9e97782008-03-12 16:06:43 +01001124 cmd.action = cpu_to_le16(priv->mac_control);
Holger Schurig835d3ac2008-03-12 16:05:40 +01001125 cmd.reserved = 0;
1126
David Woodhouse75bf45a2008-05-20 13:32:45 +01001127 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128
Holger Schurigc97329e2008-03-18 11:20:21 +01001129 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001130}
1131
1132/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001133 * lbs_allocate_cmd_buffer - allocates the command buffer and links
1134 * it to command free queue
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001135 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001136 * @priv: A pointer to &struct lbs_private structure
1137 *
1138 * returns: 0 for success or -1 on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001139 */
Holger Schurig69f90322007-11-23 15:43:44 +01001140int lbs_allocate_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141{
1142 int ret = 0;
Dan Williamsddac4522007-12-11 13:49:39 -05001143 u32 bufsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001144 u32 i;
Dan Williamsddac4522007-12-11 13:49:39 -05001145 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001146
Holger Schurig8ff12da2007-08-02 11:54:31 -04001147 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001148
Dan Williamsddac4522007-12-11 13:49:39 -05001149 /* Allocate and initialize the command array */
1150 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1151 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001152 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001153 ret = -1;
1154 goto done;
1155 }
Dan Williamsddac4522007-12-11 13:49:39 -05001156 priv->cmd_array = cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001157
Dan Williamsddac4522007-12-11 13:49:39 -05001158 /* Allocate and initialize each command buffer in the command array */
1159 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1160 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1161 if (!cmdarray[i].cmdbuf) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001162 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001163 ret = -1;
1164 goto done;
1165 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001166 }
1167
Dan Williamsddac4522007-12-11 13:49:39 -05001168 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1169 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1170 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001172 ret = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001173
1174done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001175 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001176 return ret;
1177}
1178
1179/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001180 * lbs_free_cmd_buffer - free the command buffer
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001181 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001182 * @priv: A pointer to &struct lbs_private structure
1183 *
1184 * returns: 0 for success
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001185 */
Holger Schurig69f90322007-11-23 15:43:44 +01001186int lbs_free_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001187{
Dan Williamsddac4522007-12-11 13:49:39 -05001188 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001189 unsigned int i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001190
Holger Schurig8ff12da2007-08-02 11:54:31 -04001191 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192
1193 /* need to check if cmd array is allocated or not */
David Woodhouseaa21c002007-12-08 20:04:36 +00001194 if (priv->cmd_array == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001195 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001196 goto done;
1197 }
1198
Dan Williamsddac4522007-12-11 13:49:39 -05001199 cmdarray = priv->cmd_array;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001200
1201 /* Release shared memory buffers */
Dan Williamsddac4522007-12-11 13:49:39 -05001202 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1203 if (cmdarray[i].cmdbuf) {
1204 kfree(cmdarray[i].cmdbuf);
1205 cmdarray[i].cmdbuf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001206 }
1207 }
1208
1209 /* Release cmd_ctrl_node */
David Woodhouseaa21c002007-12-08 20:04:36 +00001210 if (priv->cmd_array) {
1211 kfree(priv->cmd_array);
1212 priv->cmd_array = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001213 }
1214
1215done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001216 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001217 return 0;
1218}
1219
1220/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001221 * lbs_get_free_cmd_node - gets a free command node if available in
1222 * command free queue
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001223 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001224 * @priv: A pointer to &struct lbs_private structure
1225 *
1226 * returns: A pointer to &cmd_ctrl_node structure on success
1227 * or %NULL on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001228 */
Dan Williamsd06956b2010-07-27 13:16:24 -07001229static struct cmd_ctrl_node *lbs_get_free_cmd_node(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001230{
1231 struct cmd_ctrl_node *tempnode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001232 unsigned long flags;
1233
Holger Schurig8ff12da2007-08-02 11:54:31 -04001234 lbs_deb_enter(LBS_DEB_HOST);
1235
David Woodhouseaa21c002007-12-08 20:04:36 +00001236 if (!priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001237 return NULL;
1238
David Woodhouseaa21c002007-12-08 20:04:36 +00001239 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001240
David Woodhouseaa21c002007-12-08 20:04:36 +00001241 if (!list_empty(&priv->cmdfreeq)) {
1242 tempnode = list_first_entry(&priv->cmdfreeq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001243 struct cmd_ctrl_node, list);
1244 list_del(&tempnode->list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001245 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001246 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001247 tempnode = NULL;
1248 }
1249
David Woodhouseaa21c002007-12-08 20:04:36 +00001250 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001251
Holger Schurig8ff12da2007-08-02 11:54:31 -04001252 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001253 return tempnode;
1254}
1255
1256/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001257 * lbs_execute_next_command - execute next command in command
1258 * pending queue. Will put firmware back to PS mode if applicable.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001259 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001260 * @priv: A pointer to &struct lbs_private structure
1261 *
1262 * returns: 0 on success or -1 on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001263 */
Holger Schurig69f90322007-11-23 15:43:44 +01001264int lbs_execute_next_command(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001265{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266 struct cmd_ctrl_node *cmdnode = NULL;
Dan Williamsddac4522007-12-11 13:49:39 -05001267 struct cmd_header *cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001268 unsigned long flags;
1269 int ret = 0;
1270
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001271 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1272 * only caller to us is lbs_thread() and we get even when a
1273 * data packet is received */
Holger Schurig8ff12da2007-08-02 11:54:31 -04001274 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001275
David Woodhouseaa21c002007-12-08 20:04:36 +00001276 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001277
David Woodhouseaa21c002007-12-08 20:04:36 +00001278 if (priv->cur_cmd) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001279 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001280 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001281 ret = -1;
1282 goto done;
1283 }
1284
David Woodhouseaa21c002007-12-08 20:04:36 +00001285 if (!list_empty(&priv->cmdpendingq)) {
1286 cmdnode = list_first_entry(&priv->cmdpendingq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001287 struct cmd_ctrl_node, list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288 }
1289
David Woodhouseaa21c002007-12-08 20:04:36 +00001290 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001291
1292 if (cmdnode) {
Dan Williamsddac4522007-12-11 13:49:39 -05001293 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294
Dan Williamsddac4522007-12-11 13:49:39 -05001295 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001296 if ((priv->psstate == PS_STATE_SLEEP) ||
1297 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001298 lbs_deb_host(
1299 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001300 le16_to_cpu(cmd->command),
David Woodhouseaa21c002007-12-08 20:04:36 +00001301 priv->psstate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001302 ret = -1;
1303 goto done;
1304 }
Holger Schurig8ff12da2007-08-02 11:54:31 -04001305 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
Dan Williamsddac4522007-12-11 13:49:39 -05001306 "0x%04x in psstate %d\n",
1307 le16_to_cpu(cmd->command), priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +00001308 } else if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001309 /*
1310 * 1. Non-PS command:
1311 * Queue it. set needtowakeup to TRUE if current state
Dan Williams0bb64082010-07-27 13:08:08 -07001312 * is SLEEP, otherwise call send EXIT_PS.
1313 * 2. PS command but not EXIT_PS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001314 * Ignore it.
Dan Williams0bb64082010-07-27 13:08:08 -07001315 * 3. PS command EXIT_PS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316 * Set needtowakeup to TRUE if current state is SLEEP,
1317 * otherwise send this command down to firmware
1318 * immediately.
1319 */
Dan Williamsddac4522007-12-11 13:49:39 -05001320 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001321 /* Prepare to send Exit PS,
1322 * this non PS command will be sent later */
David Woodhouseaa21c002007-12-08 20:04:36 +00001323 if ((priv->psstate == PS_STATE_SLEEP)
1324 || (priv->psstate == PS_STATE_PRE_SLEEP)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001325 ) {
1326 /* w/ new scheme, it will not reach here.
1327 since it is blocked in main_thread. */
David Woodhouseaa21c002007-12-08 20:04:36 +00001328 priv->needtowakeup = 1;
Dan Williams0bb64082010-07-27 13:08:08 -07001329 } else {
1330 lbs_set_ps_mode(priv,
1331 PS_MODE_ACTION_EXIT_PS,
1332 false);
1333 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001334
1335 ret = 0;
1336 goto done;
1337 } else {
1338 /*
1339 * PS command. Ignore it if it is not Exit_PS.
1340 * otherwise send it down immediately.
1341 */
David Woodhouse38bfab12007-12-16 23:26:54 -05001342 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001343
Holger Schurig8ff12da2007-08-02 11:54:31 -04001344 lbs_deb_host(
1345 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346 psm->action);
1347 if (psm->action !=
Dan Williams0bb64082010-07-27 13:08:08 -07001348 cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001349 lbs_deb_host(
1350 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001351 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001352 spin_lock_irqsave(&priv->driver_lock, flags);
1353 lbs_complete_command(priv, cmdnode, 0);
1354 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001355
1356 ret = 0;
1357 goto done;
1358 }
1359
David Woodhouseaa21c002007-12-08 20:04:36 +00001360 if ((priv->psstate == PS_STATE_SLEEP) ||
1361 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001362 lbs_deb_host(
1363 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001364 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001365 spin_lock_irqsave(&priv->driver_lock, flags);
1366 lbs_complete_command(priv, cmdnode, 0);
1367 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +00001368 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001369
1370 ret = 0;
1371 goto done;
1372 }
1373
Holger Schurig8ff12da2007-08-02 11:54:31 -04001374 lbs_deb_host(
1375 "EXEC_NEXT_CMD: sending EXIT_PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001376 }
1377 }
Li Zefanabe3ed12007-12-06 13:01:21 +01001378 list_del(&cmdnode->list);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001379 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001380 le16_to_cpu(cmd->command));
David Woodhoused9896ee2007-12-15 00:09:25 -05001381 lbs_submit_command(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001382 } else {
1383 /*
1384 * check if in power save mode, if yes, put the device back
1385 * to PS mode
1386 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301387#ifdef TODO
1388 /*
1389 * This was the old code for libertas+wext. Someone that
1390 * understands this beast should re-code it in a sane way.
1391 *
1392 * I actually don't understand why this is related to WPA
1393 * and to connection status, shouldn't powering should be
1394 * independ of such things?
1395 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001396 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1397 (priv->psstate == PS_STATE_FULL_POWER) &&
1398 ((priv->connect_status == LBS_CONNECTED) ||
Holger Schurig602114a2009-12-02 15:26:01 +01001399 lbs_mesh_connected(priv))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001400 if (priv->secinfo.WPAenabled ||
1401 priv->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001402 /* check for valid WPA group keys */
David Woodhouseaa21c002007-12-08 20:04:36 +00001403 if (priv->wpa_mcast_key.len ||
1404 priv->wpa_unicast_key.len) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001405 lbs_deb_host(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1407 " go back to PS_SLEEP");
Dan Williams0bb64082010-07-27 13:08:08 -07001408 lbs_set_ps_mode(priv,
1409 PS_MODE_ACTION_ENTER_PS,
1410 false);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001411 }
1412 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001413 lbs_deb_host(
1414 "EXEC_NEXT_CMD: cmdpendingq empty, "
1415 "go back to PS_SLEEP");
Dan Williams0bb64082010-07-27 13:08:08 -07001416 lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
1417 false);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418 }
1419 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301420#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001421 }
1422
1423 ret = 0;
1424done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001425 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001426 return ret;
1427}
1428
Holger Schurigf539f2e2008-03-26 13:22:11 +01001429static void lbs_send_confirmsleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001430{
1431 unsigned long flags;
Holger Schurigf539f2e2008-03-26 13:22:11 +01001432 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001433
Holger Schurig8ff12da2007-08-02 11:54:31 -04001434 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001435 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1436 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437
Holger Schurigf539f2e2008-03-26 13:22:11 +01001438 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1439 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001440 if (ret) {
Holger Schurigf539f2e2008-03-26 13:22:11 +01001441 lbs_pr_alert("confirm_sleep failed\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001442 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001443 }
Holger Schurig7919b892008-04-01 14:50:43 +02001444
1445 spin_lock_irqsave(&priv->driver_lock, flags);
1446
Holger Schuriga01f5452008-06-04 11:10:40 +02001447 /* We don't get a response on the sleep-confirmation */
1448 priv->dnld_sent = DNLD_RES_RECEIVED;
1449
Amitkumar Karwar66fceb62010-05-19 03:24:38 -07001450 if (priv->is_host_sleep_configured) {
1451 priv->is_host_sleep_activated = 1;
1452 wake_up_interruptible(&priv->host_sleep_q);
1453 }
1454
Holger Schurig7919b892008-04-01 14:50:43 +02001455 /* If nothing to do, go back to sleep (?) */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001456 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
Holger Schurig7919b892008-04-01 14:50:43 +02001457 priv->psstate = PS_STATE_SLEEP;
1458
1459 spin_unlock_irqrestore(&priv->driver_lock, flags);
1460
1461out:
Holger Schurigf539f2e2008-03-26 13:22:11 +01001462 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001463}
1464
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001465/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001466 * lbs_ps_confirm_sleep - checks condition and prepares to
1467 * send sleep confirm command to firmware if ok
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001468 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001469 * @priv: A pointer to &struct lbs_private structure
1470 *
1471 * returns: n/a
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001472 */
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001473void lbs_ps_confirm_sleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001474{
1475 unsigned long flags =0;
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001476 int allowed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001477
Holger Schurig8ff12da2007-08-02 11:54:31 -04001478 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001479
Holger Schuriga01f5452008-06-04 11:10:40 +02001480 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig634b8f42007-05-25 13:05:16 -04001481 if (priv->dnld_sent) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001482 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001483 lbs_deb_host("dnld_sent was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001484 }
1485
Holger Schurig7919b892008-04-01 14:50:43 +02001486 /* In-progress command? */
David Woodhouseaa21c002007-12-08 20:04:36 +00001487 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001488 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001489 lbs_deb_host("cur_cmd was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001490 }
Holger Schurig7919b892008-04-01 14:50:43 +02001491
1492 /* Pending events or command responses? */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001493 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001494 allowed = 0;
Holger Schurig7919b892008-04-01 14:50:43 +02001495 lbs_deb_host("pending events or command responses\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001496 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001497 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001498
1499 if (allowed) {
Holger Schurig10078322007-11-15 18:05:47 -05001500 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
Holger Schurigf539f2e2008-03-26 13:22:11 +01001501 lbs_send_confirmsleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001502 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001503 lbs_deb_host("sleep confirm has been delayed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001504 }
1505
Holger Schurig8ff12da2007-08-02 11:54:31 -04001506 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001507}
Holger Schurig675787e2007-12-05 17:58:11 +01001508
1509
Anna Neal0112c9e2008-09-11 11:17:25 -07001510/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001511 * lbs_set_tpc_cfg - Configures the transmission power control functionality
Anna Neal0112c9e2008-09-11 11:17:25 -07001512 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001513 * @priv: A pointer to &struct lbs_private structure
1514 * @enable: Transmission power control enable
1515 * @p0: Power level when link quality is good (dBm).
1516 * @p1: Power level when link quality is fair (dBm).
1517 * @p2: Power level when link quality is poor (dBm).
1518 * @usesnr: Use Signal to Noise Ratio in TPC
Anna Neal0112c9e2008-09-11 11:17:25 -07001519 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001520 * returns: 0 on success
Anna Neal0112c9e2008-09-11 11:17:25 -07001521 */
1522int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1523 int8_t p2, int usesnr)
1524{
1525 struct cmd_ds_802_11_tpc_cfg cmd;
1526 int ret;
1527
1528 memset(&cmd, 0, sizeof(cmd));
1529 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1530 cmd.action = cpu_to_le16(CMD_ACT_SET);
1531 cmd.enable = !!enable;
Anna Neal3ed6e082008-09-26 11:34:35 -04001532 cmd.usesnr = !!usesnr;
Anna Neal0112c9e2008-09-11 11:17:25 -07001533 cmd.P0 = p0;
1534 cmd.P1 = p1;
1535 cmd.P2 = p2;
1536
1537 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1538
1539 return ret;
1540}
1541
1542/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001543 * lbs_set_power_adapt_cfg - Configures the power adaptation settings
Anna Neal0112c9e2008-09-11 11:17:25 -07001544 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001545 * @priv: A pointer to &struct lbs_private structure
1546 * @enable: Power adaptation enable
1547 * @p0: Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1548 * @p1: Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1549 * @p2: Power level for 48 and 54 Mbps (dBm).
Anna Neal0112c9e2008-09-11 11:17:25 -07001550 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001551 * returns: 0 on Success
Anna Neal0112c9e2008-09-11 11:17:25 -07001552 */
1553
1554int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1555 int8_t p1, int8_t p2)
1556{
1557 struct cmd_ds_802_11_pa_cfg cmd;
1558 int ret;
1559
1560 memset(&cmd, 0, sizeof(cmd));
1561 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1562 cmd.action = cpu_to_le16(CMD_ACT_SET);
1563 cmd.enable = !!enable;
1564 cmd.P0 = p0;
1565 cmd.P1 = p1;
1566 cmd.P2 = p2;
1567
1568 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1569
1570 return ret;
1571}
1572
1573
Holger Schurig6d898b12009-10-14 16:49:53 +02001574struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001575 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1576 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1577 unsigned long callback_arg)
Holger Schurig675787e2007-12-05 17:58:11 +01001578{
Holger Schurig675787e2007-12-05 17:58:11 +01001579 struct cmd_ctrl_node *cmdnode;
Holger Schurig675787e2007-12-05 17:58:11 +01001580
1581 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurig675787e2007-12-05 17:58:11 +01001582
David Woodhouseaa21c002007-12-08 20:04:36 +00001583 if (priv->surpriseremoved) {
Holger Schurig675787e2007-12-05 17:58:11 +01001584 lbs_deb_host("PREP_CMD: card removed\n");
David Woodhouse3399ea52007-12-15 03:09:33 -05001585 cmdnode = ERR_PTR(-ENOENT);
Holger Schurig675787e2007-12-05 17:58:11 +01001586 goto done;
1587 }
1588
Dan Williams77ccdcf2010-07-27 13:15:45 -07001589 /* No commands are allowed in Deep Sleep until we toggle the GPIO
1590 * to wake up the card and it has signaled that it's ready.
1591 */
1592 if (!priv->is_auto_deep_sleep_enabled) {
1593 if (priv->is_deep_sleep) {
1594 lbs_deb_cmd("command not allowed in deep sleep\n");
1595 cmdnode = ERR_PTR(-EBUSY);
1596 goto done;
1597 }
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001598 }
1599
Dan Williamsd06956b2010-07-27 13:16:24 -07001600 cmdnode = lbs_get_free_cmd_node(priv);
Holger Schurig675787e2007-12-05 17:58:11 +01001601 if (cmdnode == NULL) {
1602 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1603
1604 /* Wake up main thread to execute next command */
1605 wake_up_interruptible(&priv->waitq);
David Woodhouse3399ea52007-12-15 03:09:33 -05001606 cmdnode = ERR_PTR(-ENOBUFS);
Holger Schurig675787e2007-12-05 17:58:11 +01001607 goto done;
1608 }
1609
David Woodhouse448a51a2007-12-08 00:59:54 +00001610 cmdnode->callback = callback;
David Woodhouse1309b552007-12-10 13:36:10 -05001611 cmdnode->callback_arg = callback_arg;
Holger Schurig675787e2007-12-05 17:58:11 +01001612
Dan Williams7ad994d2007-12-11 12:33:30 -05001613 /* Copy the incoming command to the buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001614 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
Dan Williams7ad994d2007-12-11 12:33:30 -05001615
Holger Schurig675787e2007-12-05 17:58:11 +01001616 /* Set sequence number, clean result, move to buffer */
David Woodhouseaa21c002007-12-08 20:04:36 +00001617 priv->seqnum++;
Dan Williamsddac4522007-12-11 13:49:39 -05001618 cmdnode->cmdbuf->command = cpu_to_le16(command);
1619 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1620 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1621 cmdnode->cmdbuf->result = 0;
Holger Schurig675787e2007-12-05 17:58:11 +01001622
1623 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1624
Holger Schurig675787e2007-12-05 17:58:11 +01001625 cmdnode->cmdwaitqwoken = 0;
David Woodhouse681ffbb2007-12-15 20:04:54 -05001626 lbs_queue_cmd(priv, cmdnode);
Holger Schurig675787e2007-12-05 17:58:11 +01001627 wake_up_interruptible(&priv->waitq);
1628
David Woodhouse3399ea52007-12-15 03:09:33 -05001629 done:
1630 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1631 return cmdnode;
1632}
1633
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001634void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1635 struct cmd_header *in_cmd, int in_cmd_size)
1636{
1637 lbs_deb_enter(LBS_DEB_CMD);
1638 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1639 lbs_cmd_async_callback, 0);
1640 lbs_deb_leave(LBS_DEB_CMD);
1641}
1642
David Woodhouse3399ea52007-12-15 03:09:33 -05001643int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1644 struct cmd_header *in_cmd, int in_cmd_size,
1645 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1646 unsigned long callback_arg)
1647{
1648 struct cmd_ctrl_node *cmdnode;
1649 unsigned long flags;
1650 int ret = 0;
1651
1652 lbs_deb_enter(LBS_DEB_HOST);
1653
1654 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1655 callback, callback_arg);
1656 if (IS_ERR(cmdnode)) {
1657 ret = PTR_ERR(cmdnode);
1658 goto done;
1659 }
1660
Holger Schurig675787e2007-12-05 17:58:11 +01001661 might_sleep();
1662 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1663
David Woodhouseaa21c002007-12-08 20:04:36 +00001664 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseae125bf2007-12-15 04:22:52 -05001665 ret = cmdnode->result;
1666 if (ret)
1667 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1668 command, ret);
David Woodhouse3399ea52007-12-15 03:09:33 -05001669
David Woodhousead12d0f2007-12-15 02:06:16 -05001670 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
David Woodhouseaa21c002007-12-08 20:04:36 +00001671 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig675787e2007-12-05 17:58:11 +01001672
1673done:
1674 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1675 return ret;
1676}
Dan Williams14e865b2007-12-10 15:11:23 -05001677EXPORT_SYMBOL_GPL(__lbs_cmd);