blob: a515a8fd02ac30570e581857bf61d4e7c9e43ef5 [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 */
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700113 netdev_info(priv->dev, "%pM, fw %u.%u.%up%u, cap 0x%08x\n",
Johannes Berge1749612008-10-27 15:59:26 -0700114 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;
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700144 netdev_info(priv->dev,
145 "unidentified region code; using the default (USA)\n");
Dan Williams6e66f032007-12-11 12:42:16 -0500146 }
147
148 if (priv->current_addr[0] == 0xff)
149 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
150
Vasily Khoruzhick75abde42011-01-21 22:44:49 +0200151 if (!priv->copied_hwaddr) {
152 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
153 if (priv->mesh_dev)
154 memcpy(priv->mesh_dev->dev_addr,
155 priv->current_addr, ETH_ALEN);
156 priv->copied_hwaddr = 1;
157 }
Dan Williams6e66f032007-12-11 12:42:16 -0500158
Dan Williams6e66f032007-12-11 12:42:16 -0500159out:
Holger Schurig9012b282007-05-25 11:27:16 -0400160 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams6e66f032007-12-11 12:42:16 -0500161 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162}
163
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700164static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
165 struct cmd_header *resp)
166{
167 lbs_deb_enter(LBS_DEB_CMD);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530168 if (priv->is_host_sleep_activated) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700169 priv->is_host_sleep_configured = 0;
170 if (priv->psstate == PS_STATE_FULL_POWER) {
171 priv->is_host_sleep_activated = 0;
172 wake_up_interruptible(&priv->host_sleep_q);
173 }
174 } else {
175 priv->is_host_sleep_configured = 1;
176 }
177 lbs_deb_leave(LBS_DEB_CMD);
178 return 0;
179}
180
Anna Neal582c1b52008-10-20 16:46:56 -0700181int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
182 struct wol_config *p_wol_config)
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500183{
184 struct cmd_ds_host_sleep cmd_config;
185 int ret;
186
Deepak Saxenaae63a332010-10-31 13:40:33 +0000187 /*
188 * Certain firmware versions do not support EHS_REMOVE_WAKEUP command
189 * and the card will return a failure. Since we need to be
190 * able to reset the mask, in those cases we set a 0 mask instead.
191 */
192 if (criteria == EHS_REMOVE_WAKEUP && !priv->ehs_remove_supported)
193 criteria = 0;
194
David Woodhouse9fae8992007-12-15 03:46:44 -0500195 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500196 cmd_config.criteria = cpu_to_le32(criteria);
David Woodhouse506e9022007-12-12 20:06:06 -0500197 cmd_config.gpio = priv->wol_gpio;
198 cmd_config.gap = priv->wol_gap;
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500199
Anna Neal582c1b52008-10-20 16:46:56 -0700200 if (p_wol_config != NULL)
201 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
202 sizeof(struct wol_config));
203 else
204 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
205
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700206 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
207 le16_to_cpu(cmd_config.hdr.size),
208 lbs_ret_host_sleep_cfg, 0);
David Woodhouse506e9022007-12-12 20:06:06 -0500209 if (!ret) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700210 if (p_wol_config)
Anna Neal582c1b52008-10-20 16:46:56 -0700211 memcpy((uint8_t *) p_wol_config,
212 (uint8_t *)&cmd_config.wol_conf,
213 sizeof(struct wol_config));
David Woodhouse506e9022007-12-12 20:06:06 -0500214 } else {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700215 netdev_info(priv->dev, "HOST_SLEEP_CFG failed %d\n", ret);
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500216 }
David Woodhouse506e9022007-12-12 20:06:06 -0500217
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500218 return ret;
219}
220EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
221
Dan Williams0bb64082010-07-27 13:08:08 -0700222/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700223 * lbs_set_ps_mode - Sets the Power Save mode
Dan Williams0bb64082010-07-27 13:08:08 -0700224 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700225 * @priv: A pointer to &struct lbs_private structure
226 * @cmd_action: The Power Save operation (PS_MODE_ACTION_ENTER_PS or
Dan Williams0bb64082010-07-27 13:08:08 -0700227 * PS_MODE_ACTION_EXIT_PS)
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700228 * @block: Whether to block on a response or not
Dan Williams0bb64082010-07-27 13:08:08 -0700229 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700230 * returns: 0 on success, error on failure
Dan Williams0bb64082010-07-27 13:08:08 -0700231 */
232int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200233{
Dan Williams0bb64082010-07-27 13:08:08 -0700234 struct cmd_ds_802_11_ps_mode cmd;
235 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236
Holger Schurig9012b282007-05-25 11:27:16 -0400237 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200238
Dan Williams0bb64082010-07-27 13:08:08 -0700239 memset(&cmd, 0, sizeof(cmd));
240 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
241 cmd.action = cpu_to_le16(cmd_action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200242
Dan Williams0bb64082010-07-27 13:08:08 -0700243 if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
244 lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
245 cmd.multipledtim = cpu_to_le16(1); /* Default DTIM multiple */
246 } else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
247 lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
248 } else {
249 /* We don't handle CONFIRM_SLEEP here because it needs to
250 * be fastpathed to the firmware.
251 */
252 lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
253 ret = -EOPNOTSUPP;
254 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255 }
256
Dan Williams0bb64082010-07-27 13:08:08 -0700257 if (block)
258 ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
259 else
260 lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
261
262out:
263 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
264 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265}
266
David Woodhouse3fbe1042007-12-17 23:48:31 -0500267int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
268 struct sleep_params *sp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200269{
David Woodhouse3fbe1042007-12-17 23:48:31 -0500270 struct cmd_ds_802_11_sleep_params cmd;
271 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200272
Holger Schurig9012b282007-05-25 11:27:16 -0400273 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274
Dan Williams0aef64d2007-08-02 11:31:18 -0400275 if (cmd_action == CMD_ACT_GET) {
David Woodhouse3fbe1042007-12-17 23:48:31 -0500276 memset(&cmd, 0, sizeof(cmd));
277 } else {
278 cmd.error = cpu_to_le16(sp->sp_error);
279 cmd.offset = cpu_to_le16(sp->sp_offset);
280 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
281 cmd.calcontrol = sp->sp_calcontrol;
282 cmd.externalsleepclk = sp->sp_extsleepclk;
283 cmd.reserved = cpu_to_le16(sp->sp_reserved);
284 }
285 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
286 cmd.action = cpu_to_le16(cmd_action);
287
288 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
289
290 if (!ret) {
291 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
292 "calcontrol 0x%x extsleepclk 0x%x\n",
293 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
294 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
295 cmd.externalsleepclk);
296
297 sp->sp_error = le16_to_cpu(cmd.error);
298 sp->sp_offset = le16_to_cpu(cmd.offset);
299 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
300 sp->sp_calcontrol = cmd.calcontrol;
301 sp->sp_extsleepclk = cmd.externalsleepclk;
302 sp->sp_reserved = le16_to_cpu(cmd.reserved);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303 }
304
David Woodhouse3fbe1042007-12-17 23:48:31 -0500305 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306 return 0;
307}
308
Amitkumar Karwar49125452009-09-30 20:04:38 -0700309static int lbs_wait_for_ds_awake(struct lbs_private *priv)
310{
311 int ret = 0;
312
313 lbs_deb_enter(LBS_DEB_CMD);
314
315 if (priv->is_deep_sleep) {
316 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
317 !priv->is_deep_sleep, (10 * HZ))) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700318 netdev_err(priv->dev, "ds_awake_q: timer expired\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -0700319 ret = -1;
320 }
321 }
322
323 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
324 return ret;
325}
326
327int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
328{
329 int ret = 0;
330
331 lbs_deb_enter(LBS_DEB_CMD);
332
333 if (deep_sleep) {
334 if (priv->is_deep_sleep != 1) {
335 lbs_deb_cmd("deep sleep: sleep\n");
336 BUG_ON(!priv->enter_deep_sleep);
337 ret = priv->enter_deep_sleep(priv);
338 if (!ret) {
339 netif_stop_queue(priv->dev);
340 netif_carrier_off(priv->dev);
341 }
342 } else {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700343 netdev_err(priv->dev, "deep sleep: already enabled\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -0700344 }
345 } else {
346 if (priv->is_deep_sleep) {
347 lbs_deb_cmd("deep sleep: wakeup\n");
348 BUG_ON(!priv->exit_deep_sleep);
349 ret = priv->exit_deep_sleep(priv);
350 if (!ret) {
351 ret = lbs_wait_for_ds_awake(priv);
352 if (ret)
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700353 netdev_err(priv->dev,
354 "deep sleep: wakeup failed\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -0700355 }
356 }
357 }
358
359 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
360 return ret;
361}
362
Amitkumar Karwar13118432010-07-08 06:43:48 +0530363static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
364 unsigned long dummy,
365 struct cmd_header *cmd)
366{
367 lbs_deb_enter(LBS_DEB_FW);
368 priv->is_host_sleep_activated = 1;
369 wake_up_interruptible(&priv->host_sleep_q);
370 lbs_deb_leave(LBS_DEB_FW);
371 return 0;
372}
373
374int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
375{
376 struct cmd_header cmd;
377 int ret = 0;
378 uint32_t criteria = EHS_REMOVE_WAKEUP;
379
380 lbs_deb_enter(LBS_DEB_CMD);
381
382 if (host_sleep) {
383 if (priv->is_host_sleep_activated != 1) {
384 memset(&cmd, 0, sizeof(cmd));
385 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
386 (struct wol_config *)NULL);
387 if (ret) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700388 netdev_info(priv->dev,
389 "Host sleep configuration failed: %d\n",
390 ret);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530391 return ret;
392 }
393 if (priv->psstate == PS_STATE_FULL_POWER) {
394 ret = __lbs_cmd(priv,
395 CMD_802_11_HOST_SLEEP_ACTIVATE,
396 &cmd,
397 sizeof(cmd),
398 lbs_ret_host_sleep_activate, 0);
399 if (ret)
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700400 netdev_info(priv->dev,
401 "HOST_SLEEP_ACTIVATE failed: %d\n",
402 ret);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530403 }
404
405 if (!wait_event_interruptible_timeout(
406 priv->host_sleep_q,
407 priv->is_host_sleep_activated,
408 (10 * HZ))) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700409 netdev_err(priv->dev,
410 "host_sleep_q: timer expired\n");
Amitkumar Karwar13118432010-07-08 06:43:48 +0530411 ret = -1;
412 }
413 } else {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700414 netdev_err(priv->dev, "host sleep: already enabled\n");
Amitkumar Karwar13118432010-07-08 06:43:48 +0530415 }
416 } else {
417 if (priv->is_host_sleep_activated)
418 ret = lbs_host_sleep_cfg(priv, criteria,
419 (struct wol_config *)NULL);
420 }
421
422 return ret;
423}
424
Dan Williams39fcf7a2008-09-10 12:49:00 -0400425/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700426 * lbs_set_snmp_mib - Set an SNMP MIB value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400427 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700428 * @priv: A pointer to &struct lbs_private structure
429 * @oid: The OID to set in the firmware
430 * @val: Value to set the OID to
Dan Williams39fcf7a2008-09-10 12:49:00 -0400431 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700432 * returns: 0 on success, error on failure
Dan Williams39fcf7a2008-09-10 12:49:00 -0400433 */
434int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200435{
Dan Williams39fcf7a2008-09-10 12:49:00 -0400436 struct cmd_ds_802_11_snmp_mib cmd;
437 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438
Holger Schurig9012b282007-05-25 11:27:16 -0400439 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200440
Dan Williams39fcf7a2008-09-10 12:49:00 -0400441 memset(&cmd, 0, sizeof (cmd));
442 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
443 cmd.action = cpu_to_le16(CMD_ACT_SET);
444 cmd.oid = cpu_to_le16((u16) oid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200445
Dan Williams39fcf7a2008-09-10 12:49:00 -0400446 switch (oid) {
447 case SNMP_MIB_OID_BSS_TYPE:
448 cmd.bufsize = cpu_to_le16(sizeof(u8));
Holger Schurigfef06402009-10-22 15:30:59 +0200449 cmd.value[0] = val;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200450 break;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400451 case SNMP_MIB_OID_11D_ENABLE:
452 case SNMP_MIB_OID_FRAG_THRESHOLD:
453 case SNMP_MIB_OID_RTS_THRESHOLD:
454 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
455 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
456 cmd.bufsize = cpu_to_le16(sizeof(u16));
457 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200458 break;
459 default:
Dan Williams39fcf7a2008-09-10 12:49:00 -0400460 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
461 ret = -EINVAL;
462 goto out;
463 }
464
465 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
466 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
467
468 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
469
470out:
471 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
472 return ret;
473}
474
475/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700476 * lbs_get_snmp_mib - Get an SNMP MIB value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400477 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700478 * @priv: A pointer to &struct lbs_private structure
479 * @oid: The OID to retrieve from the firmware
480 * @out_val: Location for the returned value
Dan Williams39fcf7a2008-09-10 12:49:00 -0400481 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700482 * returns: 0 on success, error on failure
Dan Williams39fcf7a2008-09-10 12:49:00 -0400483 */
484int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
485{
486 struct cmd_ds_802_11_snmp_mib cmd;
487 int ret;
488
489 lbs_deb_enter(LBS_DEB_CMD);
490
491 memset(&cmd, 0, sizeof (cmd));
492 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
493 cmd.action = cpu_to_le16(CMD_ACT_GET);
494 cmd.oid = cpu_to_le16(oid);
495
496 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
497 if (ret)
498 goto out;
499
500 switch (le16_to_cpu(cmd.bufsize)) {
501 case sizeof(u8):
Holger Schurigfef06402009-10-22 15:30:59 +0200502 *out_val = cmd.value[0];
Dan Williams39fcf7a2008-09-10 12:49:00 -0400503 break;
504 case sizeof(u16):
505 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
506 break;
507 default:
508 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
509 oid, le16_to_cpu(cmd.bufsize));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 break;
511 }
512
Dan Williams39fcf7a2008-09-10 12:49:00 -0400513out:
514 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
515 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516}
517
Dan Williams87c8c722008-08-19 15:15:35 -0400518/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700519 * lbs_get_tx_power - Get the min, max, and current TX power
Dan Williams87c8c722008-08-19 15:15:35 -0400520 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700521 * @priv: A pointer to &struct lbs_private structure
522 * @curlevel: Current power level in dBm
523 * @minlevel: Minimum supported power level in dBm (optional)
524 * @maxlevel: Maximum supported power level in dBm (optional)
Dan Williams87c8c722008-08-19 15:15:35 -0400525 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700526 * returns: 0 on success, error on failure
Dan Williams87c8c722008-08-19 15:15:35 -0400527 */
528int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
529 s16 *maxlevel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530{
Dan Williams87c8c722008-08-19 15:15:35 -0400531 struct cmd_ds_802_11_rf_tx_power cmd;
532 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533
Holger Schurig9012b282007-05-25 11:27:16 -0400534 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535
Dan Williams87c8c722008-08-19 15:15:35 -0400536 memset(&cmd, 0, sizeof(cmd));
537 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
538 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539
Dan Williams87c8c722008-08-19 15:15:35 -0400540 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
541 if (ret == 0) {
542 *curlevel = le16_to_cpu(cmd.curlevel);
543 if (minlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100544 *minlevel = cmd.minlevel;
Dan Williams87c8c722008-08-19 15:15:35 -0400545 if (maxlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100546 *maxlevel = cmd.maxlevel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547 }
Holger Schurig9012b282007-05-25 11:27:16 -0400548
549 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams87c8c722008-08-19 15:15:35 -0400550 return ret;
551}
552
553/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700554 * lbs_set_tx_power - Set the TX power
Dan Williams87c8c722008-08-19 15:15:35 -0400555 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700556 * @priv: A pointer to &struct lbs_private structure
557 * @dbm: The desired power level in dBm
Dan Williams87c8c722008-08-19 15:15:35 -0400558 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700559 * returns: 0 on success, error on failure
Dan Williams87c8c722008-08-19 15:15:35 -0400560 */
561int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
562{
563 struct cmd_ds_802_11_rf_tx_power cmd;
564 int ret;
565
566 lbs_deb_enter(LBS_DEB_CMD);
567
568 memset(&cmd, 0, sizeof(cmd));
569 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
570 cmd.action = cpu_to_le16(CMD_ACT_SET);
571 cmd.curlevel = cpu_to_le16(dbm);
572
573 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
574
575 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
576
577 lbs_deb_leave(LBS_DEB_CMD);
578 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200579}
580
Dan Williamsa45b6f42010-07-27 12:54:34 -0700581/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700582 * lbs_set_monitor_mode - Enable or disable monitor mode
583 * (only implemented on OLPC usb8388 FW)
Dan Williamsa45b6f42010-07-27 12:54:34 -0700584 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700585 * @priv: A pointer to &struct lbs_private structure
586 * @enable: 1 to enable monitor mode, 0 to disable
Dan Williamsa45b6f42010-07-27 12:54:34 -0700587 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700588 * returns: 0 on success, error on failure
Dan Williamsa45b6f42010-07-27 12:54:34 -0700589 */
590int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400591{
Dan Williamsa45b6f42010-07-27 12:54:34 -0700592 struct cmd_ds_802_11_monitor_mode cmd;
593 int ret;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400594
Dan Williamsa45b6f42010-07-27 12:54:34 -0700595 memset(&cmd, 0, sizeof(cmd));
596 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
597 cmd.action = cpu_to_le16(CMD_ACT_SET);
598 if (enable)
599 cmd.mode = cpu_to_le16(0x1);
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400600
Dan Williamsa45b6f42010-07-27 12:54:34 -0700601 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
602
603 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
604 if (ret == 0) {
605 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
606 ARPHRD_ETHER;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400607 }
608
Dan Williamsa45b6f42010-07-27 12:54:34 -0700609 lbs_deb_leave(LBS_DEB_CMD);
610 return ret;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400611}
612
Dan Williams2dd4b262007-12-11 16:54:15 -0500613/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700614 * lbs_get_channel - Get the radio channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500615 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700616 * @priv: A pointer to &struct lbs_private structure
Dan Williams2dd4b262007-12-11 16:54:15 -0500617 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700618 * returns: The channel on success, error on failure
Dan Williams2dd4b262007-12-11 16:54:15 -0500619 */
Holger Schuriga3cbfb02009-10-16 17:33:56 +0200620static int lbs_get_channel(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621{
Dan Williams2dd4b262007-12-11 16:54:15 -0500622 struct cmd_ds_802_11_rf_channel cmd;
623 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624
Holger Schurig8ff12da2007-08-02 11:54:31 -0400625 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200627 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500628 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
629 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630
David Woodhouse689442d2007-12-12 16:00:42 -0500631 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500632 if (ret)
633 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634
Dan Williamscb182a62007-12-11 17:35:51 -0500635 ret = le16_to_cpu(cmd.channel);
636 lbs_deb_cmd("current radio channel is %d\n", ret);
Dan Williams2dd4b262007-12-11 16:54:15 -0500637
638out:
639 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
640 return ret;
641}
642
Holger Schurig73ab1f22008-04-02 16:52:19 +0200643int lbs_update_channel(struct lbs_private *priv)
644{
645 int ret;
646
647 /* the channel in f/w could be out of sync; get the current channel */
648 lbs_deb_enter(LBS_DEB_ASSOC);
649
650 ret = lbs_get_channel(priv);
651 if (ret > 0) {
Holger Schurigc14951f2009-10-22 15:30:50 +0200652 priv->channel = ret;
Holger Schurig73ab1f22008-04-02 16:52:19 +0200653 ret = 0;
654 }
655 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
656 return ret;
657}
658
Dan Williams2dd4b262007-12-11 16:54:15 -0500659/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700660 * lbs_set_channel - Set the radio channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500661 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700662 * @priv: A pointer to &struct lbs_private structure
663 * @channel: The desired channel, or 0 to clear a locked channel
Dan Williams2dd4b262007-12-11 16:54:15 -0500664 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700665 * returns: 0 on success, error on failure
Dan Williams2dd4b262007-12-11 16:54:15 -0500666 */
667int lbs_set_channel(struct lbs_private *priv, u8 channel)
668{
669 struct cmd_ds_802_11_rf_channel cmd;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530670#ifdef DEBUG
Holger Schurigc14951f2009-10-22 15:30:50 +0200671 u8 old_channel = priv->channel;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530672#endif
Dan Williams2dd4b262007-12-11 16:54:15 -0500673 int ret = 0;
674
675 lbs_deb_enter(LBS_DEB_CMD);
676
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200677 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500678 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
679 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
680 cmd.channel = cpu_to_le16(channel);
681
David Woodhouse689442d2007-12-12 16:00:42 -0500682 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500683 if (ret)
684 goto out;
685
Holger Schurigc14951f2009-10-22 15:30:50 +0200686 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
Dan Williamscb182a62007-12-11 17:35:51 -0500687 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
Holger Schurigc14951f2009-10-22 15:30:50 +0200688 priv->channel);
Dan Williams2dd4b262007-12-11 16:54:15 -0500689
690out:
691 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
692 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693}
694
Dan Williams9fb76632010-07-27 12:55:21 -0700695/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700696 * lbs_get_rssi - Get current RSSI and noise floor
Dan Williams9fb76632010-07-27 12:55:21 -0700697 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700698 * @priv: A pointer to &struct lbs_private structure
699 * @rssi: On successful return, signal level in mBm
700 * @nf: On successful return, Noise floor
Dan Williams9fb76632010-07-27 12:55:21 -0700701 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700702 * returns: The channel on success, error on failure
Dan Williams9fb76632010-07-27 12:55:21 -0700703 */
704int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
705{
706 struct cmd_ds_802_11_rssi cmd;
707 int ret = 0;
708
709 lbs_deb_enter(LBS_DEB_CMD);
710
711 BUG_ON(rssi == NULL);
712 BUG_ON(nf == NULL);
713
714 memset(&cmd, 0, sizeof(cmd));
715 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
716 /* Average SNR over last 8 beacons */
717 cmd.n_or_snr = cpu_to_le16(8);
718
719 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
720 if (ret == 0) {
721 *nf = CAL_NF(le16_to_cpu(cmd.nf));
722 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
723 }
724
725 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
726 return ret;
727}
728
Dan Williamscc4b9d32010-07-27 12:56:05 -0700729/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700730 * lbs_set_11d_domain_info - Send regulatory and 802.11d domain information
731 * to the firmware
Dan Williamscc4b9d32010-07-27 12:56:05 -0700732 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700733 * @priv: pointer to &struct lbs_private
734 * @request: cfg80211 regulatory request structure
735 * @bands: the device's supported bands and channels
Dan Williamscc4b9d32010-07-27 12:56:05 -0700736 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700737 * returns: 0 on success, error code on failure
Dan Williamscc4b9d32010-07-27 12:56:05 -0700738*/
739int lbs_set_11d_domain_info(struct lbs_private *priv,
740 struct regulatory_request *request,
741 struct ieee80211_supported_band **bands)
742{
743 struct cmd_ds_802_11d_domain_info cmd;
744 struct mrvl_ie_domain_param_set *domain = &cmd.domain;
745 struct ieee80211_country_ie_triplet *t;
746 enum ieee80211_band band;
747 struct ieee80211_channel *ch;
748 u8 num_triplet = 0;
749 u8 num_parsed_chan = 0;
750 u8 first_channel = 0, next_chan = 0, max_pwr = 0;
751 u8 i, flag = 0;
752 size_t triplet_size;
753 int ret;
754
755 lbs_deb_enter(LBS_DEB_11D);
756
757 memset(&cmd, 0, sizeof(cmd));
758 cmd.action = cpu_to_le16(CMD_ACT_SET);
759
760 lbs_deb_11d("Setting country code '%c%c'\n",
761 request->alpha2[0], request->alpha2[1]);
762
763 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
764
765 /* Set country code */
766 domain->country_code[0] = request->alpha2[0];
767 domain->country_code[1] = request->alpha2[1];
768 domain->country_code[2] = ' ';
769
770 /* Now set up the channel triplets; firmware is somewhat picky here
771 * and doesn't validate channel numbers and spans; hence it would
772 * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
773 * the last 3 aren't valid channels, the driver is responsible for
774 * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
775 * etc.
776 */
777 for (band = 0;
778 (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
779 band++) {
780
781 if (!bands[band])
782 continue;
783
784 for (i = 0;
785 (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
786 i++) {
787 ch = &bands[band]->channels[i];
788 if (ch->flags & IEEE80211_CHAN_DISABLED)
789 continue;
790
791 if (!flag) {
792 flag = 1;
793 next_chan = first_channel = (u32) ch->hw_value;
794 max_pwr = ch->max_power;
795 num_parsed_chan = 1;
796 continue;
797 }
798
799 if ((ch->hw_value == next_chan + 1) &&
800 (ch->max_power == max_pwr)) {
801 /* Consolidate adjacent channels */
802 next_chan++;
803 num_parsed_chan++;
804 } else {
805 /* Add this triplet */
806 lbs_deb_11d("11D triplet (%d, %d, %d)\n",
807 first_channel, num_parsed_chan,
808 max_pwr);
809 t = &domain->triplet[num_triplet];
810 t->chans.first_channel = first_channel;
811 t->chans.num_channels = num_parsed_chan;
812 t->chans.max_power = max_pwr;
813 num_triplet++;
814 flag = 0;
815 }
816 }
817
818 if (flag) {
819 /* Add last triplet */
820 lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
821 num_parsed_chan, max_pwr);
822 t = &domain->triplet[num_triplet];
823 t->chans.first_channel = first_channel;
824 t->chans.num_channels = num_parsed_chan;
825 t->chans.max_power = max_pwr;
826 num_triplet++;
827 }
828 }
829
830 lbs_deb_11d("# triplets %d\n", num_triplet);
831
832 /* Set command header sizes */
833 triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
834 domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
835 triplet_size);
836
837 lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
838 (u8 *) &cmd.domain.country_code,
839 le16_to_cpu(domain->header.len));
840
841 cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
842 sizeof(cmd.action) +
843 sizeof(cmd.domain.header) +
844 sizeof(cmd.domain.country_code) +
845 triplet_size);
846
847 ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
848
849 lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
850 return ret;
851}
852
Dan Williams4c7c6e002010-07-27 13:01:07 -0700853/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700854 * lbs_get_reg - Read a MAC, Baseband, or RF register
Dan Williams4c7c6e002010-07-27 13:01:07 -0700855 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700856 * @priv: pointer to &struct lbs_private
857 * @reg: register command, one of CMD_MAC_REG_ACCESS,
858 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
859 * @offset: byte offset of the register to get
860 * @value: on success, the value of the register at 'offset'
Dan Williams4c7c6e002010-07-27 13:01:07 -0700861 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700862 * returns: 0 on success, error code on failure
Dan Williams4c7c6e002010-07-27 13:01:07 -0700863*/
864int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200865{
Dan Williams4c7c6e002010-07-27 13:01:07 -0700866 struct cmd_ds_reg_access cmd;
867 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200868
Holger Schurig9012b282007-05-25 11:27:16 -0400869 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200870
Dan Williams4c7c6e002010-07-27 13:01:07 -0700871 BUG_ON(value == NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872
Dan Williams4c7c6e002010-07-27 13:01:07 -0700873 memset(&cmd, 0, sizeof(cmd));
874 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
875 cmd.action = cpu_to_le16(CMD_ACT_GET);
Olivier Sobrieedcc3602011-07-09 12:57:05 +0200876 cmd.offset = cpu_to_le16(offset);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877
Dan Williams4c7c6e002010-07-27 13:01:07 -0700878 if (reg != CMD_MAC_REG_ACCESS &&
879 reg != CMD_BBP_REG_ACCESS &&
880 reg != CMD_RF_REG_ACCESS) {
881 ret = -EINVAL;
882 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200883 }
884
Dan Williams4c7c6e002010-07-27 13:01:07 -0700885 ret = lbs_cmd_with_response(priv, reg, &cmd);
Olivier Sobrieedcc3602011-07-09 12:57:05 +0200886 if (!ret) {
Dan Williams4c7c6e002010-07-27 13:01:07 -0700887 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
888 *value = cmd.value.bbp_rf;
889 else if (reg == CMD_MAC_REG_ACCESS)
890 *value = le32_to_cpu(cmd.value.mac);
891 }
892
893out:
894 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
895 return ret;
896}
897
898/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700899 * lbs_set_reg - Write a MAC, Baseband, or RF register
Dan Williams4c7c6e002010-07-27 13:01:07 -0700900 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700901 * @priv: pointer to &struct lbs_private
902 * @reg: register command, one of CMD_MAC_REG_ACCESS,
903 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
904 * @offset: byte offset of the register to set
905 * @value: the value to write to the register at 'offset'
Dan Williams4c7c6e002010-07-27 13:01:07 -0700906 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700907 * returns: 0 on success, error code on failure
Dan Williams4c7c6e002010-07-27 13:01:07 -0700908*/
909int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
910{
911 struct cmd_ds_reg_access cmd;
912 int ret = 0;
913
914 lbs_deb_enter(LBS_DEB_CMD);
915
916 memset(&cmd, 0, sizeof(cmd));
917 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
918 cmd.action = cpu_to_le16(CMD_ACT_SET);
Olivier Sobrieedcc3602011-07-09 12:57:05 +0200919 cmd.offset = cpu_to_le16(offset);
Dan Williams4c7c6e002010-07-27 13:01:07 -0700920
921 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
922 cmd.value.bbp_rf = (u8) (value & 0xFF);
923 else if (reg == CMD_MAC_REG_ACCESS)
924 cmd.value.mac = cpu_to_le32(value);
925 else {
926 ret = -EINVAL;
927 goto out;
928 }
929
930 ret = lbs_cmd_with_response(priv, reg, &cmd);
931
932out:
933 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
934 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200935}
936
David Woodhouse681ffbb2007-12-15 20:04:54 -0500937static void lbs_queue_cmd(struct lbs_private *priv,
938 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200939{
940 unsigned long flags;
David Woodhouse681ffbb2007-12-15 20:04:54 -0500941 int addtail = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200942
Holger Schurig8ff12da2007-08-02 11:54:31 -0400943 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944
David Woodhousec4ab4122007-12-15 00:41:51 -0500945 if (!cmdnode) {
946 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947 goto done;
948 }
David Woodhoused9896ee2007-12-15 00:09:25 -0500949 if (!cmdnode->cmdbuf->size) {
950 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
951 goto done;
952 }
David Woodhouseae125bf2007-12-15 04:22:52 -0500953 cmdnode->result = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954
955 /* Exit_PS command needs to be queued in the header always. */
Dan Williamsddac4522007-12-11 13:49:39 -0500956 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
Dan Williams0bb64082010-07-27 13:08:08 -0700957 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf;
Dan Williamsddac4522007-12-11 13:49:39 -0500958
Dan Williams0bb64082010-07-27 13:08:08 -0700959 if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000960 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200961 addtail = 0;
962 }
963 }
964
Dan Williams0bb64082010-07-27 13:08:08 -0700965 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700966 addtail = 0;
967
David Woodhouseaa21c002007-12-08 20:04:36 +0000968 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200969
David Woodhouseac472462007-12-08 00:35:00 +0000970 if (addtail)
David Woodhouseaa21c002007-12-08 20:04:36 +0000971 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
David Woodhouseac472462007-12-08 00:35:00 +0000972 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000973 list_add(&cmdnode->list, &priv->cmdpendingq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974
David Woodhouseaa21c002007-12-08 20:04:36 +0000975 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976
Holger Schurig8ff12da2007-08-02 11:54:31 -0400977 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
David Woodhousec4ab4122007-12-15 00:41:51 -0500978 le16_to_cpu(cmdnode->cmdbuf->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979
980done:
Holger Schurig8ff12da2007-08-02 11:54:31 -0400981 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982}
983
David Woodhouse18c52e72007-12-17 16:03:58 -0500984static void lbs_submit_command(struct lbs_private *priv,
985 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200986{
987 unsigned long flags;
Dan Williamsddac4522007-12-11 13:49:39 -0500988 struct cmd_header *cmd;
David Woodhouse18c52e72007-12-17 16:03:58 -0500989 uint16_t cmdsize;
990 uint16_t command;
Holger Schurig57962f02008-05-14 16:30:28 +0200991 int timeo = 3 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500992 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200993
Holger Schurig8ff12da2007-08-02 11:54:31 -0400994 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995
Dan Williamsddac4522007-12-11 13:49:39 -0500996 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200997
David Woodhouseaa21c002007-12-08 20:04:36 +0000998 spin_lock_irqsave(&priv->driver_lock, flags);
Daniel Drake71005be2011-05-26 21:31:08 +0100999 priv->seqnum++;
1000 cmd->seqnum = cpu_to_le16(priv->seqnum);
David Woodhouseaa21c002007-12-08 20:04:36 +00001001 priv->cur_cmd = cmdnode;
David Woodhouseaa21c002007-12-08 20:04:36 +00001002 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003
Dan Williamsddac4522007-12-11 13:49:39 -05001004 cmdsize = le16_to_cpu(cmd->size);
1005 command = le16_to_cpu(cmd->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001006
David Woodhouse18c52e72007-12-17 16:03:58 -05001007 /* These commands take longer */
Dan Williamsbe0d76e2009-05-22 20:05:25 -04001008 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
Holger Schurig57962f02008-05-14 16:30:28 +02001009 timeo = 5 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -05001010
Holger Schurige5225b32008-03-26 10:04:44 +01001011 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1012 command, le16_to_cpu(cmd->seqnum), cmdsize);
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001013 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001014
Dan Williamsddac4522007-12-11 13:49:39 -05001015 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
David Woodhouse18c52e72007-12-17 16:03:58 -05001016
David Woodhoused9896ee2007-12-15 00:09:25 -05001017 if (ret) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001018 netdev_info(priv->dev, "DNLD_CMD: hw_host_to_card failed: %d\n",
1019 ret);
David Woodhouse18c52e72007-12-17 16:03:58 -05001020 /* Let the timer kick in and retry, and potentially reset
1021 the whole thing if the condition persists */
Holger Schurig57962f02008-05-14 16:30:28 +02001022 timeo = HZ/4;
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001023 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024
Amitkumar Karwar49125452009-09-30 20:04:38 -07001025 if (command == CMD_802_11_DEEP_SLEEP) {
1026 if (priv->is_auto_deep_sleep_enabled) {
1027 priv->wakeup_dev_required = 1;
1028 priv->dnld_sent = 0;
1029 }
1030 priv->is_deep_sleep = 1;
1031 lbs_complete_command(priv, cmdnode, 0);
1032 } else {
1033 /* Setup the timer after transmit command */
1034 mod_timer(&priv->command_timer, jiffies + timeo);
1035 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036
David Woodhouse18c52e72007-12-17 16:03:58 -05001037 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001038}
1039
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001040/*
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041 * This function inserts command node to cmdfreeq
David Woodhouseaa21c002007-12-08 20:04:36 +00001042 * after cleans it. Requires priv->driver_lock held.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001043 */
David Woodhouse183aeac2007-12-15 01:52:54 -05001044static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001045 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001046{
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001047 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001048
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001049 if (!cmdnode)
1050 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001052 cmdnode->callback = NULL;
1053 cmdnode->callback_arg = 0;
1054
1055 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1056
1057 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1058 out:
1059 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060}
1061
Holger Schurig69f90322007-11-23 15:43:44 +01001062static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1063 struct cmd_ctrl_node *ptempcmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064{
1065 unsigned long flags;
1066
David Woodhouseaa21c002007-12-08 20:04:36 +00001067 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig10078322007-11-15 18:05:47 -05001068 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
David Woodhouseaa21c002007-12-08 20:04:36 +00001069 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001070}
1071
Daniel Drakedf90d842011-07-09 15:41:25 +01001072void __lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1073 int result)
David Woodhouse183aeac2007-12-15 01:52:54 -05001074{
Daniel Drakedf90d842011-07-09 15:41:25 +01001075 /*
1076 * Normally, commands are removed from cmdpendingq before being
1077 * submitted. However, we can arrive here on alternative codepaths
1078 * where the command is still pending. Make sure the command really
1079 * isn't part of a list at this point.
1080 */
1081 list_del_init(&cmd->list);
1082
David Woodhouseae125bf2007-12-15 04:22:52 -05001083 cmd->result = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001084 cmd->cmdwaitqwoken = 1;
Daniel Drakedf90d842011-07-09 15:41:25 +01001085 wake_up(&cmd->cmdwait_q);
David Woodhouse5ba2f8a2007-12-15 02:02:56 -05001086
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001087 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
David Woodhousead12d0f2007-12-15 02:06:16 -05001088 __lbs_cleanup_and_insert_cmd(priv, cmd);
David Woodhouse183aeac2007-12-15 01:52:54 -05001089 priv->cur_cmd = NULL;
Daniel Drakedf90d842011-07-09 15:41:25 +01001090 wake_up_interruptible(&priv->waitq);
1091}
1092
1093void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1094 int result)
1095{
1096 unsigned long flags;
1097 spin_lock_irqsave(&priv->driver_lock, flags);
1098 __lbs_complete_command(priv, cmd, result);
1099 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouse183aeac2007-12-15 01:52:54 -05001100}
1101
Dan Williamsd5db2df2008-08-21 17:51:07 -04001102int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103{
David Woodhousea7c45892007-12-17 22:43:48 -05001104 struct cmd_ds_802_11_radio_control cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -04001105 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106
Holger Schurig9012b282007-05-25 11:27:16 -04001107 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001108
David Woodhousea7c45892007-12-17 22:43:48 -05001109 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1110 cmd.action = cpu_to_le16(CMD_ACT_SET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111
Dan Williamsd5db2df2008-08-21 17:51:07 -04001112 /* Only v8 and below support setting the preamble */
1113 if (priv->fwrelease < 0x09000000) {
1114 switch (preamble) {
1115 case RADIO_PREAMBLE_SHORT:
Dan Williamsd5db2df2008-08-21 17:51:07 -04001116 case RADIO_PREAMBLE_AUTO:
1117 case RADIO_PREAMBLE_LONG:
1118 cmd.control = cpu_to_le16(preamble);
1119 break;
1120 default:
1121 goto out;
1122 }
David Woodhousea7c45892007-12-17 22:43:48 -05001123 }
1124
Dan Williamsd5db2df2008-08-21 17:51:07 -04001125 if (radio_on)
1126 cmd.control |= cpu_to_le16(0x1);
1127 else {
1128 cmd.control &= cpu_to_le16(~0x1);
1129 priv->txpower_cur = 0;
1130 }
David Woodhousea7c45892007-12-17 22:43:48 -05001131
Dan Williamsd5db2df2008-08-21 17:51:07 -04001132 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1133 radio_on ? "ON" : "OFF", preamble);
1134
1135 priv->radio_on = radio_on;
David Woodhousea7c45892007-12-17 22:43:48 -05001136
1137 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138
Dan Williamsd5db2df2008-08-21 17:51:07 -04001139out:
Holger Schurig9012b282007-05-25 11:27:16 -04001140 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141 return ret;
1142}
1143
Holger Schurigc97329e2008-03-18 11:20:21 +01001144void lbs_set_mac_control(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001145{
Holger Schurig835d3ac2008-03-12 16:05:40 +01001146 struct cmd_ds_mac_control cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001147
Holger Schurig9012b282007-05-25 11:27:16 -04001148 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149
Holger Schurig835d3ac2008-03-12 16:05:40 +01001150 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurigd9e97782008-03-12 16:06:43 +01001151 cmd.action = cpu_to_le16(priv->mac_control);
Holger Schurig835d3ac2008-03-12 16:05:40 +01001152 cmd.reserved = 0;
1153
David Woodhouse75bf45a2008-05-20 13:32:45 +01001154 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001155
Holger Schurigc97329e2008-03-18 11:20:21 +01001156 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001157}
1158
1159/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001160 * lbs_allocate_cmd_buffer - allocates the command buffer and links
1161 * it to command free queue
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001162 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001163 * @priv: A pointer to &struct lbs_private structure
1164 *
1165 * returns: 0 for success or -1 on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001166 */
Holger Schurig69f90322007-11-23 15:43:44 +01001167int lbs_allocate_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001168{
1169 int ret = 0;
Dan Williamsddac4522007-12-11 13:49:39 -05001170 u32 bufsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 u32 i;
Dan Williamsddac4522007-12-11 13:49:39 -05001172 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001173
Holger Schurig8ff12da2007-08-02 11:54:31 -04001174 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175
Dan Williamsddac4522007-12-11 13:49:39 -05001176 /* Allocate and initialize the command array */
1177 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1178 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001179 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180 ret = -1;
1181 goto done;
1182 }
Dan Williamsddac4522007-12-11 13:49:39 -05001183 priv->cmd_array = cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001184
Dan Williamsddac4522007-12-11 13:49:39 -05001185 /* Allocate and initialize each command buffer in the command array */
1186 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1187 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1188 if (!cmdarray[i].cmdbuf) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001189 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001190 ret = -1;
1191 goto done;
1192 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001193 }
1194
Dan Williamsddac4522007-12-11 13:49:39 -05001195 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1196 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1197 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001198 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001199 ret = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001200
1201done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001202 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001203 return ret;
1204}
1205
1206/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001207 * lbs_free_cmd_buffer - free the command buffer
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001208 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001209 * @priv: A pointer to &struct lbs_private structure
1210 *
1211 * returns: 0 for success
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001212 */
Holger Schurig69f90322007-11-23 15:43:44 +01001213int lbs_free_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001214{
Dan Williamsddac4522007-12-11 13:49:39 -05001215 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001216 unsigned int i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001217
Holger Schurig8ff12da2007-08-02 11:54:31 -04001218 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001219
1220 /* need to check if cmd array is allocated or not */
David Woodhouseaa21c002007-12-08 20:04:36 +00001221 if (priv->cmd_array == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001222 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001223 goto done;
1224 }
1225
Dan Williamsddac4522007-12-11 13:49:39 -05001226 cmdarray = priv->cmd_array;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001227
1228 /* Release shared memory buffers */
Dan Williamsddac4522007-12-11 13:49:39 -05001229 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1230 if (cmdarray[i].cmdbuf) {
1231 kfree(cmdarray[i].cmdbuf);
1232 cmdarray[i].cmdbuf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001233 }
1234 }
1235
1236 /* Release cmd_ctrl_node */
David Woodhouseaa21c002007-12-08 20:04:36 +00001237 if (priv->cmd_array) {
1238 kfree(priv->cmd_array);
1239 priv->cmd_array = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001240 }
1241
1242done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001243 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001244 return 0;
1245}
1246
1247/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001248 * lbs_get_free_cmd_node - gets a free command node if available in
1249 * command free queue
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001250 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001251 * @priv: A pointer to &struct lbs_private structure
1252 *
1253 * returns: A pointer to &cmd_ctrl_node structure on success
1254 * or %NULL on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255 */
Dan Williamsd06956b2010-07-27 13:16:24 -07001256static struct cmd_ctrl_node *lbs_get_free_cmd_node(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001257{
1258 struct cmd_ctrl_node *tempnode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001259 unsigned long flags;
1260
Holger Schurig8ff12da2007-08-02 11:54:31 -04001261 lbs_deb_enter(LBS_DEB_HOST);
1262
David Woodhouseaa21c002007-12-08 20:04:36 +00001263 if (!priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001264 return NULL;
1265
David Woodhouseaa21c002007-12-08 20:04:36 +00001266 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001267
David Woodhouseaa21c002007-12-08 20:04:36 +00001268 if (!list_empty(&priv->cmdfreeq)) {
1269 tempnode = list_first_entry(&priv->cmdfreeq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001270 struct cmd_ctrl_node, list);
Daniel Drakedf90d842011-07-09 15:41:25 +01001271 list_del_init(&tempnode->list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001272 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001273 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001274 tempnode = NULL;
1275 }
1276
David Woodhouseaa21c002007-12-08 20:04:36 +00001277 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001278
Holger Schurig8ff12da2007-08-02 11:54:31 -04001279 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001280 return tempnode;
1281}
1282
1283/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001284 * lbs_execute_next_command - execute next command in command
1285 * pending queue. Will put firmware back to PS mode if applicable.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001286 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001287 * @priv: A pointer to &struct lbs_private structure
1288 *
1289 * returns: 0 on success or -1 on error
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290 */
Holger Schurig69f90322007-11-23 15:43:44 +01001291int lbs_execute_next_command(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293 struct cmd_ctrl_node *cmdnode = NULL;
Dan Williamsddac4522007-12-11 13:49:39 -05001294 struct cmd_header *cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295 unsigned long flags;
1296 int ret = 0;
1297
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001298 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1299 * only caller to us is lbs_thread() and we get even when a
1300 * data packet is received */
Holger Schurig8ff12da2007-08-02 11:54:31 -04001301 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001302
David Woodhouseaa21c002007-12-08 20:04:36 +00001303 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001304
David Woodhouseaa21c002007-12-08 20:04:36 +00001305 if (priv->cur_cmd) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001306 netdev_alert(priv->dev,
1307 "EXEC_NEXT_CMD: already processing command!\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001308 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001309 ret = -1;
1310 goto done;
1311 }
1312
David Woodhouseaa21c002007-12-08 20:04:36 +00001313 if (!list_empty(&priv->cmdpendingq)) {
1314 cmdnode = list_first_entry(&priv->cmdpendingq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001315 struct cmd_ctrl_node, list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316 }
1317
David Woodhouseaa21c002007-12-08 20:04:36 +00001318 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001319
1320 if (cmdnode) {
Dan Williamsddac4522007-12-11 13:49:39 -05001321 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001322
Dan Williamsddac4522007-12-11 13:49:39 -05001323 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001324 if ((priv->psstate == PS_STATE_SLEEP) ||
1325 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001326 lbs_deb_host(
1327 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001328 le16_to_cpu(cmd->command),
David Woodhouseaa21c002007-12-08 20:04:36 +00001329 priv->psstate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001330 ret = -1;
1331 goto done;
1332 }
Holger Schurig8ff12da2007-08-02 11:54:31 -04001333 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
Dan Williamsddac4522007-12-11 13:49:39 -05001334 "0x%04x in psstate %d\n",
1335 le16_to_cpu(cmd->command), priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +00001336 } else if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001337 /*
1338 * 1. Non-PS command:
1339 * Queue it. set needtowakeup to TRUE if current state
Dan Williams0bb64082010-07-27 13:08:08 -07001340 * is SLEEP, otherwise call send EXIT_PS.
1341 * 2. PS command but not EXIT_PS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001342 * Ignore it.
Dan Williams0bb64082010-07-27 13:08:08 -07001343 * 3. PS command EXIT_PS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001344 * Set needtowakeup to TRUE if current state is SLEEP,
1345 * otherwise send this command down to firmware
1346 * immediately.
1347 */
Dan Williamsddac4522007-12-11 13:49:39 -05001348 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001349 /* Prepare to send Exit PS,
1350 * this non PS command will be sent later */
David Woodhouseaa21c002007-12-08 20:04:36 +00001351 if ((priv->psstate == PS_STATE_SLEEP)
1352 || (priv->psstate == PS_STATE_PRE_SLEEP)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001353 ) {
1354 /* w/ new scheme, it will not reach here.
1355 since it is blocked in main_thread. */
David Woodhouseaa21c002007-12-08 20:04:36 +00001356 priv->needtowakeup = 1;
Dan Williams0bb64082010-07-27 13:08:08 -07001357 } else {
1358 lbs_set_ps_mode(priv,
1359 PS_MODE_ACTION_EXIT_PS,
1360 false);
1361 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001362
1363 ret = 0;
1364 goto done;
1365 } else {
1366 /*
1367 * PS command. Ignore it if it is not Exit_PS.
1368 * otherwise send it down immediately.
1369 */
David Woodhouse38bfab12007-12-16 23:26:54 -05001370 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001371
Holger Schurig8ff12da2007-08-02 11:54:31 -04001372 lbs_deb_host(
1373 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001374 psm->action);
1375 if (psm->action !=
Dan Williams0bb64082010-07-27 13:08:08 -07001376 cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001377 lbs_deb_host(
1378 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
David Woodhouse183aeac2007-12-15 01:52:54 -05001379 lbs_complete_command(priv, cmdnode, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001380
1381 ret = 0;
1382 goto done;
1383 }
1384
David Woodhouseaa21c002007-12-08 20:04:36 +00001385 if ((priv->psstate == PS_STATE_SLEEP) ||
1386 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001387 lbs_deb_host(
1388 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
David Woodhouse183aeac2007-12-15 01:52:54 -05001389 lbs_complete_command(priv, cmdnode, 0);
David Woodhouseaa21c002007-12-08 20:04:36 +00001390 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391
1392 ret = 0;
1393 goto done;
1394 }
1395
Holger Schurig8ff12da2007-08-02 11:54:31 -04001396 lbs_deb_host(
1397 "EXEC_NEXT_CMD: sending EXIT_PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001398 }
1399 }
Paul Fox2ae1b8b2011-05-09 10:40:42 +01001400 spin_lock_irqsave(&priv->driver_lock, flags);
Daniel Drakedf90d842011-07-09 15:41:25 +01001401 list_del_init(&cmdnode->list);
Paul Fox2ae1b8b2011-05-09 10:40:42 +01001402 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001403 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001404 le16_to_cpu(cmd->command));
David Woodhoused9896ee2007-12-15 00:09:25 -05001405 lbs_submit_command(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406 } else {
1407 /*
1408 * check if in power save mode, if yes, put the device back
1409 * to PS mode
1410 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301411#ifdef TODO
1412 /*
1413 * This was the old code for libertas+wext. Someone that
1414 * understands this beast should re-code it in a sane way.
1415 *
1416 * I actually don't understand why this is related to WPA
1417 * and to connection status, shouldn't powering should be
1418 * independ of such things?
1419 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001420 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1421 (priv->psstate == PS_STATE_FULL_POWER) &&
1422 ((priv->connect_status == LBS_CONNECTED) ||
Holger Schurig602114a2009-12-02 15:26:01 +01001423 lbs_mesh_connected(priv))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001424 if (priv->secinfo.WPAenabled ||
1425 priv->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001426 /* check for valid WPA group keys */
David Woodhouseaa21c002007-12-08 20:04:36 +00001427 if (priv->wpa_mcast_key.len ||
1428 priv->wpa_unicast_key.len) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001429 lbs_deb_host(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001430 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1431 " go back to PS_SLEEP");
Dan Williams0bb64082010-07-27 13:08:08 -07001432 lbs_set_ps_mode(priv,
1433 PS_MODE_ACTION_ENTER_PS,
1434 false);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001435 }
1436 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001437 lbs_deb_host(
1438 "EXEC_NEXT_CMD: cmdpendingq empty, "
1439 "go back to PS_SLEEP");
Dan Williams0bb64082010-07-27 13:08:08 -07001440 lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
1441 false);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001442 }
1443 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301444#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001445 }
1446
1447 ret = 0;
1448done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001449 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001450 return ret;
1451}
1452
Holger Schurigf539f2e2008-03-26 13:22:11 +01001453static void lbs_send_confirmsleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001454{
1455 unsigned long flags;
Holger Schurigf539f2e2008-03-26 13:22:11 +01001456 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001457
Holger Schurig8ff12da2007-08-02 11:54:31 -04001458 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001459 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1460 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001461
Holger Schurigf539f2e2008-03-26 13:22:11 +01001462 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1463 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001464 if (ret) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001465 netdev_alert(priv->dev, "confirm_sleep failed\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001466 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001467 }
Holger Schurig7919b892008-04-01 14:50:43 +02001468
1469 spin_lock_irqsave(&priv->driver_lock, flags);
1470
Holger Schuriga01f5452008-06-04 11:10:40 +02001471 /* We don't get a response on the sleep-confirmation */
1472 priv->dnld_sent = DNLD_RES_RECEIVED;
1473
Amitkumar Karwar66fceb62010-05-19 03:24:38 -07001474 if (priv->is_host_sleep_configured) {
1475 priv->is_host_sleep_activated = 1;
1476 wake_up_interruptible(&priv->host_sleep_q);
1477 }
1478
Holger Schurig7919b892008-04-01 14:50:43 +02001479 /* If nothing to do, go back to sleep (?) */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001480 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
Holger Schurig7919b892008-04-01 14:50:43 +02001481 priv->psstate = PS_STATE_SLEEP;
1482
1483 spin_unlock_irqrestore(&priv->driver_lock, flags);
1484
1485out:
Holger Schurigf539f2e2008-03-26 13:22:11 +01001486 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001487}
1488
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001489/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001490 * lbs_ps_confirm_sleep - checks condition and prepares to
1491 * send sleep confirm command to firmware if ok
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001492 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001493 * @priv: A pointer to &struct lbs_private structure
1494 *
1495 * returns: n/a
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001496 */
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001497void lbs_ps_confirm_sleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001498{
1499 unsigned long flags =0;
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001500 int allowed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001501
Holger Schurig8ff12da2007-08-02 11:54:31 -04001502 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001503
Holger Schuriga01f5452008-06-04 11:10:40 +02001504 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig634b8f42007-05-25 13:05:16 -04001505 if (priv->dnld_sent) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001506 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001507 lbs_deb_host("dnld_sent was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001508 }
1509
Holger Schurig7919b892008-04-01 14:50:43 +02001510 /* In-progress command? */
David Woodhouseaa21c002007-12-08 20:04:36 +00001511 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001512 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001513 lbs_deb_host("cur_cmd was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001514 }
Holger Schurig7919b892008-04-01 14:50:43 +02001515
1516 /* Pending events or command responses? */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001517 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001518 allowed = 0;
Holger Schurig7919b892008-04-01 14:50:43 +02001519 lbs_deb_host("pending events or command responses\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001520 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001521 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001522
1523 if (allowed) {
Holger Schurig10078322007-11-15 18:05:47 -05001524 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
Holger Schurigf539f2e2008-03-26 13:22:11 +01001525 lbs_send_confirmsleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001526 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001527 lbs_deb_host("sleep confirm has been delayed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001528 }
1529
Holger Schurig8ff12da2007-08-02 11:54:31 -04001530 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001531}
Holger Schurig675787e2007-12-05 17:58:11 +01001532
1533
Anna Neal0112c9e2008-09-11 11:17:25 -07001534/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001535 * lbs_set_tpc_cfg - Configures the transmission power control functionality
Anna Neal0112c9e2008-09-11 11:17:25 -07001536 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001537 * @priv: A pointer to &struct lbs_private structure
1538 * @enable: Transmission power control enable
1539 * @p0: Power level when link quality is good (dBm).
1540 * @p1: Power level when link quality is fair (dBm).
1541 * @p2: Power level when link quality is poor (dBm).
1542 * @usesnr: Use Signal to Noise Ratio in TPC
Anna Neal0112c9e2008-09-11 11:17:25 -07001543 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001544 * returns: 0 on success
Anna Neal0112c9e2008-09-11 11:17:25 -07001545 */
1546int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1547 int8_t p2, int usesnr)
1548{
1549 struct cmd_ds_802_11_tpc_cfg cmd;
1550 int ret;
1551
1552 memset(&cmd, 0, sizeof(cmd));
1553 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1554 cmd.action = cpu_to_le16(CMD_ACT_SET);
1555 cmd.enable = !!enable;
Anna Neal3ed6e082008-09-26 11:34:35 -04001556 cmd.usesnr = !!usesnr;
Anna Neal0112c9e2008-09-11 11:17:25 -07001557 cmd.P0 = p0;
1558 cmd.P1 = p1;
1559 cmd.P2 = p2;
1560
1561 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1562
1563 return ret;
1564}
1565
1566/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001567 * lbs_set_power_adapt_cfg - Configures the power adaptation settings
Anna Neal0112c9e2008-09-11 11:17:25 -07001568 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001569 * @priv: A pointer to &struct lbs_private structure
1570 * @enable: Power adaptation enable
1571 * @p0: Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1572 * @p1: Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1573 * @p2: Power level for 48 and 54 Mbps (dBm).
Anna Neal0112c9e2008-09-11 11:17:25 -07001574 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001575 * returns: 0 on Success
Anna Neal0112c9e2008-09-11 11:17:25 -07001576 */
1577
1578int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1579 int8_t p1, int8_t p2)
1580{
1581 struct cmd_ds_802_11_pa_cfg cmd;
1582 int ret;
1583
1584 memset(&cmd, 0, sizeof(cmd));
1585 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1586 cmd.action = cpu_to_le16(CMD_ACT_SET);
1587 cmd.enable = !!enable;
1588 cmd.P0 = p0;
1589 cmd.P1 = p1;
1590 cmd.P2 = p2;
1591
1592 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1593
1594 return ret;
1595}
1596
1597
Holger Schurig6d898b12009-10-14 16:49:53 +02001598struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001599 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1600 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1601 unsigned long callback_arg)
Holger Schurig675787e2007-12-05 17:58:11 +01001602{
Holger Schurig675787e2007-12-05 17:58:11 +01001603 struct cmd_ctrl_node *cmdnode;
Holger Schurig675787e2007-12-05 17:58:11 +01001604
1605 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurig675787e2007-12-05 17:58:11 +01001606
David Woodhouseaa21c002007-12-08 20:04:36 +00001607 if (priv->surpriseremoved) {
Holger Schurig675787e2007-12-05 17:58:11 +01001608 lbs_deb_host("PREP_CMD: card removed\n");
David Woodhouse3399ea52007-12-15 03:09:33 -05001609 cmdnode = ERR_PTR(-ENOENT);
Holger Schurig675787e2007-12-05 17:58:11 +01001610 goto done;
1611 }
1612
Dan Williams77ccdcf2010-07-27 13:15:45 -07001613 /* No commands are allowed in Deep Sleep until we toggle the GPIO
1614 * to wake up the card and it has signaled that it's ready.
1615 */
1616 if (!priv->is_auto_deep_sleep_enabled) {
1617 if (priv->is_deep_sleep) {
1618 lbs_deb_cmd("command not allowed in deep sleep\n");
1619 cmdnode = ERR_PTR(-EBUSY);
1620 goto done;
1621 }
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001622 }
1623
Dan Williamsd06956b2010-07-27 13:16:24 -07001624 cmdnode = lbs_get_free_cmd_node(priv);
Holger Schurig675787e2007-12-05 17:58:11 +01001625 if (cmdnode == NULL) {
1626 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1627
1628 /* Wake up main thread to execute next command */
1629 wake_up_interruptible(&priv->waitq);
David Woodhouse3399ea52007-12-15 03:09:33 -05001630 cmdnode = ERR_PTR(-ENOBUFS);
Holger Schurig675787e2007-12-05 17:58:11 +01001631 goto done;
1632 }
1633
David Woodhouse448a51a2007-12-08 00:59:54 +00001634 cmdnode->callback = callback;
David Woodhouse1309b552007-12-10 13:36:10 -05001635 cmdnode->callback_arg = callback_arg;
Holger Schurig675787e2007-12-05 17:58:11 +01001636
Dan Williams7ad994d2007-12-11 12:33:30 -05001637 /* Copy the incoming command to the buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001638 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
Dan Williams7ad994d2007-12-11 12:33:30 -05001639
Daniel Drake71005be2011-05-26 21:31:08 +01001640 /* Set command, clean result, move to buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001641 cmdnode->cmdbuf->command = cpu_to_le16(command);
1642 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
Dan Williamsddac4522007-12-11 13:49:39 -05001643 cmdnode->cmdbuf->result = 0;
Holger Schurig675787e2007-12-05 17:58:11 +01001644
1645 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1646
Holger Schurig675787e2007-12-05 17:58:11 +01001647 cmdnode->cmdwaitqwoken = 0;
David Woodhouse681ffbb2007-12-15 20:04:54 -05001648 lbs_queue_cmd(priv, cmdnode);
Holger Schurig675787e2007-12-05 17:58:11 +01001649 wake_up_interruptible(&priv->waitq);
1650
David Woodhouse3399ea52007-12-15 03:09:33 -05001651 done:
1652 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1653 return cmdnode;
1654}
1655
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001656void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1657 struct cmd_header *in_cmd, int in_cmd_size)
1658{
1659 lbs_deb_enter(LBS_DEB_CMD);
1660 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1661 lbs_cmd_async_callback, 0);
1662 lbs_deb_leave(LBS_DEB_CMD);
1663}
1664
David Woodhouse3399ea52007-12-15 03:09:33 -05001665int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1666 struct cmd_header *in_cmd, int in_cmd_size,
1667 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1668 unsigned long callback_arg)
1669{
1670 struct cmd_ctrl_node *cmdnode;
1671 unsigned long flags;
1672 int ret = 0;
1673
1674 lbs_deb_enter(LBS_DEB_HOST);
1675
1676 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1677 callback, callback_arg);
1678 if (IS_ERR(cmdnode)) {
1679 ret = PTR_ERR(cmdnode);
1680 goto done;
1681 }
1682
Holger Schurig675787e2007-12-05 17:58:11 +01001683 might_sleep();
Daniel Drakedf90d842011-07-09 15:41:25 +01001684
1685 /*
1686 * Be careful with signals here. A signal may be received as the system
1687 * goes into suspend or resume. We do not want this to interrupt the
1688 * command, so we perform an uninterruptible sleep.
1689 */
1690 wait_event(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
Holger Schurig675787e2007-12-05 17:58:11 +01001691
David Woodhouseaa21c002007-12-08 20:04:36 +00001692 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseae125bf2007-12-15 04:22:52 -05001693 ret = cmdnode->result;
1694 if (ret)
Joe Perchesf3a57fd2011-05-02 16:49:15 -07001695 netdev_info(priv->dev, "PREP_CMD: command 0x%04x failed: %d\n",
1696 command, ret);
David Woodhouse3399ea52007-12-15 03:09:33 -05001697
David Woodhousead12d0f2007-12-15 02:06:16 -05001698 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
David Woodhouseaa21c002007-12-08 20:04:36 +00001699 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig675787e2007-12-05 17:58:11 +01001700
1701done:
1702 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1703 return ret;
1704}
Dan Williams14e865b2007-12-10 15:11:23 -05001705EXPORT_SYMBOL_GPL(__lbs_cmd);