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)) {
194 lbs_deb_leave(LBS_DEB_CMD);
198 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
199 struct wol_config *p_wol_config)
201 struct cmd_ds_host_sleep cmd_config;
204 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
205 cmd_config.criteria = cpu_to_le32(criteria);
206 cmd_config.gpio = priv->wol_gpio;
207 cmd_config.gap = priv->wol_gap;
209 if (p_wol_config != NULL)
210 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
211 sizeof(struct wol_config));
213 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
215 ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
218 lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
219 priv->wol_criteria = criteria;
221 memcpy((uint8_t *) p_wol_config,
222 (uint8_t *)&cmd_config.wol_conf,
223 sizeof(struct wol_config));
225 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
230 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
232 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
235 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
237 lbs_deb_enter(LBS_DEB_CMD);
239 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
240 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
242 psm->action = cpu_to_le16(cmd_action);
243 psm->multipledtim = 0;
244 switch (cmd_action) {
245 case CMD_SUBCMD_ENTER_PS:
246 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
248 psm->locallisteninterval = 0;
249 psm->nullpktinterval = 0;
251 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
254 case CMD_SUBCMD_EXIT_PS:
255 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
258 case CMD_SUBCMD_SLEEP_CONFIRMED:
259 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
266 lbs_deb_leave(LBS_DEB_CMD);
270 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
271 struct sleep_params *sp)
273 struct cmd_ds_802_11_sleep_params cmd;
276 lbs_deb_enter(LBS_DEB_CMD);
278 if (cmd_action == CMD_ACT_GET) {
279 memset(&cmd, 0, sizeof(cmd));
281 cmd.error = cpu_to_le16(sp->sp_error);
282 cmd.offset = cpu_to_le16(sp->sp_offset);
283 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
284 cmd.calcontrol = sp->sp_calcontrol;
285 cmd.externalsleepclk = sp->sp_extsleepclk;
286 cmd.reserved = cpu_to_le16(sp->sp_reserved);
288 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
289 cmd.action = cpu_to_le16(cmd_action);
291 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
294 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
295 "calcontrol 0x%x extsleepclk 0x%x\n",
296 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
297 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
298 cmd.externalsleepclk);
300 sp->sp_error = le16_to_cpu(cmd.error);
301 sp->sp_offset = le16_to_cpu(cmd.offset);
302 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
303 sp->sp_calcontrol = cmd.calcontrol;
304 sp->sp_extsleepclk = cmd.externalsleepclk;
305 sp->sp_reserved = le16_to_cpu(cmd.reserved);
308 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
312 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
316 lbs_deb_enter(LBS_DEB_CMD);
318 if (priv->is_deep_sleep) {
319 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
320 !priv->is_deep_sleep, (10 * HZ))) {
321 lbs_pr_err("ds_awake_q: timer expired\n");
326 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
330 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
334 lbs_deb_enter(LBS_DEB_CMD);
337 if (priv->is_deep_sleep != 1) {
338 lbs_deb_cmd("deep sleep: sleep\n");
339 BUG_ON(!priv->enter_deep_sleep);
340 ret = priv->enter_deep_sleep(priv);
342 netif_stop_queue(priv->dev);
343 netif_carrier_off(priv->dev);
346 lbs_pr_err("deep sleep: already enabled\n");
349 if (priv->is_deep_sleep) {
350 lbs_deb_cmd("deep sleep: wakeup\n");
351 BUG_ON(!priv->exit_deep_sleep);
352 ret = priv->exit_deep_sleep(priv);
354 ret = lbs_wait_for_ds_awake(priv);
356 lbs_pr_err("deep sleep: wakeup"
362 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
366 int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
367 struct assoc_request *assoc)
369 struct cmd_ds_802_11_set_wep cmd;
372 lbs_deb_enter(LBS_DEB_CMD);
374 memset(&cmd, 0, sizeof(cmd));
375 cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
376 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
378 cmd.action = cpu_to_le16(cmd_action);
380 if (cmd_action == CMD_ACT_ADD) {
383 /* default tx key index */
384 cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
385 CMD_WEP_KEY_INDEX_MASK);
387 /* Copy key types and material to host command structure */
388 for (i = 0; i < 4; i++) {
389 struct enc_key *pkey = &assoc->wep_keys[i];
393 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
394 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
395 lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
397 case KEY_LEN_WEP_104:
398 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
399 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
400 lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
405 lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
412 } else if (cmd_action == CMD_ACT_REMOVE) {
413 /* ACT_REMOVE clears _all_ WEP keys */
415 /* default tx key index */
416 cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
417 CMD_WEP_KEY_INDEX_MASK);
418 lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
421 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
423 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
427 int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
430 struct cmd_ds_802_11_enable_rsn cmd;
433 lbs_deb_enter(LBS_DEB_CMD);
435 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
436 cmd.action = cpu_to_le16(cmd_action);
438 if (cmd_action == CMD_ACT_GET)
442 cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
444 cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
445 lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
448 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
449 if (!ret && cmd_action == CMD_ACT_GET)
450 *enable = le16_to_cpu(cmd.enable);
452 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
456 static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
459 lbs_deb_enter(LBS_DEB_CMD);
461 if (key->flags & KEY_INFO_WPA_ENABLED)
462 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
463 if (key->flags & KEY_INFO_WPA_UNICAST)
464 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
465 if (key->flags & KEY_INFO_WPA_MCAST)
466 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
468 keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
469 keyparam->keytypeid = cpu_to_le16(key->type);
470 keyparam->keylen = cpu_to_le16(key->len);
471 memcpy(keyparam->key, key->key, key->len);
473 /* Length field doesn't include the {type,length} header */
474 keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
475 lbs_deb_leave(LBS_DEB_CMD);
478 int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
479 struct assoc_request *assoc)
481 struct cmd_ds_802_11_key_material cmd;
485 lbs_deb_enter(LBS_DEB_CMD);
487 cmd.action = cpu_to_le16(cmd_action);
488 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
490 if (cmd_action == CMD_ACT_GET) {
491 cmd.hdr.size = cpu_to_le16(S_DS_GEN + 2);
493 memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
495 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
496 set_one_wpa_key(&cmd.keyParamSet[index],
497 &assoc->wpa_unicast_key);
501 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
502 set_one_wpa_key(&cmd.keyParamSet[index],
503 &assoc->wpa_mcast_key);
507 /* The common header and as many keys as we included */
508 cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
509 keyParamSet[index]));
511 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
512 /* Copy the returned key to driver private data */
513 if (!ret && cmd_action == CMD_ACT_GET) {
514 void *buf_ptr = cmd.keyParamSet;
515 void *resp_end = &(&cmd)[1];
517 while (buf_ptr < resp_end) {
518 struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
520 uint16_t param_set_len = le16_to_cpu(keyparam->length);
521 uint16_t key_len = le16_to_cpu(keyparam->keylen);
522 uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
523 uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
526 end = (void *)keyparam + sizeof(keyparam->type)
527 + sizeof(keyparam->length) + param_set_len;
529 /* Make sure we don't access past the end of the IEs */
533 if (key_flags & KEY_INFO_WPA_UNICAST)
534 key = &priv->wpa_unicast_key;
535 else if (key_flags & KEY_INFO_WPA_MCAST)
536 key = &priv->wpa_mcast_key;
540 /* Copy returned key into driver */
541 memset(key, 0, sizeof(struct enc_key));
542 if (key_len > sizeof(key->key))
544 key->type = key_type;
545 key->flags = key_flags;
547 memcpy(key->key, keyparam->key, key->len);
553 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
558 * @brief Set an SNMP MIB value
560 * @param priv A pointer to struct lbs_private structure
561 * @param oid The OID to set in the firmware
562 * @param val Value to set the OID to
564 * @return 0 on success, error on failure
566 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
568 struct cmd_ds_802_11_snmp_mib cmd;
571 lbs_deb_enter(LBS_DEB_CMD);
573 memset(&cmd, 0, sizeof (cmd));
574 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
575 cmd.action = cpu_to_le16(CMD_ACT_SET);
576 cmd.oid = cpu_to_le16((u16) oid);
579 case SNMP_MIB_OID_BSS_TYPE:
580 cmd.bufsize = cpu_to_le16(sizeof(u8));
581 cmd.value[0] = (val == IW_MODE_ADHOC) ? 2 : 1;
583 case SNMP_MIB_OID_11D_ENABLE:
584 case SNMP_MIB_OID_FRAG_THRESHOLD:
585 case SNMP_MIB_OID_RTS_THRESHOLD:
586 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
587 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
588 cmd.bufsize = cpu_to_le16(sizeof(u16));
589 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
592 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
597 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
598 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
600 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
603 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
608 * @brief Get an SNMP MIB value
610 * @param priv A pointer to struct lbs_private structure
611 * @param oid The OID to retrieve from the firmware
612 * @param out_val Location for the returned value
614 * @return 0 on success, error on failure
616 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
618 struct cmd_ds_802_11_snmp_mib cmd;
621 lbs_deb_enter(LBS_DEB_CMD);
623 memset(&cmd, 0, sizeof (cmd));
624 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
625 cmd.action = cpu_to_le16(CMD_ACT_GET);
626 cmd.oid = cpu_to_le16(oid);
628 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
632 switch (le16_to_cpu(cmd.bufsize)) {
634 if (oid == SNMP_MIB_OID_BSS_TYPE) {
635 if (cmd.value[0] == 2)
636 *out_val = IW_MODE_ADHOC;
638 *out_val = IW_MODE_INFRA;
640 *out_val = cmd.value[0];
643 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
646 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
647 oid, le16_to_cpu(cmd.bufsize));
652 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
657 * @brief Get the min, max, and current TX power
659 * @param priv A pointer to struct lbs_private structure
660 * @param curlevel Current power level in dBm
661 * @param minlevel Minimum supported power level in dBm (optional)
662 * @param maxlevel Maximum supported power level in dBm (optional)
664 * @return 0 on success, error on failure
666 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
669 struct cmd_ds_802_11_rf_tx_power cmd;
672 lbs_deb_enter(LBS_DEB_CMD);
674 memset(&cmd, 0, sizeof(cmd));
675 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
676 cmd.action = cpu_to_le16(CMD_ACT_GET);
678 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
680 *curlevel = le16_to_cpu(cmd.curlevel);
682 *minlevel = cmd.minlevel;
684 *maxlevel = cmd.maxlevel;
687 lbs_deb_leave(LBS_DEB_CMD);
692 * @brief Set the TX power
694 * @param priv A pointer to struct lbs_private structure
695 * @param dbm The desired power level in dBm
697 * @return 0 on success, error on failure
699 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
701 struct cmd_ds_802_11_rf_tx_power cmd;
704 lbs_deb_enter(LBS_DEB_CMD);
706 memset(&cmd, 0, sizeof(cmd));
707 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
708 cmd.action = cpu_to_le16(CMD_ACT_SET);
709 cmd.curlevel = cpu_to_le16(dbm);
711 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
713 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
715 lbs_deb_leave(LBS_DEB_CMD);
719 static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
720 u16 cmd_action, void *pdata_buf)
722 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
724 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
726 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
729 monitor->action = cpu_to_le16(cmd_action);
730 if (cmd_action == CMD_ACT_SET) {
732 cpu_to_le16((u16) (*(u32 *) pdata_buf));
738 static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
758 int i = lbs_data_rate_to_fw_index(rate);
760 ratemask = (0x1fef >> (12 - i));
763 return cpu_to_le16(ratemask);
766 int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
769 struct cmd_ds_802_11_rate_adapt_rateset cmd;
772 lbs_deb_enter(LBS_DEB_CMD);
774 if (!priv->cur_rate && !priv->enablehwauto)
777 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
779 cmd.action = cpu_to_le16(cmd_action);
780 cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
781 cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
782 ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
783 if (!ret && cmd_action == CMD_ACT_GET) {
784 priv->ratebitmap = le16_to_cpu(cmd.bitmap);
785 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
788 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
791 EXPORT_SYMBOL_GPL(lbs_cmd_802_11_rate_adapt_rateset);
794 * @brief Set the data rate
796 * @param priv A pointer to struct lbs_private structure
797 * @param rate The desired data rate, or 0 to clear a locked rate
799 * @return 0 on success, error on failure
801 int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
803 struct cmd_ds_802_11_data_rate cmd;
806 lbs_deb_enter(LBS_DEB_CMD);
808 memset(&cmd, 0, sizeof(cmd));
809 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
812 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
813 cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
814 if (cmd.rates[0] == 0) {
815 lbs_deb_cmd("DATA_RATE: invalid requested rate of"
820 lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
822 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
823 lbs_deb_cmd("DATA_RATE: setting auto\n");
826 ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
830 lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof (cmd));
832 /* FIXME: get actual rates FW can do if this command actually returns
833 * all data rates supported.
835 priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
836 lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
839 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
844 * @brief Get the radio channel
846 * @param priv A pointer to struct lbs_private structure
848 * @return The channel on success, error on failure
850 static int lbs_get_channel(struct lbs_private *priv)
852 struct cmd_ds_802_11_rf_channel cmd;
855 lbs_deb_enter(LBS_DEB_CMD);
857 memset(&cmd, 0, sizeof(cmd));
858 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
859 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
861 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
865 ret = le16_to_cpu(cmd.channel);
866 lbs_deb_cmd("current radio channel is %d\n", ret);
869 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
873 int lbs_update_channel(struct lbs_private *priv)
877 /* the channel in f/w could be out of sync; get the current channel */
878 lbs_deb_enter(LBS_DEB_ASSOC);
880 ret = lbs_get_channel(priv);
882 priv->curbssparams.channel = ret;
885 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
890 * @brief Set the radio channel
892 * @param priv A pointer to struct lbs_private structure
893 * @param channel The desired channel, or 0 to clear a locked channel
895 * @return 0 on success, error on failure
897 int lbs_set_channel(struct lbs_private *priv, u8 channel)
899 struct cmd_ds_802_11_rf_channel cmd;
901 u8 old_channel = priv->curbssparams.channel;
905 lbs_deb_enter(LBS_DEB_CMD);
907 memset(&cmd, 0, sizeof(cmd));
908 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
909 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
910 cmd.channel = cpu_to_le16(channel);
912 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
916 priv->curbssparams.channel = (uint8_t) le16_to_cpu(cmd.channel);
917 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
918 priv->curbssparams.channel);
921 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
925 static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
926 struct cmd_ds_command *cmd)
929 lbs_deb_enter(LBS_DEB_CMD);
930 cmd->command = cpu_to_le16(CMD_802_11_RSSI);
931 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
932 cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
934 /* reset Beacon SNR/NF/RSSI values */
935 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
936 priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
937 priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
938 priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
939 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
940 priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
942 lbs_deb_leave(LBS_DEB_CMD);
946 static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
947 u8 cmd_action, void *pdata_buf)
949 struct lbs_offset_value *offval;
951 lbs_deb_enter(LBS_DEB_CMD);
953 offval = (struct lbs_offset_value *)pdata_buf;
955 switch (le16_to_cpu(cmdptr->command)) {
956 case CMD_MAC_REG_ACCESS:
958 struct cmd_ds_mac_reg_access *macreg;
961 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
964 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
967 macreg->action = cpu_to_le16(cmd_action);
968 macreg->offset = cpu_to_le16((u16) offval->offset);
969 macreg->value = cpu_to_le32(offval->value);
974 case CMD_BBP_REG_ACCESS:
976 struct cmd_ds_bbp_reg_access *bbpreg;
980 (struct cmd_ds_bbp_reg_access)
983 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
986 bbpreg->action = cpu_to_le16(cmd_action);
987 bbpreg->offset = cpu_to_le16((u16) offval->offset);
988 bbpreg->value = (u8) offval->value;
993 case CMD_RF_REG_ACCESS:
995 struct cmd_ds_rf_reg_access *rfreg;
999 (struct cmd_ds_rf_reg_access) +
1002 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
1005 rfreg->action = cpu_to_le16(cmd_action);
1006 rfreg->offset = cpu_to_le16((u16) offval->offset);
1007 rfreg->value = (u8) offval->value;
1016 lbs_deb_leave(LBS_DEB_CMD);
1020 static int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
1021 u16 cmd_action, void *pdata_buf)
1023 struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
1024 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1026 cmd->command = cpu_to_le16(CMD_BT_ACCESS);
1027 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
1029 bt_access->action = cpu_to_le16(cmd_action);
1031 switch (cmd_action) {
1032 case CMD_ACT_BT_ACCESS_ADD:
1033 memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
1034 lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
1036 case CMD_ACT_BT_ACCESS_DEL:
1037 memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
1038 lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
1040 case CMD_ACT_BT_ACCESS_LIST:
1041 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
1043 case CMD_ACT_BT_ACCESS_RESET:
1045 case CMD_ACT_BT_ACCESS_SET_INVERT:
1046 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
1048 case CMD_ACT_BT_ACCESS_GET_INVERT:
1053 lbs_deb_leave(LBS_DEB_CMD);
1057 static int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
1058 u16 cmd_action, void *pdata_buf)
1060 struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
1061 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1063 cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
1064 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
1068 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
1070 memset(fwt_access, 0, sizeof(*fwt_access));
1072 fwt_access->action = cpu_to_le16(cmd_action);
1074 lbs_deb_leave(LBS_DEB_CMD);
1078 int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
1079 struct cmd_ds_mesh_access *cmd)
1083 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1085 cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
1086 cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
1087 cmd->hdr.result = 0;
1089 cmd->action = cpu_to_le16(cmd_action);
1091 ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
1093 lbs_deb_leave(LBS_DEB_CMD);
1097 static int __lbs_mesh_config_send(struct lbs_private *priv,
1098 struct cmd_ds_mesh_config *cmd,
1099 uint16_t action, uint16_t type)
1102 u16 command = CMD_MESH_CONFIG_OLD;
1104 lbs_deb_enter(LBS_DEB_CMD);
1107 * Command id is 0xac for v10 FW along with mesh interface
1108 * id in bits 14-13-12.
1110 if (priv->mesh_fw_ver == MESH_FW_NEW)
1111 command = CMD_MESH_CONFIG |
1112 (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
1114 cmd->hdr.command = cpu_to_le16(command);
1115 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
1116 cmd->hdr.result = 0;
1118 cmd->type = cpu_to_le16(type);
1119 cmd->action = cpu_to_le16(action);
1121 ret = lbs_cmd_with_response(priv, command, cmd);
1123 lbs_deb_leave(LBS_DEB_CMD);
1127 int lbs_mesh_config_send(struct lbs_private *priv,
1128 struct cmd_ds_mesh_config *cmd,
1129 uint16_t action, uint16_t type)
1133 if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
1136 ret = __lbs_mesh_config_send(priv, cmd, action, type);
1140 /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
1141 * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
1142 * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
1143 * lbs_mesh_config_send.
1145 int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
1147 struct cmd_ds_mesh_config cmd;
1148 struct mrvl_meshie *ie;
1149 DECLARE_SSID_BUF(ssid);
1151 memset(&cmd, 0, sizeof(cmd));
1152 cmd.channel = cpu_to_le16(chan);
1153 ie = (struct mrvl_meshie *)cmd.data;
1156 case CMD_ACT_MESH_CONFIG_START:
1157 ie->id = WLAN_EID_GENERIC;
1158 ie->val.oui[0] = 0x00;
1159 ie->val.oui[1] = 0x50;
1160 ie->val.oui[2] = 0x43;
1161 ie->val.type = MARVELL_MESH_IE_TYPE;
1162 ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
1163 ie->val.version = MARVELL_MESH_IE_VERSION;
1164 ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
1165 ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
1166 ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
1167 ie->val.mesh_id_len = priv->mesh_ssid_len;
1168 memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
1169 ie->len = sizeof(struct mrvl_meshie_val) -
1170 IW_ESSID_MAX_SIZE + priv->mesh_ssid_len;
1171 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
1173 case CMD_ACT_MESH_CONFIG_STOP:
1178 lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
1179 action, priv->mesh_tlv, chan,
1180 print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
1182 return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
1185 static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
1186 struct cmd_ds_command *cmd,
1189 struct cmd_ds_802_11_beacon_control
1190 *bcn_ctrl = &cmd->params.bcn_ctrl;
1192 lbs_deb_enter(LBS_DEB_CMD);
1194 cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
1196 cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
1198 bcn_ctrl->action = cpu_to_le16(cmd_action);
1199 bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
1200 bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
1202 lbs_deb_leave(LBS_DEB_CMD);
1206 static void lbs_queue_cmd(struct lbs_private *priv,
1207 struct cmd_ctrl_node *cmdnode)
1209 unsigned long flags;
1212 lbs_deb_enter(LBS_DEB_HOST);
1215 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
1218 if (!cmdnode->cmdbuf->size) {
1219 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
1222 cmdnode->result = 0;
1224 /* Exit_PS command needs to be queued in the header always. */
1225 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
1226 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
1228 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1229 if (priv->psstate != PS_STATE_FULL_POWER)
1234 spin_lock_irqsave(&priv->driver_lock, flags);
1237 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
1239 list_add(&cmdnode->list, &priv->cmdpendingq);
1241 spin_unlock_irqrestore(&priv->driver_lock, flags);
1243 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
1244 le16_to_cpu(cmdnode->cmdbuf->command));
1247 lbs_deb_leave(LBS_DEB_HOST);
1250 static void lbs_submit_command(struct lbs_private *priv,
1251 struct cmd_ctrl_node *cmdnode)
1253 unsigned long flags;
1254 struct cmd_header *cmd;
1260 lbs_deb_enter(LBS_DEB_HOST);
1262 cmd = cmdnode->cmdbuf;
1264 spin_lock_irqsave(&priv->driver_lock, flags);
1265 priv->cur_cmd = cmdnode;
1266 priv->cur_cmd_retcode = 0;
1267 spin_unlock_irqrestore(&priv->driver_lock, flags);
1269 cmdsize = le16_to_cpu(cmd->size);
1270 command = le16_to_cpu(cmd->command);
1272 /* These commands take longer */
1273 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1276 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1277 command, le16_to_cpu(cmd->seqnum), cmdsize);
1278 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1280 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1283 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1284 /* Let the timer kick in and retry, and potentially reset
1285 the whole thing if the condition persists */
1289 if (command == CMD_802_11_DEEP_SLEEP) {
1290 if (priv->is_auto_deep_sleep_enabled) {
1291 priv->wakeup_dev_required = 1;
1292 priv->dnld_sent = 0;
1294 priv->is_deep_sleep = 1;
1295 lbs_complete_command(priv, cmdnode, 0);
1297 /* Setup the timer after transmit command */
1298 mod_timer(&priv->command_timer, jiffies + timeo);
1301 lbs_deb_leave(LBS_DEB_HOST);
1305 * This function inserts command node to cmdfreeq
1306 * after cleans it. Requires priv->driver_lock held.
1308 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1309 struct cmd_ctrl_node *cmdnode)
1311 lbs_deb_enter(LBS_DEB_HOST);
1316 cmdnode->callback = NULL;
1317 cmdnode->callback_arg = 0;
1319 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1321 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1323 lbs_deb_leave(LBS_DEB_HOST);
1326 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1327 struct cmd_ctrl_node *ptempcmd)
1329 unsigned long flags;
1331 spin_lock_irqsave(&priv->driver_lock, flags);
1332 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1333 spin_unlock_irqrestore(&priv->driver_lock, flags);
1336 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1339 if (cmd == priv->cur_cmd)
1340 priv->cur_cmd_retcode = result;
1342 cmd->result = result;
1343 cmd->cmdwaitqwoken = 1;
1344 wake_up_interruptible(&cmd->cmdwait_q);
1346 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1347 __lbs_cleanup_and_insert_cmd(priv, cmd);
1348 priv->cur_cmd = NULL;
1351 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1353 struct cmd_ds_802_11_radio_control cmd;
1356 lbs_deb_enter(LBS_DEB_CMD);
1358 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1359 cmd.action = cpu_to_le16(CMD_ACT_SET);
1361 /* Only v8 and below support setting the preamble */
1362 if (priv->fwrelease < 0x09000000) {
1364 case RADIO_PREAMBLE_SHORT:
1365 if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
1368 case RADIO_PREAMBLE_AUTO:
1369 case RADIO_PREAMBLE_LONG:
1370 cmd.control = cpu_to_le16(preamble);
1378 cmd.control |= cpu_to_le16(0x1);
1380 cmd.control &= cpu_to_le16(~0x1);
1381 priv->txpower_cur = 0;
1384 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1385 radio_on ? "ON" : "OFF", preamble);
1387 priv->radio_on = radio_on;
1389 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1392 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1396 void lbs_set_mac_control(struct lbs_private *priv)
1398 struct cmd_ds_mac_control cmd;
1400 lbs_deb_enter(LBS_DEB_CMD);
1402 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1403 cmd.action = cpu_to_le16(priv->mac_control);
1406 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1408 lbs_deb_leave(LBS_DEB_CMD);
1412 * @brief This function prepare the command before send to firmware.
1414 * @param priv A pointer to struct lbs_private structure
1415 * @param cmd_no command number
1416 * @param cmd_action command action: GET or SET
1417 * @param wait_option wait option: wait response or not
1418 * @param cmd_oid cmd oid: treated as sub command
1419 * @param pdata_buf A pointer to informaion buffer
1422 int lbs_prepare_and_send_command(struct lbs_private *priv,
1425 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1428 struct cmd_ctrl_node *cmdnode;
1429 struct cmd_ds_command *cmdptr;
1430 unsigned long flags;
1432 lbs_deb_enter(LBS_DEB_HOST);
1435 lbs_deb_host("PREP_CMD: priv is NULL\n");
1440 if (priv->surpriseremoved) {
1441 lbs_deb_host("PREP_CMD: card removed\n");
1446 if (!lbs_is_cmd_allowed(priv)) {
1451 cmdnode = lbs_get_cmd_ctrl_node(priv);
1453 if (cmdnode == NULL) {
1454 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1456 /* Wake up main thread to execute next command */
1457 wake_up_interruptible(&priv->waitq);
1462 cmdnode->callback = NULL;
1463 cmdnode->callback_arg = (unsigned long)pdata_buf;
1465 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1467 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1469 /* Set sequence number, command and INT option */
1471 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1473 cmdptr->command = cpu_to_le16(cmd_no);
1477 case CMD_802_11_PS_MODE:
1478 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1481 case CMD_MAC_REG_ACCESS:
1482 case CMD_BBP_REG_ACCESS:
1483 case CMD_RF_REG_ACCESS:
1484 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
1487 case CMD_802_11_MONITOR_MODE:
1488 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
1489 cmd_action, pdata_buf);
1492 case CMD_802_11_RSSI:
1493 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1496 case CMD_802_11_SET_AFC:
1497 case CMD_802_11_GET_AFC:
1499 cmdptr->command = cpu_to_le16(cmd_no);
1500 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1503 memmove(&cmdptr->params.afc,
1504 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1509 case CMD_802_11_TPC_CFG:
1510 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1512 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1515 memmove(&cmdptr->params.tpccfg,
1516 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1520 case CMD_802_11_LED_GPIO_CTRL:
1522 struct mrvl_ie_ledgpio *gpio =
1523 (struct mrvl_ie_ledgpio*)
1524 cmdptr->params.ledgpio.data;
1526 memmove(&cmdptr->params.ledgpio,
1528 sizeof(struct cmd_ds_802_11_led_ctrl));
1531 cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
1533 #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
1535 cpu_to_le16(le16_to_cpu(gpio->header.len)
1537 + ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
1538 gpio->header.len = gpio->header.len;
1545 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1548 case CMD_FWT_ACCESS:
1549 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1553 cmdptr->command = cpu_to_le16(CMD_GET_TSF);
1554 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
1558 case CMD_802_11_BEACON_CTRL:
1559 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1561 case CMD_802_11_DEEP_SLEEP:
1562 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
1563 cmdptr->size = cpu_to_le16(S_DS_GEN);
1566 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1571 /* return error, since the command preparation failed */
1573 lbs_deb_host("PREP_CMD: command preparation failed\n");
1574 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1579 cmdnode->cmdwaitqwoken = 0;
1581 lbs_queue_cmd(priv, cmdnode);
1582 wake_up_interruptible(&priv->waitq);
1584 if (wait_option & CMD_OPTION_WAITFORRSP) {
1585 lbs_deb_host("PREP_CMD: wait for response\n");
1587 wait_event_interruptible(cmdnode->cmdwait_q,
1588 cmdnode->cmdwaitqwoken);
1591 spin_lock_irqsave(&priv->driver_lock, flags);
1592 if (priv->cur_cmd_retcode) {
1593 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1594 priv->cur_cmd_retcode);
1595 priv->cur_cmd_retcode = 0;
1598 spin_unlock_irqrestore(&priv->driver_lock, flags);
1601 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1606 * @brief This function allocates the command buffer and link
1607 * it to command free queue.
1609 * @param priv A pointer to struct lbs_private structure
1612 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1617 struct cmd_ctrl_node *cmdarray;
1619 lbs_deb_enter(LBS_DEB_HOST);
1621 /* Allocate and initialize the command array */
1622 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1623 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1624 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1628 priv->cmd_array = cmdarray;
1630 /* Allocate and initialize each command buffer in the command array */
1631 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1632 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1633 if (!cmdarray[i].cmdbuf) {
1634 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1640 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1641 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1642 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1647 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1652 * @brief This function frees the command buffer.
1654 * @param priv A pointer to struct lbs_private structure
1657 int lbs_free_cmd_buffer(struct lbs_private *priv)
1659 struct cmd_ctrl_node *cmdarray;
1662 lbs_deb_enter(LBS_DEB_HOST);
1664 /* need to check if cmd array is allocated or not */
1665 if (priv->cmd_array == NULL) {
1666 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1670 cmdarray = priv->cmd_array;
1672 /* Release shared memory buffers */
1673 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1674 if (cmdarray[i].cmdbuf) {
1675 kfree(cmdarray[i].cmdbuf);
1676 cmdarray[i].cmdbuf = NULL;
1680 /* Release cmd_ctrl_node */
1681 if (priv->cmd_array) {
1682 kfree(priv->cmd_array);
1683 priv->cmd_array = NULL;
1687 lbs_deb_leave(LBS_DEB_HOST);
1692 * @brief This function gets a free command node if available in
1693 * command free queue.
1695 * @param priv A pointer to struct lbs_private structure
1696 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1698 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1700 struct cmd_ctrl_node *tempnode;
1701 unsigned long flags;
1703 lbs_deb_enter(LBS_DEB_HOST);
1708 spin_lock_irqsave(&priv->driver_lock, flags);
1710 if (!list_empty(&priv->cmdfreeq)) {
1711 tempnode = list_first_entry(&priv->cmdfreeq,
1712 struct cmd_ctrl_node, list);
1713 list_del(&tempnode->list);
1715 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1719 spin_unlock_irqrestore(&priv->driver_lock, flags);
1721 lbs_deb_leave(LBS_DEB_HOST);
1726 * @brief This function executes next command in command
1727 * pending queue. It will put firmware back to PS mode
1730 * @param priv A pointer to struct lbs_private structure
1733 int lbs_execute_next_command(struct lbs_private *priv)
1735 struct cmd_ctrl_node *cmdnode = NULL;
1736 struct cmd_header *cmd;
1737 unsigned long flags;
1740 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1741 * only caller to us is lbs_thread() and we get even when a
1742 * data packet is received */
1743 lbs_deb_enter(LBS_DEB_THREAD);
1745 spin_lock_irqsave(&priv->driver_lock, flags);
1747 if (priv->cur_cmd) {
1748 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1749 spin_unlock_irqrestore(&priv->driver_lock, flags);
1754 if (!list_empty(&priv->cmdpendingq)) {
1755 cmdnode = list_first_entry(&priv->cmdpendingq,
1756 struct cmd_ctrl_node, list);
1759 spin_unlock_irqrestore(&priv->driver_lock, flags);
1762 cmd = cmdnode->cmdbuf;
1764 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1765 if ((priv->psstate == PS_STATE_SLEEP) ||
1766 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1768 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1769 le16_to_cpu(cmd->command),
1774 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1775 "0x%04x in psstate %d\n",
1776 le16_to_cpu(cmd->command), priv->psstate);
1777 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1779 * 1. Non-PS command:
1780 * Queue it. set needtowakeup to TRUE if current state
1781 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1782 * 2. PS command but not Exit_PS:
1784 * 3. PS command Exit_PS:
1785 * Set needtowakeup to TRUE if current state is SLEEP,
1786 * otherwise send this command down to firmware
1789 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1790 /* Prepare to send Exit PS,
1791 * this non PS command will be sent later */
1792 if ((priv->psstate == PS_STATE_SLEEP)
1793 || (priv->psstate == PS_STATE_PRE_SLEEP)
1795 /* w/ new scheme, it will not reach here.
1796 since it is blocked in main_thread. */
1797 priv->needtowakeup = 1;
1799 lbs_ps_wakeup(priv, 0);
1805 * PS command. Ignore it if it is not Exit_PS.
1806 * otherwise send it down immediately.
1808 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1811 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1814 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1816 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1817 list_del(&cmdnode->list);
1818 spin_lock_irqsave(&priv->driver_lock, flags);
1819 lbs_complete_command(priv, cmdnode, 0);
1820 spin_unlock_irqrestore(&priv->driver_lock, flags);
1826 if ((priv->psstate == PS_STATE_SLEEP) ||
1827 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1829 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1830 list_del(&cmdnode->list);
1831 spin_lock_irqsave(&priv->driver_lock, flags);
1832 lbs_complete_command(priv, cmdnode, 0);
1833 spin_unlock_irqrestore(&priv->driver_lock, flags);
1834 priv->needtowakeup = 1;
1841 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1844 list_del(&cmdnode->list);
1845 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1846 le16_to_cpu(cmd->command));
1847 lbs_submit_command(priv, cmdnode);
1850 * check if in power save mode, if yes, put the device back
1853 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1854 (priv->psstate == PS_STATE_FULL_POWER) &&
1855 ((priv->connect_status == LBS_CONNECTED) ||
1856 (priv->mesh_connect_status == LBS_CONNECTED))) {
1857 if (priv->secinfo.WPAenabled ||
1858 priv->secinfo.WPA2enabled) {
1859 /* check for valid WPA group keys */
1860 if (priv->wpa_mcast_key.len ||
1861 priv->wpa_unicast_key.len) {
1863 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1864 " go back to PS_SLEEP");
1865 lbs_ps_sleep(priv, 0);
1869 "EXEC_NEXT_CMD: cmdpendingq empty, "
1870 "go back to PS_SLEEP");
1871 lbs_ps_sleep(priv, 0);
1878 lbs_deb_leave(LBS_DEB_THREAD);
1882 void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
1884 union iwreq_data iwrq;
1887 lbs_deb_enter(LBS_DEB_WEXT);
1889 memset(&iwrq, 0, sizeof(union iwreq_data));
1890 memset(buf, 0, sizeof(buf));
1892 snprintf(buf, sizeof(buf) - 1, "%s", str);
1894 iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
1896 /* Send Event to upper layer */
1897 lbs_deb_wext("event indication string %s\n", (char *)buf);
1898 lbs_deb_wext("event indication length %d\n", iwrq.data.length);
1899 lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
1901 wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1903 lbs_deb_leave(LBS_DEB_WEXT);
1906 static void lbs_send_confirmsleep(struct lbs_private *priv)
1908 unsigned long flags;
1911 lbs_deb_enter(LBS_DEB_HOST);
1912 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1913 sizeof(confirm_sleep));
1915 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1916 sizeof(confirm_sleep));
1918 lbs_pr_alert("confirm_sleep failed\n");
1922 spin_lock_irqsave(&priv->driver_lock, flags);
1924 /* We don't get a response on the sleep-confirmation */
1925 priv->dnld_sent = DNLD_RES_RECEIVED;
1927 /* If nothing to do, go back to sleep (?) */
1928 if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1929 priv->psstate = PS_STATE_SLEEP;
1931 spin_unlock_irqrestore(&priv->driver_lock, flags);
1934 lbs_deb_leave(LBS_DEB_HOST);
1937 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1939 lbs_deb_enter(LBS_DEB_HOST);
1942 * PS is currently supported only in Infrastructure mode
1943 * Remove this check if it is to be supported in IBSS mode also
1946 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1947 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1949 lbs_deb_leave(LBS_DEB_HOST);
1953 * @brief This function sends Exit_PS command to firmware.
1955 * @param priv A pointer to struct lbs_private structure
1956 * @param wait_option wait response or not
1959 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1963 lbs_deb_enter(LBS_DEB_HOST);
1965 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1967 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1969 wait_option, 0, &Localpsmode);
1971 lbs_deb_leave(LBS_DEB_HOST);
1975 * @brief This function checks condition and prepares to
1976 * send sleep confirm command to firmware if ok.
1978 * @param priv A pointer to struct lbs_private structure
1979 * @param psmode Power Saving mode
1982 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1984 unsigned long flags =0;
1987 lbs_deb_enter(LBS_DEB_HOST);
1989 spin_lock_irqsave(&priv->driver_lock, flags);
1990 if (priv->dnld_sent) {
1992 lbs_deb_host("dnld_sent was set\n");
1995 /* In-progress command? */
1996 if (priv->cur_cmd) {
1998 lbs_deb_host("cur_cmd was set\n");
2001 /* Pending events or command responses? */
2002 if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
2004 lbs_deb_host("pending events or command responses\n");
2006 spin_unlock_irqrestore(&priv->driver_lock, flags);
2009 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
2010 lbs_send_confirmsleep(priv);
2012 lbs_deb_host("sleep confirm has been delayed\n");
2015 lbs_deb_leave(LBS_DEB_HOST);
2020 * @brief Configures the transmission power control functionality.
2022 * @param priv A pointer to struct lbs_private structure
2023 * @param enable Transmission power control enable
2024 * @param p0 Power level when link quality is good (dBm).
2025 * @param p1 Power level when link quality is fair (dBm).
2026 * @param p2 Power level when link quality is poor (dBm).
2027 * @param usesnr Use Signal to Noise Ratio in TPC
2029 * @return 0 on success
2031 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
2032 int8_t p2, int usesnr)
2034 struct cmd_ds_802_11_tpc_cfg cmd;
2037 memset(&cmd, 0, sizeof(cmd));
2038 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2039 cmd.action = cpu_to_le16(CMD_ACT_SET);
2040 cmd.enable = !!enable;
2041 cmd.usesnr = !!usesnr;
2046 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
2052 * @brief Configures the power adaptation settings.
2054 * @param priv A pointer to struct lbs_private structure
2055 * @param enable Power adaptation enable
2056 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
2057 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
2058 * @param p2 Power level for 48 and 54 Mbps (dBm).
2060 * @return 0 on Success
2063 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
2064 int8_t p1, int8_t p2)
2066 struct cmd_ds_802_11_pa_cfg cmd;
2069 memset(&cmd, 0, sizeof(cmd));
2070 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2071 cmd.action = cpu_to_le16(CMD_ACT_SET);
2072 cmd.enable = !!enable;
2077 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
2083 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
2084 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
2085 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2086 unsigned long callback_arg)
2088 struct cmd_ctrl_node *cmdnode;
2090 lbs_deb_enter(LBS_DEB_HOST);
2092 if (priv->surpriseremoved) {
2093 lbs_deb_host("PREP_CMD: card removed\n");
2094 cmdnode = ERR_PTR(-ENOENT);
2098 if (!lbs_is_cmd_allowed(priv)) {
2099 cmdnode = ERR_PTR(-EBUSY);
2103 cmdnode = lbs_get_cmd_ctrl_node(priv);
2104 if (cmdnode == NULL) {
2105 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
2107 /* Wake up main thread to execute next command */
2108 wake_up_interruptible(&priv->waitq);
2109 cmdnode = ERR_PTR(-ENOBUFS);
2113 cmdnode->callback = callback;
2114 cmdnode->callback_arg = callback_arg;
2116 /* Copy the incoming command to the buffer */
2117 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
2119 /* Set sequence number, clean result, move to buffer */
2121 cmdnode->cmdbuf->command = cpu_to_le16(command);
2122 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
2123 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
2124 cmdnode->cmdbuf->result = 0;
2126 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
2128 cmdnode->cmdwaitqwoken = 0;
2129 lbs_queue_cmd(priv, cmdnode);
2130 wake_up_interruptible(&priv->waitq);
2133 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
2137 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
2138 struct cmd_header *in_cmd, int in_cmd_size)
2140 lbs_deb_enter(LBS_DEB_CMD);
2141 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2142 lbs_cmd_async_callback, 0);
2143 lbs_deb_leave(LBS_DEB_CMD);
2146 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
2147 struct cmd_header *in_cmd, int in_cmd_size,
2148 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2149 unsigned long callback_arg)
2151 struct cmd_ctrl_node *cmdnode;
2152 unsigned long flags;
2155 lbs_deb_enter(LBS_DEB_HOST);
2157 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2158 callback, callback_arg);
2159 if (IS_ERR(cmdnode)) {
2160 ret = PTR_ERR(cmdnode);
2165 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
2167 spin_lock_irqsave(&priv->driver_lock, flags);
2168 ret = cmdnode->result;
2170 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
2173 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
2174 spin_unlock_irqrestore(&priv->driver_lock, flags);
2177 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
2180 EXPORT_SYMBOL_GPL(__lbs_cmd);