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>
18 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
22 * @brief Simple callback that copies response back into command
24 * @param priv A pointer to struct lbs_private structure
25 * @param extra A pointer to the original command structure for which
26 * 'resp' is a response
27 * @param resp A pointer to the command response
29 * @return 0 on success, error on failure
31 int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
32 struct cmd_header *resp)
34 struct cmd_header *buf = (void *)extra;
37 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
38 memcpy(buf, resp, copy_len);
41 EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
44 * @brief Simple callback that ignores the result. Use this if
45 * you just want to send a command to the hardware, but don't
46 * care for the result.
49 * @param extra ignored
52 * @return 0 for success
54 static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
55 struct cmd_header *resp)
62 * @brief Checks whether a command is allowed in Power Save mode
64 * @param command the command ID
65 * @return 1 if allowed, 0 if not allowed
67 static u8 is_command_allowed_in_ps(u16 cmd)
79 * @brief Updates the hardware details like MAC address and regulatory region
81 * @param priv A pointer to struct lbs_private structure
83 * @return 0 on success, error on failure
85 int lbs_update_hw_spec(struct lbs_private *priv)
87 struct cmd_ds_get_hw_spec cmd;
91 lbs_deb_enter(LBS_DEB_CMD);
93 memset(&cmd, 0, sizeof(cmd));
94 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
95 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
96 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
100 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
102 /* The firmware release is in an interesting format: the patch
103 * level is in the most significant nibble ... so fix that: */
104 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
105 priv->fwrelease = (priv->fwrelease << 8) |
106 (priv->fwrelease >> 24 & 0xff);
108 /* Some firmware capabilities:
109 * CF card firmware 5.0.16p0: cap 0x00000303
110 * USB dongle firmware 5.110.17p2: cap 0x00000303
112 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
114 priv->fwrelease >> 24 & 0xff,
115 priv->fwrelease >> 16 & 0xff,
116 priv->fwrelease >> 8 & 0xff,
117 priv->fwrelease & 0xff,
119 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
120 cmd.hwifversion, cmd.version);
122 /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
123 /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
124 /* 5.110.22 have mesh command with 0xa3 command id */
125 /* 10.0.0.p0 FW brings in mesh config command with different id */
126 /* Check FW version MSB and initialize mesh_fw_ver */
127 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
128 priv->mesh_fw_ver = MESH_FW_OLD;
129 else if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
130 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK))
131 priv->mesh_fw_ver = MESH_FW_NEW;
133 priv->mesh_fw_ver = MESH_NONE;
135 /* Clamp region code to 8-bit since FW spec indicates that it should
136 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
137 * returns non-zero high 8 bits here.
139 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
140 * need to check for this problem and handle it properly.
142 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
143 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
145 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
147 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
148 /* use the region code to search for the index */
149 if (priv->regioncode == lbs_region_code_to_index[i])
153 /* if it's unidentified region code, use the default (USA) */
154 if (i >= MRVDRV_MAX_REGION_CODE) {
155 priv->regioncode = 0x10;
156 lbs_pr_info("unidentified region code; using the default (USA)\n");
159 if (priv->current_addr[0] == 0xff)
160 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
162 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
164 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
166 if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
171 if (lbs_set_universaltable(priv, 0)) {
177 lbs_deb_leave(LBS_DEB_CMD);
181 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
182 struct wol_config *p_wol_config)
184 struct cmd_ds_host_sleep cmd_config;
187 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
188 cmd_config.criteria = cpu_to_le32(criteria);
189 cmd_config.gpio = priv->wol_gpio;
190 cmd_config.gap = priv->wol_gap;
192 if (p_wol_config != NULL)
193 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
194 sizeof(struct wol_config));
196 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
198 ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
201 lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
202 priv->wol_criteria = criteria;
204 memcpy((uint8_t *) p_wol_config,
205 (uint8_t *)&cmd_config.wol_conf,
206 sizeof(struct wol_config));
208 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
213 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
215 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
218 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
220 lbs_deb_enter(LBS_DEB_CMD);
222 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
223 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
225 psm->action = cpu_to_le16(cmd_action);
226 psm->multipledtim = 0;
227 switch (cmd_action) {
228 case CMD_SUBCMD_ENTER_PS:
229 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
231 psm->locallisteninterval = 0;
232 psm->nullpktinterval = 0;
234 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
237 case CMD_SUBCMD_EXIT_PS:
238 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
241 case CMD_SUBCMD_SLEEP_CONFIRMED:
242 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
249 lbs_deb_leave(LBS_DEB_CMD);
253 int lbs_cmd_802_11_inactivity_timeout(struct lbs_private *priv,
254 uint16_t cmd_action, uint16_t *timeout)
256 struct cmd_ds_802_11_inactivity_timeout cmd;
259 lbs_deb_enter(LBS_DEB_CMD);
261 cmd.hdr.command = cpu_to_le16(CMD_802_11_INACTIVITY_TIMEOUT);
262 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
264 cmd.action = cpu_to_le16(cmd_action);
266 if (cmd_action == CMD_ACT_SET)
267 cmd.timeout = cpu_to_le16(*timeout);
271 ret = lbs_cmd_with_response(priv, CMD_802_11_INACTIVITY_TIMEOUT, &cmd);
274 *timeout = le16_to_cpu(cmd.timeout);
276 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
280 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
281 struct sleep_params *sp)
283 struct cmd_ds_802_11_sleep_params cmd;
286 lbs_deb_enter(LBS_DEB_CMD);
288 if (cmd_action == CMD_ACT_GET) {
289 memset(&cmd, 0, sizeof(cmd));
291 cmd.error = cpu_to_le16(sp->sp_error);
292 cmd.offset = cpu_to_le16(sp->sp_offset);
293 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
294 cmd.calcontrol = sp->sp_calcontrol;
295 cmd.externalsleepclk = sp->sp_extsleepclk;
296 cmd.reserved = cpu_to_le16(sp->sp_reserved);
298 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
299 cmd.action = cpu_to_le16(cmd_action);
301 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
304 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
305 "calcontrol 0x%x extsleepclk 0x%x\n",
306 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
307 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
308 cmd.externalsleepclk);
310 sp->sp_error = le16_to_cpu(cmd.error);
311 sp->sp_offset = le16_to_cpu(cmd.offset);
312 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
313 sp->sp_calcontrol = cmd.calcontrol;
314 sp->sp_extsleepclk = cmd.externalsleepclk;
315 sp->sp_reserved = le16_to_cpu(cmd.reserved);
318 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
322 int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
323 struct assoc_request *assoc)
325 struct cmd_ds_802_11_set_wep cmd;
328 lbs_deb_enter(LBS_DEB_CMD);
330 memset(&cmd, 0, sizeof(cmd));
331 cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
332 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
334 cmd.action = cpu_to_le16(cmd_action);
336 if (cmd_action == CMD_ACT_ADD) {
339 /* default tx key index */
340 cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
341 CMD_WEP_KEY_INDEX_MASK);
343 /* Copy key types and material to host command structure */
344 for (i = 0; i < 4; i++) {
345 struct enc_key *pkey = &assoc->wep_keys[i];
349 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
350 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
351 lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
353 case KEY_LEN_WEP_104:
354 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
355 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
356 lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
361 lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
368 } else if (cmd_action == CMD_ACT_REMOVE) {
369 /* ACT_REMOVE clears _all_ WEP keys */
371 /* default tx key index */
372 cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
373 CMD_WEP_KEY_INDEX_MASK);
374 lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
377 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
379 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
383 int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
386 struct cmd_ds_802_11_enable_rsn cmd;
389 lbs_deb_enter(LBS_DEB_CMD);
391 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
392 cmd.action = cpu_to_le16(cmd_action);
394 if (cmd_action == CMD_ACT_GET)
398 cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
400 cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
401 lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
404 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
405 if (!ret && cmd_action == CMD_ACT_GET)
406 *enable = le16_to_cpu(cmd.enable);
408 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
412 static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
415 lbs_deb_enter(LBS_DEB_CMD);
417 if (key->flags & KEY_INFO_WPA_ENABLED)
418 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
419 if (key->flags & KEY_INFO_WPA_UNICAST)
420 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
421 if (key->flags & KEY_INFO_WPA_MCAST)
422 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
424 keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
425 keyparam->keytypeid = cpu_to_le16(key->type);
426 keyparam->keylen = cpu_to_le16(key->len);
427 memcpy(keyparam->key, key->key, key->len);
429 /* Length field doesn't include the {type,length} header */
430 keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
431 lbs_deb_leave(LBS_DEB_CMD);
434 int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
435 struct assoc_request *assoc)
437 struct cmd_ds_802_11_key_material cmd;
441 lbs_deb_enter(LBS_DEB_CMD);
443 cmd.action = cpu_to_le16(cmd_action);
444 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
446 if (cmd_action == CMD_ACT_GET) {
447 cmd.hdr.size = cpu_to_le16(S_DS_GEN + 2);
449 memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
451 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
452 set_one_wpa_key(&cmd.keyParamSet[index],
453 &assoc->wpa_unicast_key);
457 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
458 set_one_wpa_key(&cmd.keyParamSet[index],
459 &assoc->wpa_mcast_key);
463 /* The common header and as many keys as we included */
464 cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
465 keyParamSet[index]));
467 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
468 /* Copy the returned key to driver private data */
469 if (!ret && cmd_action == CMD_ACT_GET) {
470 void *buf_ptr = cmd.keyParamSet;
471 void *resp_end = &(&cmd)[1];
473 while (buf_ptr < resp_end) {
474 struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
476 uint16_t param_set_len = le16_to_cpu(keyparam->length);
477 uint16_t key_len = le16_to_cpu(keyparam->keylen);
478 uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
479 uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
482 end = (void *)keyparam + sizeof(keyparam->type)
483 + sizeof(keyparam->length) + param_set_len;
485 /* Make sure we don't access past the end of the IEs */
489 if (key_flags & KEY_INFO_WPA_UNICAST)
490 key = &priv->wpa_unicast_key;
491 else if (key_flags & KEY_INFO_WPA_MCAST)
492 key = &priv->wpa_mcast_key;
496 /* Copy returned key into driver */
497 memset(key, 0, sizeof(struct enc_key));
498 if (key_len > sizeof(key->key))
500 key->type = key_type;
501 key->flags = key_flags;
503 memcpy(key->key, keyparam->key, key->len);
509 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
514 * @brief Set an SNMP MIB value
516 * @param priv A pointer to struct lbs_private structure
517 * @param oid The OID to set in the firmware
518 * @param val Value to set the OID to
520 * @return 0 on success, error on failure
522 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
524 struct cmd_ds_802_11_snmp_mib cmd;
527 lbs_deb_enter(LBS_DEB_CMD);
529 memset(&cmd, 0, sizeof (cmd));
530 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
531 cmd.action = cpu_to_le16(CMD_ACT_SET);
532 cmd.oid = cpu_to_le16((u16) oid);
535 case SNMP_MIB_OID_BSS_TYPE:
536 cmd.bufsize = cpu_to_le16(sizeof(u8));
537 cmd.value[0] = (val == IW_MODE_ADHOC) ? 2 : 1;
539 case SNMP_MIB_OID_11D_ENABLE:
540 case SNMP_MIB_OID_FRAG_THRESHOLD:
541 case SNMP_MIB_OID_RTS_THRESHOLD:
542 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
543 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
544 cmd.bufsize = cpu_to_le16(sizeof(u16));
545 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
548 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
553 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
554 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
556 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
559 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
564 * @brief Get an SNMP MIB value
566 * @param priv A pointer to struct lbs_private structure
567 * @param oid The OID to retrieve from the firmware
568 * @param out_val Location for the returned value
570 * @return 0 on success, error on failure
572 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
574 struct cmd_ds_802_11_snmp_mib cmd;
577 lbs_deb_enter(LBS_DEB_CMD);
579 memset(&cmd, 0, sizeof (cmd));
580 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
581 cmd.action = cpu_to_le16(CMD_ACT_GET);
582 cmd.oid = cpu_to_le16(oid);
584 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
588 switch (le16_to_cpu(cmd.bufsize)) {
590 if (oid == SNMP_MIB_OID_BSS_TYPE) {
591 if (cmd.value[0] == 2)
592 *out_val = IW_MODE_ADHOC;
594 *out_val = IW_MODE_INFRA;
596 *out_val = cmd.value[0];
599 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
602 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
603 oid, le16_to_cpu(cmd.bufsize));
608 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
613 * @brief Get the min, max, and current TX power
615 * @param priv A pointer to struct lbs_private structure
616 * @param curlevel Current power level in dBm
617 * @param minlevel Minimum supported power level in dBm (optional)
618 * @param maxlevel Maximum supported power level in dBm (optional)
620 * @return 0 on success, error on failure
622 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
625 struct cmd_ds_802_11_rf_tx_power cmd;
628 lbs_deb_enter(LBS_DEB_CMD);
630 memset(&cmd, 0, sizeof(cmd));
631 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
632 cmd.action = cpu_to_le16(CMD_ACT_GET);
634 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
636 *curlevel = le16_to_cpu(cmd.curlevel);
638 *minlevel = cmd.minlevel;
640 *maxlevel = cmd.maxlevel;
643 lbs_deb_leave(LBS_DEB_CMD);
648 * @brief Set the TX power
650 * @param priv A pointer to struct lbs_private structure
651 * @param dbm The desired power level in dBm
653 * @return 0 on success, error on failure
655 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
657 struct cmd_ds_802_11_rf_tx_power cmd;
660 lbs_deb_enter(LBS_DEB_CMD);
662 memset(&cmd, 0, sizeof(cmd));
663 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
664 cmd.action = cpu_to_le16(CMD_ACT_SET);
665 cmd.curlevel = cpu_to_le16(dbm);
667 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
669 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
671 lbs_deb_leave(LBS_DEB_CMD);
675 static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
676 u16 cmd_action, void *pdata_buf)
678 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
680 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
682 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
685 monitor->action = cpu_to_le16(cmd_action);
686 if (cmd_action == CMD_ACT_SET) {
688 cpu_to_le16((u16) (*(u32 *) pdata_buf));
694 static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
714 int i = lbs_data_rate_to_fw_index(rate);
716 ratemask = (0x1fef >> (12 - i));
719 return cpu_to_le16(ratemask);
722 int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
725 struct cmd_ds_802_11_rate_adapt_rateset cmd;
728 lbs_deb_enter(LBS_DEB_CMD);
730 if (!priv->cur_rate && !priv->enablehwauto)
733 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
735 cmd.action = cpu_to_le16(cmd_action);
736 cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
737 cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
738 ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
739 if (!ret && cmd_action == CMD_ACT_GET) {
740 priv->ratebitmap = le16_to_cpu(cmd.bitmap);
741 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
744 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
747 EXPORT_SYMBOL_GPL(lbs_cmd_802_11_rate_adapt_rateset);
750 * @brief Set the data rate
752 * @param priv A pointer to struct lbs_private structure
753 * @param rate The desired data rate, or 0 to clear a locked rate
755 * @return 0 on success, error on failure
757 int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
759 struct cmd_ds_802_11_data_rate cmd;
762 lbs_deb_enter(LBS_DEB_CMD);
764 memset(&cmd, 0, sizeof(cmd));
765 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
768 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
769 cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
770 if (cmd.rates[0] == 0) {
771 lbs_deb_cmd("DATA_RATE: invalid requested rate of"
776 lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
778 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
779 lbs_deb_cmd("DATA_RATE: setting auto\n");
782 ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
786 lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof (cmd));
788 /* FIXME: get actual rates FW can do if this command actually returns
789 * all data rates supported.
791 priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
792 lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
795 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
800 * @brief Get the radio channel
802 * @param priv A pointer to struct lbs_private structure
804 * @return The channel on success, error on failure
806 int lbs_get_channel(struct lbs_private *priv)
808 struct cmd_ds_802_11_rf_channel cmd;
811 lbs_deb_enter(LBS_DEB_CMD);
813 memset(&cmd, 0, sizeof(cmd));
814 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
815 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
817 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
821 ret = le16_to_cpu(cmd.channel);
822 lbs_deb_cmd("current radio channel is %d\n", ret);
825 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
829 int lbs_update_channel(struct lbs_private *priv)
833 /* the channel in f/w could be out of sync; get the current channel */
834 lbs_deb_enter(LBS_DEB_ASSOC);
836 ret = lbs_get_channel(priv);
838 priv->curbssparams.channel = ret;
841 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
846 * @brief Set the radio channel
848 * @param priv A pointer to struct lbs_private structure
849 * @param channel The desired channel, or 0 to clear a locked channel
851 * @return 0 on success, error on failure
853 int lbs_set_channel(struct lbs_private *priv, u8 channel)
855 struct cmd_ds_802_11_rf_channel cmd;
857 u8 old_channel = priv->curbssparams.channel;
861 lbs_deb_enter(LBS_DEB_CMD);
863 memset(&cmd, 0, sizeof(cmd));
864 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
865 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
866 cmd.channel = cpu_to_le16(channel);
868 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
872 priv->curbssparams.channel = (uint8_t) le16_to_cpu(cmd.channel);
873 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
874 priv->curbssparams.channel);
877 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
881 static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
882 struct cmd_ds_command *cmd)
885 lbs_deb_enter(LBS_DEB_CMD);
886 cmd->command = cpu_to_le16(CMD_802_11_RSSI);
887 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
888 cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
890 /* reset Beacon SNR/NF/RSSI values */
891 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
892 priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
893 priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
894 priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
895 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
896 priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
898 lbs_deb_leave(LBS_DEB_CMD);
902 static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
903 u8 cmd_action, void *pdata_buf)
905 struct lbs_offset_value *offval;
907 lbs_deb_enter(LBS_DEB_CMD);
909 offval = (struct lbs_offset_value *)pdata_buf;
911 switch (le16_to_cpu(cmdptr->command)) {
912 case CMD_MAC_REG_ACCESS:
914 struct cmd_ds_mac_reg_access *macreg;
917 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
920 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
923 macreg->action = cpu_to_le16(cmd_action);
924 macreg->offset = cpu_to_le16((u16) offval->offset);
925 macreg->value = cpu_to_le32(offval->value);
930 case CMD_BBP_REG_ACCESS:
932 struct cmd_ds_bbp_reg_access *bbpreg;
936 (struct cmd_ds_bbp_reg_access)
939 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
942 bbpreg->action = cpu_to_le16(cmd_action);
943 bbpreg->offset = cpu_to_le16((u16) offval->offset);
944 bbpreg->value = (u8) offval->value;
949 case CMD_RF_REG_ACCESS:
951 struct cmd_ds_rf_reg_access *rfreg;
955 (struct cmd_ds_rf_reg_access) +
958 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
961 rfreg->action = cpu_to_le16(cmd_action);
962 rfreg->offset = cpu_to_le16((u16) offval->offset);
963 rfreg->value = (u8) offval->value;
972 lbs_deb_leave(LBS_DEB_CMD);
976 static int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
977 u16 cmd_action, void *pdata_buf)
979 struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
980 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
982 cmd->command = cpu_to_le16(CMD_BT_ACCESS);
983 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
985 bt_access->action = cpu_to_le16(cmd_action);
987 switch (cmd_action) {
988 case CMD_ACT_BT_ACCESS_ADD:
989 memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
990 lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
992 case CMD_ACT_BT_ACCESS_DEL:
993 memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
994 lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
996 case CMD_ACT_BT_ACCESS_LIST:
997 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
999 case CMD_ACT_BT_ACCESS_RESET:
1001 case CMD_ACT_BT_ACCESS_SET_INVERT:
1002 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
1004 case CMD_ACT_BT_ACCESS_GET_INVERT:
1009 lbs_deb_leave(LBS_DEB_CMD);
1013 static int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
1014 u16 cmd_action, void *pdata_buf)
1016 struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
1017 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1019 cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
1020 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
1024 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
1026 memset(fwt_access, 0, sizeof(*fwt_access));
1028 fwt_access->action = cpu_to_le16(cmd_action);
1030 lbs_deb_leave(LBS_DEB_CMD);
1034 int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
1035 struct cmd_ds_mesh_access *cmd)
1039 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1041 cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
1042 cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
1043 cmd->hdr.result = 0;
1045 cmd->action = cpu_to_le16(cmd_action);
1047 ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
1049 lbs_deb_leave(LBS_DEB_CMD);
1053 static int __lbs_mesh_config_send(struct lbs_private *priv,
1054 struct cmd_ds_mesh_config *cmd,
1055 uint16_t action, uint16_t type)
1058 u16 command = CMD_MESH_CONFIG_OLD;
1060 lbs_deb_enter(LBS_DEB_CMD);
1063 * Command id is 0xac for v10 FW along with mesh interface
1064 * id in bits 14-13-12.
1066 if (priv->mesh_fw_ver == MESH_FW_NEW)
1067 command = CMD_MESH_CONFIG |
1068 (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
1070 cmd->hdr.command = cpu_to_le16(command);
1071 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
1072 cmd->hdr.result = 0;
1074 cmd->type = cpu_to_le16(type);
1075 cmd->action = cpu_to_le16(action);
1077 ret = lbs_cmd_with_response(priv, command, cmd);
1079 lbs_deb_leave(LBS_DEB_CMD);
1083 int lbs_mesh_config_send(struct lbs_private *priv,
1084 struct cmd_ds_mesh_config *cmd,
1085 uint16_t action, uint16_t type)
1089 if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
1092 ret = __lbs_mesh_config_send(priv, cmd, action, type);
1096 /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
1097 * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
1098 * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
1099 * lbs_mesh_config_send.
1101 int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
1103 struct cmd_ds_mesh_config cmd;
1104 struct mrvl_meshie *ie;
1105 DECLARE_SSID_BUF(ssid);
1107 memset(&cmd, 0, sizeof(cmd));
1108 cmd.channel = cpu_to_le16(chan);
1109 ie = (struct mrvl_meshie *)cmd.data;
1112 case CMD_ACT_MESH_CONFIG_START:
1113 ie->id = WLAN_EID_GENERIC;
1114 ie->val.oui[0] = 0x00;
1115 ie->val.oui[1] = 0x50;
1116 ie->val.oui[2] = 0x43;
1117 ie->val.type = MARVELL_MESH_IE_TYPE;
1118 ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
1119 ie->val.version = MARVELL_MESH_IE_VERSION;
1120 ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
1121 ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
1122 ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
1123 ie->val.mesh_id_len = priv->mesh_ssid_len;
1124 memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
1125 ie->len = sizeof(struct mrvl_meshie_val) -
1126 IW_ESSID_MAX_SIZE + priv->mesh_ssid_len;
1127 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
1129 case CMD_ACT_MESH_CONFIG_STOP:
1134 lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
1135 action, priv->mesh_tlv, chan,
1136 print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
1138 return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
1141 static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
1142 struct cmd_ds_command *cmd,
1145 struct cmd_ds_802_11_beacon_control
1146 *bcn_ctrl = &cmd->params.bcn_ctrl;
1148 lbs_deb_enter(LBS_DEB_CMD);
1150 cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
1152 cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
1154 bcn_ctrl->action = cpu_to_le16(cmd_action);
1155 bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
1156 bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
1158 lbs_deb_leave(LBS_DEB_CMD);
1162 static void lbs_queue_cmd(struct lbs_private *priv,
1163 struct cmd_ctrl_node *cmdnode)
1165 unsigned long flags;
1168 lbs_deb_enter(LBS_DEB_HOST);
1171 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
1174 if (!cmdnode->cmdbuf->size) {
1175 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
1178 cmdnode->result = 0;
1180 /* Exit_PS command needs to be queued in the header always. */
1181 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
1182 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
1184 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1185 if (priv->psstate != PS_STATE_FULL_POWER)
1190 spin_lock_irqsave(&priv->driver_lock, flags);
1193 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
1195 list_add(&cmdnode->list, &priv->cmdpendingq);
1197 spin_unlock_irqrestore(&priv->driver_lock, flags);
1199 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
1200 le16_to_cpu(cmdnode->cmdbuf->command));
1203 lbs_deb_leave(LBS_DEB_HOST);
1206 static void lbs_submit_command(struct lbs_private *priv,
1207 struct cmd_ctrl_node *cmdnode)
1209 unsigned long flags;
1210 struct cmd_header *cmd;
1216 lbs_deb_enter(LBS_DEB_HOST);
1218 cmd = cmdnode->cmdbuf;
1220 spin_lock_irqsave(&priv->driver_lock, flags);
1221 priv->cur_cmd = cmdnode;
1222 priv->cur_cmd_retcode = 0;
1223 spin_unlock_irqrestore(&priv->driver_lock, flags);
1225 cmdsize = le16_to_cpu(cmd->size);
1226 command = le16_to_cpu(cmd->command);
1228 /* These commands take longer */
1229 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1232 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1233 command, le16_to_cpu(cmd->seqnum), cmdsize);
1234 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1236 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1239 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1240 /* Let the timer kick in and retry, and potentially reset
1241 the whole thing if the condition persists */
1245 /* Setup the timer after transmit command */
1246 mod_timer(&priv->command_timer, jiffies + timeo);
1248 lbs_deb_leave(LBS_DEB_HOST);
1252 * This function inserts command node to cmdfreeq
1253 * after cleans it. Requires priv->driver_lock held.
1255 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1256 struct cmd_ctrl_node *cmdnode)
1258 lbs_deb_enter(LBS_DEB_HOST);
1263 cmdnode->callback = NULL;
1264 cmdnode->callback_arg = 0;
1266 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1268 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1270 lbs_deb_leave(LBS_DEB_HOST);
1273 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1274 struct cmd_ctrl_node *ptempcmd)
1276 unsigned long flags;
1278 spin_lock_irqsave(&priv->driver_lock, flags);
1279 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1280 spin_unlock_irqrestore(&priv->driver_lock, flags);
1283 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1286 if (cmd == priv->cur_cmd)
1287 priv->cur_cmd_retcode = result;
1289 cmd->result = result;
1290 cmd->cmdwaitqwoken = 1;
1291 wake_up_interruptible(&cmd->cmdwait_q);
1293 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1294 __lbs_cleanup_and_insert_cmd(priv, cmd);
1295 priv->cur_cmd = NULL;
1298 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1300 struct cmd_ds_802_11_radio_control cmd;
1303 lbs_deb_enter(LBS_DEB_CMD);
1305 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1306 cmd.action = cpu_to_le16(CMD_ACT_SET);
1308 /* Only v8 and below support setting the preamble */
1309 if (priv->fwrelease < 0x09000000) {
1311 case RADIO_PREAMBLE_SHORT:
1312 if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
1315 case RADIO_PREAMBLE_AUTO:
1316 case RADIO_PREAMBLE_LONG:
1317 cmd.control = cpu_to_le16(preamble);
1325 cmd.control |= cpu_to_le16(0x1);
1327 cmd.control &= cpu_to_le16(~0x1);
1328 priv->txpower_cur = 0;
1331 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1332 radio_on ? "ON" : "OFF", preamble);
1334 priv->radio_on = radio_on;
1336 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1339 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1343 void lbs_set_mac_control(struct lbs_private *priv)
1345 struct cmd_ds_mac_control cmd;
1347 lbs_deb_enter(LBS_DEB_CMD);
1349 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1350 cmd.action = cpu_to_le16(priv->mac_control);
1353 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1355 lbs_deb_leave(LBS_DEB_CMD);
1359 * @brief This function prepare the command before send to firmware.
1361 * @param priv A pointer to struct lbs_private structure
1362 * @param cmd_no command number
1363 * @param cmd_action command action: GET or SET
1364 * @param wait_option wait option: wait response or not
1365 * @param cmd_oid cmd oid: treated as sub command
1366 * @param pdata_buf A pointer to informaion buffer
1369 int lbs_prepare_and_send_command(struct lbs_private *priv,
1372 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1375 struct cmd_ctrl_node *cmdnode;
1376 struct cmd_ds_command *cmdptr;
1377 unsigned long flags;
1379 lbs_deb_enter(LBS_DEB_HOST);
1382 lbs_deb_host("PREP_CMD: priv is NULL\n");
1387 if (priv->surpriseremoved) {
1388 lbs_deb_host("PREP_CMD: card removed\n");
1393 cmdnode = lbs_get_cmd_ctrl_node(priv);
1395 if (cmdnode == NULL) {
1396 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1398 /* Wake up main thread to execute next command */
1399 wake_up_interruptible(&priv->waitq);
1404 cmdnode->callback = NULL;
1405 cmdnode->callback_arg = (unsigned long)pdata_buf;
1407 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1409 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1411 /* Set sequence number, command and INT option */
1413 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1415 cmdptr->command = cpu_to_le16(cmd_no);
1419 case CMD_802_11_PS_MODE:
1420 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1423 case CMD_MAC_REG_ACCESS:
1424 case CMD_BBP_REG_ACCESS:
1425 case CMD_RF_REG_ACCESS:
1426 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
1429 case CMD_802_11_MONITOR_MODE:
1430 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
1431 cmd_action, pdata_buf);
1434 case CMD_802_11_RSSI:
1435 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1438 case CMD_802_11_SET_AFC:
1439 case CMD_802_11_GET_AFC:
1441 cmdptr->command = cpu_to_le16(cmd_no);
1442 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1445 memmove(&cmdptr->params.afc,
1446 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1451 case CMD_802_11D_DOMAIN_INFO:
1452 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr,
1453 cmd_no, cmd_action);
1456 case CMD_802_11_TPC_CFG:
1457 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1459 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1462 memmove(&cmdptr->params.tpccfg,
1463 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1467 case CMD_802_11_LED_GPIO_CTRL:
1469 struct mrvl_ie_ledgpio *gpio =
1470 (struct mrvl_ie_ledgpio*)
1471 cmdptr->params.ledgpio.data;
1473 memmove(&cmdptr->params.ledgpio,
1475 sizeof(struct cmd_ds_802_11_led_ctrl));
1478 cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
1480 #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
1482 cpu_to_le16(le16_to_cpu(gpio->header.len)
1484 + ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
1485 gpio->header.len = gpio->header.len;
1492 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1495 case CMD_FWT_ACCESS:
1496 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1500 cmdptr->command = cpu_to_le16(CMD_GET_TSF);
1501 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
1505 case CMD_802_11_BEACON_CTRL:
1506 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1509 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1514 /* return error, since the command preparation failed */
1516 lbs_deb_host("PREP_CMD: command preparation failed\n");
1517 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1522 cmdnode->cmdwaitqwoken = 0;
1524 lbs_queue_cmd(priv, cmdnode);
1525 wake_up_interruptible(&priv->waitq);
1527 if (wait_option & CMD_OPTION_WAITFORRSP) {
1528 lbs_deb_host("PREP_CMD: wait for response\n");
1530 wait_event_interruptible(cmdnode->cmdwait_q,
1531 cmdnode->cmdwaitqwoken);
1534 spin_lock_irqsave(&priv->driver_lock, flags);
1535 if (priv->cur_cmd_retcode) {
1536 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1537 priv->cur_cmd_retcode);
1538 priv->cur_cmd_retcode = 0;
1541 spin_unlock_irqrestore(&priv->driver_lock, flags);
1544 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1549 * @brief This function allocates the command buffer and link
1550 * it to command free queue.
1552 * @param priv A pointer to struct lbs_private structure
1555 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1560 struct cmd_ctrl_node *cmdarray;
1562 lbs_deb_enter(LBS_DEB_HOST);
1564 /* Allocate and initialize the command array */
1565 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1566 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1567 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1571 priv->cmd_array = cmdarray;
1573 /* Allocate and initialize each command buffer in the command array */
1574 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1575 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1576 if (!cmdarray[i].cmdbuf) {
1577 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1583 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1584 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1585 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1590 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1595 * @brief This function frees the command buffer.
1597 * @param priv A pointer to struct lbs_private structure
1600 int lbs_free_cmd_buffer(struct lbs_private *priv)
1602 struct cmd_ctrl_node *cmdarray;
1605 lbs_deb_enter(LBS_DEB_HOST);
1607 /* need to check if cmd array is allocated or not */
1608 if (priv->cmd_array == NULL) {
1609 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1613 cmdarray = priv->cmd_array;
1615 /* Release shared memory buffers */
1616 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1617 if (cmdarray[i].cmdbuf) {
1618 kfree(cmdarray[i].cmdbuf);
1619 cmdarray[i].cmdbuf = NULL;
1623 /* Release cmd_ctrl_node */
1624 if (priv->cmd_array) {
1625 kfree(priv->cmd_array);
1626 priv->cmd_array = NULL;
1630 lbs_deb_leave(LBS_DEB_HOST);
1635 * @brief This function gets a free command node if available in
1636 * command free queue.
1638 * @param priv A pointer to struct lbs_private structure
1639 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1641 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1643 struct cmd_ctrl_node *tempnode;
1644 unsigned long flags;
1646 lbs_deb_enter(LBS_DEB_HOST);
1651 spin_lock_irqsave(&priv->driver_lock, flags);
1653 if (!list_empty(&priv->cmdfreeq)) {
1654 tempnode = list_first_entry(&priv->cmdfreeq,
1655 struct cmd_ctrl_node, list);
1656 list_del(&tempnode->list);
1658 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1662 spin_unlock_irqrestore(&priv->driver_lock, flags);
1664 lbs_deb_leave(LBS_DEB_HOST);
1669 * @brief This function executes next command in command
1670 * pending queue. It will put firmware back to PS mode
1673 * @param priv A pointer to struct lbs_private structure
1676 int lbs_execute_next_command(struct lbs_private *priv)
1678 struct cmd_ctrl_node *cmdnode = NULL;
1679 struct cmd_header *cmd;
1680 unsigned long flags;
1683 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1684 * only caller to us is lbs_thread() and we get even when a
1685 * data packet is received */
1686 lbs_deb_enter(LBS_DEB_THREAD);
1688 spin_lock_irqsave(&priv->driver_lock, flags);
1690 if (priv->cur_cmd) {
1691 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1692 spin_unlock_irqrestore(&priv->driver_lock, flags);
1697 if (!list_empty(&priv->cmdpendingq)) {
1698 cmdnode = list_first_entry(&priv->cmdpendingq,
1699 struct cmd_ctrl_node, list);
1702 spin_unlock_irqrestore(&priv->driver_lock, flags);
1705 cmd = cmdnode->cmdbuf;
1707 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1708 if ((priv->psstate == PS_STATE_SLEEP) ||
1709 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1711 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1712 le16_to_cpu(cmd->command),
1717 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1718 "0x%04x in psstate %d\n",
1719 le16_to_cpu(cmd->command), priv->psstate);
1720 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1722 * 1. Non-PS command:
1723 * Queue it. set needtowakeup to TRUE if current state
1724 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1725 * 2. PS command but not Exit_PS:
1727 * 3. PS command Exit_PS:
1728 * Set needtowakeup to TRUE if current state is SLEEP,
1729 * otherwise send this command down to firmware
1732 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1733 /* Prepare to send Exit PS,
1734 * this non PS command will be sent later */
1735 if ((priv->psstate == PS_STATE_SLEEP)
1736 || (priv->psstate == PS_STATE_PRE_SLEEP)
1738 /* w/ new scheme, it will not reach here.
1739 since it is blocked in main_thread. */
1740 priv->needtowakeup = 1;
1742 lbs_ps_wakeup(priv, 0);
1748 * PS command. Ignore it if it is not Exit_PS.
1749 * otherwise send it down immediately.
1751 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1754 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1757 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1759 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1760 list_del(&cmdnode->list);
1761 spin_lock_irqsave(&priv->driver_lock, flags);
1762 lbs_complete_command(priv, cmdnode, 0);
1763 spin_unlock_irqrestore(&priv->driver_lock, flags);
1769 if ((priv->psstate == PS_STATE_SLEEP) ||
1770 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1772 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1773 list_del(&cmdnode->list);
1774 spin_lock_irqsave(&priv->driver_lock, flags);
1775 lbs_complete_command(priv, cmdnode, 0);
1776 spin_unlock_irqrestore(&priv->driver_lock, flags);
1777 priv->needtowakeup = 1;
1784 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1787 list_del(&cmdnode->list);
1788 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1789 le16_to_cpu(cmd->command));
1790 lbs_submit_command(priv, cmdnode);
1793 * check if in power save mode, if yes, put the device back
1796 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1797 (priv->psstate == PS_STATE_FULL_POWER) &&
1798 ((priv->connect_status == LBS_CONNECTED) ||
1799 (priv->mesh_connect_status == LBS_CONNECTED))) {
1800 if (priv->secinfo.WPAenabled ||
1801 priv->secinfo.WPA2enabled) {
1802 /* check for valid WPA group keys */
1803 if (priv->wpa_mcast_key.len ||
1804 priv->wpa_unicast_key.len) {
1806 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1807 " go back to PS_SLEEP");
1808 lbs_ps_sleep(priv, 0);
1812 "EXEC_NEXT_CMD: cmdpendingq empty, "
1813 "go back to PS_SLEEP");
1814 lbs_ps_sleep(priv, 0);
1821 lbs_deb_leave(LBS_DEB_THREAD);
1825 void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
1827 union iwreq_data iwrq;
1830 lbs_deb_enter(LBS_DEB_WEXT);
1832 memset(&iwrq, 0, sizeof(union iwreq_data));
1833 memset(buf, 0, sizeof(buf));
1835 snprintf(buf, sizeof(buf) - 1, "%s", str);
1837 iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
1839 /* Send Event to upper layer */
1840 lbs_deb_wext("event indication string %s\n", (char *)buf);
1841 lbs_deb_wext("event indication length %d\n", iwrq.data.length);
1842 lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
1844 wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1846 lbs_deb_leave(LBS_DEB_WEXT);
1849 static void lbs_send_confirmsleep(struct lbs_private *priv)
1851 unsigned long flags;
1854 lbs_deb_enter(LBS_DEB_HOST);
1855 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1856 sizeof(confirm_sleep));
1858 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1859 sizeof(confirm_sleep));
1861 lbs_pr_alert("confirm_sleep failed\n");
1865 spin_lock_irqsave(&priv->driver_lock, flags);
1867 /* We don't get a response on the sleep-confirmation */
1868 priv->dnld_sent = DNLD_RES_RECEIVED;
1870 /* If nothing to do, go back to sleep (?) */
1871 if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1872 priv->psstate = PS_STATE_SLEEP;
1874 spin_unlock_irqrestore(&priv->driver_lock, flags);
1877 lbs_deb_leave(LBS_DEB_HOST);
1880 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1882 lbs_deb_enter(LBS_DEB_HOST);
1885 * PS is currently supported only in Infrastructure mode
1886 * Remove this check if it is to be supported in IBSS mode also
1889 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1890 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1892 lbs_deb_leave(LBS_DEB_HOST);
1896 * @brief This function sends Exit_PS command to firmware.
1898 * @param priv A pointer to struct lbs_private structure
1899 * @param wait_option wait response or not
1902 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1906 lbs_deb_enter(LBS_DEB_HOST);
1908 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1910 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1912 wait_option, 0, &Localpsmode);
1914 lbs_deb_leave(LBS_DEB_HOST);
1918 * @brief This function checks condition and prepares to
1919 * send sleep confirm command to firmware if ok.
1921 * @param priv A pointer to struct lbs_private structure
1922 * @param psmode Power Saving mode
1925 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1927 unsigned long flags =0;
1930 lbs_deb_enter(LBS_DEB_HOST);
1932 spin_lock_irqsave(&priv->driver_lock, flags);
1933 if (priv->dnld_sent) {
1935 lbs_deb_host("dnld_sent was set\n");
1938 /* In-progress command? */
1939 if (priv->cur_cmd) {
1941 lbs_deb_host("cur_cmd was set\n");
1944 /* Pending events or command responses? */
1945 if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1947 lbs_deb_host("pending events or command responses\n");
1949 spin_unlock_irqrestore(&priv->driver_lock, flags);
1952 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1953 lbs_send_confirmsleep(priv);
1955 lbs_deb_host("sleep confirm has been delayed\n");
1958 lbs_deb_leave(LBS_DEB_HOST);
1963 * @brief Configures the transmission power control functionality.
1965 * @param priv A pointer to struct lbs_private structure
1966 * @param enable Transmission power control enable
1967 * @param p0 Power level when link quality is good (dBm).
1968 * @param p1 Power level when link quality is fair (dBm).
1969 * @param p2 Power level when link quality is poor (dBm).
1970 * @param usesnr Use Signal to Noise Ratio in TPC
1972 * @return 0 on success
1974 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1975 int8_t p2, int usesnr)
1977 struct cmd_ds_802_11_tpc_cfg cmd;
1980 memset(&cmd, 0, sizeof(cmd));
1981 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1982 cmd.action = cpu_to_le16(CMD_ACT_SET);
1983 cmd.enable = !!enable;
1984 cmd.usesnr = !!usesnr;
1989 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1995 * @brief Configures the power adaptation settings.
1997 * @param priv A pointer to struct lbs_private structure
1998 * @param enable Power adaptation enable
1999 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
2000 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
2001 * @param p2 Power level for 48 and 54 Mbps (dBm).
2003 * @return 0 on Success
2006 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
2007 int8_t p1, int8_t p2)
2009 struct cmd_ds_802_11_pa_cfg cmd;
2012 memset(&cmd, 0, sizeof(cmd));
2013 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2014 cmd.action = cpu_to_le16(CMD_ACT_SET);
2015 cmd.enable = !!enable;
2020 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
2026 static struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
2027 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
2028 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2029 unsigned long callback_arg)
2031 struct cmd_ctrl_node *cmdnode;
2033 lbs_deb_enter(LBS_DEB_HOST);
2035 if (priv->surpriseremoved) {
2036 lbs_deb_host("PREP_CMD: card removed\n");
2037 cmdnode = ERR_PTR(-ENOENT);
2041 cmdnode = lbs_get_cmd_ctrl_node(priv);
2042 if (cmdnode == NULL) {
2043 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
2045 /* Wake up main thread to execute next command */
2046 wake_up_interruptible(&priv->waitq);
2047 cmdnode = ERR_PTR(-ENOBUFS);
2051 cmdnode->callback = callback;
2052 cmdnode->callback_arg = callback_arg;
2054 /* Copy the incoming command to the buffer */
2055 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
2057 /* Set sequence number, clean result, move to buffer */
2059 cmdnode->cmdbuf->command = cpu_to_le16(command);
2060 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
2061 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
2062 cmdnode->cmdbuf->result = 0;
2064 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
2066 cmdnode->cmdwaitqwoken = 0;
2067 lbs_queue_cmd(priv, cmdnode);
2068 wake_up_interruptible(&priv->waitq);
2071 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
2075 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
2076 struct cmd_header *in_cmd, int in_cmd_size)
2078 lbs_deb_enter(LBS_DEB_CMD);
2079 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2080 lbs_cmd_async_callback, 0);
2081 lbs_deb_leave(LBS_DEB_CMD);
2084 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
2085 struct cmd_header *in_cmd, int in_cmd_size,
2086 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2087 unsigned long callback_arg)
2089 struct cmd_ctrl_node *cmdnode;
2090 unsigned long flags;
2093 lbs_deb_enter(LBS_DEB_HOST);
2095 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2096 callback, callback_arg);
2097 if (IS_ERR(cmdnode)) {
2098 ret = PTR_ERR(cmdnode);
2103 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
2105 spin_lock_irqsave(&priv->driver_lock, flags);
2106 ret = cmdnode->result;
2108 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
2111 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
2112 spin_unlock_irqrestore(&priv->driver_lock, flags);
2115 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
2118 EXPORT_SYMBOL_GPL(__lbs_cmd);