2 * This file contains the handling of command.
3 * It prepares command and sends it to firmware when it is ready.
6 #include <net/iw_handler.h>
7 #include <net/lib80211.h>
8 #include <linux/kfifo.h>
17 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
20 * @brief Simple callback that copies response back into command
22 * @param priv A pointer to struct lbs_private structure
23 * @param extra A pointer to the original command structure for which
24 * 'resp' is a response
25 * @param resp A pointer to the command response
27 * @return 0 on success, error on failure
29 int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
30 struct cmd_header *resp)
32 struct cmd_header *buf = (void *)extra;
35 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
36 memcpy(buf, resp, copy_len);
39 EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
42 * @brief Simple callback that ignores the result. Use this if
43 * you just want to send a command to the hardware, but don't
44 * care for the result.
47 * @param extra ignored
50 * @return 0 for success
52 static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
53 struct cmd_header *resp)
60 * @brief Checks whether a command is allowed in Power Save mode
62 * @param command the command ID
63 * @return 1 if allowed, 0 if not allowed
65 static u8 is_command_allowed_in_ps(u16 cmd)
77 * @brief This function checks if the command is allowed.
79 * @param priv A pointer to lbs_private structure
80 * @return allowed or not allowed.
83 static int lbs_is_cmd_allowed(struct lbs_private *priv)
87 lbs_deb_enter(LBS_DEB_CMD);
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");
96 lbs_deb_leave(LBS_DEB_CMD);
101 * @brief Updates the hardware details like MAC address and regulatory region
103 * @param priv A pointer to struct lbs_private structure
105 * @return 0 on success, error on failure
107 int lbs_update_hw_spec(struct lbs_private *priv)
109 struct cmd_ds_get_hw_spec cmd;
113 lbs_deb_enter(LBS_DEB_CMD);
115 memset(&cmd, 0, sizeof(cmd));
116 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
117 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
118 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
122 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
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);
130 /* Some firmware capabilities:
131 * CF card firmware 5.0.16p0: cap 0x00000303
132 * USB dongle firmware 5.110.17p2: cap 0x00000303
134 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
136 priv->fwrelease >> 24 & 0xff,
137 priv->fwrelease >> 16 & 0xff,
138 priv->fwrelease >> 8 & 0xff,
139 priv->fwrelease & 0xff,
141 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
142 cmd.hwifversion, cmd.version);
144 /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
145 /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
146 /* 5.110.22 have mesh command with 0xa3 command id */
147 /* 10.0.0.p0 FW brings in mesh config command with different id */
148 /* Check FW version MSB and initialize mesh_fw_ver */
149 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
150 priv->mesh_fw_ver = MESH_FW_OLD;
151 else if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
152 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK))
153 priv->mesh_fw_ver = MESH_FW_NEW;
155 priv->mesh_fw_ver = MESH_NONE;
157 /* Clamp region code to 8-bit since FW spec indicates that it should
158 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
159 * returns non-zero high 8 bits here.
161 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
162 * need to check for this problem and handle it properly.
164 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
165 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
167 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
169 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
170 /* use the region code to search for the index */
171 if (priv->regioncode == lbs_region_code_to_index[i])
175 /* if it's unidentified region code, use the default (USA) */
176 if (i >= MRVDRV_MAX_REGION_CODE) {
177 priv->regioncode = 0x10;
178 lbs_pr_info("unidentified region code; using the default (USA)\n");
181 if (priv->current_addr[0] == 0xff)
182 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
184 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
186 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
188 if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
193 if (lbs_set_universaltable(priv, 0)) {
199 lbs_deb_leave(LBS_DEB_CMD);
203 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
204 struct wol_config *p_wol_config)
206 struct cmd_ds_host_sleep cmd_config;
209 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
210 cmd_config.criteria = cpu_to_le32(criteria);
211 cmd_config.gpio = priv->wol_gpio;
212 cmd_config.gap = priv->wol_gap;
214 if (p_wol_config != NULL)
215 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
216 sizeof(struct wol_config));
218 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
220 ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
223 lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
224 priv->wol_criteria = criteria;
226 memcpy((uint8_t *) p_wol_config,
227 (uint8_t *)&cmd_config.wol_conf,
228 sizeof(struct wol_config));
230 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
235 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
237 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
240 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
242 lbs_deb_enter(LBS_DEB_CMD);
244 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
245 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
247 psm->action = cpu_to_le16(cmd_action);
248 psm->multipledtim = 0;
249 switch (cmd_action) {
250 case CMD_SUBCMD_ENTER_PS:
251 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
253 psm->locallisteninterval = 0;
254 psm->nullpktinterval = 0;
256 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
259 case CMD_SUBCMD_EXIT_PS:
260 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
263 case CMD_SUBCMD_SLEEP_CONFIRMED:
264 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
271 lbs_deb_leave(LBS_DEB_CMD);
275 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
276 struct sleep_params *sp)
278 struct cmd_ds_802_11_sleep_params cmd;
281 lbs_deb_enter(LBS_DEB_CMD);
283 if (cmd_action == CMD_ACT_GET) {
284 memset(&cmd, 0, sizeof(cmd));
286 cmd.error = cpu_to_le16(sp->sp_error);
287 cmd.offset = cpu_to_le16(sp->sp_offset);
288 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
289 cmd.calcontrol = sp->sp_calcontrol;
290 cmd.externalsleepclk = sp->sp_extsleepclk;
291 cmd.reserved = cpu_to_le16(sp->sp_reserved);
293 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
294 cmd.action = cpu_to_le16(cmd_action);
296 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
299 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
300 "calcontrol 0x%x extsleepclk 0x%x\n",
301 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
302 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
303 cmd.externalsleepclk);
305 sp->sp_error = le16_to_cpu(cmd.error);
306 sp->sp_offset = le16_to_cpu(cmd.offset);
307 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
308 sp->sp_calcontrol = cmd.calcontrol;
309 sp->sp_extsleepclk = cmd.externalsleepclk;
310 sp->sp_reserved = le16_to_cpu(cmd.reserved);
313 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
317 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
321 lbs_deb_enter(LBS_DEB_CMD);
323 if (priv->is_deep_sleep) {
324 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
325 !priv->is_deep_sleep, (10 * HZ))) {
326 lbs_pr_err("ds_awake_q: timer expired\n");
331 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
335 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
339 lbs_deb_enter(LBS_DEB_CMD);
342 if (priv->is_deep_sleep != 1) {
343 lbs_deb_cmd("deep sleep: sleep\n");
344 BUG_ON(!priv->enter_deep_sleep);
345 ret = priv->enter_deep_sleep(priv);
347 netif_stop_queue(priv->dev);
348 netif_carrier_off(priv->dev);
351 lbs_pr_err("deep sleep: already enabled\n");
354 if (priv->is_deep_sleep) {
355 lbs_deb_cmd("deep sleep: wakeup\n");
356 BUG_ON(!priv->exit_deep_sleep);
357 ret = priv->exit_deep_sleep(priv);
359 ret = lbs_wait_for_ds_awake(priv);
361 lbs_pr_err("deep sleep: wakeup"
367 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
371 int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
372 struct assoc_request *assoc)
374 struct cmd_ds_802_11_set_wep cmd;
377 lbs_deb_enter(LBS_DEB_CMD);
379 memset(&cmd, 0, sizeof(cmd));
380 cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
381 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
383 cmd.action = cpu_to_le16(cmd_action);
385 if (cmd_action == CMD_ACT_ADD) {
388 /* default tx key index */
389 cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
390 CMD_WEP_KEY_INDEX_MASK);
392 /* Copy key types and material to host command structure */
393 for (i = 0; i < 4; i++) {
394 struct enc_key *pkey = &assoc->wep_keys[i];
398 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
399 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
400 lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
402 case KEY_LEN_WEP_104:
403 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
404 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
405 lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
410 lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
417 } else if (cmd_action == CMD_ACT_REMOVE) {
418 /* ACT_REMOVE clears _all_ WEP keys */
420 /* default tx key index */
421 cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
422 CMD_WEP_KEY_INDEX_MASK);
423 lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
426 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
428 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
432 int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
435 struct cmd_ds_802_11_enable_rsn cmd;
438 lbs_deb_enter(LBS_DEB_CMD);
440 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
441 cmd.action = cpu_to_le16(cmd_action);
443 if (cmd_action == CMD_ACT_GET)
447 cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
449 cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
450 lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
453 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
454 if (!ret && cmd_action == CMD_ACT_GET)
455 *enable = le16_to_cpu(cmd.enable);
457 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
461 static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
464 lbs_deb_enter(LBS_DEB_CMD);
466 if (key->flags & KEY_INFO_WPA_ENABLED)
467 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
468 if (key->flags & KEY_INFO_WPA_UNICAST)
469 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
470 if (key->flags & KEY_INFO_WPA_MCAST)
471 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
473 keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
474 keyparam->keytypeid = cpu_to_le16(key->type);
475 keyparam->keylen = cpu_to_le16(key->len);
476 memcpy(keyparam->key, key->key, key->len);
478 /* Length field doesn't include the {type,length} header */
479 keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
480 lbs_deb_leave(LBS_DEB_CMD);
483 int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
484 struct assoc_request *assoc)
486 struct cmd_ds_802_11_key_material cmd;
490 lbs_deb_enter(LBS_DEB_CMD);
492 cmd.action = cpu_to_le16(cmd_action);
493 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
495 if (cmd_action == CMD_ACT_GET) {
496 cmd.hdr.size = cpu_to_le16(S_DS_GEN + 2);
498 memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
500 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
501 set_one_wpa_key(&cmd.keyParamSet[index],
502 &assoc->wpa_unicast_key);
506 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
507 set_one_wpa_key(&cmd.keyParamSet[index],
508 &assoc->wpa_mcast_key);
512 /* The common header and as many keys as we included */
513 cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
514 keyParamSet[index]));
516 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
517 /* Copy the returned key to driver private data */
518 if (!ret && cmd_action == CMD_ACT_GET) {
519 void *buf_ptr = cmd.keyParamSet;
520 void *resp_end = &(&cmd)[1];
522 while (buf_ptr < resp_end) {
523 struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
525 uint16_t param_set_len = le16_to_cpu(keyparam->length);
526 uint16_t key_len = le16_to_cpu(keyparam->keylen);
527 uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
528 uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
531 end = (void *)keyparam + sizeof(keyparam->type)
532 + sizeof(keyparam->length) + param_set_len;
534 /* Make sure we don't access past the end of the IEs */
538 if (key_flags & KEY_INFO_WPA_UNICAST)
539 key = &priv->wpa_unicast_key;
540 else if (key_flags & KEY_INFO_WPA_MCAST)
541 key = &priv->wpa_mcast_key;
545 /* Copy returned key into driver */
546 memset(key, 0, sizeof(struct enc_key));
547 if (key_len > sizeof(key->key))
549 key->type = key_type;
550 key->flags = key_flags;
552 memcpy(key->key, keyparam->key, key->len);
558 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
563 * @brief Set an SNMP MIB value
565 * @param priv A pointer to struct lbs_private structure
566 * @param oid The OID to set in the firmware
567 * @param val Value to set the OID to
569 * @return 0 on success, error on failure
571 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
573 struct cmd_ds_802_11_snmp_mib cmd;
576 lbs_deb_enter(LBS_DEB_CMD);
578 memset(&cmd, 0, sizeof (cmd));
579 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
580 cmd.action = cpu_to_le16(CMD_ACT_SET);
581 cmd.oid = cpu_to_le16((u16) oid);
584 case SNMP_MIB_OID_BSS_TYPE:
585 cmd.bufsize = cpu_to_le16(sizeof(u8));
586 cmd.value[0] = (val == IW_MODE_ADHOC) ? 2 : 1;
588 case SNMP_MIB_OID_11D_ENABLE:
589 case SNMP_MIB_OID_FRAG_THRESHOLD:
590 case SNMP_MIB_OID_RTS_THRESHOLD:
591 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
592 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
593 cmd.bufsize = cpu_to_le16(sizeof(u16));
594 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
597 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
602 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
603 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
605 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
608 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
613 * @brief Get an SNMP MIB value
615 * @param priv A pointer to struct lbs_private structure
616 * @param oid The OID to retrieve from the firmware
617 * @param out_val Location for the returned value
619 * @return 0 on success, error on failure
621 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
623 struct cmd_ds_802_11_snmp_mib cmd;
626 lbs_deb_enter(LBS_DEB_CMD);
628 memset(&cmd, 0, sizeof (cmd));
629 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
630 cmd.action = cpu_to_le16(CMD_ACT_GET);
631 cmd.oid = cpu_to_le16(oid);
633 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
637 switch (le16_to_cpu(cmd.bufsize)) {
639 if (oid == SNMP_MIB_OID_BSS_TYPE) {
640 if (cmd.value[0] == 2)
641 *out_val = IW_MODE_ADHOC;
643 *out_val = IW_MODE_INFRA;
645 *out_val = cmd.value[0];
648 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
651 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
652 oid, le16_to_cpu(cmd.bufsize));
657 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
662 * @brief Get the min, max, and current TX power
664 * @param priv A pointer to struct lbs_private structure
665 * @param curlevel Current power level in dBm
666 * @param minlevel Minimum supported power level in dBm (optional)
667 * @param maxlevel Maximum supported power level in dBm (optional)
669 * @return 0 on success, error on failure
671 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
674 struct cmd_ds_802_11_rf_tx_power cmd;
677 lbs_deb_enter(LBS_DEB_CMD);
679 memset(&cmd, 0, sizeof(cmd));
680 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
681 cmd.action = cpu_to_le16(CMD_ACT_GET);
683 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
685 *curlevel = le16_to_cpu(cmd.curlevel);
687 *minlevel = cmd.minlevel;
689 *maxlevel = cmd.maxlevel;
692 lbs_deb_leave(LBS_DEB_CMD);
697 * @brief Set the TX power
699 * @param priv A pointer to struct lbs_private structure
700 * @param dbm The desired power level in dBm
702 * @return 0 on success, error on failure
704 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
706 struct cmd_ds_802_11_rf_tx_power cmd;
709 lbs_deb_enter(LBS_DEB_CMD);
711 memset(&cmd, 0, sizeof(cmd));
712 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
713 cmd.action = cpu_to_le16(CMD_ACT_SET);
714 cmd.curlevel = cpu_to_le16(dbm);
716 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
718 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
720 lbs_deb_leave(LBS_DEB_CMD);
724 static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
725 u16 cmd_action, void *pdata_buf)
727 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
729 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
731 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
734 monitor->action = cpu_to_le16(cmd_action);
735 if (cmd_action == CMD_ACT_SET) {
737 cpu_to_le16((u16) (*(u32 *) pdata_buf));
743 static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
763 int i = lbs_data_rate_to_fw_index(rate);
765 ratemask = (0x1fef >> (12 - i));
768 return cpu_to_le16(ratemask);
771 int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
774 struct cmd_ds_802_11_rate_adapt_rateset cmd;
777 lbs_deb_enter(LBS_DEB_CMD);
779 if (!priv->cur_rate && !priv->enablehwauto)
782 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
784 cmd.action = cpu_to_le16(cmd_action);
785 cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
786 cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
787 ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
788 if (!ret && cmd_action == CMD_ACT_GET) {
789 priv->ratebitmap = le16_to_cpu(cmd.bitmap);
790 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
793 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
796 EXPORT_SYMBOL_GPL(lbs_cmd_802_11_rate_adapt_rateset);
799 * @brief Set the data rate
801 * @param priv A pointer to struct lbs_private structure
802 * @param rate The desired data rate, or 0 to clear a locked rate
804 * @return 0 on success, error on failure
806 int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
808 struct cmd_ds_802_11_data_rate cmd;
811 lbs_deb_enter(LBS_DEB_CMD);
813 memset(&cmd, 0, sizeof(cmd));
814 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
817 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
818 cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
819 if (cmd.rates[0] == 0) {
820 lbs_deb_cmd("DATA_RATE: invalid requested rate of"
825 lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
827 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
828 lbs_deb_cmd("DATA_RATE: setting auto\n");
831 ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
835 lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof (cmd));
837 /* FIXME: get actual rates FW can do if this command actually returns
838 * all data rates supported.
840 priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
841 lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
844 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
849 * @brief Get the radio channel
851 * @param priv A pointer to struct lbs_private structure
853 * @return The channel on success, error on failure
855 static int lbs_get_channel(struct lbs_private *priv)
857 struct cmd_ds_802_11_rf_channel cmd;
860 lbs_deb_enter(LBS_DEB_CMD);
862 memset(&cmd, 0, sizeof(cmd));
863 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
864 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
866 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
870 ret = le16_to_cpu(cmd.channel);
871 lbs_deb_cmd("current radio channel is %d\n", ret);
874 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
878 int lbs_update_channel(struct lbs_private *priv)
882 /* the channel in f/w could be out of sync; get the current channel */
883 lbs_deb_enter(LBS_DEB_ASSOC);
885 ret = lbs_get_channel(priv);
887 priv->curbssparams.channel = ret;
890 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
895 * @brief Set the radio channel
897 * @param priv A pointer to struct lbs_private structure
898 * @param channel The desired channel, or 0 to clear a locked channel
900 * @return 0 on success, error on failure
902 int lbs_set_channel(struct lbs_private *priv, u8 channel)
904 struct cmd_ds_802_11_rf_channel cmd;
906 u8 old_channel = priv->curbssparams.channel;
910 lbs_deb_enter(LBS_DEB_CMD);
912 memset(&cmd, 0, sizeof(cmd));
913 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
914 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
915 cmd.channel = cpu_to_le16(channel);
917 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
921 priv->curbssparams.channel = (uint8_t) le16_to_cpu(cmd.channel);
922 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
923 priv->curbssparams.channel);
926 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
930 static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
931 struct cmd_ds_command *cmd)
934 lbs_deb_enter(LBS_DEB_CMD);
935 cmd->command = cpu_to_le16(CMD_802_11_RSSI);
936 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
937 cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
939 /* reset Beacon SNR/NF/RSSI values */
940 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
941 priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
942 priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
943 priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
944 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
945 priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
947 lbs_deb_leave(LBS_DEB_CMD);
951 static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
952 u8 cmd_action, void *pdata_buf)
954 struct lbs_offset_value *offval;
956 lbs_deb_enter(LBS_DEB_CMD);
958 offval = (struct lbs_offset_value *)pdata_buf;
960 switch (le16_to_cpu(cmdptr->command)) {
961 case CMD_MAC_REG_ACCESS:
963 struct cmd_ds_mac_reg_access *macreg;
966 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
969 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
972 macreg->action = cpu_to_le16(cmd_action);
973 macreg->offset = cpu_to_le16((u16) offval->offset);
974 macreg->value = cpu_to_le32(offval->value);
979 case CMD_BBP_REG_ACCESS:
981 struct cmd_ds_bbp_reg_access *bbpreg;
985 (struct cmd_ds_bbp_reg_access)
988 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
991 bbpreg->action = cpu_to_le16(cmd_action);
992 bbpreg->offset = cpu_to_le16((u16) offval->offset);
993 bbpreg->value = (u8) offval->value;
998 case CMD_RF_REG_ACCESS:
1000 struct cmd_ds_rf_reg_access *rfreg;
1004 (struct cmd_ds_rf_reg_access) +
1007 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
1010 rfreg->action = cpu_to_le16(cmd_action);
1011 rfreg->offset = cpu_to_le16((u16) offval->offset);
1012 rfreg->value = (u8) offval->value;
1021 lbs_deb_leave(LBS_DEB_CMD);
1025 static int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
1026 u16 cmd_action, void *pdata_buf)
1028 struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
1029 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1031 cmd->command = cpu_to_le16(CMD_BT_ACCESS);
1032 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
1034 bt_access->action = cpu_to_le16(cmd_action);
1036 switch (cmd_action) {
1037 case CMD_ACT_BT_ACCESS_ADD:
1038 memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
1039 lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
1041 case CMD_ACT_BT_ACCESS_DEL:
1042 memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
1043 lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
1045 case CMD_ACT_BT_ACCESS_LIST:
1046 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
1048 case CMD_ACT_BT_ACCESS_RESET:
1050 case CMD_ACT_BT_ACCESS_SET_INVERT:
1051 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
1053 case CMD_ACT_BT_ACCESS_GET_INVERT:
1058 lbs_deb_leave(LBS_DEB_CMD);
1062 static int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
1063 u16 cmd_action, void *pdata_buf)
1065 struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
1066 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1068 cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
1069 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
1073 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
1075 memset(fwt_access, 0, sizeof(*fwt_access));
1077 fwt_access->action = cpu_to_le16(cmd_action);
1079 lbs_deb_leave(LBS_DEB_CMD);
1083 int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
1084 struct cmd_ds_mesh_access *cmd)
1088 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1090 cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
1091 cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
1092 cmd->hdr.result = 0;
1094 cmd->action = cpu_to_le16(cmd_action);
1096 ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
1098 lbs_deb_leave(LBS_DEB_CMD);
1102 static int __lbs_mesh_config_send(struct lbs_private *priv,
1103 struct cmd_ds_mesh_config *cmd,
1104 uint16_t action, uint16_t type)
1107 u16 command = CMD_MESH_CONFIG_OLD;
1109 lbs_deb_enter(LBS_DEB_CMD);
1112 * Command id is 0xac for v10 FW along with mesh interface
1113 * id in bits 14-13-12.
1115 if (priv->mesh_fw_ver == MESH_FW_NEW)
1116 command = CMD_MESH_CONFIG |
1117 (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
1119 cmd->hdr.command = cpu_to_le16(command);
1120 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
1121 cmd->hdr.result = 0;
1123 cmd->type = cpu_to_le16(type);
1124 cmd->action = cpu_to_le16(action);
1126 ret = lbs_cmd_with_response(priv, command, cmd);
1128 lbs_deb_leave(LBS_DEB_CMD);
1132 int lbs_mesh_config_send(struct lbs_private *priv,
1133 struct cmd_ds_mesh_config *cmd,
1134 uint16_t action, uint16_t type)
1138 if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
1141 ret = __lbs_mesh_config_send(priv, cmd, action, type);
1145 /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
1146 * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
1147 * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
1148 * lbs_mesh_config_send.
1150 int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
1152 struct cmd_ds_mesh_config cmd;
1153 struct mrvl_meshie *ie;
1154 DECLARE_SSID_BUF(ssid);
1156 memset(&cmd, 0, sizeof(cmd));
1157 cmd.channel = cpu_to_le16(chan);
1158 ie = (struct mrvl_meshie *)cmd.data;
1161 case CMD_ACT_MESH_CONFIG_START:
1162 ie->id = WLAN_EID_GENERIC;
1163 ie->val.oui[0] = 0x00;
1164 ie->val.oui[1] = 0x50;
1165 ie->val.oui[2] = 0x43;
1166 ie->val.type = MARVELL_MESH_IE_TYPE;
1167 ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
1168 ie->val.version = MARVELL_MESH_IE_VERSION;
1169 ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
1170 ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
1171 ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
1172 ie->val.mesh_id_len = priv->mesh_ssid_len;
1173 memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
1174 ie->len = sizeof(struct mrvl_meshie_val) -
1175 IW_ESSID_MAX_SIZE + priv->mesh_ssid_len;
1176 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
1178 case CMD_ACT_MESH_CONFIG_STOP:
1183 lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
1184 action, priv->mesh_tlv, chan,
1185 print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
1187 return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
1190 static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
1191 struct cmd_ds_command *cmd,
1194 struct cmd_ds_802_11_beacon_control
1195 *bcn_ctrl = &cmd->params.bcn_ctrl;
1197 lbs_deb_enter(LBS_DEB_CMD);
1199 cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
1201 cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
1203 bcn_ctrl->action = cpu_to_le16(cmd_action);
1204 bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
1205 bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
1207 lbs_deb_leave(LBS_DEB_CMD);
1211 static void lbs_queue_cmd(struct lbs_private *priv,
1212 struct cmd_ctrl_node *cmdnode)
1214 unsigned long flags;
1217 lbs_deb_enter(LBS_DEB_HOST);
1220 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
1223 if (!cmdnode->cmdbuf->size) {
1224 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
1227 cmdnode->result = 0;
1229 /* Exit_PS command needs to be queued in the header always. */
1230 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
1231 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
1233 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1234 if (priv->psstate != PS_STATE_FULL_POWER)
1239 spin_lock_irqsave(&priv->driver_lock, flags);
1242 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
1244 list_add(&cmdnode->list, &priv->cmdpendingq);
1246 spin_unlock_irqrestore(&priv->driver_lock, flags);
1248 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
1249 le16_to_cpu(cmdnode->cmdbuf->command));
1252 lbs_deb_leave(LBS_DEB_HOST);
1255 static void lbs_submit_command(struct lbs_private *priv,
1256 struct cmd_ctrl_node *cmdnode)
1258 unsigned long flags;
1259 struct cmd_header *cmd;
1265 lbs_deb_enter(LBS_DEB_HOST);
1267 cmd = cmdnode->cmdbuf;
1269 spin_lock_irqsave(&priv->driver_lock, flags);
1270 priv->cur_cmd = cmdnode;
1271 priv->cur_cmd_retcode = 0;
1272 spin_unlock_irqrestore(&priv->driver_lock, flags);
1274 cmdsize = le16_to_cpu(cmd->size);
1275 command = le16_to_cpu(cmd->command);
1277 /* These commands take longer */
1278 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1281 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1282 command, le16_to_cpu(cmd->seqnum), cmdsize);
1283 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1285 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1288 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1289 /* Let the timer kick in and retry, and potentially reset
1290 the whole thing if the condition persists */
1294 if (command == CMD_802_11_DEEP_SLEEP) {
1295 if (priv->is_auto_deep_sleep_enabled) {
1296 priv->wakeup_dev_required = 1;
1297 priv->dnld_sent = 0;
1299 priv->is_deep_sleep = 1;
1300 lbs_complete_command(priv, cmdnode, 0);
1302 /* Setup the timer after transmit command */
1303 mod_timer(&priv->command_timer, jiffies + timeo);
1306 lbs_deb_leave(LBS_DEB_HOST);
1310 * This function inserts command node to cmdfreeq
1311 * after cleans it. Requires priv->driver_lock held.
1313 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1314 struct cmd_ctrl_node *cmdnode)
1316 lbs_deb_enter(LBS_DEB_HOST);
1321 cmdnode->callback = NULL;
1322 cmdnode->callback_arg = 0;
1324 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1326 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1328 lbs_deb_leave(LBS_DEB_HOST);
1331 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1332 struct cmd_ctrl_node *ptempcmd)
1334 unsigned long flags;
1336 spin_lock_irqsave(&priv->driver_lock, flags);
1337 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1338 spin_unlock_irqrestore(&priv->driver_lock, flags);
1341 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1344 if (cmd == priv->cur_cmd)
1345 priv->cur_cmd_retcode = result;
1347 cmd->result = result;
1348 cmd->cmdwaitqwoken = 1;
1349 wake_up_interruptible(&cmd->cmdwait_q);
1351 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1352 __lbs_cleanup_and_insert_cmd(priv, cmd);
1353 priv->cur_cmd = NULL;
1356 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1358 struct cmd_ds_802_11_radio_control cmd;
1361 lbs_deb_enter(LBS_DEB_CMD);
1363 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1364 cmd.action = cpu_to_le16(CMD_ACT_SET);
1366 /* Only v8 and below support setting the preamble */
1367 if (priv->fwrelease < 0x09000000) {
1369 case RADIO_PREAMBLE_SHORT:
1370 if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
1373 case RADIO_PREAMBLE_AUTO:
1374 case RADIO_PREAMBLE_LONG:
1375 cmd.control = cpu_to_le16(preamble);
1383 cmd.control |= cpu_to_le16(0x1);
1385 cmd.control &= cpu_to_le16(~0x1);
1386 priv->txpower_cur = 0;
1389 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1390 radio_on ? "ON" : "OFF", preamble);
1392 priv->radio_on = radio_on;
1394 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1397 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1401 void lbs_set_mac_control(struct lbs_private *priv)
1403 struct cmd_ds_mac_control cmd;
1405 lbs_deb_enter(LBS_DEB_CMD);
1407 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1408 cmd.action = cpu_to_le16(priv->mac_control);
1411 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1413 lbs_deb_leave(LBS_DEB_CMD);
1417 * @brief This function prepare the command before send to firmware.
1419 * @param priv A pointer to struct lbs_private structure
1420 * @param cmd_no command number
1421 * @param cmd_action command action: GET or SET
1422 * @param wait_option wait option: wait response or not
1423 * @param cmd_oid cmd oid: treated as sub command
1424 * @param pdata_buf A pointer to informaion buffer
1427 int lbs_prepare_and_send_command(struct lbs_private *priv,
1430 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1433 struct cmd_ctrl_node *cmdnode;
1434 struct cmd_ds_command *cmdptr;
1435 unsigned long flags;
1437 lbs_deb_enter(LBS_DEB_HOST);
1440 lbs_deb_host("PREP_CMD: priv is NULL\n");
1445 if (priv->surpriseremoved) {
1446 lbs_deb_host("PREP_CMD: card removed\n");
1451 if (!lbs_is_cmd_allowed(priv)) {
1456 cmdnode = lbs_get_cmd_ctrl_node(priv);
1458 if (cmdnode == NULL) {
1459 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1461 /* Wake up main thread to execute next command */
1462 wake_up_interruptible(&priv->waitq);
1467 cmdnode->callback = NULL;
1468 cmdnode->callback_arg = (unsigned long)pdata_buf;
1470 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1472 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1474 /* Set sequence number, command and INT option */
1476 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1478 cmdptr->command = cpu_to_le16(cmd_no);
1482 case CMD_802_11_PS_MODE:
1483 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1486 case CMD_MAC_REG_ACCESS:
1487 case CMD_BBP_REG_ACCESS:
1488 case CMD_RF_REG_ACCESS:
1489 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
1492 case CMD_802_11_MONITOR_MODE:
1493 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
1494 cmd_action, pdata_buf);
1497 case CMD_802_11_RSSI:
1498 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1501 case CMD_802_11_SET_AFC:
1502 case CMD_802_11_GET_AFC:
1504 cmdptr->command = cpu_to_le16(cmd_no);
1505 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1508 memmove(&cmdptr->params.afc,
1509 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1514 case CMD_802_11D_DOMAIN_INFO:
1515 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr,
1516 cmd_no, cmd_action);
1519 case CMD_802_11_TPC_CFG:
1520 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1522 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1525 memmove(&cmdptr->params.tpccfg,
1526 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1530 case CMD_802_11_LED_GPIO_CTRL:
1532 struct mrvl_ie_ledgpio *gpio =
1533 (struct mrvl_ie_ledgpio*)
1534 cmdptr->params.ledgpio.data;
1536 memmove(&cmdptr->params.ledgpio,
1538 sizeof(struct cmd_ds_802_11_led_ctrl));
1541 cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
1543 #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
1545 cpu_to_le16(le16_to_cpu(gpio->header.len)
1547 + ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
1548 gpio->header.len = gpio->header.len;
1555 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1558 case CMD_FWT_ACCESS:
1559 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1563 cmdptr->command = cpu_to_le16(CMD_GET_TSF);
1564 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
1568 case CMD_802_11_BEACON_CTRL:
1569 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1571 case CMD_802_11_DEEP_SLEEP:
1572 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
1573 cmdptr->size = cpu_to_le16(S_DS_GEN);
1576 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1581 /* return error, since the command preparation failed */
1583 lbs_deb_host("PREP_CMD: command preparation failed\n");
1584 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1589 cmdnode->cmdwaitqwoken = 0;
1591 lbs_queue_cmd(priv, cmdnode);
1592 wake_up_interruptible(&priv->waitq);
1594 if (wait_option & CMD_OPTION_WAITFORRSP) {
1595 lbs_deb_host("PREP_CMD: wait for response\n");
1597 wait_event_interruptible(cmdnode->cmdwait_q,
1598 cmdnode->cmdwaitqwoken);
1601 spin_lock_irqsave(&priv->driver_lock, flags);
1602 if (priv->cur_cmd_retcode) {
1603 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1604 priv->cur_cmd_retcode);
1605 priv->cur_cmd_retcode = 0;
1608 spin_unlock_irqrestore(&priv->driver_lock, flags);
1611 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1616 * @brief This function allocates the command buffer and link
1617 * it to command free queue.
1619 * @param priv A pointer to struct lbs_private structure
1622 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1627 struct cmd_ctrl_node *cmdarray;
1629 lbs_deb_enter(LBS_DEB_HOST);
1631 /* Allocate and initialize the command array */
1632 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1633 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1634 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1638 priv->cmd_array = cmdarray;
1640 /* Allocate and initialize each command buffer in the command array */
1641 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1642 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1643 if (!cmdarray[i].cmdbuf) {
1644 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1650 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1651 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1652 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1657 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1662 * @brief This function frees the command buffer.
1664 * @param priv A pointer to struct lbs_private structure
1667 int lbs_free_cmd_buffer(struct lbs_private *priv)
1669 struct cmd_ctrl_node *cmdarray;
1672 lbs_deb_enter(LBS_DEB_HOST);
1674 /* need to check if cmd array is allocated or not */
1675 if (priv->cmd_array == NULL) {
1676 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1680 cmdarray = priv->cmd_array;
1682 /* Release shared memory buffers */
1683 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1684 if (cmdarray[i].cmdbuf) {
1685 kfree(cmdarray[i].cmdbuf);
1686 cmdarray[i].cmdbuf = NULL;
1690 /* Release cmd_ctrl_node */
1691 if (priv->cmd_array) {
1692 kfree(priv->cmd_array);
1693 priv->cmd_array = NULL;
1697 lbs_deb_leave(LBS_DEB_HOST);
1702 * @brief This function gets a free command node if available in
1703 * command free queue.
1705 * @param priv A pointer to struct lbs_private structure
1706 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1708 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1710 struct cmd_ctrl_node *tempnode;
1711 unsigned long flags;
1713 lbs_deb_enter(LBS_DEB_HOST);
1718 spin_lock_irqsave(&priv->driver_lock, flags);
1720 if (!list_empty(&priv->cmdfreeq)) {
1721 tempnode = list_first_entry(&priv->cmdfreeq,
1722 struct cmd_ctrl_node, list);
1723 list_del(&tempnode->list);
1725 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1729 spin_unlock_irqrestore(&priv->driver_lock, flags);
1731 lbs_deb_leave(LBS_DEB_HOST);
1736 * @brief This function executes next command in command
1737 * pending queue. It will put firmware back to PS mode
1740 * @param priv A pointer to struct lbs_private structure
1743 int lbs_execute_next_command(struct lbs_private *priv)
1745 struct cmd_ctrl_node *cmdnode = NULL;
1746 struct cmd_header *cmd;
1747 unsigned long flags;
1750 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1751 * only caller to us is lbs_thread() and we get even when a
1752 * data packet is received */
1753 lbs_deb_enter(LBS_DEB_THREAD);
1755 spin_lock_irqsave(&priv->driver_lock, flags);
1757 if (priv->cur_cmd) {
1758 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1759 spin_unlock_irqrestore(&priv->driver_lock, flags);
1764 if (!list_empty(&priv->cmdpendingq)) {
1765 cmdnode = list_first_entry(&priv->cmdpendingq,
1766 struct cmd_ctrl_node, list);
1769 spin_unlock_irqrestore(&priv->driver_lock, flags);
1772 cmd = cmdnode->cmdbuf;
1774 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1775 if ((priv->psstate == PS_STATE_SLEEP) ||
1776 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1778 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1779 le16_to_cpu(cmd->command),
1784 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1785 "0x%04x in psstate %d\n",
1786 le16_to_cpu(cmd->command), priv->psstate);
1787 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1789 * 1. Non-PS command:
1790 * Queue it. set needtowakeup to TRUE if current state
1791 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1792 * 2. PS command but not Exit_PS:
1794 * 3. PS command Exit_PS:
1795 * Set needtowakeup to TRUE if current state is SLEEP,
1796 * otherwise send this command down to firmware
1799 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1800 /* Prepare to send Exit PS,
1801 * this non PS command will be sent later */
1802 if ((priv->psstate == PS_STATE_SLEEP)
1803 || (priv->psstate == PS_STATE_PRE_SLEEP)
1805 /* w/ new scheme, it will not reach here.
1806 since it is blocked in main_thread. */
1807 priv->needtowakeup = 1;
1809 lbs_ps_wakeup(priv, 0);
1815 * PS command. Ignore it if it is not Exit_PS.
1816 * otherwise send it down immediately.
1818 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1821 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1824 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1826 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1827 list_del(&cmdnode->list);
1828 spin_lock_irqsave(&priv->driver_lock, flags);
1829 lbs_complete_command(priv, cmdnode, 0);
1830 spin_unlock_irqrestore(&priv->driver_lock, flags);
1836 if ((priv->psstate == PS_STATE_SLEEP) ||
1837 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1839 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1840 list_del(&cmdnode->list);
1841 spin_lock_irqsave(&priv->driver_lock, flags);
1842 lbs_complete_command(priv, cmdnode, 0);
1843 spin_unlock_irqrestore(&priv->driver_lock, flags);
1844 priv->needtowakeup = 1;
1851 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1854 list_del(&cmdnode->list);
1855 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1856 le16_to_cpu(cmd->command));
1857 lbs_submit_command(priv, cmdnode);
1860 * check if in power save mode, if yes, put the device back
1863 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1864 (priv->psstate == PS_STATE_FULL_POWER) &&
1865 ((priv->connect_status == LBS_CONNECTED) ||
1866 (priv->mesh_connect_status == LBS_CONNECTED))) {
1867 if (priv->secinfo.WPAenabled ||
1868 priv->secinfo.WPA2enabled) {
1869 /* check for valid WPA group keys */
1870 if (priv->wpa_mcast_key.len ||
1871 priv->wpa_unicast_key.len) {
1873 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1874 " go back to PS_SLEEP");
1875 lbs_ps_sleep(priv, 0);
1879 "EXEC_NEXT_CMD: cmdpendingq empty, "
1880 "go back to PS_SLEEP");
1881 lbs_ps_sleep(priv, 0);
1888 lbs_deb_leave(LBS_DEB_THREAD);
1892 void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
1894 union iwreq_data iwrq;
1897 lbs_deb_enter(LBS_DEB_WEXT);
1899 memset(&iwrq, 0, sizeof(union iwreq_data));
1900 memset(buf, 0, sizeof(buf));
1902 snprintf(buf, sizeof(buf) - 1, "%s", str);
1904 iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
1906 /* Send Event to upper layer */
1907 lbs_deb_wext("event indication string %s\n", (char *)buf);
1908 lbs_deb_wext("event indication length %d\n", iwrq.data.length);
1909 lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
1911 wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1913 lbs_deb_leave(LBS_DEB_WEXT);
1916 static void lbs_send_confirmsleep(struct lbs_private *priv)
1918 unsigned long flags;
1921 lbs_deb_enter(LBS_DEB_HOST);
1922 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1923 sizeof(confirm_sleep));
1925 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1926 sizeof(confirm_sleep));
1928 lbs_pr_alert("confirm_sleep failed\n");
1932 spin_lock_irqsave(&priv->driver_lock, flags);
1934 /* We don't get a response on the sleep-confirmation */
1935 priv->dnld_sent = DNLD_RES_RECEIVED;
1937 /* If nothing to do, go back to sleep (?) */
1938 if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1939 priv->psstate = PS_STATE_SLEEP;
1941 spin_unlock_irqrestore(&priv->driver_lock, flags);
1944 lbs_deb_leave(LBS_DEB_HOST);
1947 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1949 lbs_deb_enter(LBS_DEB_HOST);
1952 * PS is currently supported only in Infrastructure mode
1953 * Remove this check if it is to be supported in IBSS mode also
1956 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1957 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1959 lbs_deb_leave(LBS_DEB_HOST);
1963 * @brief This function sends Exit_PS command to firmware.
1965 * @param priv A pointer to struct lbs_private structure
1966 * @param wait_option wait response or not
1969 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1973 lbs_deb_enter(LBS_DEB_HOST);
1975 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1977 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1979 wait_option, 0, &Localpsmode);
1981 lbs_deb_leave(LBS_DEB_HOST);
1985 * @brief This function checks condition and prepares to
1986 * send sleep confirm command to firmware if ok.
1988 * @param priv A pointer to struct lbs_private structure
1989 * @param psmode Power Saving mode
1992 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1994 unsigned long flags =0;
1997 lbs_deb_enter(LBS_DEB_HOST);
1999 spin_lock_irqsave(&priv->driver_lock, flags);
2000 if (priv->dnld_sent) {
2002 lbs_deb_host("dnld_sent was set\n");
2005 /* In-progress command? */
2006 if (priv->cur_cmd) {
2008 lbs_deb_host("cur_cmd was set\n");
2011 /* Pending events or command responses? */
2012 if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
2014 lbs_deb_host("pending events or command responses\n");
2016 spin_unlock_irqrestore(&priv->driver_lock, flags);
2019 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
2020 lbs_send_confirmsleep(priv);
2022 lbs_deb_host("sleep confirm has been delayed\n");
2025 lbs_deb_leave(LBS_DEB_HOST);
2030 * @brief Configures the transmission power control functionality.
2032 * @param priv A pointer to struct lbs_private structure
2033 * @param enable Transmission power control enable
2034 * @param p0 Power level when link quality is good (dBm).
2035 * @param p1 Power level when link quality is fair (dBm).
2036 * @param p2 Power level when link quality is poor (dBm).
2037 * @param usesnr Use Signal to Noise Ratio in TPC
2039 * @return 0 on success
2041 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
2042 int8_t p2, int usesnr)
2044 struct cmd_ds_802_11_tpc_cfg cmd;
2047 memset(&cmd, 0, sizeof(cmd));
2048 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2049 cmd.action = cpu_to_le16(CMD_ACT_SET);
2050 cmd.enable = !!enable;
2051 cmd.usesnr = !!usesnr;
2056 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
2062 * @brief Configures the power adaptation settings.
2064 * @param priv A pointer to struct lbs_private structure
2065 * @param enable Power adaptation enable
2066 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
2067 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
2068 * @param p2 Power level for 48 and 54 Mbps (dBm).
2070 * @return 0 on Success
2073 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
2074 int8_t p1, int8_t p2)
2076 struct cmd_ds_802_11_pa_cfg cmd;
2079 memset(&cmd, 0, sizeof(cmd));
2080 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2081 cmd.action = cpu_to_le16(CMD_ACT_SET);
2082 cmd.enable = !!enable;
2087 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
2093 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
2094 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
2095 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2096 unsigned long callback_arg)
2098 struct cmd_ctrl_node *cmdnode;
2100 lbs_deb_enter(LBS_DEB_HOST);
2102 if (priv->surpriseremoved) {
2103 lbs_deb_host("PREP_CMD: card removed\n");
2104 cmdnode = ERR_PTR(-ENOENT);
2108 if (!lbs_is_cmd_allowed(priv)) {
2109 cmdnode = ERR_PTR(-EBUSY);
2113 cmdnode = lbs_get_cmd_ctrl_node(priv);
2114 if (cmdnode == NULL) {
2115 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
2117 /* Wake up main thread to execute next command */
2118 wake_up_interruptible(&priv->waitq);
2119 cmdnode = ERR_PTR(-ENOBUFS);
2123 cmdnode->callback = callback;
2124 cmdnode->callback_arg = callback_arg;
2126 /* Copy the incoming command to the buffer */
2127 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
2129 /* Set sequence number, clean result, move to buffer */
2131 cmdnode->cmdbuf->command = cpu_to_le16(command);
2132 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
2133 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
2134 cmdnode->cmdbuf->result = 0;
2136 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
2138 cmdnode->cmdwaitqwoken = 0;
2139 lbs_queue_cmd(priv, cmdnode);
2140 wake_up_interruptible(&priv->waitq);
2143 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
2147 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
2148 struct cmd_header *in_cmd, int in_cmd_size)
2150 lbs_deb_enter(LBS_DEB_CMD);
2151 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2152 lbs_cmd_async_callback, 0);
2153 lbs_deb_leave(LBS_DEB_CMD);
2156 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
2157 struct cmd_header *in_cmd, int in_cmd_size,
2158 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2159 unsigned long callback_arg)
2161 struct cmd_ctrl_node *cmdnode;
2162 unsigned long flags;
2165 lbs_deb_enter(LBS_DEB_HOST);
2167 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2168 callback, callback_arg);
2169 if (IS_ERR(cmdnode)) {
2170 ret = PTR_ERR(cmdnode);
2175 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
2177 spin_lock_irqsave(&priv->driver_lock, flags);
2178 ret = cmdnode->result;
2180 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
2183 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
2184 spin_unlock_irqrestore(&priv->driver_lock, flags);
2187 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
2190 EXPORT_SYMBOL_GPL(__lbs_cmd);