mm-only debug patch...
[mmotm.git] / drivers / net / wireless / libertas / cmd.c
blob56175327c4b0ce4f8cc29836c85548b2213e8f02
1 /**
2 * This file contains the handling of command.
3 * It prepares command and sends it to firmware when it is ready.
4 */
6 #include <net/iw_handler.h>
7 #include <net/lib80211.h>
8 #include <linux/kfifo.h>
9 #include <linux/sched.h>
10 #include "host.h"
11 #include "hostcmd.h"
12 #include "decl.h"
13 #include "defs.h"
14 #include "dev.h"
15 #include "assoc.h"
16 #include "wext.h"
17 #include "cmd.h"
19 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
21 /**
22 * @brief Simple callback that copies response back into command
24 * @param priv A pointer to struct lbs_private structure
25 * @param extra A pointer to the original command structure for which
26 * 'resp' is a response
27 * @param resp A pointer to the command response
29 * @return 0 on success, error on failure
31 int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
32 struct cmd_header *resp)
34 struct cmd_header *buf = (void *)extra;
35 uint16_t copy_len;
37 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
38 memcpy(buf, resp, copy_len);
39 return 0;
41 EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
43 /**
44 * @brief Simple callback that ignores the result. Use this if
45 * you just want to send a command to the hardware, but don't
46 * care for the result.
48 * @param priv ignored
49 * @param extra ignored
50 * @param resp ignored
52 * @return 0 for success
54 static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
55 struct cmd_header *resp)
57 return 0;
61 /**
62 * @brief Checks whether a command is allowed in Power Save mode
64 * @param command the command ID
65 * @return 1 if allowed, 0 if not allowed
67 static u8 is_command_allowed_in_ps(u16 cmd)
69 switch (cmd) {
70 case CMD_802_11_RSSI:
71 return 1;
72 default:
73 break;
75 return 0;
78 /**
79 * @brief This function checks if the command is allowed.
81 * @param priv A pointer to lbs_private structure
82 * @return allowed or not allowed.
85 static int lbs_is_cmd_allowed(struct lbs_private *priv)
87 int ret = 1;
89 lbs_deb_enter(LBS_DEB_CMD);
91 if (!priv->is_auto_deep_sleep_enabled) {
92 if (priv->is_deep_sleep) {
93 lbs_deb_cmd("command not allowed in deep sleep\n");
94 ret = 0;
98 lbs_deb_leave(LBS_DEB_CMD);
99 return ret;
103 * @brief Updates the hardware details like MAC address and regulatory region
105 * @param priv A pointer to struct lbs_private structure
107 * @return 0 on success, error on failure
109 int lbs_update_hw_spec(struct lbs_private *priv)
111 struct cmd_ds_get_hw_spec cmd;
112 int ret = -1;
113 u32 i;
115 lbs_deb_enter(LBS_DEB_CMD);
117 memset(&cmd, 0, sizeof(cmd));
118 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
119 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
120 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
121 if (ret)
122 goto out;
124 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
126 /* The firmware release is in an interesting format: the patch
127 * level is in the most significant nibble ... so fix that: */
128 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
129 priv->fwrelease = (priv->fwrelease << 8) |
130 (priv->fwrelease >> 24 & 0xff);
132 /* Some firmware capabilities:
133 * CF card firmware 5.0.16p0: cap 0x00000303
134 * USB dongle firmware 5.110.17p2: cap 0x00000303
136 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
137 cmd.permanentaddr,
138 priv->fwrelease >> 24 & 0xff,
139 priv->fwrelease >> 16 & 0xff,
140 priv->fwrelease >> 8 & 0xff,
141 priv->fwrelease & 0xff,
142 priv->fwcapinfo);
143 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
144 cmd.hwifversion, cmd.version);
146 /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
147 /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
148 /* 5.110.22 have mesh command with 0xa3 command id */
149 /* 10.0.0.p0 FW brings in mesh config command with different id */
150 /* Check FW version MSB and initialize mesh_fw_ver */
151 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
152 priv->mesh_fw_ver = MESH_FW_OLD;
153 else if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
154 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK))
155 priv->mesh_fw_ver = MESH_FW_NEW;
156 else
157 priv->mesh_fw_ver = MESH_NONE;
159 /* Clamp region code to 8-bit since FW spec indicates that it should
160 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
161 * returns non-zero high 8 bits here.
163 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
164 * need to check for this problem and handle it properly.
166 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
167 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
168 else
169 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
171 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
172 /* use the region code to search for the index */
173 if (priv->regioncode == lbs_region_code_to_index[i])
174 break;
177 /* if it's unidentified region code, use the default (USA) */
178 if (i >= MRVDRV_MAX_REGION_CODE) {
179 priv->regioncode = 0x10;
180 lbs_pr_info("unidentified region code; using the default (USA)\n");
183 if (priv->current_addr[0] == 0xff)
184 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
186 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
187 if (priv->mesh_dev)
188 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
190 if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
191 ret = -1;
192 goto out;
195 if (lbs_set_universaltable(priv, 0)) {
196 ret = -1;
197 goto out;
200 out:
201 lbs_deb_leave(LBS_DEB_CMD);
202 return ret;
205 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
206 struct wol_config *p_wol_config)
208 struct cmd_ds_host_sleep cmd_config;
209 int ret;
211 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
212 cmd_config.criteria = cpu_to_le32(criteria);
213 cmd_config.gpio = priv->wol_gpio;
214 cmd_config.gap = priv->wol_gap;
216 if (p_wol_config != NULL)
217 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
218 sizeof(struct wol_config));
219 else
220 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
222 ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
223 if (!ret) {
224 if (criteria) {
225 lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
226 priv->wol_criteria = criteria;
227 } else
228 memcpy((uint8_t *) p_wol_config,
229 (uint8_t *)&cmd_config.wol_conf,
230 sizeof(struct wol_config));
231 } else {
232 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
235 return ret;
237 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
239 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
240 u16 cmd_action)
242 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
244 lbs_deb_enter(LBS_DEB_CMD);
246 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
247 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
248 S_DS_GEN);
249 psm->action = cpu_to_le16(cmd_action);
250 psm->multipledtim = 0;
251 switch (cmd_action) {
252 case CMD_SUBCMD_ENTER_PS:
253 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
255 psm->locallisteninterval = 0;
256 psm->nullpktinterval = 0;
257 psm->multipledtim =
258 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
259 break;
261 case CMD_SUBCMD_EXIT_PS:
262 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
263 break;
265 case CMD_SUBCMD_SLEEP_CONFIRMED:
266 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
267 break;
269 default:
270 break;
273 lbs_deb_leave(LBS_DEB_CMD);
274 return 0;
277 int lbs_cmd_802_11_inactivity_timeout(struct lbs_private *priv,
278 uint16_t cmd_action, uint16_t *timeout)
280 struct cmd_ds_802_11_inactivity_timeout cmd;
281 int ret;
283 lbs_deb_enter(LBS_DEB_CMD);
285 cmd.hdr.command = cpu_to_le16(CMD_802_11_INACTIVITY_TIMEOUT);
286 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
288 cmd.action = cpu_to_le16(cmd_action);
290 if (cmd_action == CMD_ACT_SET)
291 cmd.timeout = cpu_to_le16(*timeout);
292 else
293 cmd.timeout = 0;
295 ret = lbs_cmd_with_response(priv, CMD_802_11_INACTIVITY_TIMEOUT, &cmd);
297 if (!ret)
298 *timeout = le16_to_cpu(cmd.timeout);
300 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
301 return 0;
304 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
305 struct sleep_params *sp)
307 struct cmd_ds_802_11_sleep_params cmd;
308 int ret;
310 lbs_deb_enter(LBS_DEB_CMD);
312 if (cmd_action == CMD_ACT_GET) {
313 memset(&cmd, 0, sizeof(cmd));
314 } else {
315 cmd.error = cpu_to_le16(sp->sp_error);
316 cmd.offset = cpu_to_le16(sp->sp_offset);
317 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
318 cmd.calcontrol = sp->sp_calcontrol;
319 cmd.externalsleepclk = sp->sp_extsleepclk;
320 cmd.reserved = cpu_to_le16(sp->sp_reserved);
322 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
323 cmd.action = cpu_to_le16(cmd_action);
325 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
327 if (!ret) {
328 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
329 "calcontrol 0x%x extsleepclk 0x%x\n",
330 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
331 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
332 cmd.externalsleepclk);
334 sp->sp_error = le16_to_cpu(cmd.error);
335 sp->sp_offset = le16_to_cpu(cmd.offset);
336 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
337 sp->sp_calcontrol = cmd.calcontrol;
338 sp->sp_extsleepclk = cmd.externalsleepclk;
339 sp->sp_reserved = le16_to_cpu(cmd.reserved);
342 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
343 return 0;
346 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
348 int ret = 0;
350 lbs_deb_enter(LBS_DEB_CMD);
352 if (priv->is_deep_sleep) {
353 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
354 !priv->is_deep_sleep, (10 * HZ))) {
355 lbs_pr_err("ds_awake_q: timer expired\n");
356 ret = -1;
360 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
361 return ret;
364 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
366 int ret = 0;
368 lbs_deb_enter(LBS_DEB_CMD);
370 if (deep_sleep) {
371 if (priv->is_deep_sleep != 1) {
372 lbs_deb_cmd("deep sleep: sleep\n");
373 BUG_ON(!priv->enter_deep_sleep);
374 ret = priv->enter_deep_sleep(priv);
375 if (!ret) {
376 netif_stop_queue(priv->dev);
377 netif_carrier_off(priv->dev);
379 } else {
380 lbs_pr_err("deep sleep: already enabled\n");
382 } else {
383 if (priv->is_deep_sleep) {
384 lbs_deb_cmd("deep sleep: wakeup\n");
385 BUG_ON(!priv->exit_deep_sleep);
386 ret = priv->exit_deep_sleep(priv);
387 if (!ret) {
388 ret = lbs_wait_for_ds_awake(priv);
389 if (ret)
390 lbs_pr_err("deep sleep: wakeup"
391 "failed\n");
396 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
397 return ret;
400 int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
401 struct assoc_request *assoc)
403 struct cmd_ds_802_11_set_wep cmd;
404 int ret = 0;
406 lbs_deb_enter(LBS_DEB_CMD);
408 memset(&cmd, 0, sizeof(cmd));
409 cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
410 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
412 cmd.action = cpu_to_le16(cmd_action);
414 if (cmd_action == CMD_ACT_ADD) {
415 int i;
417 /* default tx key index */
418 cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
419 CMD_WEP_KEY_INDEX_MASK);
421 /* Copy key types and material to host command structure */
422 for (i = 0; i < 4; i++) {
423 struct enc_key *pkey = &assoc->wep_keys[i];
425 switch (pkey->len) {
426 case KEY_LEN_WEP_40:
427 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
428 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
429 lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
430 break;
431 case KEY_LEN_WEP_104:
432 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
433 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
434 lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
435 break;
436 case 0:
437 break;
438 default:
439 lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
440 i, pkey->len);
441 ret = -1;
442 goto done;
443 break;
446 } else if (cmd_action == CMD_ACT_REMOVE) {
447 /* ACT_REMOVE clears _all_ WEP keys */
449 /* default tx key index */
450 cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
451 CMD_WEP_KEY_INDEX_MASK);
452 lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
455 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
456 done:
457 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
458 return ret;
461 int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
462 uint16_t *enable)
464 struct cmd_ds_802_11_enable_rsn cmd;
465 int ret;
467 lbs_deb_enter(LBS_DEB_CMD);
469 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
470 cmd.action = cpu_to_le16(cmd_action);
472 if (cmd_action == CMD_ACT_GET)
473 cmd.enable = 0;
474 else {
475 if (*enable)
476 cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
477 else
478 cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
479 lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
482 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
483 if (!ret && cmd_action == CMD_ACT_GET)
484 *enable = le16_to_cpu(cmd.enable);
486 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
487 return ret;
490 static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
491 struct enc_key *key)
493 lbs_deb_enter(LBS_DEB_CMD);
495 if (key->flags & KEY_INFO_WPA_ENABLED)
496 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
497 if (key->flags & KEY_INFO_WPA_UNICAST)
498 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
499 if (key->flags & KEY_INFO_WPA_MCAST)
500 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
502 keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
503 keyparam->keytypeid = cpu_to_le16(key->type);
504 keyparam->keylen = cpu_to_le16(key->len);
505 memcpy(keyparam->key, key->key, key->len);
507 /* Length field doesn't include the {type,length} header */
508 keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
509 lbs_deb_leave(LBS_DEB_CMD);
512 int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
513 struct assoc_request *assoc)
515 struct cmd_ds_802_11_key_material cmd;
516 int ret = 0;
517 int index = 0;
519 lbs_deb_enter(LBS_DEB_CMD);
521 cmd.action = cpu_to_le16(cmd_action);
522 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
524 if (cmd_action == CMD_ACT_GET) {
525 cmd.hdr.size = cpu_to_le16(S_DS_GEN + 2);
526 } else {
527 memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
529 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
530 set_one_wpa_key(&cmd.keyParamSet[index],
531 &assoc->wpa_unicast_key);
532 index++;
535 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
536 set_one_wpa_key(&cmd.keyParamSet[index],
537 &assoc->wpa_mcast_key);
538 index++;
541 /* The common header and as many keys as we included */
542 cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
543 keyParamSet[index]));
545 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
546 /* Copy the returned key to driver private data */
547 if (!ret && cmd_action == CMD_ACT_GET) {
548 void *buf_ptr = cmd.keyParamSet;
549 void *resp_end = &(&cmd)[1];
551 while (buf_ptr < resp_end) {
552 struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
553 struct enc_key *key;
554 uint16_t param_set_len = le16_to_cpu(keyparam->length);
555 uint16_t key_len = le16_to_cpu(keyparam->keylen);
556 uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
557 uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
558 void *end;
560 end = (void *)keyparam + sizeof(keyparam->type)
561 + sizeof(keyparam->length) + param_set_len;
563 /* Make sure we don't access past the end of the IEs */
564 if (end > resp_end)
565 break;
567 if (key_flags & KEY_INFO_WPA_UNICAST)
568 key = &priv->wpa_unicast_key;
569 else if (key_flags & KEY_INFO_WPA_MCAST)
570 key = &priv->wpa_mcast_key;
571 else
572 break;
574 /* Copy returned key into driver */
575 memset(key, 0, sizeof(struct enc_key));
576 if (key_len > sizeof(key->key))
577 break;
578 key->type = key_type;
579 key->flags = key_flags;
580 key->len = key_len;
581 memcpy(key->key, keyparam->key, key->len);
583 buf_ptr = end + 1;
587 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
588 return ret;
592 * @brief Set an SNMP MIB value
594 * @param priv A pointer to struct lbs_private structure
595 * @param oid The OID to set in the firmware
596 * @param val Value to set the OID to
598 * @return 0 on success, error on failure
600 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
602 struct cmd_ds_802_11_snmp_mib cmd;
603 int ret;
605 lbs_deb_enter(LBS_DEB_CMD);
607 memset(&cmd, 0, sizeof (cmd));
608 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
609 cmd.action = cpu_to_le16(CMD_ACT_SET);
610 cmd.oid = cpu_to_le16((u16) oid);
612 switch (oid) {
613 case SNMP_MIB_OID_BSS_TYPE:
614 cmd.bufsize = cpu_to_le16(sizeof(u8));
615 cmd.value[0] = (val == IW_MODE_ADHOC) ? 2 : 1;
616 break;
617 case SNMP_MIB_OID_11D_ENABLE:
618 case SNMP_MIB_OID_FRAG_THRESHOLD:
619 case SNMP_MIB_OID_RTS_THRESHOLD:
620 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
621 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
622 cmd.bufsize = cpu_to_le16(sizeof(u16));
623 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
624 break;
625 default:
626 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
627 ret = -EINVAL;
628 goto out;
631 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
632 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
634 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
636 out:
637 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
638 return ret;
642 * @brief Get an SNMP MIB value
644 * @param priv A pointer to struct lbs_private structure
645 * @param oid The OID to retrieve from the firmware
646 * @param out_val Location for the returned value
648 * @return 0 on success, error on failure
650 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
652 struct cmd_ds_802_11_snmp_mib cmd;
653 int ret;
655 lbs_deb_enter(LBS_DEB_CMD);
657 memset(&cmd, 0, sizeof (cmd));
658 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
659 cmd.action = cpu_to_le16(CMD_ACT_GET);
660 cmd.oid = cpu_to_le16(oid);
662 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
663 if (ret)
664 goto out;
666 switch (le16_to_cpu(cmd.bufsize)) {
667 case sizeof(u8):
668 if (oid == SNMP_MIB_OID_BSS_TYPE) {
669 if (cmd.value[0] == 2)
670 *out_val = IW_MODE_ADHOC;
671 else
672 *out_val = IW_MODE_INFRA;
673 } else
674 *out_val = cmd.value[0];
675 break;
676 case sizeof(u16):
677 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
678 break;
679 default:
680 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
681 oid, le16_to_cpu(cmd.bufsize));
682 break;
685 out:
686 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
687 return ret;
691 * @brief Get the min, max, and current TX power
693 * @param priv A pointer to struct lbs_private structure
694 * @param curlevel Current power level in dBm
695 * @param minlevel Minimum supported power level in dBm (optional)
696 * @param maxlevel Maximum supported power level in dBm (optional)
698 * @return 0 on success, error on failure
700 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
701 s16 *maxlevel)
703 struct cmd_ds_802_11_rf_tx_power cmd;
704 int ret;
706 lbs_deb_enter(LBS_DEB_CMD);
708 memset(&cmd, 0, sizeof(cmd));
709 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
710 cmd.action = cpu_to_le16(CMD_ACT_GET);
712 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
713 if (ret == 0) {
714 *curlevel = le16_to_cpu(cmd.curlevel);
715 if (minlevel)
716 *minlevel = cmd.minlevel;
717 if (maxlevel)
718 *maxlevel = cmd.maxlevel;
721 lbs_deb_leave(LBS_DEB_CMD);
722 return ret;
726 * @brief Set the TX power
728 * @param priv A pointer to struct lbs_private structure
729 * @param dbm The desired power level in dBm
731 * @return 0 on success, error on failure
733 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
735 struct cmd_ds_802_11_rf_tx_power cmd;
736 int ret;
738 lbs_deb_enter(LBS_DEB_CMD);
740 memset(&cmd, 0, sizeof(cmd));
741 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
742 cmd.action = cpu_to_le16(CMD_ACT_SET);
743 cmd.curlevel = cpu_to_le16(dbm);
745 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
747 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
749 lbs_deb_leave(LBS_DEB_CMD);
750 return ret;
753 static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
754 u16 cmd_action, void *pdata_buf)
756 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
758 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
759 cmd->size =
760 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
761 S_DS_GEN);
763 monitor->action = cpu_to_le16(cmd_action);
764 if (cmd_action == CMD_ACT_SET) {
765 monitor->mode =
766 cpu_to_le16((u16) (*(u32 *) pdata_buf));
769 return 0;
772 static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
774 /* Bit Rate
775 * 15:13 Reserved
776 * 12 54 Mbps
777 * 11 48 Mbps
778 * 10 36 Mbps
779 * 9 24 Mbps
780 * 8 18 Mbps
781 * 7 12 Mbps
782 * 6 9 Mbps
783 * 5 6 Mbps
784 * 4 Reserved
785 * 3 11 Mbps
786 * 2 5.5 Mbps
787 * 1 2 Mbps
788 * 0 1 Mbps
791 uint16_t ratemask;
792 int i = lbs_data_rate_to_fw_index(rate);
793 if (lower_rates_ok)
794 ratemask = (0x1fef >> (12 - i));
795 else
796 ratemask = (1 << i);
797 return cpu_to_le16(ratemask);
800 int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
801 uint16_t cmd_action)
803 struct cmd_ds_802_11_rate_adapt_rateset cmd;
804 int ret;
806 lbs_deb_enter(LBS_DEB_CMD);
808 if (!priv->cur_rate && !priv->enablehwauto)
809 return -EINVAL;
811 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
813 cmd.action = cpu_to_le16(cmd_action);
814 cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
815 cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
816 ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
817 if (!ret && cmd_action == CMD_ACT_GET) {
818 priv->ratebitmap = le16_to_cpu(cmd.bitmap);
819 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
822 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
823 return ret;
825 EXPORT_SYMBOL_GPL(lbs_cmd_802_11_rate_adapt_rateset);
828 * @brief Set the data rate
830 * @param priv A pointer to struct lbs_private structure
831 * @param rate The desired data rate, or 0 to clear a locked rate
833 * @return 0 on success, error on failure
835 int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
837 struct cmd_ds_802_11_data_rate cmd;
838 int ret = 0;
840 lbs_deb_enter(LBS_DEB_CMD);
842 memset(&cmd, 0, sizeof(cmd));
843 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
845 if (rate > 0) {
846 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
847 cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
848 if (cmd.rates[0] == 0) {
849 lbs_deb_cmd("DATA_RATE: invalid requested rate of"
850 " 0x%02X\n", rate);
851 ret = 0;
852 goto out;
854 lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
855 } else {
856 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
857 lbs_deb_cmd("DATA_RATE: setting auto\n");
860 ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
861 if (ret)
862 goto out;
864 lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof (cmd));
866 /* FIXME: get actual rates FW can do if this command actually returns
867 * all data rates supported.
869 priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
870 lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
872 out:
873 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
874 return ret;
878 * @brief Get the radio channel
880 * @param priv A pointer to struct lbs_private structure
882 * @return The channel on success, error on failure
884 int lbs_get_channel(struct lbs_private *priv)
886 struct cmd_ds_802_11_rf_channel cmd;
887 int ret = 0;
889 lbs_deb_enter(LBS_DEB_CMD);
891 memset(&cmd, 0, sizeof(cmd));
892 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
893 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
895 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
896 if (ret)
897 goto out;
899 ret = le16_to_cpu(cmd.channel);
900 lbs_deb_cmd("current radio channel is %d\n", ret);
902 out:
903 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
904 return ret;
907 int lbs_update_channel(struct lbs_private *priv)
909 int ret;
911 /* the channel in f/w could be out of sync; get the current channel */
912 lbs_deb_enter(LBS_DEB_ASSOC);
914 ret = lbs_get_channel(priv);
915 if (ret > 0) {
916 priv->curbssparams.channel = ret;
917 ret = 0;
919 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
920 return ret;
924 * @brief Set the radio channel
926 * @param priv A pointer to struct lbs_private structure
927 * @param channel The desired channel, or 0 to clear a locked channel
929 * @return 0 on success, error on failure
931 int lbs_set_channel(struct lbs_private *priv, u8 channel)
933 struct cmd_ds_802_11_rf_channel cmd;
934 #ifdef DEBUG
935 u8 old_channel = priv->curbssparams.channel;
936 #endif
937 int ret = 0;
939 lbs_deb_enter(LBS_DEB_CMD);
941 memset(&cmd, 0, sizeof(cmd));
942 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
943 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
944 cmd.channel = cpu_to_le16(channel);
946 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
947 if (ret)
948 goto out;
950 priv->curbssparams.channel = (uint8_t) le16_to_cpu(cmd.channel);
951 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
952 priv->curbssparams.channel);
954 out:
955 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
956 return ret;
959 static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
960 struct cmd_ds_command *cmd)
963 lbs_deb_enter(LBS_DEB_CMD);
964 cmd->command = cpu_to_le16(CMD_802_11_RSSI);
965 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
966 cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
968 /* reset Beacon SNR/NF/RSSI values */
969 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
970 priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
971 priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
972 priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
973 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
974 priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
976 lbs_deb_leave(LBS_DEB_CMD);
977 return 0;
980 static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
981 u8 cmd_action, void *pdata_buf)
983 struct lbs_offset_value *offval;
985 lbs_deb_enter(LBS_DEB_CMD);
987 offval = (struct lbs_offset_value *)pdata_buf;
989 switch (le16_to_cpu(cmdptr->command)) {
990 case CMD_MAC_REG_ACCESS:
992 struct cmd_ds_mac_reg_access *macreg;
994 cmdptr->size =
995 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
996 + S_DS_GEN);
997 macreg =
998 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
999 macreg;
1001 macreg->action = cpu_to_le16(cmd_action);
1002 macreg->offset = cpu_to_le16((u16) offval->offset);
1003 macreg->value = cpu_to_le32(offval->value);
1005 break;
1008 case CMD_BBP_REG_ACCESS:
1010 struct cmd_ds_bbp_reg_access *bbpreg;
1012 cmdptr->size =
1013 cpu_to_le16(sizeof
1014 (struct cmd_ds_bbp_reg_access)
1015 + S_DS_GEN);
1016 bbpreg =
1017 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
1018 bbpreg;
1020 bbpreg->action = cpu_to_le16(cmd_action);
1021 bbpreg->offset = cpu_to_le16((u16) offval->offset);
1022 bbpreg->value = (u8) offval->value;
1024 break;
1027 case CMD_RF_REG_ACCESS:
1029 struct cmd_ds_rf_reg_access *rfreg;
1031 cmdptr->size =
1032 cpu_to_le16(sizeof
1033 (struct cmd_ds_rf_reg_access) +
1034 S_DS_GEN);
1035 rfreg =
1036 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
1037 rfreg;
1039 rfreg->action = cpu_to_le16(cmd_action);
1040 rfreg->offset = cpu_to_le16((u16) offval->offset);
1041 rfreg->value = (u8) offval->value;
1043 break;
1046 default:
1047 break;
1050 lbs_deb_leave(LBS_DEB_CMD);
1051 return 0;
1054 static int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
1055 u16 cmd_action, void *pdata_buf)
1057 struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
1058 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1060 cmd->command = cpu_to_le16(CMD_BT_ACCESS);
1061 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
1062 cmd->result = 0;
1063 bt_access->action = cpu_to_le16(cmd_action);
1065 switch (cmd_action) {
1066 case CMD_ACT_BT_ACCESS_ADD:
1067 memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
1068 lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
1069 break;
1070 case CMD_ACT_BT_ACCESS_DEL:
1071 memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
1072 lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
1073 break;
1074 case CMD_ACT_BT_ACCESS_LIST:
1075 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
1076 break;
1077 case CMD_ACT_BT_ACCESS_RESET:
1078 break;
1079 case CMD_ACT_BT_ACCESS_SET_INVERT:
1080 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
1081 break;
1082 case CMD_ACT_BT_ACCESS_GET_INVERT:
1083 break;
1084 default:
1085 break;
1087 lbs_deb_leave(LBS_DEB_CMD);
1088 return 0;
1091 static int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
1092 u16 cmd_action, void *pdata_buf)
1094 struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
1095 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1097 cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
1098 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
1099 cmd->result = 0;
1101 if (pdata_buf)
1102 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
1103 else
1104 memset(fwt_access, 0, sizeof(*fwt_access));
1106 fwt_access->action = cpu_to_le16(cmd_action);
1108 lbs_deb_leave(LBS_DEB_CMD);
1109 return 0;
1112 int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
1113 struct cmd_ds_mesh_access *cmd)
1115 int ret;
1117 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1119 cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
1120 cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
1121 cmd->hdr.result = 0;
1123 cmd->action = cpu_to_le16(cmd_action);
1125 ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
1127 lbs_deb_leave(LBS_DEB_CMD);
1128 return ret;
1131 static int __lbs_mesh_config_send(struct lbs_private *priv,
1132 struct cmd_ds_mesh_config *cmd,
1133 uint16_t action, uint16_t type)
1135 int ret;
1136 u16 command = CMD_MESH_CONFIG_OLD;
1138 lbs_deb_enter(LBS_DEB_CMD);
1141 * Command id is 0xac for v10 FW along with mesh interface
1142 * id in bits 14-13-12.
1144 if (priv->mesh_fw_ver == MESH_FW_NEW)
1145 command = CMD_MESH_CONFIG |
1146 (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
1148 cmd->hdr.command = cpu_to_le16(command);
1149 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
1150 cmd->hdr.result = 0;
1152 cmd->type = cpu_to_le16(type);
1153 cmd->action = cpu_to_le16(action);
1155 ret = lbs_cmd_with_response(priv, command, cmd);
1157 lbs_deb_leave(LBS_DEB_CMD);
1158 return ret;
1161 int lbs_mesh_config_send(struct lbs_private *priv,
1162 struct cmd_ds_mesh_config *cmd,
1163 uint16_t action, uint16_t type)
1165 int ret;
1167 if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
1168 return -EOPNOTSUPP;
1170 ret = __lbs_mesh_config_send(priv, cmd, action, type);
1171 return ret;
1174 /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
1175 * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
1176 * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
1177 * lbs_mesh_config_send.
1179 int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
1181 struct cmd_ds_mesh_config cmd;
1182 struct mrvl_meshie *ie;
1183 DECLARE_SSID_BUF(ssid);
1185 memset(&cmd, 0, sizeof(cmd));
1186 cmd.channel = cpu_to_le16(chan);
1187 ie = (struct mrvl_meshie *)cmd.data;
1189 switch (action) {
1190 case CMD_ACT_MESH_CONFIG_START:
1191 ie->id = WLAN_EID_GENERIC;
1192 ie->val.oui[0] = 0x00;
1193 ie->val.oui[1] = 0x50;
1194 ie->val.oui[2] = 0x43;
1195 ie->val.type = MARVELL_MESH_IE_TYPE;
1196 ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
1197 ie->val.version = MARVELL_MESH_IE_VERSION;
1198 ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
1199 ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
1200 ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
1201 ie->val.mesh_id_len = priv->mesh_ssid_len;
1202 memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
1203 ie->len = sizeof(struct mrvl_meshie_val) -
1204 IW_ESSID_MAX_SIZE + priv->mesh_ssid_len;
1205 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
1206 break;
1207 case CMD_ACT_MESH_CONFIG_STOP:
1208 break;
1209 default:
1210 return -1;
1212 lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
1213 action, priv->mesh_tlv, chan,
1214 print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
1216 return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
1219 static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
1220 struct cmd_ds_command *cmd,
1221 u16 cmd_action)
1223 struct cmd_ds_802_11_beacon_control
1224 *bcn_ctrl = &cmd->params.bcn_ctrl;
1226 lbs_deb_enter(LBS_DEB_CMD);
1227 cmd->size =
1228 cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
1229 + S_DS_GEN);
1230 cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
1232 bcn_ctrl->action = cpu_to_le16(cmd_action);
1233 bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
1234 bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
1236 lbs_deb_leave(LBS_DEB_CMD);
1237 return 0;
1240 static void lbs_queue_cmd(struct lbs_private *priv,
1241 struct cmd_ctrl_node *cmdnode)
1243 unsigned long flags;
1244 int addtail = 1;
1246 lbs_deb_enter(LBS_DEB_HOST);
1248 if (!cmdnode) {
1249 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
1250 goto done;
1252 if (!cmdnode->cmdbuf->size) {
1253 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
1254 goto done;
1256 cmdnode->result = 0;
1258 /* Exit_PS command needs to be queued in the header always. */
1259 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
1260 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
1262 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1263 if (priv->psstate != PS_STATE_FULL_POWER)
1264 addtail = 0;
1268 spin_lock_irqsave(&priv->driver_lock, flags);
1270 if (addtail)
1271 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
1272 else
1273 list_add(&cmdnode->list, &priv->cmdpendingq);
1275 spin_unlock_irqrestore(&priv->driver_lock, flags);
1277 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
1278 le16_to_cpu(cmdnode->cmdbuf->command));
1280 done:
1281 lbs_deb_leave(LBS_DEB_HOST);
1284 static void lbs_submit_command(struct lbs_private *priv,
1285 struct cmd_ctrl_node *cmdnode)
1287 unsigned long flags;
1288 struct cmd_header *cmd;
1289 uint16_t cmdsize;
1290 uint16_t command;
1291 int timeo = 3 * HZ;
1292 int ret;
1294 lbs_deb_enter(LBS_DEB_HOST);
1296 cmd = cmdnode->cmdbuf;
1298 spin_lock_irqsave(&priv->driver_lock, flags);
1299 priv->cur_cmd = cmdnode;
1300 priv->cur_cmd_retcode = 0;
1301 spin_unlock_irqrestore(&priv->driver_lock, flags);
1303 cmdsize = le16_to_cpu(cmd->size);
1304 command = le16_to_cpu(cmd->command);
1306 /* These commands take longer */
1307 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1308 timeo = 5 * HZ;
1310 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1311 command, le16_to_cpu(cmd->seqnum), cmdsize);
1312 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1314 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1316 if (ret) {
1317 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1318 /* Let the timer kick in and retry, and potentially reset
1319 the whole thing if the condition persists */
1320 timeo = HZ/4;
1323 if (command == CMD_802_11_DEEP_SLEEP) {
1324 if (priv->is_auto_deep_sleep_enabled) {
1325 priv->wakeup_dev_required = 1;
1326 priv->dnld_sent = 0;
1328 priv->is_deep_sleep = 1;
1329 lbs_complete_command(priv, cmdnode, 0);
1330 } else {
1331 /* Setup the timer after transmit command */
1332 mod_timer(&priv->command_timer, jiffies + timeo);
1335 lbs_deb_leave(LBS_DEB_HOST);
1339 * This function inserts command node to cmdfreeq
1340 * after cleans it. Requires priv->driver_lock held.
1342 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1343 struct cmd_ctrl_node *cmdnode)
1345 lbs_deb_enter(LBS_DEB_HOST);
1347 if (!cmdnode)
1348 goto out;
1350 cmdnode->callback = NULL;
1351 cmdnode->callback_arg = 0;
1353 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1355 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1356 out:
1357 lbs_deb_leave(LBS_DEB_HOST);
1360 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1361 struct cmd_ctrl_node *ptempcmd)
1363 unsigned long flags;
1365 spin_lock_irqsave(&priv->driver_lock, flags);
1366 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1367 spin_unlock_irqrestore(&priv->driver_lock, flags);
1370 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1371 int result)
1373 if (cmd == priv->cur_cmd)
1374 priv->cur_cmd_retcode = result;
1376 cmd->result = result;
1377 cmd->cmdwaitqwoken = 1;
1378 wake_up_interruptible(&cmd->cmdwait_q);
1380 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1381 __lbs_cleanup_and_insert_cmd(priv, cmd);
1382 priv->cur_cmd = NULL;
1385 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1387 struct cmd_ds_802_11_radio_control cmd;
1388 int ret = -EINVAL;
1390 lbs_deb_enter(LBS_DEB_CMD);
1392 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1393 cmd.action = cpu_to_le16(CMD_ACT_SET);
1395 /* Only v8 and below support setting the preamble */
1396 if (priv->fwrelease < 0x09000000) {
1397 switch (preamble) {
1398 case RADIO_PREAMBLE_SHORT:
1399 if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
1400 goto out;
1401 /* Fall through */
1402 case RADIO_PREAMBLE_AUTO:
1403 case RADIO_PREAMBLE_LONG:
1404 cmd.control = cpu_to_le16(preamble);
1405 break;
1406 default:
1407 goto out;
1411 if (radio_on)
1412 cmd.control |= cpu_to_le16(0x1);
1413 else {
1414 cmd.control &= cpu_to_le16(~0x1);
1415 priv->txpower_cur = 0;
1418 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1419 radio_on ? "ON" : "OFF", preamble);
1421 priv->radio_on = radio_on;
1423 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1425 out:
1426 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1427 return ret;
1430 void lbs_set_mac_control(struct lbs_private *priv)
1432 struct cmd_ds_mac_control cmd;
1434 lbs_deb_enter(LBS_DEB_CMD);
1436 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1437 cmd.action = cpu_to_le16(priv->mac_control);
1438 cmd.reserved = 0;
1440 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1442 lbs_deb_leave(LBS_DEB_CMD);
1446 * @brief This function prepare the command before send to firmware.
1448 * @param priv A pointer to struct lbs_private structure
1449 * @param cmd_no command number
1450 * @param cmd_action command action: GET or SET
1451 * @param wait_option wait option: wait response or not
1452 * @param cmd_oid cmd oid: treated as sub command
1453 * @param pdata_buf A pointer to informaion buffer
1454 * @return 0 or -1
1456 int lbs_prepare_and_send_command(struct lbs_private *priv,
1457 u16 cmd_no,
1458 u16 cmd_action,
1459 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1461 int ret = 0;
1462 struct cmd_ctrl_node *cmdnode;
1463 struct cmd_ds_command *cmdptr;
1464 unsigned long flags;
1466 lbs_deb_enter(LBS_DEB_HOST);
1468 if (!priv) {
1469 lbs_deb_host("PREP_CMD: priv is NULL\n");
1470 ret = -1;
1471 goto done;
1474 if (priv->surpriseremoved) {
1475 lbs_deb_host("PREP_CMD: card removed\n");
1476 ret = -1;
1477 goto done;
1480 if (!lbs_is_cmd_allowed(priv)) {
1481 ret = -EBUSY;
1482 goto done;
1485 cmdnode = lbs_get_cmd_ctrl_node(priv);
1487 if (cmdnode == NULL) {
1488 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1490 /* Wake up main thread to execute next command */
1491 wake_up_interruptible(&priv->waitq);
1492 ret = -1;
1493 goto done;
1496 cmdnode->callback = NULL;
1497 cmdnode->callback_arg = (unsigned long)pdata_buf;
1499 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1501 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1503 /* Set sequence number, command and INT option */
1504 priv->seqnum++;
1505 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1507 cmdptr->command = cpu_to_le16(cmd_no);
1508 cmdptr->result = 0;
1510 switch (cmd_no) {
1511 case CMD_802_11_PS_MODE:
1512 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1513 break;
1515 case CMD_MAC_REG_ACCESS:
1516 case CMD_BBP_REG_ACCESS:
1517 case CMD_RF_REG_ACCESS:
1518 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
1519 break;
1521 case CMD_802_11_MONITOR_MODE:
1522 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
1523 cmd_action, pdata_buf);
1524 break;
1526 case CMD_802_11_RSSI:
1527 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1528 break;
1530 case CMD_802_11_SET_AFC:
1531 case CMD_802_11_GET_AFC:
1533 cmdptr->command = cpu_to_le16(cmd_no);
1534 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1535 S_DS_GEN);
1537 memmove(&cmdptr->params.afc,
1538 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1540 ret = 0;
1541 goto done;
1543 case CMD_802_11D_DOMAIN_INFO:
1544 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr,
1545 cmd_no, cmd_action);
1546 break;
1548 case CMD_802_11_TPC_CFG:
1549 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1550 cmdptr->size =
1551 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1552 S_DS_GEN);
1554 memmove(&cmdptr->params.tpccfg,
1555 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1557 ret = 0;
1558 break;
1559 case CMD_802_11_LED_GPIO_CTRL:
1561 struct mrvl_ie_ledgpio *gpio =
1562 (struct mrvl_ie_ledgpio*)
1563 cmdptr->params.ledgpio.data;
1565 memmove(&cmdptr->params.ledgpio,
1566 pdata_buf,
1567 sizeof(struct cmd_ds_802_11_led_ctrl));
1569 cmdptr->command =
1570 cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
1572 #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
1573 cmdptr->size =
1574 cpu_to_le16(le16_to_cpu(gpio->header.len)
1575 + S_DS_GEN
1576 + ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
1577 gpio->header.len = gpio->header.len;
1579 ret = 0;
1580 break;
1583 case CMD_BT_ACCESS:
1584 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1585 break;
1587 case CMD_FWT_ACCESS:
1588 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1589 break;
1591 case CMD_GET_TSF:
1592 cmdptr->command = cpu_to_le16(CMD_GET_TSF);
1593 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
1594 S_DS_GEN);
1595 ret = 0;
1596 break;
1597 case CMD_802_11_BEACON_CTRL:
1598 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1599 break;
1600 case CMD_802_11_DEEP_SLEEP:
1601 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
1602 cmdptr->size = cpu_to_le16(S_DS_GEN);
1603 break;
1604 default:
1605 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1606 ret = -1;
1607 break;
1610 /* return error, since the command preparation failed */
1611 if (ret != 0) {
1612 lbs_deb_host("PREP_CMD: command preparation failed\n");
1613 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1614 ret = -1;
1615 goto done;
1618 cmdnode->cmdwaitqwoken = 0;
1620 lbs_queue_cmd(priv, cmdnode);
1621 wake_up_interruptible(&priv->waitq);
1623 if (wait_option & CMD_OPTION_WAITFORRSP) {
1624 lbs_deb_host("PREP_CMD: wait for response\n");
1625 might_sleep();
1626 wait_event_interruptible(cmdnode->cmdwait_q,
1627 cmdnode->cmdwaitqwoken);
1630 spin_lock_irqsave(&priv->driver_lock, flags);
1631 if (priv->cur_cmd_retcode) {
1632 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1633 priv->cur_cmd_retcode);
1634 priv->cur_cmd_retcode = 0;
1635 ret = -1;
1637 spin_unlock_irqrestore(&priv->driver_lock, flags);
1639 done:
1640 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1641 return ret;
1645 * @brief This function allocates the command buffer and link
1646 * it to command free queue.
1648 * @param priv A pointer to struct lbs_private structure
1649 * @return 0 or -1
1651 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1653 int ret = 0;
1654 u32 bufsize;
1655 u32 i;
1656 struct cmd_ctrl_node *cmdarray;
1658 lbs_deb_enter(LBS_DEB_HOST);
1660 /* Allocate and initialize the command array */
1661 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1662 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1663 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1664 ret = -1;
1665 goto done;
1667 priv->cmd_array = cmdarray;
1669 /* Allocate and initialize each command buffer in the command array */
1670 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1671 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1672 if (!cmdarray[i].cmdbuf) {
1673 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1674 ret = -1;
1675 goto done;
1679 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1680 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1681 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1683 ret = 0;
1685 done:
1686 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1687 return ret;
1691 * @brief This function frees the command buffer.
1693 * @param priv A pointer to struct lbs_private structure
1694 * @return 0 or -1
1696 int lbs_free_cmd_buffer(struct lbs_private *priv)
1698 struct cmd_ctrl_node *cmdarray;
1699 unsigned int i;
1701 lbs_deb_enter(LBS_DEB_HOST);
1703 /* need to check if cmd array is allocated or not */
1704 if (priv->cmd_array == NULL) {
1705 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1706 goto done;
1709 cmdarray = priv->cmd_array;
1711 /* Release shared memory buffers */
1712 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1713 if (cmdarray[i].cmdbuf) {
1714 kfree(cmdarray[i].cmdbuf);
1715 cmdarray[i].cmdbuf = NULL;
1719 /* Release cmd_ctrl_node */
1720 if (priv->cmd_array) {
1721 kfree(priv->cmd_array);
1722 priv->cmd_array = NULL;
1725 done:
1726 lbs_deb_leave(LBS_DEB_HOST);
1727 return 0;
1731 * @brief This function gets a free command node if available in
1732 * command free queue.
1734 * @param priv A pointer to struct lbs_private structure
1735 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1737 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1739 struct cmd_ctrl_node *tempnode;
1740 unsigned long flags;
1742 lbs_deb_enter(LBS_DEB_HOST);
1744 if (!priv)
1745 return NULL;
1747 spin_lock_irqsave(&priv->driver_lock, flags);
1749 if (!list_empty(&priv->cmdfreeq)) {
1750 tempnode = list_first_entry(&priv->cmdfreeq,
1751 struct cmd_ctrl_node, list);
1752 list_del(&tempnode->list);
1753 } else {
1754 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1755 tempnode = NULL;
1758 spin_unlock_irqrestore(&priv->driver_lock, flags);
1760 lbs_deb_leave(LBS_DEB_HOST);
1761 return tempnode;
1765 * @brief This function executes next command in command
1766 * pending queue. It will put firmware back to PS mode
1767 * if applicable.
1769 * @param priv A pointer to struct lbs_private structure
1770 * @return 0 or -1
1772 int lbs_execute_next_command(struct lbs_private *priv)
1774 struct cmd_ctrl_node *cmdnode = NULL;
1775 struct cmd_header *cmd;
1776 unsigned long flags;
1777 int ret = 0;
1779 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1780 * only caller to us is lbs_thread() and we get even when a
1781 * data packet is received */
1782 lbs_deb_enter(LBS_DEB_THREAD);
1784 spin_lock_irqsave(&priv->driver_lock, flags);
1786 if (priv->cur_cmd) {
1787 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1788 spin_unlock_irqrestore(&priv->driver_lock, flags);
1789 ret = -1;
1790 goto done;
1793 if (!list_empty(&priv->cmdpendingq)) {
1794 cmdnode = list_first_entry(&priv->cmdpendingq,
1795 struct cmd_ctrl_node, list);
1798 spin_unlock_irqrestore(&priv->driver_lock, flags);
1800 if (cmdnode) {
1801 cmd = cmdnode->cmdbuf;
1803 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1804 if ((priv->psstate == PS_STATE_SLEEP) ||
1805 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1806 lbs_deb_host(
1807 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1808 le16_to_cpu(cmd->command),
1809 priv->psstate);
1810 ret = -1;
1811 goto done;
1813 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1814 "0x%04x in psstate %d\n",
1815 le16_to_cpu(cmd->command), priv->psstate);
1816 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1818 * 1. Non-PS command:
1819 * Queue it. set needtowakeup to TRUE if current state
1820 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1821 * 2. PS command but not Exit_PS:
1822 * Ignore it.
1823 * 3. PS command Exit_PS:
1824 * Set needtowakeup to TRUE if current state is SLEEP,
1825 * otherwise send this command down to firmware
1826 * immediately.
1828 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1829 /* Prepare to send Exit PS,
1830 * this non PS command will be sent later */
1831 if ((priv->psstate == PS_STATE_SLEEP)
1832 || (priv->psstate == PS_STATE_PRE_SLEEP)
1834 /* w/ new scheme, it will not reach here.
1835 since it is blocked in main_thread. */
1836 priv->needtowakeup = 1;
1837 } else
1838 lbs_ps_wakeup(priv, 0);
1840 ret = 0;
1841 goto done;
1842 } else {
1844 * PS command. Ignore it if it is not Exit_PS.
1845 * otherwise send it down immediately.
1847 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1849 lbs_deb_host(
1850 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1851 psm->action);
1852 if (psm->action !=
1853 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1854 lbs_deb_host(
1855 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1856 list_del(&cmdnode->list);
1857 spin_lock_irqsave(&priv->driver_lock, flags);
1858 lbs_complete_command(priv, cmdnode, 0);
1859 spin_unlock_irqrestore(&priv->driver_lock, flags);
1861 ret = 0;
1862 goto done;
1865 if ((priv->psstate == PS_STATE_SLEEP) ||
1866 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1867 lbs_deb_host(
1868 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1869 list_del(&cmdnode->list);
1870 spin_lock_irqsave(&priv->driver_lock, flags);
1871 lbs_complete_command(priv, cmdnode, 0);
1872 spin_unlock_irqrestore(&priv->driver_lock, flags);
1873 priv->needtowakeup = 1;
1875 ret = 0;
1876 goto done;
1879 lbs_deb_host(
1880 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1883 list_del(&cmdnode->list);
1884 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1885 le16_to_cpu(cmd->command));
1886 lbs_submit_command(priv, cmdnode);
1887 } else {
1889 * check if in power save mode, if yes, put the device back
1890 * to PS mode
1892 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1893 (priv->psstate == PS_STATE_FULL_POWER) &&
1894 ((priv->connect_status == LBS_CONNECTED) ||
1895 (priv->mesh_connect_status == LBS_CONNECTED))) {
1896 if (priv->secinfo.WPAenabled ||
1897 priv->secinfo.WPA2enabled) {
1898 /* check for valid WPA group keys */
1899 if (priv->wpa_mcast_key.len ||
1900 priv->wpa_unicast_key.len) {
1901 lbs_deb_host(
1902 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1903 " go back to PS_SLEEP");
1904 lbs_ps_sleep(priv, 0);
1906 } else {
1907 lbs_deb_host(
1908 "EXEC_NEXT_CMD: cmdpendingq empty, "
1909 "go back to PS_SLEEP");
1910 lbs_ps_sleep(priv, 0);
1915 ret = 0;
1916 done:
1917 lbs_deb_leave(LBS_DEB_THREAD);
1918 return ret;
1921 void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
1923 union iwreq_data iwrq;
1924 u8 buf[50];
1926 lbs_deb_enter(LBS_DEB_WEXT);
1928 memset(&iwrq, 0, sizeof(union iwreq_data));
1929 memset(buf, 0, sizeof(buf));
1931 snprintf(buf, sizeof(buf) - 1, "%s", str);
1933 iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
1935 /* Send Event to upper layer */
1936 lbs_deb_wext("event indication string %s\n", (char *)buf);
1937 lbs_deb_wext("event indication length %d\n", iwrq.data.length);
1938 lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
1940 wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1942 lbs_deb_leave(LBS_DEB_WEXT);
1945 static void lbs_send_confirmsleep(struct lbs_private *priv)
1947 unsigned long flags;
1948 int ret;
1950 lbs_deb_enter(LBS_DEB_HOST);
1951 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1952 sizeof(confirm_sleep));
1954 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1955 sizeof(confirm_sleep));
1956 if (ret) {
1957 lbs_pr_alert("confirm_sleep failed\n");
1958 goto out;
1961 spin_lock_irqsave(&priv->driver_lock, flags);
1963 /* We don't get a response on the sleep-confirmation */
1964 priv->dnld_sent = DNLD_RES_RECEIVED;
1966 /* If nothing to do, go back to sleep (?) */
1967 if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1968 priv->psstate = PS_STATE_SLEEP;
1970 spin_unlock_irqrestore(&priv->driver_lock, flags);
1972 out:
1973 lbs_deb_leave(LBS_DEB_HOST);
1976 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1978 lbs_deb_enter(LBS_DEB_HOST);
1981 * PS is currently supported only in Infrastructure mode
1982 * Remove this check if it is to be supported in IBSS mode also
1985 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1986 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1988 lbs_deb_leave(LBS_DEB_HOST);
1992 * @brief This function sends Exit_PS command to firmware.
1994 * @param priv A pointer to struct lbs_private structure
1995 * @param wait_option wait response or not
1996 * @return n/a
1998 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
2000 __le32 Localpsmode;
2002 lbs_deb_enter(LBS_DEB_HOST);
2004 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
2006 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
2007 CMD_SUBCMD_EXIT_PS,
2008 wait_option, 0, &Localpsmode);
2010 lbs_deb_leave(LBS_DEB_HOST);
2014 * @brief This function checks condition and prepares to
2015 * send sleep confirm command to firmware if ok.
2017 * @param priv A pointer to struct lbs_private structure
2018 * @param psmode Power Saving mode
2019 * @return n/a
2021 void lbs_ps_confirm_sleep(struct lbs_private *priv)
2023 unsigned long flags =0;
2024 int allowed = 1;
2026 lbs_deb_enter(LBS_DEB_HOST);
2028 spin_lock_irqsave(&priv->driver_lock, flags);
2029 if (priv->dnld_sent) {
2030 allowed = 0;
2031 lbs_deb_host("dnld_sent was set\n");
2034 /* In-progress command? */
2035 if (priv->cur_cmd) {
2036 allowed = 0;
2037 lbs_deb_host("cur_cmd was set\n");
2040 /* Pending events or command responses? */
2041 if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
2042 allowed = 0;
2043 lbs_deb_host("pending events or command responses\n");
2045 spin_unlock_irqrestore(&priv->driver_lock, flags);
2047 if (allowed) {
2048 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
2049 lbs_send_confirmsleep(priv);
2050 } else {
2051 lbs_deb_host("sleep confirm has been delayed\n");
2054 lbs_deb_leave(LBS_DEB_HOST);
2059 * @brief Configures the transmission power control functionality.
2061 * @param priv A pointer to struct lbs_private structure
2062 * @param enable Transmission power control enable
2063 * @param p0 Power level when link quality is good (dBm).
2064 * @param p1 Power level when link quality is fair (dBm).
2065 * @param p2 Power level when link quality is poor (dBm).
2066 * @param usesnr Use Signal to Noise Ratio in TPC
2068 * @return 0 on success
2070 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
2071 int8_t p2, int usesnr)
2073 struct cmd_ds_802_11_tpc_cfg cmd;
2074 int ret;
2076 memset(&cmd, 0, sizeof(cmd));
2077 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2078 cmd.action = cpu_to_le16(CMD_ACT_SET);
2079 cmd.enable = !!enable;
2080 cmd.usesnr = !!usesnr;
2081 cmd.P0 = p0;
2082 cmd.P1 = p1;
2083 cmd.P2 = p2;
2085 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
2087 return ret;
2091 * @brief Configures the power adaptation settings.
2093 * @param priv A pointer to struct lbs_private structure
2094 * @param enable Power adaptation enable
2095 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
2096 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
2097 * @param p2 Power level for 48 and 54 Mbps (dBm).
2099 * @return 0 on Success
2102 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
2103 int8_t p1, int8_t p2)
2105 struct cmd_ds_802_11_pa_cfg cmd;
2106 int ret;
2108 memset(&cmd, 0, sizeof(cmd));
2109 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2110 cmd.action = cpu_to_le16(CMD_ACT_SET);
2111 cmd.enable = !!enable;
2112 cmd.P0 = p0;
2113 cmd.P1 = p1;
2114 cmd.P2 = p2;
2116 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
2118 return ret;
2122 static struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
2123 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
2124 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2125 unsigned long callback_arg)
2127 struct cmd_ctrl_node *cmdnode;
2129 lbs_deb_enter(LBS_DEB_HOST);
2131 if (priv->surpriseremoved) {
2132 lbs_deb_host("PREP_CMD: card removed\n");
2133 cmdnode = ERR_PTR(-ENOENT);
2134 goto done;
2137 if (!lbs_is_cmd_allowed(priv)) {
2138 cmdnode = ERR_PTR(-EBUSY);
2139 goto done;
2142 cmdnode = lbs_get_cmd_ctrl_node(priv);
2143 if (cmdnode == NULL) {
2144 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
2146 /* Wake up main thread to execute next command */
2147 wake_up_interruptible(&priv->waitq);
2148 cmdnode = ERR_PTR(-ENOBUFS);
2149 goto done;
2152 cmdnode->callback = callback;
2153 cmdnode->callback_arg = callback_arg;
2155 /* Copy the incoming command to the buffer */
2156 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
2158 /* Set sequence number, clean result, move to buffer */
2159 priv->seqnum++;
2160 cmdnode->cmdbuf->command = cpu_to_le16(command);
2161 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
2162 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
2163 cmdnode->cmdbuf->result = 0;
2165 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
2167 cmdnode->cmdwaitqwoken = 0;
2168 lbs_queue_cmd(priv, cmdnode);
2169 wake_up_interruptible(&priv->waitq);
2171 done:
2172 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
2173 return cmdnode;
2176 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
2177 struct cmd_header *in_cmd, int in_cmd_size)
2179 lbs_deb_enter(LBS_DEB_CMD);
2180 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2181 lbs_cmd_async_callback, 0);
2182 lbs_deb_leave(LBS_DEB_CMD);
2185 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
2186 struct cmd_header *in_cmd, int in_cmd_size,
2187 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2188 unsigned long callback_arg)
2190 struct cmd_ctrl_node *cmdnode;
2191 unsigned long flags;
2192 int ret = 0;
2194 lbs_deb_enter(LBS_DEB_HOST);
2196 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2197 callback, callback_arg);
2198 if (IS_ERR(cmdnode)) {
2199 ret = PTR_ERR(cmdnode);
2200 goto done;
2203 might_sleep();
2204 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
2206 spin_lock_irqsave(&priv->driver_lock, flags);
2207 ret = cmdnode->result;
2208 if (ret)
2209 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
2210 command, ret);
2212 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
2213 spin_unlock_irqrestore(&priv->driver_lock, flags);
2215 done:
2216 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
2217 return ret;
2219 EXPORT_SYMBOL_GPL(__lbs_cmd);