]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/libertas/cmd.c
libertas: Added support for host sleep feature
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / libertas / cmd.c
CommitLineData
876c9d3a
MT
1/**
2 * This file contains the handling of command.
3 * It prepares command and sends it to firmware when it is ready.
4 */
5
7919b89c 6#include <linux/kfifo.h>
e93156e7 7#include <linux/sched.h>
5a0e3ad6 8#include <linux/slab.h>
e93156e7 9
876c9d3a 10#include "decl.h"
e86dc1ca 11#include "cfg.h"
6e66f03f 12#include "cmd.h"
876c9d3a 13
e93156e7 14
2fd6cfe3 15static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
0d61d042 16
8db4a2b9
HS
17/**
18 * @brief Simple callback that copies response back into command
19 *
20 * @param priv A pointer to struct lbs_private structure
21 * @param extra A pointer to the original command structure for which
22 * 'resp' is a response
23 * @param resp A pointer to the command response
24 *
25 * @return 0 on success, error on failure
26 */
27int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
28 struct cmd_header *resp)
29{
30 struct cmd_header *buf = (void *)extra;
31 uint16_t copy_len;
32
33 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
34 memcpy(buf, resp, copy_len);
35 return 0;
36}
37EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
38
39/**
40 * @brief Simple callback that ignores the result. Use this if
41 * you just want to send a command to the hardware, but don't
42 * care for the result.
43 *
44 * @param priv ignored
45 * @param extra ignored
46 * @param resp ignored
47 *
48 * @return 0 for success
49 */
50static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
51 struct cmd_header *resp)
52{
53 return 0;
54}
55
56
876c9d3a 57/**
852e1f2a 58 * @brief Checks whether a command is allowed in Power Save mode
876c9d3a
MT
59 *
60 * @param command the command ID
852e1f2a 61 * @return 1 if allowed, 0 if not allowed
876c9d3a 62 */
852e1f2a 63static u8 is_command_allowed_in_ps(u16 cmd)
876c9d3a 64{
852e1f2a
DW
65 switch (cmd) {
66 case CMD_802_11_RSSI:
67 return 1;
66fceb69
AK
68 case CMD_802_11_HOST_SLEEP_CFG:
69 return 1;
852e1f2a
DW
70 default:
71 break;
876c9d3a 72 }
876c9d3a
MT
73 return 0;
74}
75
63f275df
AK
76/**
77 * @brief This function checks if the command is allowed.
78 *
79 * @param priv A pointer to lbs_private structure
80 * @return allowed or not allowed.
81 */
82
83static int lbs_is_cmd_allowed(struct lbs_private *priv)
84{
85 int ret = 1;
86
87 lbs_deb_enter(LBS_DEB_CMD);
88
89 if (!priv->is_auto_deep_sleep_enabled) {
90 if (priv->is_deep_sleep) {
91 lbs_deb_cmd("command not allowed in deep sleep\n");
92 ret = 0;
93 }
94 }
95
96 lbs_deb_leave(LBS_DEB_CMD);
97 return ret;
98}
99
6e66f03f
DW
100/**
101 * @brief Updates the hardware details like MAC address and regulatory region
102 *
103 * @param priv A pointer to struct lbs_private structure
104 *
105 * @return 0 on success, error on failure
106 */
107int lbs_update_hw_spec(struct lbs_private *priv)
876c9d3a 108{
6e66f03f
DW
109 struct cmd_ds_get_hw_spec cmd;
110 int ret = -1;
111 u32 i;
876c9d3a 112
9012b28a 113 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 114
6e66f03f
DW
115 memset(&cmd, 0, sizeof(cmd));
116 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
117 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
689442dc 118 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
6e66f03f
DW
119 if (ret)
120 goto out;
121
122 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
6e66f03f 123
dac10a9f
HS
124 /* The firmware release is in an interesting format: the patch
125 * level is in the most significant nibble ... so fix that: */
126 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
127 priv->fwrelease = (priv->fwrelease << 8) |
128 (priv->fwrelease >> 24 & 0xff);
129
130 /* Some firmware capabilities:
131 * CF card firmware 5.0.16p0: cap 0x00000303
132 * USB dongle firmware 5.110.17p2: cap 0x00000303
133 */
e174961c
JB
134 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
135 cmd.permanentaddr,
dac10a9f
HS
136 priv->fwrelease >> 24 & 0xff,
137 priv->fwrelease >> 16 & 0xff,
138 priv->fwrelease >> 8 & 0xff,
139 priv->fwrelease & 0xff,
140 priv->fwcapinfo);
6e66f03f
DW
141 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
142 cmd.hwifversion, cmd.version);
143
144 /* Clamp region code to 8-bit since FW spec indicates that it should
145 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
146 * returns non-zero high 8 bits here.
15483996
MV
147 *
148 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
149 * need to check for this problem and handle it properly.
6e66f03f 150 */
15483996
MV
151 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
152 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
153 else
154 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
6e66f03f
DW
155
156 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
157 /* use the region code to search for the index */
158 if (priv->regioncode == lbs_region_code_to_index[i])
159 break;
160 }
161
162 /* if it's unidentified region code, use the default (USA) */
163 if (i >= MRVDRV_MAX_REGION_CODE) {
164 priv->regioncode = 0x10;
165 lbs_pr_info("unidentified region code; using the default (USA)\n");
166 }
167
168 if (priv->current_addr[0] == 0xff)
169 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
876c9d3a 170
6e66f03f
DW
171 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
172 if (priv->mesh_dev)
173 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
174
6e66f03f 175out:
9012b28a 176 lbs_deb_leave(LBS_DEB_CMD);
6e66f03f 177 return ret;
876c9d3a
MT
178}
179
66fceb69
AK
180static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
181 struct cmd_header *resp)
182{
183 lbs_deb_enter(LBS_DEB_CMD);
1311843c 184 if (priv->is_host_sleep_activated) {
66fceb69
AK
185 priv->is_host_sleep_configured = 0;
186 if (priv->psstate == PS_STATE_FULL_POWER) {
187 priv->is_host_sleep_activated = 0;
188 wake_up_interruptible(&priv->host_sleep_q);
189 }
190 } else {
191 priv->is_host_sleep_configured = 1;
192 }
193 lbs_deb_leave(LBS_DEB_CMD);
194 return 0;
195}
196
582c1b53
AN
197int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
198 struct wol_config *p_wol_config)
6ce4fd2a
DW
199{
200 struct cmd_ds_host_sleep cmd_config;
201 int ret;
202
9fae899c 203 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
6ce4fd2a 204 cmd_config.criteria = cpu_to_le32(criteria);
506e9025
DW
205 cmd_config.gpio = priv->wol_gpio;
206 cmd_config.gap = priv->wol_gap;
6ce4fd2a 207
582c1b53
AN
208 if (p_wol_config != NULL)
209 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
210 sizeof(struct wol_config));
211 else
212 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
213
66fceb69
AK
214 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
215 le16_to_cpu(cmd_config.hdr.size),
216 lbs_ret_host_sleep_cfg, 0);
506e9025 217 if (!ret) {
66fceb69 218 if (p_wol_config)
582c1b53
AN
219 memcpy((uint8_t *) p_wol_config,
220 (uint8_t *)&cmd_config.wol_conf,
221 sizeof(struct wol_config));
506e9025 222 } else {
6ce4fd2a 223 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
6ce4fd2a 224 }
506e9025 225
6ce4fd2a
DW
226 return ret;
227}
228EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
229
e98a88dd 230static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
876c9d3a
MT
231 u16 cmd_action)
232{
233 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
876c9d3a 234
9012b28a 235 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 236
0aef64d7 237 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
981f187b 238 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
8ec97cc8 239 sizeof(struct cmd_header));
876c9d3a
MT
240 psm->action = cpu_to_le16(cmd_action);
241 psm->multipledtim = 0;
981f187b 242 switch (cmd_action) {
0aef64d7 243 case CMD_SUBCMD_ENTER_PS:
9012b28a 244 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
876c9d3a 245
252cf0d1 246 psm->locallisteninterval = 0;
97605c3e 247 psm->nullpktinterval = 0;
876c9d3a 248 psm->multipledtim =
56c4656e 249 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
876c9d3a
MT
250 break;
251
0aef64d7 252 case CMD_SUBCMD_EXIT_PS:
9012b28a 253 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
876c9d3a
MT
254 break;
255
0aef64d7 256 case CMD_SUBCMD_SLEEP_CONFIRMED:
9012b28a 257 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
876c9d3a
MT
258 break;
259
260 default:
261 break;
262 }
263
9012b28a 264 lbs_deb_leave(LBS_DEB_CMD);
876c9d3a
MT
265 return 0;
266}
267
3fbe104c
DW
268int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
269 struct sleep_params *sp)
876c9d3a 270{
3fbe104c
DW
271 struct cmd_ds_802_11_sleep_params cmd;
272 int ret;
876c9d3a 273
9012b28a 274 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 275
0aef64d7 276 if (cmd_action == CMD_ACT_GET) {
3fbe104c
DW
277 memset(&cmd, 0, sizeof(cmd));
278 } else {
279 cmd.error = cpu_to_le16(sp->sp_error);
280 cmd.offset = cpu_to_le16(sp->sp_offset);
281 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
282 cmd.calcontrol = sp->sp_calcontrol;
283 cmd.externalsleepclk = sp->sp_extsleepclk;
284 cmd.reserved = cpu_to_le16(sp->sp_reserved);
876c9d3a 285 }
3fbe104c
DW
286 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
287 cmd.action = cpu_to_le16(cmd_action);
876c9d3a 288
3fbe104c
DW
289 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
290
291 if (!ret) {
292 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
293 "calcontrol 0x%x extsleepclk 0x%x\n",
294 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
295 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
296 cmd.externalsleepclk);
297
298 sp->sp_error = le16_to_cpu(cmd.error);
299 sp->sp_offset = le16_to_cpu(cmd.offset);
300 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
301 sp->sp_calcontrol = cmd.calcontrol;
302 sp->sp_extsleepclk = cmd.externalsleepclk;
303 sp->sp_reserved = le16_to_cpu(cmd.reserved);
304 }
305
306 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
876c9d3a
MT
307 return 0;
308}
309
49125454
AK
310static int lbs_wait_for_ds_awake(struct lbs_private *priv)
311{
312 int ret = 0;
313
314 lbs_deb_enter(LBS_DEB_CMD);
315
316 if (priv->is_deep_sleep) {
317 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
318 !priv->is_deep_sleep, (10 * HZ))) {
319 lbs_pr_err("ds_awake_q: timer expired\n");
320 ret = -1;
321 }
322 }
323
324 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
325 return ret;
326}
327
328int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
329{
330 int ret = 0;
331
332 lbs_deb_enter(LBS_DEB_CMD);
333
334 if (deep_sleep) {
335 if (priv->is_deep_sleep != 1) {
336 lbs_deb_cmd("deep sleep: sleep\n");
337 BUG_ON(!priv->enter_deep_sleep);
338 ret = priv->enter_deep_sleep(priv);
339 if (!ret) {
340 netif_stop_queue(priv->dev);
341 netif_carrier_off(priv->dev);
342 }
343 } else {
344 lbs_pr_err("deep sleep: already enabled\n");
345 }
346 } else {
347 if (priv->is_deep_sleep) {
348 lbs_deb_cmd("deep sleep: wakeup\n");
349 BUG_ON(!priv->exit_deep_sleep);
350 ret = priv->exit_deep_sleep(priv);
351 if (!ret) {
352 ret = lbs_wait_for_ds_awake(priv);
353 if (ret)
354 lbs_pr_err("deep sleep: wakeup"
355 "failed\n");
356 }
357 }
358 }
359
360 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
361 return ret;
362}
363
1311843c
AK
364static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
365 unsigned long dummy,
366 struct cmd_header *cmd)
367{
368 lbs_deb_enter(LBS_DEB_FW);
369 priv->is_host_sleep_activated = 1;
370 wake_up_interruptible(&priv->host_sleep_q);
371 lbs_deb_leave(LBS_DEB_FW);
372 return 0;
373}
374
375int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
376{
377 struct cmd_header cmd;
378 int ret = 0;
379 uint32_t criteria = EHS_REMOVE_WAKEUP;
380
381 lbs_deb_enter(LBS_DEB_CMD);
382
383 if (host_sleep) {
384 if (priv->is_host_sleep_activated != 1) {
385 memset(&cmd, 0, sizeof(cmd));
386 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
387 (struct wol_config *)NULL);
388 if (ret) {
389 lbs_pr_info("Host sleep configuration failed: "
390 "%d\n", ret);
391 return ret;
392 }
393 if (priv->psstate == PS_STATE_FULL_POWER) {
394 ret = __lbs_cmd(priv,
395 CMD_802_11_HOST_SLEEP_ACTIVATE,
396 &cmd,
397 sizeof(cmd),
398 lbs_ret_host_sleep_activate, 0);
399 if (ret)
400 lbs_pr_info("HOST_SLEEP_ACTIVATE "
401 "failed: %d\n", ret);
402 }
403
404 if (!wait_event_interruptible_timeout(
405 priv->host_sleep_q,
406 priv->is_host_sleep_activated,
407 (10 * HZ))) {
408 lbs_pr_err("host_sleep_q: timer expired\n");
409 ret = -1;
410 }
411 } else {
412 lbs_pr_err("host sleep: already enabled\n");
413 }
414 } else {
415 if (priv->is_host_sleep_activated)
416 ret = lbs_host_sleep_cfg(priv, criteria,
417 (struct wol_config *)NULL);
418 }
419
420 return ret;
421}
422
39fcf7a3
DW
423/**
424 * @brief Set an SNMP MIB value
425 *
426 * @param priv A pointer to struct lbs_private structure
427 * @param oid The OID to set in the firmware
428 * @param val Value to set the OID to
429 *
430 * @return 0 on success, error on failure
431 */
432int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
876c9d3a 433{
39fcf7a3
DW
434 struct cmd_ds_802_11_snmp_mib cmd;
435 int ret;
876c9d3a 436
9012b28a 437 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 438
39fcf7a3
DW
439 memset(&cmd, 0, sizeof (cmd));
440 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
441 cmd.action = cpu_to_le16(CMD_ACT_SET);
442 cmd.oid = cpu_to_le16((u16) oid);
876c9d3a 443
39fcf7a3
DW
444 switch (oid) {
445 case SNMP_MIB_OID_BSS_TYPE:
446 cmd.bufsize = cpu_to_le16(sizeof(u8));
fef0640e 447 cmd.value[0] = val;
39fcf7a3
DW
448 break;
449 case SNMP_MIB_OID_11D_ENABLE:
450 case SNMP_MIB_OID_FRAG_THRESHOLD:
451 case SNMP_MIB_OID_RTS_THRESHOLD:
452 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
453 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
454 cmd.bufsize = cpu_to_le16(sizeof(u16));
455 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
876c9d3a 456 break;
39fcf7a3
DW
457 default:
458 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
459 ret = -EINVAL;
460 goto out;
876c9d3a
MT
461 }
462
39fcf7a3
DW
463 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
464 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
876c9d3a 465
39fcf7a3 466 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
876c9d3a 467
39fcf7a3
DW
468out:
469 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
470 return ret;
471}
876c9d3a 472
39fcf7a3
DW
473/**
474 * @brief Get an SNMP MIB value
475 *
476 * @param priv A pointer to struct lbs_private structure
477 * @param oid The OID to retrieve from the firmware
478 * @param out_val Location for the returned value
479 *
480 * @return 0 on success, error on failure
481 */
482int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
483{
484 struct cmd_ds_802_11_snmp_mib cmd;
485 int ret;
876c9d3a 486
39fcf7a3 487 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 488
39fcf7a3
DW
489 memset(&cmd, 0, sizeof (cmd));
490 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
491 cmd.action = cpu_to_le16(CMD_ACT_GET);
492 cmd.oid = cpu_to_le16(oid);
876c9d3a 493
39fcf7a3
DW
494 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
495 if (ret)
496 goto out;
876c9d3a 497
39fcf7a3
DW
498 switch (le16_to_cpu(cmd.bufsize)) {
499 case sizeof(u8):
fef0640e 500 *out_val = cmd.value[0];
39fcf7a3
DW
501 break;
502 case sizeof(u16):
503 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
876c9d3a
MT
504 break;
505 default:
39fcf7a3
DW
506 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
507 oid, le16_to_cpu(cmd.bufsize));
876c9d3a
MT
508 break;
509 }
510
39fcf7a3
DW
511out:
512 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
513 return ret;
876c9d3a
MT
514}
515
87c8c72d
DW
516/**
517 * @brief Get the min, max, and current TX power
518 *
519 * @param priv A pointer to struct lbs_private structure
520 * @param curlevel Current power level in dBm
521 * @param minlevel Minimum supported power level in dBm (optional)
522 * @param maxlevel Maximum supported power level in dBm (optional)
523 *
524 * @return 0 on success, error on failure
525 */
526int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
527 s16 *maxlevel)
876c9d3a 528{
87c8c72d
DW
529 struct cmd_ds_802_11_rf_tx_power cmd;
530 int ret;
876c9d3a 531
9012b28a 532 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 533
87c8c72d
DW
534 memset(&cmd, 0, sizeof(cmd));
535 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
536 cmd.action = cpu_to_le16(CMD_ACT_GET);
537
538 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
539 if (ret == 0) {
540 *curlevel = le16_to_cpu(cmd.curlevel);
541 if (minlevel)
87bf24f3 542 *minlevel = cmd.minlevel;
87c8c72d 543 if (maxlevel)
87bf24f3 544 *maxlevel = cmd.maxlevel;
87c8c72d 545 }
876c9d3a 546
87c8c72d
DW
547 lbs_deb_leave(LBS_DEB_CMD);
548 return ret;
549}
876c9d3a 550
87c8c72d
DW
551/**
552 * @brief Set the TX power
553 *
554 * @param priv A pointer to struct lbs_private structure
555 * @param dbm The desired power level in dBm
556 *
557 * @return 0 on success, error on failure
558 */
559int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
560{
561 struct cmd_ds_802_11_rf_tx_power cmd;
562 int ret;
876c9d3a 563
87c8c72d 564 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 565
87c8c72d
DW
566 memset(&cmd, 0, sizeof(cmd));
567 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
568 cmd.action = cpu_to_le16(CMD_ACT_SET);
569 cmd.curlevel = cpu_to_le16(dbm);
876c9d3a 570
87c8c72d
DW
571 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
572
573 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
9012b28a
HS
574
575 lbs_deb_leave(LBS_DEB_CMD);
87c8c72d 576 return ret;
876c9d3a
MT
577}
578
e98a88dd 579static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
965f8bbc
LCC
580 u16 cmd_action, void *pdata_buf)
581{
582 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
583
584 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
585 cmd->size =
586 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
8ec97cc8 587 sizeof(struct cmd_header));
965f8bbc
LCC
588
589 monitor->action = cpu_to_le16(cmd_action);
590 if (cmd_action == CMD_ACT_SET) {
591 monitor->mode =
592 cpu_to_le16((u16) (*(u32 *) pdata_buf));
593 }
594
595 return 0;
596}
597
2dd4b262
DW
598/**
599 * @brief Get the radio channel
600 *
601 * @param priv A pointer to struct lbs_private structure
602 *
603 * @return The channel on success, error on failure
604 */
a3cbfb08 605static int lbs_get_channel(struct lbs_private *priv)
876c9d3a 606{
2dd4b262
DW
607 struct cmd_ds_802_11_rf_channel cmd;
608 int ret = 0;
876c9d3a 609
8ff12da1 610 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 611
8d0c7fad 612 memset(&cmd, 0, sizeof(cmd));
2dd4b262
DW
613 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
614 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
876c9d3a 615
689442dc 616 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
2dd4b262
DW
617 if (ret)
618 goto out;
876c9d3a 619
cb182a60
DW
620 ret = le16_to_cpu(cmd.channel);
621 lbs_deb_cmd("current radio channel is %d\n", ret);
2dd4b262
DW
622
623out:
624 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
625 return ret;
626}
627
73ab1f25
HS
628int lbs_update_channel(struct lbs_private *priv)
629{
630 int ret;
631
632 /* the channel in f/w could be out of sync; get the current channel */
633 lbs_deb_enter(LBS_DEB_ASSOC);
634
635 ret = lbs_get_channel(priv);
636 if (ret > 0) {
c14951fe 637 priv->channel = ret;
73ab1f25
HS
638 ret = 0;
639 }
640 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
641 return ret;
642}
643
2dd4b262
DW
644/**
645 * @brief Set the radio channel
646 *
647 * @param priv A pointer to struct lbs_private structure
648 * @param channel The desired channel, or 0 to clear a locked channel
649 *
650 * @return 0 on success, error on failure
651 */
652int lbs_set_channel(struct lbs_private *priv, u8 channel)
653{
654 struct cmd_ds_802_11_rf_channel cmd;
96d46d5d 655#ifdef DEBUG
c14951fe 656 u8 old_channel = priv->channel;
96d46d5d 657#endif
2dd4b262
DW
658 int ret = 0;
659
660 lbs_deb_enter(LBS_DEB_CMD);
661
8d0c7fad 662 memset(&cmd, 0, sizeof(cmd));
2dd4b262
DW
663 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
664 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
665 cmd.channel = cpu_to_le16(channel);
666
689442dc 667 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
2dd4b262
DW
668 if (ret)
669 goto out;
670
c14951fe 671 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
cb182a60 672 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
c14951fe 673 priv->channel);
2dd4b262
DW
674
675out:
676 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
677 return ret;
876c9d3a
MT
678}
679
e98a88dd 680static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
876c9d3a
MT
681 u8 cmd_action, void *pdata_buf)
682{
10078321 683 struct lbs_offset_value *offval;
876c9d3a 684
9012b28a 685 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 686
10078321 687 offval = (struct lbs_offset_value *)pdata_buf;
876c9d3a 688
c2df2efe 689 switch (le16_to_cpu(cmdptr->command)) {
0aef64d7 690 case CMD_MAC_REG_ACCESS:
876c9d3a
MT
691 {
692 struct cmd_ds_mac_reg_access *macreg;
693
694 cmdptr->size =
981f187b 695 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
8ec97cc8 696 + sizeof(struct cmd_header));
876c9d3a
MT
697 macreg =
698 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
699 macreg;
700
701 macreg->action = cpu_to_le16(cmd_action);
702 macreg->offset = cpu_to_le16((u16) offval->offset);
703 macreg->value = cpu_to_le32(offval->value);
704
705 break;
706 }
707
0aef64d7 708 case CMD_BBP_REG_ACCESS:
876c9d3a
MT
709 {
710 struct cmd_ds_bbp_reg_access *bbpreg;
711
712 cmdptr->size =
713 cpu_to_le16(sizeof
714 (struct cmd_ds_bbp_reg_access)
8ec97cc8 715 + sizeof(struct cmd_header));
876c9d3a
MT
716 bbpreg =
717 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
718 bbpreg;
719
720 bbpreg->action = cpu_to_le16(cmd_action);
721 bbpreg->offset = cpu_to_le16((u16) offval->offset);
722 bbpreg->value = (u8) offval->value;
723
724 break;
725 }
726
0aef64d7 727 case CMD_RF_REG_ACCESS:
876c9d3a
MT
728 {
729 struct cmd_ds_rf_reg_access *rfreg;
730
731 cmdptr->size =
732 cpu_to_le16(sizeof
733 (struct cmd_ds_rf_reg_access) +
8ec97cc8 734 sizeof(struct cmd_header));
876c9d3a
MT
735 rfreg =
736 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
737 rfreg;
738
739 rfreg->action = cpu_to_le16(cmd_action);
740 rfreg->offset = cpu_to_le16((u16) offval->offset);
741 rfreg->value = (u8) offval->value;
742
743 break;
744 }
745
746 default:
747 break;
748 }
749
9012b28a 750 lbs_deb_leave(LBS_DEB_CMD);
876c9d3a
MT
751 return 0;
752}
753
681ffbb7
DW
754static void lbs_queue_cmd(struct lbs_private *priv,
755 struct cmd_ctrl_node *cmdnode)
876c9d3a
MT
756{
757 unsigned long flags;
681ffbb7 758 int addtail = 1;
876c9d3a 759
8ff12da1 760 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a 761
c4ab4127
DW
762 if (!cmdnode) {
763 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
876c9d3a
MT
764 goto done;
765 }
d9896ee1
DW
766 if (!cmdnode->cmdbuf->size) {
767 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
768 goto done;
769 }
ae125bf8 770 cmdnode->result = 0;
876c9d3a
MT
771
772 /* Exit_PS command needs to be queued in the header always. */
ddac4526 773 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
38bfab1a 774 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
ddac4526 775
0aef64d7 776 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
aa21c004 777 if (priv->psstate != PS_STATE_FULL_POWER)
876c9d3a
MT
778 addtail = 0;
779 }
780 }
781
66fceb69
AK
782 if (le16_to_cpu(cmdnode->cmdbuf->command) ==
783 CMD_802_11_WAKEUP_CONFIRM)
784 addtail = 0;
785
aa21c004 786 spin_lock_irqsave(&priv->driver_lock, flags);
876c9d3a 787
ac47246e 788 if (addtail)
aa21c004 789 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
ac47246e 790 else
aa21c004 791 list_add(&cmdnode->list, &priv->cmdpendingq);
876c9d3a 792
aa21c004 793 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a 794
8ff12da1 795 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
c4ab4127 796 le16_to_cpu(cmdnode->cmdbuf->command));
876c9d3a
MT
797
798done:
8ff12da1 799 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
800}
801
18c52e7c
DW
802static void lbs_submit_command(struct lbs_private *priv,
803 struct cmd_ctrl_node *cmdnode)
876c9d3a
MT
804{
805 unsigned long flags;
ddac4526 806 struct cmd_header *cmd;
18c52e7c
DW
807 uint16_t cmdsize;
808 uint16_t command;
57962f0b 809 int timeo = 3 * HZ;
18c52e7c 810 int ret;
876c9d3a 811
8ff12da1 812 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a 813
ddac4526 814 cmd = cmdnode->cmdbuf;
876c9d3a 815
aa21c004 816 spin_lock_irqsave(&priv->driver_lock, flags);
aa21c004
DW
817 priv->cur_cmd = cmdnode;
818 priv->cur_cmd_retcode = 0;
819 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a 820
ddac4526
DW
821 cmdsize = le16_to_cpu(cmd->size);
822 command = le16_to_cpu(cmd->command);
876c9d3a 823
18c52e7c 824 /* These commands take longer */
be0d76e4 825 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
57962f0b 826 timeo = 5 * HZ;
18c52e7c 827
e5225b39
HS
828 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
829 command, le16_to_cpu(cmd->seqnum), cmdsize);
1afc09ab 830 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
8ff12da1 831
ddac4526 832 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
18c52e7c 833
d9896ee1
DW
834 if (ret) {
835 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
18c52e7c
DW
836 /* Let the timer kick in and retry, and potentially reset
837 the whole thing if the condition persists */
57962f0b 838 timeo = HZ/4;
1afc09ab 839 }
876c9d3a 840
49125454
AK
841 if (command == CMD_802_11_DEEP_SLEEP) {
842 if (priv->is_auto_deep_sleep_enabled) {
843 priv->wakeup_dev_required = 1;
844 priv->dnld_sent = 0;
845 }
846 priv->is_deep_sleep = 1;
847 lbs_complete_command(priv, cmdnode, 0);
848 } else {
849 /* Setup the timer after transmit command */
850 mod_timer(&priv->command_timer, jiffies + timeo);
851 }
876c9d3a 852
18c52e7c 853 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
854}
855
876c9d3a
MT
856/**
857 * This function inserts command node to cmdfreeq
aa21c004 858 * after cleans it. Requires priv->driver_lock held.
876c9d3a 859 */
183aeac1 860static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
5ba2f8a0 861 struct cmd_ctrl_node *cmdnode)
876c9d3a 862{
5ba2f8a0
DW
863 lbs_deb_enter(LBS_DEB_HOST);
864
865 if (!cmdnode)
866 goto out;
867
5ba2f8a0
DW
868 cmdnode->callback = NULL;
869 cmdnode->callback_arg = 0;
876c9d3a 870
5ba2f8a0 871 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
876c9d3a 872
5ba2f8a0
DW
873 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
874 out:
875 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
876}
877
69f9032d
HS
878static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
879 struct cmd_ctrl_node *ptempcmd)
876c9d3a
MT
880{
881 unsigned long flags;
882
aa21c004 883 spin_lock_irqsave(&priv->driver_lock, flags);
10078321 884 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
aa21c004 885 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a
MT
886}
887
183aeac1
DW
888void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
889 int result)
890{
891 if (cmd == priv->cur_cmd)
892 priv->cur_cmd_retcode = result;
5ba2f8a0 893
ae125bf8 894 cmd->result = result;
5ba2f8a0
DW
895 cmd->cmdwaitqwoken = 1;
896 wake_up_interruptible(&cmd->cmdwait_q);
897
8db4a2b9 898 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
ad12d0f4 899 __lbs_cleanup_and_insert_cmd(priv, cmd);
183aeac1
DW
900 priv->cur_cmd = NULL;
901}
902
d5db2dfa 903int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
876c9d3a 904{
a7c45890 905 struct cmd_ds_802_11_radio_control cmd;
d5db2dfa 906 int ret = -EINVAL;
876c9d3a 907
9012b28a 908 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 909
a7c45890
DW
910 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
911 cmd.action = cpu_to_le16(CMD_ACT_SET);
912
d5db2dfa
DW
913 /* Only v8 and below support setting the preamble */
914 if (priv->fwrelease < 0x09000000) {
915 switch (preamble) {
916 case RADIO_PREAMBLE_SHORT:
d5db2dfa
DW
917 case RADIO_PREAMBLE_AUTO:
918 case RADIO_PREAMBLE_LONG:
919 cmd.control = cpu_to_le16(preamble);
920 break;
921 default:
922 goto out;
923 }
924 }
a7c45890 925
d5db2dfa
DW
926 if (radio_on)
927 cmd.control |= cpu_to_le16(0x1);
928 else {
929 cmd.control &= cpu_to_le16(~0x1);
930 priv->txpower_cur = 0;
a7c45890 931 }
876c9d3a 932
d5db2dfa
DW
933 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
934 radio_on ? "ON" : "OFF", preamble);
a7c45890 935
d5db2dfa 936 priv->radio_on = radio_on;
a7c45890
DW
937
938 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
876c9d3a 939
d5db2dfa 940out:
9012b28a 941 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
876c9d3a
MT
942 return ret;
943}
944
c97329e2 945void lbs_set_mac_control(struct lbs_private *priv)
876c9d3a 946{
835d3ac5 947 struct cmd_ds_mac_control cmd;
876c9d3a 948
9012b28a 949 lbs_deb_enter(LBS_DEB_CMD);
876c9d3a 950
835d3ac5 951 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
d9e9778c 952 cmd.action = cpu_to_le16(priv->mac_control);
835d3ac5
HS
953 cmd.reserved = 0;
954
75bf45a7 955 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
876c9d3a 956
c97329e2 957 lbs_deb_leave(LBS_DEB_CMD);
876c9d3a
MT
958}
959
1047d5ed
KD
960/**
961 * @brief This function implements command CMD_802_11D_DOMAIN_INFO
962 * @param priv pointer to struct lbs_private
963 * @param cmd pointer to cmd buffer
964 * @param cmdno cmd ID
965 * @param cmdOption cmd action
966 * @return 0
967*/
968int lbs_cmd_802_11d_domain_info(struct lbs_private *priv,
969 struct cmd_ds_command *cmd,
970 u16 cmdoption)
971{
972 struct cmd_ds_802_11d_domain_info *pdomaininfo =
973 &cmd->params.domaininfo;
974 struct mrvl_ie_domain_param_set *domain = &pdomaininfo->domain;
975 u8 nr_triplet = priv->domain_reg.no_triplet;
976
977 lbs_deb_enter(LBS_DEB_11D);
978
979 lbs_deb_11d("nr_triplet=%x\n", nr_triplet);
980
981 pdomaininfo->action = cpu_to_le16(cmdoption);
982 if (cmdoption == CMD_ACT_GET) {
983 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
984 sizeof(struct cmd_header));
985 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
986 le16_to_cpu(cmd->size));
987 goto done;
988 }
989
990 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
991 memcpy(domain->countrycode, priv->domain_reg.country_code,
992 sizeof(domain->countrycode));
993
994 domain->header.len = cpu_to_le16(nr_triplet
995 * sizeof(struct ieee80211_country_ie_triplet)
996 + sizeof(domain->countrycode));
997
998 if (nr_triplet) {
999 memcpy(domain->triplet, priv->domain_reg.triplet,
1000 nr_triplet *
1001 sizeof(struct ieee80211_country_ie_triplet));
1002
1003 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1004 le16_to_cpu(domain->header.len) +
1005 sizeof(struct mrvl_ie_header) +
1006 sizeof(struct cmd_header));
1007 } else {
1008 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1009 sizeof(struct cmd_header));
1010 }
1011
1012 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
1013 le16_to_cpu(cmd->size));
1014
1015done:
1016 lbs_deb_enter(LBS_DEB_11D);
1017 return 0;
1018}
1019
876c9d3a
MT
1020/**
1021 * @brief This function prepare the command before send to firmware.
1022 *
69f9032d 1023 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1024 * @param cmd_no command number
1025 * @param cmd_action command action: GET or SET
1026 * @param wait_option wait option: wait response or not
1027 * @param cmd_oid cmd oid: treated as sub command
1028 * @param pdata_buf A pointer to informaion buffer
1029 * @return 0 or -1
1030 */
69f9032d 1031int lbs_prepare_and_send_command(struct lbs_private *priv,
876c9d3a
MT
1032 u16 cmd_no,
1033 u16 cmd_action,
1034 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1035{
1036 int ret = 0;
876c9d3a
MT
1037 struct cmd_ctrl_node *cmdnode;
1038 struct cmd_ds_command *cmdptr;
1039 unsigned long flags;
1040
8ff12da1 1041 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a 1042
aa21c004
DW
1043 if (!priv) {
1044 lbs_deb_host("PREP_CMD: priv is NULL\n");
876c9d3a
MT
1045 ret = -1;
1046 goto done;
1047 }
1048
aa21c004 1049 if (priv->surpriseremoved) {
8ff12da1 1050 lbs_deb_host("PREP_CMD: card removed\n");
876c9d3a
MT
1051 ret = -1;
1052 goto done;
1053 }
1054
63f275df
AK
1055 if (!lbs_is_cmd_allowed(priv)) {
1056 ret = -EBUSY;
1057 goto done;
1058 }
1059
0d61d042 1060 cmdnode = lbs_get_cmd_ctrl_node(priv);
876c9d3a
MT
1061
1062 if (cmdnode == NULL) {
8ff12da1 1063 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
876c9d3a
MT
1064
1065 /* Wake up main thread to execute next command */
fe336150 1066 wake_up_interruptible(&priv->waitq);
876c9d3a
MT
1067 ret = -1;
1068 goto done;
1069 }
1070
e98a88dd
HS
1071 cmdnode->callback = NULL;
1072 cmdnode->callback_arg = (unsigned long)pdata_buf;
876c9d3a 1073
ddac4526 1074 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
876c9d3a 1075
8ff12da1 1076 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
876c9d3a 1077
876c9d3a 1078 /* Set sequence number, command and INT option */
aa21c004
DW
1079 priv->seqnum++;
1080 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
876c9d3a 1081
981f187b 1082 cmdptr->command = cpu_to_le16(cmd_no);
876c9d3a
MT
1083 cmdptr->result = 0;
1084
1085 switch (cmd_no) {
0aef64d7 1086 case CMD_802_11_PS_MODE:
e98a88dd 1087 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
876c9d3a
MT
1088 break;
1089
0aef64d7
DW
1090 case CMD_MAC_REG_ACCESS:
1091 case CMD_BBP_REG_ACCESS:
1092 case CMD_RF_REG_ACCESS:
e98a88dd 1093 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
876c9d3a
MT
1094 break;
1095
965f8bbc 1096 case CMD_802_11_MONITOR_MODE:
e98a88dd 1097 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
965f8bbc
LCC
1098 cmd_action, pdata_buf);
1099 break;
1100
0aef64d7 1101 case CMD_802_11_RSSI:
10078321 1102 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
876c9d3a
MT
1103 break;
1104
0aef64d7
DW
1105 case CMD_802_11_SET_AFC:
1106 case CMD_802_11_GET_AFC:
876c9d3a
MT
1107
1108 cmdptr->command = cpu_to_le16(cmd_no);
981f187b 1109 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
8ec97cc8 1110 sizeof(struct cmd_header));
876c9d3a
MT
1111
1112 memmove(&cmdptr->params.afc,
1113 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1114
1115 ret = 0;
1116 goto done;
1117
1047d5ed
KD
1118 case CMD_802_11D_DOMAIN_INFO:
1119 cmdptr->command = cpu_to_le16(cmd_no);
1120 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr, cmd_action);
1121 break;
1122
0aef64d7
DW
1123 case CMD_802_11_TPC_CFG:
1124 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
876c9d3a
MT
1125 cmdptr->size =
1126 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
8ec97cc8 1127 sizeof(struct cmd_header));
876c9d3a
MT
1128
1129 memmove(&cmdptr->params.tpccfg,
1130 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1131
1132 ret = 0;
1133 break;
5844d12e 1134
4143a23d
HS
1135#ifdef CONFIG_LIBERTAS_MESH
1136
0aef64d7 1137 case CMD_BT_ACCESS:
e98a88dd 1138 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
876c9d3a
MT
1139 break;
1140
0aef64d7 1141 case CMD_FWT_ACCESS:
e98a88dd 1142 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
876c9d3a
MT
1143 break;
1144
4143a23d
HS
1145#endif
1146
96287ac4
BD
1147 case CMD_802_11_BEACON_CTRL:
1148 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1149 break;
49125454
AK
1150 case CMD_802_11_DEEP_SLEEP:
1151 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
8ec97cc8 1152 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
49125454 1153 break;
876c9d3a 1154 default:
e37fc6e1 1155 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
876c9d3a
MT
1156 ret = -1;
1157 break;
1158 }
1159
1160 /* return error, since the command preparation failed */
1161 if (ret != 0) {
8ff12da1 1162 lbs_deb_host("PREP_CMD: command preparation failed\n");
10078321 1163 lbs_cleanup_and_insert_cmd(priv, cmdnode);
876c9d3a
MT
1164 ret = -1;
1165 goto done;
1166 }
1167
1168 cmdnode->cmdwaitqwoken = 0;
1169
681ffbb7 1170 lbs_queue_cmd(priv, cmdnode);
fe336150 1171 wake_up_interruptible(&priv->waitq);
876c9d3a 1172
0aef64d7 1173 if (wait_option & CMD_OPTION_WAITFORRSP) {
8ff12da1 1174 lbs_deb_host("PREP_CMD: wait for response\n");
876c9d3a
MT
1175 might_sleep();
1176 wait_event_interruptible(cmdnode->cmdwait_q,
1177 cmdnode->cmdwaitqwoken);
1178 }
1179
aa21c004
DW
1180 spin_lock_irqsave(&priv->driver_lock, flags);
1181 if (priv->cur_cmd_retcode) {
8ff12da1 1182 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
aa21c004
DW
1183 priv->cur_cmd_retcode);
1184 priv->cur_cmd_retcode = 0;
876c9d3a
MT
1185 ret = -1;
1186 }
aa21c004 1187 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a
MT
1188
1189done:
8ff12da1 1190 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
876c9d3a
MT
1191 return ret;
1192}
1193
1194/**
1195 * @brief This function allocates the command buffer and link
1196 * it to command free queue.
1197 *
69f9032d 1198 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1199 * @return 0 or -1
1200 */
69f9032d 1201int lbs_allocate_cmd_buffer(struct lbs_private *priv)
876c9d3a
MT
1202{
1203 int ret = 0;
ddac4526 1204 u32 bufsize;
876c9d3a 1205 u32 i;
ddac4526 1206 struct cmd_ctrl_node *cmdarray;
876c9d3a 1207
8ff12da1 1208 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a 1209
ddac4526
DW
1210 /* Allocate and initialize the command array */
1211 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1212 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
8ff12da1 1213 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
876c9d3a
MT
1214 ret = -1;
1215 goto done;
1216 }
ddac4526 1217 priv->cmd_array = cmdarray;
876c9d3a 1218
ddac4526
DW
1219 /* Allocate and initialize each command buffer in the command array */
1220 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1221 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1222 if (!cmdarray[i].cmdbuf) {
8ff12da1 1223 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
876c9d3a
MT
1224 ret = -1;
1225 goto done;
1226 }
876c9d3a
MT
1227 }
1228
ddac4526
DW
1229 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1230 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1231 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
876c9d3a 1232 }
876c9d3a 1233 ret = 0;
9012b28a
HS
1234
1235done:
8ff12da1 1236 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
876c9d3a
MT
1237 return ret;
1238}
1239
1240/**
1241 * @brief This function frees the command buffer.
1242 *
69f9032d 1243 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1244 * @return 0 or -1
1245 */
69f9032d 1246int lbs_free_cmd_buffer(struct lbs_private *priv)
876c9d3a 1247{
ddac4526 1248 struct cmd_ctrl_node *cmdarray;
876c9d3a 1249 unsigned int i;
876c9d3a 1250
8ff12da1 1251 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a
MT
1252
1253 /* need to check if cmd array is allocated or not */
aa21c004 1254 if (priv->cmd_array == NULL) {
8ff12da1 1255 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
876c9d3a
MT
1256 goto done;
1257 }
1258
ddac4526 1259 cmdarray = priv->cmd_array;
876c9d3a
MT
1260
1261 /* Release shared memory buffers */
ddac4526
DW
1262 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1263 if (cmdarray[i].cmdbuf) {
1264 kfree(cmdarray[i].cmdbuf);
1265 cmdarray[i].cmdbuf = NULL;
876c9d3a
MT
1266 }
1267 }
1268
1269 /* Release cmd_ctrl_node */
aa21c004
DW
1270 if (priv->cmd_array) {
1271 kfree(priv->cmd_array);
1272 priv->cmd_array = NULL;
876c9d3a
MT
1273 }
1274
1275done:
8ff12da1 1276 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
1277 return 0;
1278}
1279
1280/**
1281 * @brief This function gets a free command node if available in
1282 * command free queue.
1283 *
69f9032d 1284 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1285 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1286 */
2fd6cfe3 1287static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
876c9d3a
MT
1288{
1289 struct cmd_ctrl_node *tempnode;
876c9d3a
MT
1290 unsigned long flags;
1291
8ff12da1
HS
1292 lbs_deb_enter(LBS_DEB_HOST);
1293
aa21c004 1294 if (!priv)
876c9d3a
MT
1295 return NULL;
1296
aa21c004 1297 spin_lock_irqsave(&priv->driver_lock, flags);
876c9d3a 1298
aa21c004
DW
1299 if (!list_empty(&priv->cmdfreeq)) {
1300 tempnode = list_first_entry(&priv->cmdfreeq,
abe3ed14
LZ
1301 struct cmd_ctrl_node, list);
1302 list_del(&tempnode->list);
876c9d3a 1303 } else {
8ff12da1 1304 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
876c9d3a
MT
1305 tempnode = NULL;
1306 }
1307
aa21c004 1308 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a 1309
8ff12da1 1310 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
1311 return tempnode;
1312}
1313
876c9d3a
MT
1314/**
1315 * @brief This function executes next command in command
877d0310 1316 * pending queue. It will put firmware back to PS mode
876c9d3a
MT
1317 * if applicable.
1318 *
69f9032d 1319 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1320 * @return 0 or -1
1321 */
69f9032d 1322int lbs_execute_next_command(struct lbs_private *priv)
876c9d3a 1323{
876c9d3a 1324 struct cmd_ctrl_node *cmdnode = NULL;
ddac4526 1325 struct cmd_header *cmd;
876c9d3a
MT
1326 unsigned long flags;
1327 int ret = 0;
1328
1afc09ab
HS
1329 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1330 * only caller to us is lbs_thread() and we get even when a
1331 * data packet is received */
8ff12da1 1332 lbs_deb_enter(LBS_DEB_THREAD);
876c9d3a 1333
aa21c004 1334 spin_lock_irqsave(&priv->driver_lock, flags);
876c9d3a 1335
aa21c004 1336 if (priv->cur_cmd) {
8ff12da1 1337 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
aa21c004 1338 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a
MT
1339 ret = -1;
1340 goto done;
1341 }
1342
aa21c004
DW
1343 if (!list_empty(&priv->cmdpendingq)) {
1344 cmdnode = list_first_entry(&priv->cmdpendingq,
abe3ed14 1345 struct cmd_ctrl_node, list);
876c9d3a
MT
1346 }
1347
aa21c004 1348 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a
MT
1349
1350 if (cmdnode) {
ddac4526 1351 cmd = cmdnode->cmdbuf;
876c9d3a 1352
ddac4526 1353 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
aa21c004
DW
1354 if ((priv->psstate == PS_STATE_SLEEP) ||
1355 (priv->psstate == PS_STATE_PRE_SLEEP)) {
8ff12da1
HS
1356 lbs_deb_host(
1357 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
ddac4526 1358 le16_to_cpu(cmd->command),
aa21c004 1359 priv->psstate);
876c9d3a
MT
1360 ret = -1;
1361 goto done;
1362 }
8ff12da1 1363 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
ddac4526
DW
1364 "0x%04x in psstate %d\n",
1365 le16_to_cpu(cmd->command), priv->psstate);
aa21c004 1366 } else if (priv->psstate != PS_STATE_FULL_POWER) {
876c9d3a
MT
1367 /*
1368 * 1. Non-PS command:
1369 * Queue it. set needtowakeup to TRUE if current state
10078321 1370 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
876c9d3a
MT
1371 * 2. PS command but not Exit_PS:
1372 * Ignore it.
1373 * 3. PS command Exit_PS:
1374 * Set needtowakeup to TRUE if current state is SLEEP,
1375 * otherwise send this command down to firmware
1376 * immediately.
1377 */
ddac4526 1378 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
876c9d3a
MT
1379 /* Prepare to send Exit PS,
1380 * this non PS command will be sent later */
aa21c004
DW
1381 if ((priv->psstate == PS_STATE_SLEEP)
1382 || (priv->psstate == PS_STATE_PRE_SLEEP)
876c9d3a
MT
1383 ) {
1384 /* w/ new scheme, it will not reach here.
1385 since it is blocked in main_thread. */
aa21c004 1386 priv->needtowakeup = 1;
876c9d3a 1387 } else
10078321 1388 lbs_ps_wakeup(priv, 0);
876c9d3a
MT
1389
1390 ret = 0;
1391 goto done;
1392 } else {
1393 /*
1394 * PS command. Ignore it if it is not Exit_PS.
1395 * otherwise send it down immediately.
1396 */
38bfab1a 1397 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
876c9d3a 1398
8ff12da1
HS
1399 lbs_deb_host(
1400 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
876c9d3a
MT
1401 psm->action);
1402 if (psm->action !=
0aef64d7 1403 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
8ff12da1
HS
1404 lbs_deb_host(
1405 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
abe3ed14 1406 list_del(&cmdnode->list);
183aeac1
DW
1407 spin_lock_irqsave(&priv->driver_lock, flags);
1408 lbs_complete_command(priv, cmdnode, 0);
1409 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a
MT
1410
1411 ret = 0;
1412 goto done;
1413 }
1414
aa21c004
DW
1415 if ((priv->psstate == PS_STATE_SLEEP) ||
1416 (priv->psstate == PS_STATE_PRE_SLEEP)) {
8ff12da1
HS
1417 lbs_deb_host(
1418 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
abe3ed14 1419 list_del(&cmdnode->list);
183aeac1
DW
1420 spin_lock_irqsave(&priv->driver_lock, flags);
1421 lbs_complete_command(priv, cmdnode, 0);
1422 spin_unlock_irqrestore(&priv->driver_lock, flags);
aa21c004 1423 priv->needtowakeup = 1;
876c9d3a
MT
1424
1425 ret = 0;
1426 goto done;
1427 }
1428
8ff12da1
HS
1429 lbs_deb_host(
1430 "EXEC_NEXT_CMD: sending EXIT_PS\n");
876c9d3a
MT
1431 }
1432 }
abe3ed14 1433 list_del(&cmdnode->list);
8ff12da1 1434 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
ddac4526 1435 le16_to_cpu(cmd->command));
d9896ee1 1436 lbs_submit_command(priv, cmdnode);
876c9d3a
MT
1437 } else {
1438 /*
1439 * check if in power save mode, if yes, put the device back
1440 * to PS mode
1441 */
e86dc1ca
KD
1442#ifdef TODO
1443 /*
1444 * This was the old code for libertas+wext. Someone that
1445 * understands this beast should re-code it in a sane way.
1446 *
1447 * I actually don't understand why this is related to WPA
1448 * and to connection status, shouldn't powering should be
1449 * independ of such things?
1450 */
aa21c004
DW
1451 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1452 (priv->psstate == PS_STATE_FULL_POWER) &&
1453 ((priv->connect_status == LBS_CONNECTED) ||
602114ae 1454 lbs_mesh_connected(priv))) {
aa21c004
DW
1455 if (priv->secinfo.WPAenabled ||
1456 priv->secinfo.WPA2enabled) {
876c9d3a 1457 /* check for valid WPA group keys */
aa21c004
DW
1458 if (priv->wpa_mcast_key.len ||
1459 priv->wpa_unicast_key.len) {
8ff12da1 1460 lbs_deb_host(
876c9d3a
MT
1461 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1462 " go back to PS_SLEEP");
10078321 1463 lbs_ps_sleep(priv, 0);
876c9d3a
MT
1464 }
1465 } else {
8ff12da1
HS
1466 lbs_deb_host(
1467 "EXEC_NEXT_CMD: cmdpendingq empty, "
1468 "go back to PS_SLEEP");
10078321 1469 lbs_ps_sleep(priv, 0);
876c9d3a
MT
1470 }
1471 }
e86dc1ca 1472#endif
876c9d3a
MT
1473 }
1474
1475 ret = 0;
1476done:
8ff12da1 1477 lbs_deb_leave(LBS_DEB_THREAD);
876c9d3a
MT
1478 return ret;
1479}
1480
f539f2ef 1481static void lbs_send_confirmsleep(struct lbs_private *priv)
876c9d3a
MT
1482{
1483 unsigned long flags;
f539f2ef 1484 int ret;
876c9d3a 1485
8ff12da1 1486 lbs_deb_enter(LBS_DEB_HOST);
f539f2ef
HS
1487 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1488 sizeof(confirm_sleep));
876c9d3a 1489
f539f2ef
HS
1490 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1491 sizeof(confirm_sleep));
876c9d3a 1492 if (ret) {
f539f2ef 1493 lbs_pr_alert("confirm_sleep failed\n");
7919b89c 1494 goto out;
876c9d3a 1495 }
7919b89c
HS
1496
1497 spin_lock_irqsave(&priv->driver_lock, flags);
1498
a01f5450
HS
1499 /* We don't get a response on the sleep-confirmation */
1500 priv->dnld_sent = DNLD_RES_RECEIVED;
1501
66fceb69
AK
1502 if (priv->is_host_sleep_configured) {
1503 priv->is_host_sleep_activated = 1;
1504 wake_up_interruptible(&priv->host_sleep_q);
1505 }
1506
7919b89c 1507 /* If nothing to do, go back to sleep (?) */
e64c026d 1508 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
7919b89c
HS
1509 priv->psstate = PS_STATE_SLEEP;
1510
1511 spin_unlock_irqrestore(&priv->driver_lock, flags);
1512
1513out:
f539f2ef 1514 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
1515}
1516
69f9032d 1517void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
876c9d3a 1518{
8ff12da1 1519 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a
MT
1520
1521 /*
1522 * PS is currently supported only in Infrastructure mode
1523 * Remove this check if it is to be supported in IBSS mode also
1524 */
1525
10078321 1526 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
0aef64d7 1527 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
876c9d3a 1528
8ff12da1 1529 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
1530}
1531
1532/**
8ff12da1 1533 * @brief This function sends Exit_PS command to firmware.
876c9d3a 1534 *
69f9032d 1535 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1536 * @param wait_option wait response or not
1537 * @return n/a
1538 */
69f9032d 1539void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
876c9d3a 1540{
981f187b 1541 __le32 Localpsmode;
876c9d3a 1542
8ff12da1 1543 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a 1544
10078321 1545 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
876c9d3a 1546
10078321 1547 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
0aef64d7 1548 CMD_SUBCMD_EXIT_PS,
876c9d3a
MT
1549 wait_option, 0, &Localpsmode);
1550
8ff12da1 1551 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a
MT
1552}
1553
1554/**
1555 * @brief This function checks condition and prepares to
1556 * send sleep confirm command to firmware if ok.
1557 *
69f9032d 1558 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1559 * @param psmode Power Saving mode
1560 * @return n/a
1561 */
d4ff0ef6 1562void lbs_ps_confirm_sleep(struct lbs_private *priv)
876c9d3a
MT
1563{
1564 unsigned long flags =0;
d4ff0ef6 1565 int allowed = 1;
876c9d3a 1566
8ff12da1 1567 lbs_deb_enter(LBS_DEB_HOST);
876c9d3a 1568
a01f5450 1569 spin_lock_irqsave(&priv->driver_lock, flags);
634b8f49 1570 if (priv->dnld_sent) {
876c9d3a 1571 allowed = 0;
23d36eec 1572 lbs_deb_host("dnld_sent was set\n");
876c9d3a
MT
1573 }
1574
7919b89c 1575 /* In-progress command? */
aa21c004 1576 if (priv->cur_cmd) {
876c9d3a 1577 allowed = 0;
23d36eec 1578 lbs_deb_host("cur_cmd was set\n");
876c9d3a 1579 }
7919b89c
HS
1580
1581 /* Pending events or command responses? */
e64c026d 1582 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
876c9d3a 1583 allowed = 0;
7919b89c 1584 lbs_deb_host("pending events or command responses\n");
876c9d3a 1585 }
aa21c004 1586 spin_unlock_irqrestore(&priv->driver_lock, flags);
876c9d3a
MT
1587
1588 if (allowed) {
10078321 1589 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
f539f2ef 1590 lbs_send_confirmsleep(priv);
876c9d3a 1591 } else {
8ff12da1 1592 lbs_deb_host("sleep confirm has been delayed\n");
876c9d3a
MT
1593 }
1594
8ff12da1 1595 lbs_deb_leave(LBS_DEB_HOST);
876c9d3a 1596}
675787e2
HS
1597
1598
0112c9e9
AN
1599/**
1600 * @brief Configures the transmission power control functionality.
1601 *
1602 * @param priv A pointer to struct lbs_private structure
1603 * @param enable Transmission power control enable
1604 * @param p0 Power level when link quality is good (dBm).
1605 * @param p1 Power level when link quality is fair (dBm).
1606 * @param p2 Power level when link quality is poor (dBm).
1607 * @param usesnr Use Signal to Noise Ratio in TPC
1608 *
1609 * @return 0 on success
1610 */
1611int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1612 int8_t p2, int usesnr)
1613{
1614 struct cmd_ds_802_11_tpc_cfg cmd;
1615 int ret;
1616
1617 memset(&cmd, 0, sizeof(cmd));
1618 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1619 cmd.action = cpu_to_le16(CMD_ACT_SET);
1620 cmd.enable = !!enable;
3ed6e080 1621 cmd.usesnr = !!usesnr;
0112c9e9
AN
1622 cmd.P0 = p0;
1623 cmd.P1 = p1;
1624 cmd.P2 = p2;
1625
1626 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1627
1628 return ret;
1629}
1630
1631/**
1632 * @brief Configures the power adaptation settings.
1633 *
1634 * @param priv A pointer to struct lbs_private structure
1635 * @param enable Power adaptation enable
1636 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1637 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1638 * @param p2 Power level for 48 and 54 Mbps (dBm).
1639 *
1640 * @return 0 on Success
1641 */
1642
1643int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1644 int8_t p1, int8_t p2)
1645{
1646 struct cmd_ds_802_11_pa_cfg cmd;
1647 int ret;
1648
1649 memset(&cmd, 0, sizeof(cmd));
1650 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1651 cmd.action = cpu_to_le16(CMD_ACT_SET);
1652 cmd.enable = !!enable;
1653 cmd.P0 = p0;
1654 cmd.P1 = p1;
1655 cmd.P2 = p2;
1656
1657 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1658
1659 return ret;
1660}
1661
1662
6d898b19 1663struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
8db4a2b9
HS
1664 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1665 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1666 unsigned long callback_arg)
675787e2 1667{
675787e2 1668 struct cmd_ctrl_node *cmdnode;
675787e2
HS
1669
1670 lbs_deb_enter(LBS_DEB_HOST);
675787e2 1671
aa21c004 1672 if (priv->surpriseremoved) {
675787e2 1673 lbs_deb_host("PREP_CMD: card removed\n");
3399ea5f 1674 cmdnode = ERR_PTR(-ENOENT);
675787e2
HS
1675 goto done;
1676 }
1677
63f275df
AK
1678 if (!lbs_is_cmd_allowed(priv)) {
1679 cmdnode = ERR_PTR(-EBUSY);
1680 goto done;
1681 }
1682
675787e2 1683 cmdnode = lbs_get_cmd_ctrl_node(priv);
675787e2
HS
1684 if (cmdnode == NULL) {
1685 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1686
1687 /* Wake up main thread to execute next command */
1688 wake_up_interruptible(&priv->waitq);
3399ea5f 1689 cmdnode = ERR_PTR(-ENOBUFS);
675787e2
HS
1690 goto done;
1691 }
1692
448a51ae 1693 cmdnode->callback = callback;
1309b55b 1694 cmdnode->callback_arg = callback_arg;
675787e2 1695
7ad994de 1696 /* Copy the incoming command to the buffer */
ddac4526 1697 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
7ad994de 1698
675787e2 1699 /* Set sequence number, clean result, move to buffer */
aa21c004 1700 priv->seqnum++;
ddac4526
DW
1701 cmdnode->cmdbuf->command = cpu_to_le16(command);
1702 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1703 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1704 cmdnode->cmdbuf->result = 0;
675787e2
HS
1705
1706 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1707
675787e2 1708 cmdnode->cmdwaitqwoken = 0;
681ffbb7 1709 lbs_queue_cmd(priv, cmdnode);
675787e2
HS
1710 wake_up_interruptible(&priv->waitq);
1711
3399ea5f
DW
1712 done:
1713 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1714 return cmdnode;
1715}
1716
8db4a2b9
HS
1717void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1718 struct cmd_header *in_cmd, int in_cmd_size)
1719{
1720 lbs_deb_enter(LBS_DEB_CMD);
1721 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1722 lbs_cmd_async_callback, 0);
1723 lbs_deb_leave(LBS_DEB_CMD);
1724}
1725
3399ea5f
DW
1726int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1727 struct cmd_header *in_cmd, int in_cmd_size,
1728 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1729 unsigned long callback_arg)
1730{
1731 struct cmd_ctrl_node *cmdnode;
1732 unsigned long flags;
1733 int ret = 0;
1734
1735 lbs_deb_enter(LBS_DEB_HOST);
1736
1737 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1738 callback, callback_arg);
1739 if (IS_ERR(cmdnode)) {
1740 ret = PTR_ERR(cmdnode);
1741 goto done;
1742 }
1743
675787e2
HS
1744 might_sleep();
1745 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1746
aa21c004 1747 spin_lock_irqsave(&priv->driver_lock, flags);
ae125bf8
DW
1748 ret = cmdnode->result;
1749 if (ret)
1750 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1751 command, ret);
3399ea5f 1752
ad12d0f4 1753 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
aa21c004 1754 spin_unlock_irqrestore(&priv->driver_lock, flags);
675787e2
HS
1755
1756done:
1757 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1758 return ret;
1759}
14e865ba 1760EXPORT_SYMBOL_GPL(__lbs_cmd);