blob: d798bcc0d83a9561f8a8fdb68826d720de9fc970 [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
Alexey Dobriyana6b7a402011-06-06 10:43:46 +00006#include <linux/hardirq.h>
Holger Schurig7919b892008-04-01 14:50:43 +02007#include <linux/kfifo.h>
Holger Schurige93156e2009-10-22 15:30:58 +02008#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Dan Williamsa45b6f42010-07-27 12:54:34 -070010#include <linux/if_arp.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040011#include <linux/export.h>
Holger Schurige93156e2009-10-22 15:30:58 +020012
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "decl.h"
Kiran Divekare86dc1c2010-06-14 22:01:26 +053014#include "cfg.h"
Dan Williams6e66f032007-12-11 12:42:16 -050015#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020016
Dan Williams9fb76632010-07-27 12:55:21 -070017#define CAL_NF(nf) ((s32)(-(s32)(nf)))
18#define CAL_RSSI(snr, nf) ((s32)((s32)(snr) + CAL_NF(nf)))
Holger Schurige93156e2009-10-22 15:30:58 +020019
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070021 * lbs_cmd_copyback - Simple callback that copies response back into command
Holger Schurig8db4a2b2008-03-19 10:11:00 +010022 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070023 * @priv: A pointer to &struct lbs_private structure
24 * @extra: A pointer to the original command structure for which
25 * 'resp' is a response
26 * @resp: A pointer to the command response
Holger Schurig8db4a2b2008-03-19 10:11:00 +010027 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070028 * returns: 0 on success, error on failure
Holger Schurig8db4a2b2008-03-19 10:11:00 +010029 */
30int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
31 struct cmd_header *resp)
32{
33 struct cmd_header *buf = (void *)extra;
34 uint16_t copy_len;
35
36 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
37 memcpy(buf, resp, copy_len);
38 return 0;
39}
40EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
41
42/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070043 * lbs_cmd_async_callback - Simple callback that ignores the result.
44 * Use this if you just want to send a command to the hardware, but don't
Holger Schurig8db4a2b2008-03-19 10:11:00 +010045 * care for the result.
46 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070047 * @priv: ignored
48 * @extra: ignored
49 * @resp: ignored
Holger Schurig8db4a2b2008-03-19 10:11:00 +010050 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070051 * returns: 0 for success
Holger Schurig8db4a2b2008-03-19 10:11:00 +010052 */
53static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
54 struct cmd_header *resp)
55{
56 return 0;
57}
58
59
60/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070061 * is_command_allowed_in_ps - tests if a command is allowed in Power Save mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020062 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070063 * @cmd: the command ID
64 *
65 * returns: 1 if allowed, 0 if not allowed
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020066 */
Dan Williams852e1f22007-12-10 15:24:47 -050067static u8 is_command_allowed_in_ps(u16 cmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020068{
Dan Williams852e1f22007-12-10 15:24:47 -050069 switch (cmd) {
70 case CMD_802_11_RSSI:
71 return 1;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -070072 case CMD_802_11_HOST_SLEEP_CFG:
73 return 1;
Dan Williams852e1f22007-12-10 15:24:47 -050074 default:
75 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020076 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020077 return 0;
78}
79
Dan Williams6e66f032007-12-11 12:42:16 -050080/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070081 * lbs_update_hw_spec - Updates the hardware details like MAC address
82 * and regulatory region
Dan Williams6e66f032007-12-11 12:42:16 -050083 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070084 * @priv: A pointer to &struct lbs_private structure
Dan Williams6e66f032007-12-11 12:42:16 -050085 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070086 * returns: 0 on success, error on failure
Dan Williams6e66f032007-12-11 12:42:16 -050087 */
88int lbs_update_hw_spec(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020089{
Dan Williams6e66f032007-12-11 12:42:16 -050090 struct cmd_ds_get_hw_spec cmd;
91 int ret = -1;
92 u32 i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020093
Holger Schurig9012b282007-05-25 11:27:16 -040094 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020095
Dan Williams6e66f032007-12-11 12:42:16 -050096 memset(&cmd, 0, sizeof(cmd));
97 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
98 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
David Woodhouse689442d2007-12-12 16:00:42 -050099 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
Dan Williams6e66f032007-12-11 12:42:16 -0500100 if (ret)
101 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200102
Dan Williams6e66f032007-12-11 12:42:16 -0500103 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500104
Holger Schurigdac10a92008-01-16 15:55:22 +0100105 /* The firmware release is in an interesting format: the patch
106 * level is in the most significant nibble ... so fix that: */
107 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
108 priv->fwrelease = (priv->fwrelease << 8) |
109 (priv->fwrelease >> 24 & 0xff);
110
111 /* Some firmware capabilities:
112 * CF card firmware 5.0.16p0: cap 0x00000303
113 * USB dongle firmware 5.110.17p2: cap 0x00000303
114 */
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700115 netdev_info(priv->dev, "%pM, fw %u.%u.%up%u, cap 0x%08x\n",
Johannes Berge1749612008-10-27 15:59:26 -0700116 cmd.permanentaddr,
Holger Schurigdac10a92008-01-16 15:55:22 +0100117 priv->fwrelease >> 24 & 0xff,
118 priv->fwrelease >> 16 & 0xff,
119 priv->fwrelease >> 8 & 0xff,
120 priv->fwrelease & 0xff,
121 priv->fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500122 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
123 cmd.hwifversion, cmd.version);
124
125 /* Clamp region code to 8-bit since FW spec indicates that it should
126 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
127 * returns non-zero high 8 bits here.
Marek Vasut15483992009-07-16 19:19:53 +0200128 *
129 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
130 * need to check for this problem and handle it properly.
Dan Williams6e66f032007-12-11 12:42:16 -0500131 */
Marek Vasut15483992009-07-16 19:19:53 +0200132 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
133 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
134 else
135 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
Dan Williams6e66f032007-12-11 12:42:16 -0500136
137 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
138 /* use the region code to search for the index */
139 if (priv->regioncode == lbs_region_code_to_index[i])
140 break;
141 }
142
143 /* if it's unidentified region code, use the default (USA) */
144 if (i >= MRVDRV_MAX_REGION_CODE) {
145 priv->regioncode = 0x10;
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700146 netdev_info(priv->dev,
147 "unidentified region code; using the default (USA)\n");
Dan Williams6e66f032007-12-11 12:42:16 -0500148 }
149
150 if (priv->current_addr[0] == 0xff)
151 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
152
Vasily Khoruzhick75abde42011-01-21 22:44:49 +0200153 if (!priv->copied_hwaddr) {
154 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
155 if (priv->mesh_dev)
156 memcpy(priv->mesh_dev->dev_addr,
157 priv->current_addr, ETH_ALEN);
158 priv->copied_hwaddr = 1;
159 }
Dan Williams6e66f032007-12-11 12:42:16 -0500160
Dan Williams6e66f032007-12-11 12:42:16 -0500161out:
Holger Schurig9012b282007-05-25 11:27:16 -0400162 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams6e66f032007-12-11 12:42:16 -0500163 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164}
165
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700166static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
167 struct cmd_header *resp)
168{
169 lbs_deb_enter(LBS_DEB_CMD);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530170 if (priv->is_host_sleep_activated) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700171 priv->is_host_sleep_configured = 0;
172 if (priv->psstate == PS_STATE_FULL_POWER) {
173 priv->is_host_sleep_activated = 0;
174 wake_up_interruptible(&priv->host_sleep_q);
175 }
176 } else {
177 priv->is_host_sleep_configured = 1;
178 }
179 lbs_deb_leave(LBS_DEB_CMD);
180 return 0;
181}
182
Anna Neal582c1b52008-10-20 16:46:56 -0700183int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
184 struct wol_config *p_wol_config)
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500185{
186 struct cmd_ds_host_sleep cmd_config;
187 int ret;
188
Deepak Saxenaae63a332010-10-31 13:40:33 +0000189 /*
190 * Certain firmware versions do not support EHS_REMOVE_WAKEUP command
191 * and the card will return a failure. Since we need to be
192 * able to reset the mask, in those cases we set a 0 mask instead.
193 */
194 if (criteria == EHS_REMOVE_WAKEUP && !priv->ehs_remove_supported)
195 criteria = 0;
196
David Woodhouse9fae8992007-12-15 03:46:44 -0500197 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500198 cmd_config.criteria = cpu_to_le32(criteria);
David Woodhouse506e9022007-12-12 20:06:06 -0500199 cmd_config.gpio = priv->wol_gpio;
200 cmd_config.gap = priv->wol_gap;
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500201
Anna Neal582c1b52008-10-20 16:46:56 -0700202 if (p_wol_config != NULL)
203 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
204 sizeof(struct wol_config));
205 else
206 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
207
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700208 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
209 le16_to_cpu(cmd_config.hdr.size),
210 lbs_ret_host_sleep_cfg, 0);
David Woodhouse506e9022007-12-12 20:06:06 -0500211 if (!ret) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700212 if (p_wol_config)
Anna Neal582c1b52008-10-20 16:46:56 -0700213 memcpy((uint8_t *) p_wol_config,
214 (uint8_t *)&cmd_config.wol_conf,
215 sizeof(struct wol_config));
David Woodhouse506e9022007-12-12 20:06:06 -0500216 } else {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700217 netdev_info(priv->dev, "HOST_SLEEP_CFG failed %d\n", ret);
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500218 }
David Woodhouse506e9022007-12-12 20:06:06 -0500219
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500220 return ret;
221}
222EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
223
Dan Williams0bb64082010-07-27 13:08:08 -0700224/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700225 * lbs_set_ps_mode - Sets the Power Save mode
Dan Williams0bb64082010-07-27 13:08:08 -0700226 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700227 * @priv: A pointer to &struct lbs_private structure
228 * @cmd_action: The Power Save operation (PS_MODE_ACTION_ENTER_PS or
Dan Williams0bb64082010-07-27 13:08:08 -0700229 * PS_MODE_ACTION_EXIT_PS)
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700230 * @block: Whether to block on a response or not
Dan Williams0bb64082010-07-27 13:08:08 -0700231 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700232 * returns: 0 on success, error on failure
Dan Williams0bb64082010-07-27 13:08:08 -0700233 */
234int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200235{
Dan Williams0bb64082010-07-27 13:08:08 -0700236 struct cmd_ds_802_11_ps_mode cmd;
237 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200238
Holger Schurig9012b282007-05-25 11:27:16 -0400239 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200240
Dan Williams0bb64082010-07-27 13:08:08 -0700241 memset(&cmd, 0, sizeof(cmd));
242 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
243 cmd.action = cpu_to_le16(cmd_action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200244
Dan Williams0bb64082010-07-27 13:08:08 -0700245 if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
246 lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
247 cmd.multipledtim = cpu_to_le16(1); /* Default DTIM multiple */
248 } else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
249 lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
250 } else {
251 /* We don't handle CONFIRM_SLEEP here because it needs to
252 * be fastpathed to the firmware.
253 */
254 lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
255 ret = -EOPNOTSUPP;
256 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257 }
258
Dan Williams0bb64082010-07-27 13:08:08 -0700259 if (block)
260 ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
261 else
262 lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
263
264out:
265 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
266 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200267}
268
David Woodhouse3fbe1042007-12-17 23:48:31 -0500269int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
270 struct sleep_params *sp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271{
David Woodhouse3fbe1042007-12-17 23:48:31 -0500272 struct cmd_ds_802_11_sleep_params cmd;
273 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274
Holger Schurig9012b282007-05-25 11:27:16 -0400275 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276
Dan Williams0aef64d2007-08-02 11:31:18 -0400277 if (cmd_action == CMD_ACT_GET) {
David Woodhouse3fbe1042007-12-17 23:48:31 -0500278 memset(&cmd, 0, sizeof(cmd));
279 } else {
280 cmd.error = cpu_to_le16(sp->sp_error);
281 cmd.offset = cpu_to_le16(sp->sp_offset);
282 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
283 cmd.calcontrol = sp->sp_calcontrol;
284 cmd.externalsleepclk = sp->sp_extsleepclk;
285 cmd.reserved = cpu_to_le16(sp->sp_reserved);
286 }
287 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
288 cmd.action = cpu_to_le16(cmd_action);
289
290 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
291
292 if (!ret) {
293 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
294 "calcontrol 0x%x extsleepclk 0x%x\n",
295 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
296 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
297 cmd.externalsleepclk);
298
299 sp->sp_error = le16_to_cpu(cmd.error);
300 sp->sp_offset = le16_to_cpu(cmd.offset);
301 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
302 sp->sp_calcontrol = cmd.calcontrol;
303 sp->sp_extsleepclk = cmd.externalsleepclk;
304 sp->sp_reserved = le16_to_cpu(cmd.reserved);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305 }
306
David Woodhouse3fbe1042007-12-17 23:48:31 -0500307 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200308 return 0;
309}
310
Amitkumar Karwar49125452009-09-30 20:04:38 -0700311static int lbs_wait_for_ds_awake(struct lbs_private *priv)
312{
313 int ret = 0;
314
315 lbs_deb_enter(LBS_DEB_CMD);
316
317 if (priv->is_deep_sleep) {
318 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
319 !priv->is_deep_sleep, (10 * HZ))) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700320 netdev_err(priv->dev, "ds_awake_q: timer expired\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -0700321 ret = -1;
322 }
323 }
324
325 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
326 return ret;
327}
328
329int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
330{
331 int ret = 0;
332
333 lbs_deb_enter(LBS_DEB_CMD);
334
335 if (deep_sleep) {
336 if (priv->is_deep_sleep != 1) {
337 lbs_deb_cmd("deep sleep: sleep\n");
338 BUG_ON(!priv->enter_deep_sleep);
339 ret = priv->enter_deep_sleep(priv);
340 if (!ret) {
341 netif_stop_queue(priv->dev);
342 netif_carrier_off(priv->dev);
343 }
344 } else {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700345 netdev_err(priv->dev, "deep sleep: already enabled\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -0700346 }
347 } else {
348 if (priv->is_deep_sleep) {
349 lbs_deb_cmd("deep sleep: wakeup\n");
350 BUG_ON(!priv->exit_deep_sleep);
351 ret = priv->exit_deep_sleep(priv);
352 if (!ret) {
353 ret = lbs_wait_for_ds_awake(priv);
354 if (ret)
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700355 netdev_err(priv->dev,
356 "deep sleep: wakeup failed\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -0700357 }
358 }
359 }
360
361 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
362 return ret;
363}
364
Amitkumar Karwar13118432010-07-08 06:43:48 +0530365static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
366 unsigned long dummy,
367 struct cmd_header *cmd)
368{
369 lbs_deb_enter(LBS_DEB_FW);
370 priv->is_host_sleep_activated = 1;
371 wake_up_interruptible(&priv->host_sleep_q);
372 lbs_deb_leave(LBS_DEB_FW);
373 return 0;
374}
375
376int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
377{
378 struct cmd_header cmd;
379 int ret = 0;
380 uint32_t criteria = EHS_REMOVE_WAKEUP;
381
382 lbs_deb_enter(LBS_DEB_CMD);
383
384 if (host_sleep) {
385 if (priv->is_host_sleep_activated != 1) {
386 memset(&cmd, 0, sizeof(cmd));
387 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
388 (struct wol_config *)NULL);
389 if (ret) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700390 netdev_info(priv->dev,
391 "Host sleep configuration failed: %d\n",
392 ret);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530393 return ret;
394 }
395 if (priv->psstate == PS_STATE_FULL_POWER) {
396 ret = __lbs_cmd(priv,
397 CMD_802_11_HOST_SLEEP_ACTIVATE,
398 &cmd,
399 sizeof(cmd),
400 lbs_ret_host_sleep_activate, 0);
401 if (ret)
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700402 netdev_info(priv->dev,
403 "HOST_SLEEP_ACTIVATE failed: %d\n",
404 ret);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530405 }
406
407 if (!wait_event_interruptible_timeout(
408 priv->host_sleep_q,
409 priv->is_host_sleep_activated,
410 (10 * HZ))) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700411 netdev_err(priv->dev,
412 "host_sleep_q: timer expired\n");
Amitkumar Karwar13118432010-07-08 06:43:48 +0530413 ret = -1;
414 }
415 } else {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700416 netdev_err(priv->dev, "host sleep: already enabled\n");
Amitkumar Karwar13118432010-07-08 06:43:48 +0530417 }
418 } else {
419 if (priv->is_host_sleep_activated)
420 ret = lbs_host_sleep_cfg(priv, criteria,
421 (struct wol_config *)NULL);
422 }
423
424 return ret;
425}
426
Dan Williams39fcf7a2008-09-10 12:49:00 -0400427/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700428 * lbs_set_snmp_mib - Set an SNMP MIB value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400429 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700430 * @priv: A pointer to &struct lbs_private structure
431 * @oid: The OID to set in the firmware
432 * @val: Value to set the OID to
Dan Williams39fcf7a2008-09-10 12:49:00 -0400433 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700434 * returns: 0 on success, error on failure
Dan Williams39fcf7a2008-09-10 12:49:00 -0400435 */
436int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437{
Dan Williams39fcf7a2008-09-10 12:49:00 -0400438 struct cmd_ds_802_11_snmp_mib cmd;
439 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440
Holger Schurig9012b282007-05-25 11:27:16 -0400441 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200442
Dan Williams39fcf7a2008-09-10 12:49:00 -0400443 memset(&cmd, 0, sizeof (cmd));
444 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
445 cmd.action = cpu_to_le16(CMD_ACT_SET);
446 cmd.oid = cpu_to_le16((u16) oid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447
Dan Williams39fcf7a2008-09-10 12:49:00 -0400448 switch (oid) {
449 case SNMP_MIB_OID_BSS_TYPE:
450 cmd.bufsize = cpu_to_le16(sizeof(u8));
Holger Schurigfef06402009-10-22 15:30:59 +0200451 cmd.value[0] = val;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200452 break;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400453 case SNMP_MIB_OID_11D_ENABLE:
454 case SNMP_MIB_OID_FRAG_THRESHOLD:
455 case SNMP_MIB_OID_RTS_THRESHOLD:
456 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
457 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
458 cmd.bufsize = cpu_to_le16(sizeof(u16));
459 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460 break;
461 default:
Dan Williams39fcf7a2008-09-10 12:49:00 -0400462 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
463 ret = -EINVAL;
464 goto out;
465 }
466
467 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
468 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
469
470 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
471
472out:
473 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
474 return ret;
475}
476
477/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700478 * lbs_get_snmp_mib - Get an SNMP MIB value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400479 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700480 * @priv: A pointer to &struct lbs_private structure
481 * @oid: The OID to retrieve from the firmware
482 * @out_val: Location for the returned value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400483 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700484 * returns: 0 on success, error on failure
Dan Williams39fcf7a2008-09-10 12:49:00 -0400485 */
486int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
487{
488 struct cmd_ds_802_11_snmp_mib cmd;
489 int ret;
490
491 lbs_deb_enter(LBS_DEB_CMD);
492
493 memset(&cmd, 0, sizeof (cmd));
494 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
495 cmd.action = cpu_to_le16(CMD_ACT_GET);
496 cmd.oid = cpu_to_le16(oid);
497
498 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
499 if (ret)
500 goto out;
501
502 switch (le16_to_cpu(cmd.bufsize)) {
503 case sizeof(u8):
Holger Schurigfef06402009-10-22 15:30:59 +0200504 *out_val = cmd.value[0];
Dan Williams39fcf7a2008-09-10 12:49:00 -0400505 break;
506 case sizeof(u16):
507 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
508 break;
509 default:
510 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
511 oid, le16_to_cpu(cmd.bufsize));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512 break;
513 }
514
Dan Williams39fcf7a2008-09-10 12:49:00 -0400515out:
516 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
517 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200518}
519
Dan Williams87c8c722008-08-19 15:15:35 -0400520/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700521 * lbs_get_tx_power - Get the min, max, and current TX power
Dan Williams87c8c722008-08-19 15:15:35 -0400522 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700523 * @priv: A pointer to &struct lbs_private structure
524 * @curlevel: Current power level in dBm
525 * @minlevel: Minimum supported power level in dBm (optional)
526 * @maxlevel: Maximum supported power level in dBm (optional)
Dan Williams87c8c722008-08-19 15:15:35 -0400527 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700528 * returns: 0 on success, error on failure
Dan Williams87c8c722008-08-19 15:15:35 -0400529 */
530int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
531 s16 *maxlevel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532{
Dan Williams87c8c722008-08-19 15:15:35 -0400533 struct cmd_ds_802_11_rf_tx_power cmd;
534 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535
Holger Schurig9012b282007-05-25 11:27:16 -0400536 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537
Dan Williams87c8c722008-08-19 15:15:35 -0400538 memset(&cmd, 0, sizeof(cmd));
539 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
540 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541
Dan Williams87c8c722008-08-19 15:15:35 -0400542 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
543 if (ret == 0) {
544 *curlevel = le16_to_cpu(cmd.curlevel);
545 if (minlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100546 *minlevel = cmd.minlevel;
Dan Williams87c8c722008-08-19 15:15:35 -0400547 if (maxlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100548 *maxlevel = cmd.maxlevel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 }
Holger Schurig9012b282007-05-25 11:27:16 -0400550
551 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams87c8c722008-08-19 15:15:35 -0400552 return ret;
553}
554
555/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700556 * lbs_set_tx_power - Set the TX power
Dan Williams87c8c722008-08-19 15:15:35 -0400557 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700558 * @priv: A pointer to &struct lbs_private structure
559 * @dbm: The desired power level in dBm
Dan Williams87c8c722008-08-19 15:15:35 -0400560 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700561 * returns: 0 on success, error on failure
Dan Williams87c8c722008-08-19 15:15:35 -0400562 */
563int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
564{
565 struct cmd_ds_802_11_rf_tx_power cmd;
566 int ret;
567
568 lbs_deb_enter(LBS_DEB_CMD);
569
570 memset(&cmd, 0, sizeof(cmd));
571 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
572 cmd.action = cpu_to_le16(CMD_ACT_SET);
573 cmd.curlevel = cpu_to_le16(dbm);
574
575 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
576
577 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
578
579 lbs_deb_leave(LBS_DEB_CMD);
580 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581}
582
Dan Williamsa45b6f42010-07-27 12:54:34 -0700583/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700584 * lbs_set_monitor_mode - Enable or disable monitor mode
585 * (only implemented on OLPC usb8388 FW)
Dan Williamsa45b6f42010-07-27 12:54:34 -0700586 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700587 * @priv: A pointer to &struct lbs_private structure
588 * @enable: 1 to enable monitor mode, 0 to disable
Dan Williamsa45b6f42010-07-27 12:54:34 -0700589 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700590 * returns: 0 on success, error on failure
Dan Williamsa45b6f42010-07-27 12:54:34 -0700591 */
592int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400593{
Dan Williamsa45b6f42010-07-27 12:54:34 -0700594 struct cmd_ds_802_11_monitor_mode cmd;
595 int ret;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400596
Dan Williamsa45b6f42010-07-27 12:54:34 -0700597 memset(&cmd, 0, sizeof(cmd));
598 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
599 cmd.action = cpu_to_le16(CMD_ACT_SET);
600 if (enable)
601 cmd.mode = cpu_to_le16(0x1);
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400602
Dan Williamsa45b6f42010-07-27 12:54:34 -0700603 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
604
605 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
606 if (ret == 0) {
607 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
608 ARPHRD_ETHER;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400609 }
610
Dan Williamsa45b6f42010-07-27 12:54:34 -0700611 lbs_deb_leave(LBS_DEB_CMD);
612 return ret;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400613}
614
Dan Williams2dd4b262007-12-11 16:54:15 -0500615/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700616 * lbs_get_channel - Get the radio channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500617 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700618 * @priv: A pointer to &struct lbs_private structure
Dan Williams2dd4b262007-12-11 16:54:15 -0500619 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700620 * returns: The channel on success, error on failure
Dan Williams2dd4b262007-12-11 16:54:15 -0500621 */
Holger Schuriga3cbfb02009-10-16 17:33:56 +0200622static int lbs_get_channel(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623{
Dan Williams2dd4b262007-12-11 16:54:15 -0500624 struct cmd_ds_802_11_rf_channel cmd;
625 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626
Holger Schurig8ff12da2007-08-02 11:54:31 -0400627 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200628
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200629 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500630 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
631 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200632
David Woodhouse689442d2007-12-12 16:00:42 -0500633 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500634 if (ret)
635 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200636
Dan Williamscb182a62007-12-11 17:35:51 -0500637 ret = le16_to_cpu(cmd.channel);
638 lbs_deb_cmd("current radio channel is %d\n", ret);
Dan Williams2dd4b262007-12-11 16:54:15 -0500639
640out:
641 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
642 return ret;
643}
644
Holger Schurig73ab1f22008-04-02 16:52:19 +0200645int lbs_update_channel(struct lbs_private *priv)
646{
647 int ret;
648
649 /* the channel in f/w could be out of sync; get the current channel */
650 lbs_deb_enter(LBS_DEB_ASSOC);
651
652 ret = lbs_get_channel(priv);
653 if (ret > 0) {
Holger Schurigc14951f2009-10-22 15:30:50 +0200654 priv->channel = ret;
Holger Schurig73ab1f22008-04-02 16:52:19 +0200655 ret = 0;
656 }
657 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
658 return ret;
659}
660
Dan Williams2dd4b262007-12-11 16:54:15 -0500661/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700662 * lbs_set_channel - Set the radio channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500663 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700664 * @priv: A pointer to &struct lbs_private structure
665 * @channel: The desired channel, or 0 to clear a locked channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500666 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700667 * returns: 0 on success, error on failure
Dan Williams2dd4b262007-12-11 16:54:15 -0500668 */
669int lbs_set_channel(struct lbs_private *priv, u8 channel)
670{
671 struct cmd_ds_802_11_rf_channel cmd;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530672#ifdef DEBUG
Holger Schurigc14951f2009-10-22 15:30:50 +0200673 u8 old_channel = priv->channel;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530674#endif
Dan Williams2dd4b262007-12-11 16:54:15 -0500675 int ret = 0;
676
677 lbs_deb_enter(LBS_DEB_CMD);
678
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200679 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500680 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
681 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
682 cmd.channel = cpu_to_le16(channel);
683
David Woodhouse689442d2007-12-12 16:00:42 -0500684 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500685 if (ret)
686 goto out;
687
Holger Schurigc14951f2009-10-22 15:30:50 +0200688 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
Dan Williamscb182a62007-12-11 17:35:51 -0500689 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
Holger Schurigc14951f2009-10-22 15:30:50 +0200690 priv->channel);
Dan Williams2dd4b262007-12-11 16:54:15 -0500691
692out:
693 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
694 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695}
696
Dan Williams9fb76632010-07-27 12:55:21 -0700697/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700698 * lbs_get_rssi - Get current RSSI and noise floor
Dan Williams9fb76632010-07-27 12:55:21 -0700699 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700700 * @priv: A pointer to &struct lbs_private structure
701 * @rssi: On successful return, signal level in mBm
702 * @nf: On successful return, Noise floor
Dan Williams9fb76632010-07-27 12:55:21 -0700703 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700704 * returns: The channel on success, error on failure
Dan Williams9fb76632010-07-27 12:55:21 -0700705 */
706int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
707{
708 struct cmd_ds_802_11_rssi cmd;
709 int ret = 0;
710
711 lbs_deb_enter(LBS_DEB_CMD);
712
713 BUG_ON(rssi == NULL);
714 BUG_ON(nf == NULL);
715
716 memset(&cmd, 0, sizeof(cmd));
717 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
718 /* Average SNR over last 8 beacons */
719 cmd.n_or_snr = cpu_to_le16(8);
720
721 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
722 if (ret == 0) {
723 *nf = CAL_NF(le16_to_cpu(cmd.nf));
724 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
725 }
726
727 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
728 return ret;
729}
730
Dan Williamscc4b9d32010-07-27 12:56:05 -0700731/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700732 * lbs_set_11d_domain_info - Send regulatory and 802.11d domain information
733 * to the firmware
Dan Williamscc4b9d32010-07-27 12:56:05 -0700734 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700735 * @priv: pointer to &struct lbs_private
736 * @request: cfg80211 regulatory request structure
737 * @bands: the device's supported bands and channels
Dan Williamscc4b9d32010-07-27 12:56:05 -0700738 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700739 * returns: 0 on success, error code on failure
Dan Williamscc4b9d32010-07-27 12:56:05 -0700740*/
741int lbs_set_11d_domain_info(struct lbs_private *priv,
742 struct regulatory_request *request,
743 struct ieee80211_supported_band **bands)
744{
745 struct cmd_ds_802_11d_domain_info cmd;
746 struct mrvl_ie_domain_param_set *domain = &cmd.domain;
747 struct ieee80211_country_ie_triplet *t;
748 enum ieee80211_band band;
749 struct ieee80211_channel *ch;
750 u8 num_triplet = 0;
751 u8 num_parsed_chan = 0;
752 u8 first_channel = 0, next_chan = 0, max_pwr = 0;
753 u8 i, flag = 0;
754 size_t triplet_size;
755 int ret;
756
757 lbs_deb_enter(LBS_DEB_11D);
758
759 memset(&cmd, 0, sizeof(cmd));
760 cmd.action = cpu_to_le16(CMD_ACT_SET);
761
762 lbs_deb_11d("Setting country code '%c%c'\n",
763 request->alpha2[0], request->alpha2[1]);
764
765 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
766
767 /* Set country code */
768 domain->country_code[0] = request->alpha2[0];
769 domain->country_code[1] = request->alpha2[1];
770 domain->country_code[2] = ' ';
771
772 /* Now set up the channel triplets; firmware is somewhat picky here
773 * and doesn't validate channel numbers and spans; hence it would
774 * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
775 * the last 3 aren't valid channels, the driver is responsible for
776 * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
777 * etc.
778 */
779 for (band = 0;
780 (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
781 band++) {
782
783 if (!bands[band])
784 continue;
785
786 for (i = 0;
787 (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
788 i++) {
789 ch = &bands[band]->channels[i];
790 if (ch->flags & IEEE80211_CHAN_DISABLED)
791 continue;
792
793 if (!flag) {
794 flag = 1;
795 next_chan = first_channel = (u32) ch->hw_value;
796 max_pwr = ch->max_power;
797 num_parsed_chan = 1;
798 continue;
799 }
800
801 if ((ch->hw_value == next_chan + 1) &&
802 (ch->max_power == max_pwr)) {
803 /* Consolidate adjacent channels */
804 next_chan++;
805 num_parsed_chan++;
806 } else {
807 /* Add this triplet */
808 lbs_deb_11d("11D triplet (%d, %d, %d)\n",
809 first_channel, num_parsed_chan,
810 max_pwr);
811 t = &domain->triplet[num_triplet];
812 t->chans.first_channel = first_channel;
813 t->chans.num_channels = num_parsed_chan;
814 t->chans.max_power = max_pwr;
815 num_triplet++;
816 flag = 0;
817 }
818 }
819
820 if (flag) {
821 /* Add last triplet */
822 lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
823 num_parsed_chan, max_pwr);
824 t = &domain->triplet[num_triplet];
825 t->chans.first_channel = first_channel;
826 t->chans.num_channels = num_parsed_chan;
827 t->chans.max_power = max_pwr;
828 num_triplet++;
829 }
830 }
831
832 lbs_deb_11d("# triplets %d\n", num_triplet);
833
834 /* Set command header sizes */
835 triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
836 domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
837 triplet_size);
838
839 lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
840 (u8 *) &cmd.domain.country_code,
841 le16_to_cpu(domain->header.len));
842
843 cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
844 sizeof(cmd.action) +
845 sizeof(cmd.domain.header) +
846 sizeof(cmd.domain.country_code) +
847 triplet_size);
848
849 ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
850
851 lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
852 return ret;
853}
854
Dan Williams4c7c6e002010-07-27 13:01:07 -0700855/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700856 * lbs_get_reg - Read a MAC, Baseband, or RF register
Dan Williams4c7c6e002010-07-27 13:01:07 -0700857 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700858 * @priv: pointer to &struct lbs_private
859 * @reg: register command, one of CMD_MAC_REG_ACCESS,
860 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
861 * @offset: byte offset of the register to get
862 * @value: on success, the value of the register at 'offset'
Dan Williams4c7c6e002010-07-27 13:01:07 -0700863 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700864 * returns: 0 on success, error code on failure
Dan Williams4c7c6e002010-07-27 13:01:07 -0700865*/
866int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200867{
Dan Williams4c7c6e002010-07-27 13:01:07 -0700868 struct cmd_ds_reg_access cmd;
869 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200870
Holger Schurig9012b282007-05-25 11:27:16 -0400871 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872
Dan Williams4c7c6e002010-07-27 13:01:07 -0700873 BUG_ON(value == NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200874
Dan Williams4c7c6e002010-07-27 13:01:07 -0700875 memset(&cmd, 0, sizeof(cmd));
876 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
877 cmd.action = cpu_to_le16(CMD_ACT_GET);
Olivier Sobrieedcc3602011-07-09 12:57:05 +0200878 cmd.offset = cpu_to_le16(offset);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200879
Dan Williams4c7c6e002010-07-27 13:01:07 -0700880 if (reg != CMD_MAC_REG_ACCESS &&
881 reg != CMD_BBP_REG_ACCESS &&
882 reg != CMD_RF_REG_ACCESS) {
883 ret = -EINVAL;
884 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200885 }
886
Dan Williams4c7c6e002010-07-27 13:01:07 -0700887 ret = lbs_cmd_with_response(priv, reg, &cmd);
Olivier Sobrieedcc3602011-07-09 12:57:05 +0200888 if (!ret) {
Dan Williams4c7c6e002010-07-27 13:01:07 -0700889 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
890 *value = cmd.value.bbp_rf;
891 else if (reg == CMD_MAC_REG_ACCESS)
892 *value = le32_to_cpu(cmd.value.mac);
893 }
894
895out:
896 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
897 return ret;
898}
899
900/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700901 * lbs_set_reg - Write a MAC, Baseband, or RF register
Dan Williams4c7c6e002010-07-27 13:01:07 -0700902 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700903 * @priv: pointer to &struct lbs_private
904 * @reg: register command, one of CMD_MAC_REG_ACCESS,
905 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
906 * @offset: byte offset of the register to set
907 * @value: the value to write to the register at 'offset'
Dan Williams4c7c6e002010-07-27 13:01:07 -0700908 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700909 * returns: 0 on success, error code on failure
Dan Williams4c7c6e002010-07-27 13:01:07 -0700910*/
911int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
912{
913 struct cmd_ds_reg_access cmd;
914 int ret = 0;
915
916 lbs_deb_enter(LBS_DEB_CMD);
917
918 memset(&cmd, 0, sizeof(cmd));
919 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
920 cmd.action = cpu_to_le16(CMD_ACT_SET);
Olivier Sobrieedcc3602011-07-09 12:57:05 +0200921 cmd.offset = cpu_to_le16(offset);
Dan Williams4c7c6e002010-07-27 13:01:07 -0700922
923 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
924 cmd.value.bbp_rf = (u8) (value & 0xFF);
925 else if (reg == CMD_MAC_REG_ACCESS)
926 cmd.value.mac = cpu_to_le32(value);
927 else {
928 ret = -EINVAL;
929 goto out;
930 }
931
932 ret = lbs_cmd_with_response(priv, reg, &cmd);
933
934out:
935 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
936 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200937}
938
David Woodhouse681ffbb2007-12-15 20:04:54 -0500939static void lbs_queue_cmd(struct lbs_private *priv,
940 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200941{
942 unsigned long flags;
David Woodhouse681ffbb2007-12-15 20:04:54 -0500943 int addtail = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944
Holger Schurig8ff12da2007-08-02 11:54:31 -0400945 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946
David Woodhousec4ab4122007-12-15 00:41:51 -0500947 if (!cmdnode) {
948 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949 goto done;
950 }
David Woodhoused9896ee2007-12-15 00:09:25 -0500951 if (!cmdnode->cmdbuf->size) {
952 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
953 goto done;
954 }
David Woodhouseae125bf2007-12-15 04:22:52 -0500955 cmdnode->result = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200956
957 /* Exit_PS command needs to be queued in the header always. */
Dan Williamsddac4522007-12-11 13:49:39 -0500958 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
Dan Williams0bb64082010-07-27 13:08:08 -0700959 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf;
Dan Williamsddac4522007-12-11 13:49:39 -0500960
Dan Williams0bb64082010-07-27 13:08:08 -0700961 if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000962 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963 addtail = 0;
964 }
965 }
966
Dan Williams0bb64082010-07-27 13:08:08 -0700967 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700968 addtail = 0;
969
David Woodhouseaa21c002007-12-08 20:04:36 +0000970 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971
David Woodhouseac472462007-12-08 00:35:00 +0000972 if (addtail)
David Woodhouseaa21c002007-12-08 20:04:36 +0000973 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
David Woodhouseac472462007-12-08 00:35:00 +0000974 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000975 list_add(&cmdnode->list, &priv->cmdpendingq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976
David Woodhouseaa21c002007-12-08 20:04:36 +0000977 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200978
Holger Schurig8ff12da2007-08-02 11:54:31 -0400979 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
David Woodhousec4ab4122007-12-15 00:41:51 -0500980 le16_to_cpu(cmdnode->cmdbuf->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
982done:
Holger Schurig8ff12da2007-08-02 11:54:31 -0400983 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200984}
985
David Woodhouse18c52e72007-12-17 16:03:58 -0500986static void lbs_submit_command(struct lbs_private *priv,
987 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988{
989 unsigned long flags;
Dan Williamsddac4522007-12-11 13:49:39 -0500990 struct cmd_header *cmd;
David Woodhouse18c52e72007-12-17 16:03:58 -0500991 uint16_t cmdsize;
992 uint16_t command;
Holger Schurig57962f02008-05-14 16:30:28 +0200993 int timeo = 3 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500994 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995
Holger Schurig8ff12da2007-08-02 11:54:31 -0400996 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200997
Dan Williamsddac4522007-12-11 13:49:39 -0500998 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200999
David Woodhouseaa21c002007-12-08 20:04:36 +00001000 spin_lock_irqsave(&priv->driver_lock, flags);
Daniel Drake71005be2011-05-26 21:31:08 +01001001 priv->seqnum++;
1002 cmd->seqnum = cpu_to_le16(priv->seqnum);
David Woodhouseaa21c002007-12-08 20:04:36 +00001003 priv->cur_cmd = cmdnode;
David Woodhouseaa21c002007-12-08 20:04:36 +00001004 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005
Dan Williamsddac4522007-12-11 13:49:39 -05001006 cmdsize = le16_to_cpu(cmd->size);
1007 command = le16_to_cpu(cmd->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001008
David Woodhouse18c52e72007-12-17 16:03:58 -05001009 /* These commands take longer */
Dan Williamsbe0d76e2009-05-22 20:05:25 -04001010 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
Holger Schurig57962f02008-05-14 16:30:28 +02001011 timeo = 5 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -05001012
Holger Schurige5225b32008-03-26 10:04:44 +01001013 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1014 command, le16_to_cpu(cmd->seqnum), cmdsize);
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001015 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001016
Dan Williamsddac4522007-12-11 13:49:39 -05001017 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
David Woodhouse18c52e72007-12-17 16:03:58 -05001018
David Woodhoused9896ee2007-12-15 00:09:25 -05001019 if (ret) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001020 netdev_info(priv->dev, "DNLD_CMD: hw_host_to_card failed: %d\n",
1021 ret);
David Woodhouse18c52e72007-12-17 16:03:58 -05001022 /* Let the timer kick in and retry, and potentially reset
1023 the whole thing if the condition persists */
Holger Schurig57962f02008-05-14 16:30:28 +02001024 timeo = HZ/4;
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001025 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026
Amitkumar Karwar49125452009-09-30 20:04:38 -07001027 if (command == CMD_802_11_DEEP_SLEEP) {
1028 if (priv->is_auto_deep_sleep_enabled) {
1029 priv->wakeup_dev_required = 1;
1030 priv->dnld_sent = 0;
1031 }
1032 priv->is_deep_sleep = 1;
1033 lbs_complete_command(priv, cmdnode, 0);
1034 } else {
1035 /* Setup the timer after transmit command */
1036 mod_timer(&priv->command_timer, jiffies + timeo);
1037 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001038
David Woodhouse18c52e72007-12-17 16:03:58 -05001039 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001040}
1041
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001042/*
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001043 * This function inserts command node to cmdfreeq
David Woodhouseaa21c002007-12-08 20:04:36 +00001044 * after cleans it. Requires priv->driver_lock held.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045 */
David Woodhouse183aeac2007-12-15 01:52:54 -05001046static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001047 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001048{
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001049 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001051 if (!cmdnode)
1052 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001053
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001054 cmdnode->callback = NULL;
1055 cmdnode->callback_arg = 0;
1056
1057 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1058
1059 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1060 out:
1061 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001062}
1063
Holger Schurig69f90322007-11-23 15:43:44 +01001064static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1065 struct cmd_ctrl_node *ptempcmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066{
1067 unsigned long flags;
1068
David Woodhouseaa21c002007-12-08 20:04:36 +00001069 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig10078322007-11-15 18:05:47 -05001070 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
David Woodhouseaa21c002007-12-08 20:04:36 +00001071 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001072}
1073
Daniel Drakedf90d842011-07-09 15:41:25 +01001074void __lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1075 int result)
David Woodhouse183aeac2007-12-15 01:52:54 -05001076{
Daniel Drakedf90d842011-07-09 15:41:25 +01001077 /*
1078 * Normally, commands are removed from cmdpendingq before being
1079 * submitted. However, we can arrive here on alternative codepaths
1080 * where the command is still pending. Make sure the command really
1081 * isn't part of a list at this point.
1082 */
1083 list_del_init(&cmd->list);
1084
David Woodhouseae125bf2007-12-15 04:22:52 -05001085 cmd->result = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001086 cmd->cmdwaitqwoken = 1;
Daniel Drakedf90d842011-07-09 15:41:25 +01001087 wake_up(&cmd->cmdwait_q);
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001088
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001089 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
David Woodhousead12d0f2007-12-15 02:06:16 -05001090 __lbs_cleanup_and_insert_cmd(priv, cmd);
David Woodhouse183aeac2007-12-15 01:52:54 -05001091 priv->cur_cmd = NULL;
Daniel Draked2e7b342011-08-01 16:43:13 +01001092 wake_up(&priv->waitq);
Daniel Drakedf90d842011-07-09 15:41:25 +01001093}
1094
1095void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1096 int result)
1097{
1098 unsigned long flags;
1099 spin_lock_irqsave(&priv->driver_lock, flags);
1100 __lbs_complete_command(priv, cmd, result);
1101 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouse183aeac2007-12-15 01:52:54 -05001102}
1103
Dan Williamsd5db2df2008-08-21 17:51:07 -04001104int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001105{
David Woodhousea7c45892007-12-17 22:43:48 -05001106 struct cmd_ds_802_11_radio_control cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -04001107 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001108
Holger Schurig9012b282007-05-25 11:27:16 -04001109 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001110
David Woodhousea7c45892007-12-17 22:43:48 -05001111 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1112 cmd.action = cpu_to_le16(CMD_ACT_SET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001113
Dan Williamsd5db2df2008-08-21 17:51:07 -04001114 /* Only v8 and below support setting the preamble */
1115 if (priv->fwrelease < 0x09000000) {
1116 switch (preamble) {
1117 case RADIO_PREAMBLE_SHORT:
Dan Williamsd5db2df2008-08-21 17:51:07 -04001118 case RADIO_PREAMBLE_AUTO:
1119 case RADIO_PREAMBLE_LONG:
1120 cmd.control = cpu_to_le16(preamble);
1121 break;
1122 default:
1123 goto out;
1124 }
David Woodhousea7c45892007-12-17 22:43:48 -05001125 }
1126
Dan Williamsd5db2df2008-08-21 17:51:07 -04001127 if (radio_on)
1128 cmd.control |= cpu_to_le16(0x1);
1129 else {
1130 cmd.control &= cpu_to_le16(~0x1);
1131 priv->txpower_cur = 0;
1132 }
David Woodhousea7c45892007-12-17 22:43:48 -05001133
Dan Williamsd5db2df2008-08-21 17:51:07 -04001134 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1135 radio_on ? "ON" : "OFF", preamble);
1136
1137 priv->radio_on = radio_on;
David Woodhousea7c45892007-12-17 22:43:48 -05001138
1139 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001140
Dan Williamsd5db2df2008-08-21 17:51:07 -04001141out:
Holger Schurig9012b282007-05-25 11:27:16 -04001142 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001143 return ret;
1144}
1145
Holger Schurigc97329e2008-03-18 11:20:21 +01001146void lbs_set_mac_control(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001147{
Holger Schurig835d3ac2008-03-12 16:05:40 +01001148 struct cmd_ds_mac_control cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149
Holger Schurig9012b282007-05-25 11:27:16 -04001150 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151
Holger Schurig835d3ac2008-03-12 16:05:40 +01001152 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurigd9e97782008-03-12 16:06:43 +01001153 cmd.action = cpu_to_le16(priv->mac_control);
Holger Schurig835d3ac2008-03-12 16:05:40 +01001154 cmd.reserved = 0;
1155
David Woodhouse75bf45a2008-05-20 13:32:45 +01001156 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001157
Holger Schurigc97329e2008-03-18 11:20:21 +01001158 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001159}
1160
1161/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001162 * lbs_allocate_cmd_buffer - allocates the command buffer and links
1163 * it to command free queue
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001165 * @priv: A pointer to &struct lbs_private structure
1166 *
1167 * returns: 0 for success or -1 on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001168 */
Holger Schurig69f90322007-11-23 15:43:44 +01001169int lbs_allocate_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001170{
1171 int ret = 0;
Dan Williamsddac4522007-12-11 13:49:39 -05001172 u32 bufsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001173 u32 i;
Dan Williamsddac4522007-12-11 13:49:39 -05001174 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175
Holger Schurig8ff12da2007-08-02 11:54:31 -04001176 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177
Dan Williamsddac4522007-12-11 13:49:39 -05001178 /* Allocate and initialize the command array */
1179 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1180 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001181 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001182 ret = -1;
1183 goto done;
1184 }
Dan Williamsddac4522007-12-11 13:49:39 -05001185 priv->cmd_array = cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001186
Dan Williamsddac4522007-12-11 13:49:39 -05001187 /* Allocate and initialize each command buffer in the command array */
1188 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1189 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1190 if (!cmdarray[i].cmdbuf) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001191 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192 ret = -1;
1193 goto done;
1194 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001195 }
1196
Dan Williamsddac4522007-12-11 13:49:39 -05001197 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1198 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1199 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001200 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001201 ret = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001202
1203done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001204 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001205 return ret;
1206}
1207
1208/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001209 * lbs_free_cmd_buffer - free the command buffer
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001210 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001211 * @priv: A pointer to &struct lbs_private structure
1212 *
1213 * returns: 0 for success
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001214 */
Holger Schurig69f90322007-11-23 15:43:44 +01001215int lbs_free_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001216{
Dan Williamsddac4522007-12-11 13:49:39 -05001217 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001218 unsigned int i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001219
Holger Schurig8ff12da2007-08-02 11:54:31 -04001220 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001221
1222 /* need to check if cmd array is allocated or not */
David Woodhouseaa21c002007-12-08 20:04:36 +00001223 if (priv->cmd_array == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001224 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001225 goto done;
1226 }
1227
Dan Williamsddac4522007-12-11 13:49:39 -05001228 cmdarray = priv->cmd_array;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001229
1230 /* Release shared memory buffers */
Dan Williamsddac4522007-12-11 13:49:39 -05001231 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1232 if (cmdarray[i].cmdbuf) {
1233 kfree(cmdarray[i].cmdbuf);
1234 cmdarray[i].cmdbuf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001235 }
1236 }
1237
1238 /* Release cmd_ctrl_node */
David Woodhouseaa21c002007-12-08 20:04:36 +00001239 if (priv->cmd_array) {
1240 kfree(priv->cmd_array);
1241 priv->cmd_array = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001242 }
1243
1244done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001245 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001246 return 0;
1247}
1248
1249/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001250 * lbs_get_free_cmd_node - gets a free command node if available in
1251 * command free queue
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001252 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001253 * @priv: A pointer to &struct lbs_private structure
1254 *
1255 * returns: A pointer to &cmd_ctrl_node structure on success
1256 * or %NULL on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001257 */
Dan Williamsd06956b2010-07-27 13:16:24 -07001258static struct cmd_ctrl_node *lbs_get_free_cmd_node(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001259{
1260 struct cmd_ctrl_node *tempnode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001261 unsigned long flags;
1262
Holger Schurig8ff12da2007-08-02 11:54:31 -04001263 lbs_deb_enter(LBS_DEB_HOST);
1264
David Woodhouseaa21c002007-12-08 20:04:36 +00001265 if (!priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266 return NULL;
1267
David Woodhouseaa21c002007-12-08 20:04:36 +00001268 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001269
David Woodhouseaa21c002007-12-08 20:04:36 +00001270 if (!list_empty(&priv->cmdfreeq)) {
1271 tempnode = list_first_entry(&priv->cmdfreeq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001272 struct cmd_ctrl_node, list);
Daniel Drakedf90d842011-07-09 15:41:25 +01001273 list_del_init(&tempnode->list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001274 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001275 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001276 tempnode = NULL;
1277 }
1278
David Woodhouseaa21c002007-12-08 20:04:36 +00001279 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001280
Holger Schurig8ff12da2007-08-02 11:54:31 -04001281 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001282 return tempnode;
1283}
1284
1285/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001286 * lbs_execute_next_command - execute next command in command
1287 * pending queue. Will put firmware back to PS mode if applicable.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001289 * @priv: A pointer to &struct lbs_private structure
1290 *
1291 * returns: 0 on success or -1 on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292 */
Holger Schurig69f90322007-11-23 15:43:44 +01001293int lbs_execute_next_command(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295 struct cmd_ctrl_node *cmdnode = NULL;
Dan Williamsddac4522007-12-11 13:49:39 -05001296 struct cmd_header *cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297 unsigned long flags;
1298 int ret = 0;
1299
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001300 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1301 * only caller to us is lbs_thread() and we get even when a
1302 * data packet is received */
Holger Schurig8ff12da2007-08-02 11:54:31 -04001303 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001304
David Woodhouseaa21c002007-12-08 20:04:36 +00001305 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306
David Woodhouseaa21c002007-12-08 20:04:36 +00001307 if (priv->cur_cmd) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001308 netdev_alert(priv->dev,
1309 "EXEC_NEXT_CMD: already processing command!\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001310 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311 ret = -1;
1312 goto done;
1313 }
1314
David Woodhouseaa21c002007-12-08 20:04:36 +00001315 if (!list_empty(&priv->cmdpendingq)) {
1316 cmdnode = list_first_entry(&priv->cmdpendingq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001317 struct cmd_ctrl_node, list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318 }
1319
David Woodhouseaa21c002007-12-08 20:04:36 +00001320 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001321
1322 if (cmdnode) {
Dan Williamsddac4522007-12-11 13:49:39 -05001323 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001324
Dan Williamsddac4522007-12-11 13:49:39 -05001325 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001326 if ((priv->psstate == PS_STATE_SLEEP) ||
1327 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001328 lbs_deb_host(
1329 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001330 le16_to_cpu(cmd->command),
David Woodhouseaa21c002007-12-08 20:04:36 +00001331 priv->psstate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001332 ret = -1;
1333 goto done;
1334 }
Holger Schurig8ff12da2007-08-02 11:54:31 -04001335 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
Dan Williamsddac4522007-12-11 13:49:39 -05001336 "0x%04x in psstate %d\n",
1337 le16_to_cpu(cmd->command), priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +00001338 } else if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001339 /*
1340 * 1. Non-PS command:
1341 * Queue it. set needtowakeup to TRUE if current state
Dan Williams0bb64082010-07-27 13:08:08 -07001342 * is SLEEP, otherwise call send EXIT_PS.
1343 * 2. PS command but not EXIT_PS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001344 * Ignore it.
Dan Williams0bb64082010-07-27 13:08:08 -07001345 * 3. PS command EXIT_PS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346 * Set needtowakeup to TRUE if current state is SLEEP,
1347 * otherwise send this command down to firmware
1348 * immediately.
1349 */
Dan Williamsddac4522007-12-11 13:49:39 -05001350 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001351 /* Prepare to send Exit PS,
1352 * this non PS command will be sent later */
David Woodhouseaa21c002007-12-08 20:04:36 +00001353 if ((priv->psstate == PS_STATE_SLEEP)
1354 || (priv->psstate == PS_STATE_PRE_SLEEP)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001355 ) {
1356 /* w/ new scheme, it will not reach here.
1357 since it is blocked in main_thread. */
David Woodhouseaa21c002007-12-08 20:04:36 +00001358 priv->needtowakeup = 1;
Dan Williams0bb64082010-07-27 13:08:08 -07001359 } else {
1360 lbs_set_ps_mode(priv,
1361 PS_MODE_ACTION_EXIT_PS,
1362 false);
1363 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001364
1365 ret = 0;
1366 goto done;
1367 } else {
1368 /*
1369 * PS command. Ignore it if it is not Exit_PS.
1370 * otherwise send it down immediately.
1371 */
David Woodhouse38bfab12007-12-16 23:26:54 -05001372 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001373
Holger Schurig8ff12da2007-08-02 11:54:31 -04001374 lbs_deb_host(
1375 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001376 psm->action);
1377 if (psm->action !=
Dan Williams0bb64082010-07-27 13:08:08 -07001378 cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001379 lbs_deb_host(
1380 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
David Woodhouse183aeac2007-12-15 01:52:54 -05001381 lbs_complete_command(priv, cmdnode, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001382
1383 ret = 0;
1384 goto done;
1385 }
1386
David Woodhouseaa21c002007-12-08 20:04:36 +00001387 if ((priv->psstate == PS_STATE_SLEEP) ||
1388 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001389 lbs_deb_host(
1390 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
David Woodhouse183aeac2007-12-15 01:52:54 -05001391 lbs_complete_command(priv, cmdnode, 0);
David Woodhouseaa21c002007-12-08 20:04:36 +00001392 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001393
1394 ret = 0;
1395 goto done;
1396 }
1397
Holger Schurig8ff12da2007-08-02 11:54:31 -04001398 lbs_deb_host(
1399 "EXEC_NEXT_CMD: sending EXIT_PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 }
1401 }
Paul Fox2ae1b8b2011-05-09 10:40:42 +01001402 spin_lock_irqsave(&priv->driver_lock, flags);
Daniel Drakedf90d842011-07-09 15:41:25 +01001403 list_del_init(&cmdnode->list);
Paul Fox2ae1b8b2011-05-09 10:40:42 +01001404 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001405 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001406 le16_to_cpu(cmd->command));
David Woodhoused9896ee2007-12-15 00:09:25 -05001407 lbs_submit_command(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001408 } else {
1409 /*
1410 * check if in power save mode, if yes, put the device back
1411 * to PS mode
1412 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301413#ifdef TODO
1414 /*
1415 * This was the old code for libertas+wext. Someone that
1416 * understands this beast should re-code it in a sane way.
1417 *
1418 * I actually don't understand why this is related to WPA
1419 * and to connection status, shouldn't powering should be
1420 * independ of such things?
1421 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001422 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1423 (priv->psstate == PS_STATE_FULL_POWER) &&
1424 ((priv->connect_status == LBS_CONNECTED) ||
Holger Schurig602114a2009-12-02 15:26:01 +01001425 lbs_mesh_connected(priv))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001426 if (priv->secinfo.WPAenabled ||
1427 priv->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001428 /* check for valid WPA group keys */
David Woodhouseaa21c002007-12-08 20:04:36 +00001429 if (priv->wpa_mcast_key.len ||
1430 priv->wpa_unicast_key.len) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001431 lbs_deb_host(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001432 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1433 " go back to PS_SLEEP");
Dan Williams0bb64082010-07-27 13:08:08 -07001434 lbs_set_ps_mode(priv,
1435 PS_MODE_ACTION_ENTER_PS,
1436 false);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437 }
1438 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001439 lbs_deb_host(
1440 "EXEC_NEXT_CMD: cmdpendingq empty, "
1441 "go back to PS_SLEEP");
Dan Williams0bb64082010-07-27 13:08:08 -07001442 lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
1443 false);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001444 }
1445 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301446#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001447 }
1448
1449 ret = 0;
1450done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001451 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001452 return ret;
1453}
1454
Holger Schurigf539f2e2008-03-26 13:22:11 +01001455static void lbs_send_confirmsleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001456{
1457 unsigned long flags;
Holger Schurigf539f2e2008-03-26 13:22:11 +01001458 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001459
Holger Schurig8ff12da2007-08-02 11:54:31 -04001460 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001461 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1462 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001463
Holger Schurigf539f2e2008-03-26 13:22:11 +01001464 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1465 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001466 if (ret) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001467 netdev_alert(priv->dev, "confirm_sleep failed\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001468 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001469 }
Holger Schurig7919b892008-04-01 14:50:43 +02001470
1471 spin_lock_irqsave(&priv->driver_lock, flags);
1472
Holger Schuriga01f5452008-06-04 11:10:40 +02001473 /* We don't get a response on the sleep-confirmation */
1474 priv->dnld_sent = DNLD_RES_RECEIVED;
1475
Amitkumar Karwar66fceb62010-05-19 03:24:38 -07001476 if (priv->is_host_sleep_configured) {
1477 priv->is_host_sleep_activated = 1;
1478 wake_up_interruptible(&priv->host_sleep_q);
1479 }
1480
Holger Schurig7919b892008-04-01 14:50:43 +02001481 /* If nothing to do, go back to sleep (?) */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001482 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
Holger Schurig7919b892008-04-01 14:50:43 +02001483 priv->psstate = PS_STATE_SLEEP;
1484
1485 spin_unlock_irqrestore(&priv->driver_lock, flags);
1486
1487out:
Holger Schurigf539f2e2008-03-26 13:22:11 +01001488 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001489}
1490
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001491/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001492 * lbs_ps_confirm_sleep - checks condition and prepares to
1493 * send sleep confirm command to firmware if ok
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001494 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001495 * @priv: A pointer to &struct lbs_private structure
1496 *
1497 * returns: n/a
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001498 */
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001499void lbs_ps_confirm_sleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001500{
1501 unsigned long flags =0;
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001502 int allowed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001503
Holger Schurig8ff12da2007-08-02 11:54:31 -04001504 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001505
Holger Schuriga01f5452008-06-04 11:10:40 +02001506 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig634b8f42007-05-25 13:05:16 -04001507 if (priv->dnld_sent) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001508 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001509 lbs_deb_host("dnld_sent was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001510 }
1511
Holger Schurig7919b892008-04-01 14:50:43 +02001512 /* In-progress command? */
David Woodhouseaa21c002007-12-08 20:04:36 +00001513 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001514 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001515 lbs_deb_host("cur_cmd was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001516 }
Holger Schurig7919b892008-04-01 14:50:43 +02001517
1518 /* Pending events or command responses? */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001519 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001520 allowed = 0;
Holger Schurig7919b892008-04-01 14:50:43 +02001521 lbs_deb_host("pending events or command responses\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001522 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001523 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001524
1525 if (allowed) {
Holger Schurig10078322007-11-15 18:05:47 -05001526 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
Holger Schurigf539f2e2008-03-26 13:22:11 +01001527 lbs_send_confirmsleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001528 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001529 lbs_deb_host("sleep confirm has been delayed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001530 }
1531
Holger Schurig8ff12da2007-08-02 11:54:31 -04001532 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001533}
Holger Schurig675787e2007-12-05 17:58:11 +01001534
1535
Anna Neal0112c9e2008-09-11 11:17:25 -07001536/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001537 * lbs_set_tpc_cfg - Configures the transmission power control functionality
Anna Neal0112c9e2008-09-11 11:17:25 -07001538 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001539 * @priv: A pointer to &struct lbs_private structure
1540 * @enable: Transmission power control enable
1541 * @p0: Power level when link quality is good (dBm).
1542 * @p1: Power level when link quality is fair (dBm).
1543 * @p2: Power level when link quality is poor (dBm).
1544 * @usesnr: Use Signal to Noise Ratio in TPC
Anna Neal0112c9e2008-09-11 11:17:25 -07001545 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001546 * returns: 0 on success
Anna Neal0112c9e2008-09-11 11:17:25 -07001547 */
1548int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1549 int8_t p2, int usesnr)
1550{
1551 struct cmd_ds_802_11_tpc_cfg cmd;
1552 int ret;
1553
1554 memset(&cmd, 0, sizeof(cmd));
1555 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1556 cmd.action = cpu_to_le16(CMD_ACT_SET);
1557 cmd.enable = !!enable;
Anna Neal3ed6e082008-09-26 11:34:35 -04001558 cmd.usesnr = !!usesnr;
Anna Neal0112c9e2008-09-11 11:17:25 -07001559 cmd.P0 = p0;
1560 cmd.P1 = p1;
1561 cmd.P2 = p2;
1562
1563 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1564
1565 return ret;
1566}
1567
1568/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001569 * lbs_set_power_adapt_cfg - Configures the power adaptation settings
Anna Neal0112c9e2008-09-11 11:17:25 -07001570 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001571 * @priv: A pointer to &struct lbs_private structure
1572 * @enable: Power adaptation enable
1573 * @p0: Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1574 * @p1: Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1575 * @p2: Power level for 48 and 54 Mbps (dBm).
Anna Neal0112c9e2008-09-11 11:17:25 -07001576 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001577 * returns: 0 on Success
Anna Neal0112c9e2008-09-11 11:17:25 -07001578 */
1579
1580int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1581 int8_t p1, int8_t p2)
1582{
1583 struct cmd_ds_802_11_pa_cfg cmd;
1584 int ret;
1585
1586 memset(&cmd, 0, sizeof(cmd));
1587 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1588 cmd.action = cpu_to_le16(CMD_ACT_SET);
1589 cmd.enable = !!enable;
1590 cmd.P0 = p0;
1591 cmd.P1 = p1;
1592 cmd.P2 = p2;
1593
1594 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1595
1596 return ret;
1597}
1598
1599
Holger Schurig6d898b12009-10-14 16:49:53 +02001600struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001601 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1602 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1603 unsigned long callback_arg)
Holger Schurig675787e2007-12-05 17:58:11 +01001604{
Holger Schurig675787e2007-12-05 17:58:11 +01001605 struct cmd_ctrl_node *cmdnode;
Holger Schurig675787e2007-12-05 17:58:11 +01001606
1607 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurig675787e2007-12-05 17:58:11 +01001608
David Woodhouseaa21c002007-12-08 20:04:36 +00001609 if (priv->surpriseremoved) {
Holger Schurig675787e2007-12-05 17:58:11 +01001610 lbs_deb_host("PREP_CMD: card removed\n");
David Woodhouse3399ea52007-12-15 03:09:33 -05001611 cmdnode = ERR_PTR(-ENOENT);
Holger Schurig675787e2007-12-05 17:58:11 +01001612 goto done;
1613 }
1614
Dan Williams77ccdcf2010-07-27 13:15:45 -07001615 /* No commands are allowed in Deep Sleep until we toggle the GPIO
1616 * to wake up the card and it has signaled that it's ready.
1617 */
1618 if (!priv->is_auto_deep_sleep_enabled) {
1619 if (priv->is_deep_sleep) {
1620 lbs_deb_cmd("command not allowed in deep sleep\n");
1621 cmdnode = ERR_PTR(-EBUSY);
1622 goto done;
1623 }
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001624 }
1625
Dan Williamsd06956b2010-07-27 13:16:24 -07001626 cmdnode = lbs_get_free_cmd_node(priv);
Holger Schurig675787e2007-12-05 17:58:11 +01001627 if (cmdnode == NULL) {
1628 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1629
1630 /* Wake up main thread to execute next command */
Daniel Draked2e7b342011-08-01 16:43:13 +01001631 wake_up(&priv->waitq);
David Woodhouse3399ea52007-12-15 03:09:33 -05001632 cmdnode = ERR_PTR(-ENOBUFS);
Holger Schurig675787e2007-12-05 17:58:11 +01001633 goto done;
1634 }
1635
David Woodhouse448a51a2007-12-08 00:59:54 +00001636 cmdnode->callback = callback;
David Woodhouse1309b552007-12-10 13:36:10 -05001637 cmdnode->callback_arg = callback_arg;
Holger Schurig675787e2007-12-05 17:58:11 +01001638
Dan Williams7ad994d2007-12-11 12:33:30 -05001639 /* Copy the incoming command to the buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001640 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
Dan Williams7ad994d2007-12-11 12:33:30 -05001641
Daniel Drake71005be2011-05-26 21:31:08 +01001642 /* Set command, clean result, move to buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001643 cmdnode->cmdbuf->command = cpu_to_le16(command);
1644 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
Dan Williamsddac4522007-12-11 13:49:39 -05001645 cmdnode->cmdbuf->result = 0;
Holger Schurig675787e2007-12-05 17:58:11 +01001646
1647 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1648
Holger Schurig675787e2007-12-05 17:58:11 +01001649 cmdnode->cmdwaitqwoken = 0;
David Woodhouse681ffbb2007-12-15 20:04:54 -05001650 lbs_queue_cmd(priv, cmdnode);
Daniel Draked2e7b342011-08-01 16:43:13 +01001651 wake_up(&priv->waitq);
Holger Schurig675787e2007-12-05 17:58:11 +01001652
David Woodhouse3399ea52007-12-15 03:09:33 -05001653 done:
1654 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1655 return cmdnode;
1656}
1657
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001658void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1659 struct cmd_header *in_cmd, int in_cmd_size)
1660{
1661 lbs_deb_enter(LBS_DEB_CMD);
1662 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1663 lbs_cmd_async_callback, 0);
1664 lbs_deb_leave(LBS_DEB_CMD);
1665}
1666
David Woodhouse3399ea52007-12-15 03:09:33 -05001667int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1668 struct cmd_header *in_cmd, int in_cmd_size,
1669 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1670 unsigned long callback_arg)
1671{
1672 struct cmd_ctrl_node *cmdnode;
1673 unsigned long flags;
1674 int ret = 0;
1675
1676 lbs_deb_enter(LBS_DEB_HOST);
1677
1678 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1679 callback, callback_arg);
1680 if (IS_ERR(cmdnode)) {
1681 ret = PTR_ERR(cmdnode);
1682 goto done;
1683 }
1684
Holger Schurig675787e2007-12-05 17:58:11 +01001685 might_sleep();
Daniel Drakedf90d842011-07-09 15:41:25 +01001686
1687 /*
1688 * Be careful with signals here. A signal may be received as the system
1689 * goes into suspend or resume. We do not want this to interrupt the
1690 * command, so we perform an uninterruptible sleep.
1691 */
1692 wait_event(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
Holger Schurig675787e2007-12-05 17:58:11 +01001693
David Woodhouseaa21c002007-12-08 20:04:36 +00001694 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseae125bf2007-12-15 04:22:52 -05001695 ret = cmdnode->result;
1696 if (ret)
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001697 netdev_info(priv->dev, "PREP_CMD: command 0x%04x failed: %d\n",
1698 command, ret);
David Woodhouse3399ea52007-12-15 03:09:33 -05001699
David Woodhousead12d0f2007-12-15 02:06:16 -05001700 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
David Woodhouseaa21c002007-12-08 20:04:36 +00001701 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig675787e2007-12-05 17:58:11 +01001702
1703done:
1704 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1705 return ret;
1706}
Dan Williams14e865b2007-12-10 15:11:23 -05001707EXPORT_SYMBOL_GPL(__lbs_cmd);