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 /* Clamp region code to 8-bit since FW spec indicates that it should
123 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
124 * returns non-zero high 8 bits here.
126 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
128 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
129 /* use the region code to search for the index */
130 if (priv->regioncode == lbs_region_code_to_index[i])
134 /* if it's unidentified region code, use the default (USA) */
135 if (i >= MRVDRV_MAX_REGION_CODE) {
136 priv->regioncode = 0x10;
137 lbs_pr_info("unidentified region code; using the default (USA)\n");
140 if (priv->current_addr[0] == 0xff)
141 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
143 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
145 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
147 if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
152 if (lbs_set_universaltable(priv, 0)) {
158 lbs_deb_leave(LBS_DEB_CMD);
162 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
163 struct wol_config *p_wol_config)
165 struct cmd_ds_host_sleep cmd_config;
168 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
169 cmd_config.criteria = cpu_to_le32(criteria);
170 cmd_config.gpio = priv->wol_gpio;
171 cmd_config.gap = priv->wol_gap;
173 if (p_wol_config != NULL)
174 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
175 sizeof(struct wol_config));
177 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
179 ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
182 lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
183 priv->wol_criteria = criteria;
185 memcpy((uint8_t *) p_wol_config,
186 (uint8_t *)&cmd_config.wol_conf,
187 sizeof(struct wol_config));
189 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
194 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
196 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
199 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
201 lbs_deb_enter(LBS_DEB_CMD);
203 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
204 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
206 psm->action = cpu_to_le16(cmd_action);
207 psm->multipledtim = 0;
208 switch (cmd_action) {
209 case CMD_SUBCMD_ENTER_PS:
210 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
212 psm->locallisteninterval = 0;
213 psm->nullpktinterval = 0;
215 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
218 case CMD_SUBCMD_EXIT_PS:
219 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
222 case CMD_SUBCMD_SLEEP_CONFIRMED:
223 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
230 lbs_deb_leave(LBS_DEB_CMD);
234 int lbs_cmd_802_11_inactivity_timeout(struct lbs_private *priv,
235 uint16_t cmd_action, uint16_t *timeout)
237 struct cmd_ds_802_11_inactivity_timeout cmd;
240 lbs_deb_enter(LBS_DEB_CMD);
242 cmd.hdr.command = cpu_to_le16(CMD_802_11_INACTIVITY_TIMEOUT);
243 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
245 cmd.action = cpu_to_le16(cmd_action);
247 if (cmd_action == CMD_ACT_SET)
248 cmd.timeout = cpu_to_le16(*timeout);
252 ret = lbs_cmd_with_response(priv, CMD_802_11_INACTIVITY_TIMEOUT, &cmd);
255 *timeout = le16_to_cpu(cmd.timeout);
257 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
261 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
262 struct sleep_params *sp)
264 struct cmd_ds_802_11_sleep_params cmd;
267 lbs_deb_enter(LBS_DEB_CMD);
269 if (cmd_action == CMD_ACT_GET) {
270 memset(&cmd, 0, sizeof(cmd));
272 cmd.error = cpu_to_le16(sp->sp_error);
273 cmd.offset = cpu_to_le16(sp->sp_offset);
274 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
275 cmd.calcontrol = sp->sp_calcontrol;
276 cmd.externalsleepclk = sp->sp_extsleepclk;
277 cmd.reserved = cpu_to_le16(sp->sp_reserved);
279 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
280 cmd.action = cpu_to_le16(cmd_action);
282 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
285 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
286 "calcontrol 0x%x extsleepclk 0x%x\n",
287 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
288 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
289 cmd.externalsleepclk);
291 sp->sp_error = le16_to_cpu(cmd.error);
292 sp->sp_offset = le16_to_cpu(cmd.offset);
293 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
294 sp->sp_calcontrol = cmd.calcontrol;
295 sp->sp_extsleepclk = cmd.externalsleepclk;
296 sp->sp_reserved = le16_to_cpu(cmd.reserved);
299 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
303 int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
304 struct assoc_request *assoc)
306 struct cmd_ds_802_11_set_wep cmd;
309 lbs_deb_enter(LBS_DEB_CMD);
311 memset(&cmd, 0, sizeof(cmd));
312 cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
313 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
315 cmd.action = cpu_to_le16(cmd_action);
317 if (cmd_action == CMD_ACT_ADD) {
320 /* default tx key index */
321 cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
322 CMD_WEP_KEY_INDEX_MASK);
324 /* Copy key types and material to host command structure */
325 for (i = 0; i < 4; i++) {
326 struct enc_key *pkey = &assoc->wep_keys[i];
330 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
331 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
332 lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
334 case KEY_LEN_WEP_104:
335 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
336 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
337 lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
342 lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
349 } else if (cmd_action == CMD_ACT_REMOVE) {
350 /* ACT_REMOVE clears _all_ WEP keys */
352 /* default tx key index */
353 cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
354 CMD_WEP_KEY_INDEX_MASK);
355 lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
358 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
360 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
364 int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
367 struct cmd_ds_802_11_enable_rsn cmd;
370 lbs_deb_enter(LBS_DEB_CMD);
372 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
373 cmd.action = cpu_to_le16(cmd_action);
375 if (cmd_action == CMD_ACT_GET)
379 cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
381 cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
382 lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
385 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
386 if (!ret && cmd_action == CMD_ACT_GET)
387 *enable = le16_to_cpu(cmd.enable);
389 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
393 static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
396 lbs_deb_enter(LBS_DEB_CMD);
398 if (key->flags & KEY_INFO_WPA_ENABLED)
399 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
400 if (key->flags & KEY_INFO_WPA_UNICAST)
401 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
402 if (key->flags & KEY_INFO_WPA_MCAST)
403 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
405 keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
406 keyparam->keytypeid = cpu_to_le16(key->type);
407 keyparam->keylen = cpu_to_le16(key->len);
408 memcpy(keyparam->key, key->key, key->len);
410 /* Length field doesn't include the {type,length} header */
411 keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
412 lbs_deb_leave(LBS_DEB_CMD);
415 int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
416 struct assoc_request *assoc)
418 struct cmd_ds_802_11_key_material cmd;
422 lbs_deb_enter(LBS_DEB_CMD);
424 cmd.action = cpu_to_le16(cmd_action);
425 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
427 if (cmd_action == CMD_ACT_GET) {
428 cmd.hdr.size = cpu_to_le16(S_DS_GEN + 2);
430 memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
432 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
433 set_one_wpa_key(&cmd.keyParamSet[index],
434 &assoc->wpa_unicast_key);
438 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
439 set_one_wpa_key(&cmd.keyParamSet[index],
440 &assoc->wpa_mcast_key);
444 /* The common header and as many keys as we included */
445 cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
446 keyParamSet[index]));
448 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
449 /* Copy the returned key to driver private data */
450 if (!ret && cmd_action == CMD_ACT_GET) {
451 void *buf_ptr = cmd.keyParamSet;
452 void *resp_end = &(&cmd)[1];
454 while (buf_ptr < resp_end) {
455 struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
457 uint16_t param_set_len = le16_to_cpu(keyparam->length);
458 uint16_t key_len = le16_to_cpu(keyparam->keylen);
459 uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
460 uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
463 end = (void *)keyparam + sizeof(keyparam->type)
464 + sizeof(keyparam->length) + param_set_len;
466 /* Make sure we don't access past the end of the IEs */
470 if (key_flags & KEY_INFO_WPA_UNICAST)
471 key = &priv->wpa_unicast_key;
472 else if (key_flags & KEY_INFO_WPA_MCAST)
473 key = &priv->wpa_mcast_key;
477 /* Copy returned key into driver */
478 memset(key, 0, sizeof(struct enc_key));
479 if (key_len > sizeof(key->key))
481 key->type = key_type;
482 key->flags = key_flags;
484 memcpy(key->key, keyparam->key, key->len);
490 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
495 * @brief Set an SNMP MIB value
497 * @param priv A pointer to struct lbs_private structure
498 * @param oid The OID to set in the firmware
499 * @param val Value to set the OID to
501 * @return 0 on success, error on failure
503 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
505 struct cmd_ds_802_11_snmp_mib cmd;
508 lbs_deb_enter(LBS_DEB_CMD);
510 memset(&cmd, 0, sizeof (cmd));
511 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
512 cmd.action = cpu_to_le16(CMD_ACT_SET);
513 cmd.oid = cpu_to_le16((u16) oid);
516 case SNMP_MIB_OID_BSS_TYPE:
517 cmd.bufsize = cpu_to_le16(sizeof(u8));
518 cmd.value[0] = (val == IW_MODE_ADHOC) ? 2 : 1;
520 case SNMP_MIB_OID_11D_ENABLE:
521 case SNMP_MIB_OID_FRAG_THRESHOLD:
522 case SNMP_MIB_OID_RTS_THRESHOLD:
523 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
524 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
525 cmd.bufsize = cpu_to_le16(sizeof(u16));
526 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
529 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
534 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
535 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
537 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
540 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
545 * @brief Get an SNMP MIB value
547 * @param priv A pointer to struct lbs_private structure
548 * @param oid The OID to retrieve from the firmware
549 * @param out_val Location for the returned value
551 * @return 0 on success, error on failure
553 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
555 struct cmd_ds_802_11_snmp_mib cmd;
558 lbs_deb_enter(LBS_DEB_CMD);
560 memset(&cmd, 0, sizeof (cmd));
561 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
562 cmd.action = cpu_to_le16(CMD_ACT_GET);
563 cmd.oid = cpu_to_le16(oid);
565 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
569 switch (le16_to_cpu(cmd.bufsize)) {
571 if (oid == SNMP_MIB_OID_BSS_TYPE) {
572 if (cmd.value[0] == 2)
573 *out_val = IW_MODE_ADHOC;
575 *out_val = IW_MODE_INFRA;
577 *out_val = cmd.value[0];
580 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
583 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
584 oid, le16_to_cpu(cmd.bufsize));
589 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
594 * @brief Get the min, max, and current TX power
596 * @param priv A pointer to struct lbs_private structure
597 * @param curlevel Current power level in dBm
598 * @param minlevel Minimum supported power level in dBm (optional)
599 * @param maxlevel Maximum supported power level in dBm (optional)
601 * @return 0 on success, error on failure
603 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
606 struct cmd_ds_802_11_rf_tx_power cmd;
609 lbs_deb_enter(LBS_DEB_CMD);
611 memset(&cmd, 0, sizeof(cmd));
612 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
613 cmd.action = cpu_to_le16(CMD_ACT_GET);
615 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
617 *curlevel = le16_to_cpu(cmd.curlevel);
619 *minlevel = cmd.minlevel;
621 *maxlevel = cmd.maxlevel;
624 lbs_deb_leave(LBS_DEB_CMD);
629 * @brief Set the TX power
631 * @param priv A pointer to struct lbs_private structure
632 * @param dbm The desired power level in dBm
634 * @return 0 on success, error on failure
636 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
638 struct cmd_ds_802_11_rf_tx_power cmd;
641 lbs_deb_enter(LBS_DEB_CMD);
643 memset(&cmd, 0, sizeof(cmd));
644 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
645 cmd.action = cpu_to_le16(CMD_ACT_SET);
646 cmd.curlevel = cpu_to_le16(dbm);
648 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
650 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
652 lbs_deb_leave(LBS_DEB_CMD);
656 static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
657 u16 cmd_action, void *pdata_buf)
659 struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
661 cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
663 cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
666 monitor->action = cpu_to_le16(cmd_action);
667 if (cmd_action == CMD_ACT_SET) {
669 cpu_to_le16((u16) (*(u32 *) pdata_buf));
675 static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
695 int i = lbs_data_rate_to_fw_index(rate);
697 ratemask = (0x1fef >> (12 - i));
700 return cpu_to_le16(ratemask);
703 int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
706 struct cmd_ds_802_11_rate_adapt_rateset cmd;
709 lbs_deb_enter(LBS_DEB_CMD);
711 if (!priv->cur_rate && !priv->enablehwauto)
714 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
716 cmd.action = cpu_to_le16(cmd_action);
717 cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
718 cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
719 ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
720 if (!ret && cmd_action == CMD_ACT_GET) {
721 priv->ratebitmap = le16_to_cpu(cmd.bitmap);
722 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
725 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
728 EXPORT_SYMBOL_GPL(lbs_cmd_802_11_rate_adapt_rateset);
731 * @brief Set the data rate
733 * @param priv A pointer to struct lbs_private structure
734 * @param rate The desired data rate, or 0 to clear a locked rate
736 * @return 0 on success, error on failure
738 int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
740 struct cmd_ds_802_11_data_rate cmd;
743 lbs_deb_enter(LBS_DEB_CMD);
745 memset(&cmd, 0, sizeof(cmd));
746 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
749 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
750 cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
751 if (cmd.rates[0] == 0) {
752 lbs_deb_cmd("DATA_RATE: invalid requested rate of"
757 lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
759 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
760 lbs_deb_cmd("DATA_RATE: setting auto\n");
763 ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
767 lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof (cmd));
769 /* FIXME: get actual rates FW can do if this command actually returns
770 * all data rates supported.
772 priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
773 lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
776 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
781 * @brief Get the radio channel
783 * @param priv A pointer to struct lbs_private structure
785 * @return The channel on success, error on failure
787 int lbs_get_channel(struct lbs_private *priv)
789 struct cmd_ds_802_11_rf_channel cmd;
792 lbs_deb_enter(LBS_DEB_CMD);
794 memset(&cmd, 0, sizeof(cmd));
795 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
796 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
798 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
802 ret = le16_to_cpu(cmd.channel);
803 lbs_deb_cmd("current radio channel is %d\n", ret);
806 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
810 int lbs_update_channel(struct lbs_private *priv)
814 /* the channel in f/w could be out of sync; get the current channel */
815 lbs_deb_enter(LBS_DEB_ASSOC);
817 ret = lbs_get_channel(priv);
819 priv->curbssparams.channel = ret;
822 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
827 * @brief Set the radio channel
829 * @param priv A pointer to struct lbs_private structure
830 * @param channel The desired channel, or 0 to clear a locked channel
832 * @return 0 on success, error on failure
834 int lbs_set_channel(struct lbs_private *priv, u8 channel)
836 struct cmd_ds_802_11_rf_channel cmd;
838 u8 old_channel = priv->curbssparams.channel;
842 lbs_deb_enter(LBS_DEB_CMD);
844 memset(&cmd, 0, sizeof(cmd));
845 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
846 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
847 cmd.channel = cpu_to_le16(channel);
849 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
853 priv->curbssparams.channel = (uint8_t) le16_to_cpu(cmd.channel);
854 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
855 priv->curbssparams.channel);
858 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
862 static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
863 struct cmd_ds_command *cmd)
866 lbs_deb_enter(LBS_DEB_CMD);
867 cmd->command = cpu_to_le16(CMD_802_11_RSSI);
868 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
869 cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
871 /* reset Beacon SNR/NF/RSSI values */
872 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
873 priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
874 priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
875 priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
876 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
877 priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
879 lbs_deb_leave(LBS_DEB_CMD);
883 static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
884 u8 cmd_action, void *pdata_buf)
886 struct lbs_offset_value *offval;
888 lbs_deb_enter(LBS_DEB_CMD);
890 offval = (struct lbs_offset_value *)pdata_buf;
892 switch (le16_to_cpu(cmdptr->command)) {
893 case CMD_MAC_REG_ACCESS:
895 struct cmd_ds_mac_reg_access *macreg;
898 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
901 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
904 macreg->action = cpu_to_le16(cmd_action);
905 macreg->offset = cpu_to_le16((u16) offval->offset);
906 macreg->value = cpu_to_le32(offval->value);
911 case CMD_BBP_REG_ACCESS:
913 struct cmd_ds_bbp_reg_access *bbpreg;
917 (struct cmd_ds_bbp_reg_access)
920 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
923 bbpreg->action = cpu_to_le16(cmd_action);
924 bbpreg->offset = cpu_to_le16((u16) offval->offset);
925 bbpreg->value = (u8) offval->value;
930 case CMD_RF_REG_ACCESS:
932 struct cmd_ds_rf_reg_access *rfreg;
936 (struct cmd_ds_rf_reg_access) +
939 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
942 rfreg->action = cpu_to_le16(cmd_action);
943 rfreg->offset = cpu_to_le16((u16) offval->offset);
944 rfreg->value = (u8) offval->value;
953 lbs_deb_leave(LBS_DEB_CMD);
957 static int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
958 u16 cmd_action, void *pdata_buf)
960 struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
961 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
963 cmd->command = cpu_to_le16(CMD_BT_ACCESS);
964 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
966 bt_access->action = cpu_to_le16(cmd_action);
968 switch (cmd_action) {
969 case CMD_ACT_BT_ACCESS_ADD:
970 memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
971 lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
973 case CMD_ACT_BT_ACCESS_DEL:
974 memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
975 lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
977 case CMD_ACT_BT_ACCESS_LIST:
978 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
980 case CMD_ACT_BT_ACCESS_RESET:
982 case CMD_ACT_BT_ACCESS_SET_INVERT:
983 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
985 case CMD_ACT_BT_ACCESS_GET_INVERT:
990 lbs_deb_leave(LBS_DEB_CMD);
994 static int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
995 u16 cmd_action, void *pdata_buf)
997 struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
998 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1000 cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
1001 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
1005 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
1007 memset(fwt_access, 0, sizeof(*fwt_access));
1009 fwt_access->action = cpu_to_le16(cmd_action);
1011 lbs_deb_leave(LBS_DEB_CMD);
1015 int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
1016 struct cmd_ds_mesh_access *cmd)
1020 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
1022 cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
1023 cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
1024 cmd->hdr.result = 0;
1026 cmd->action = cpu_to_le16(cmd_action);
1028 ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
1030 lbs_deb_leave(LBS_DEB_CMD);
1034 static int __lbs_mesh_config_send(struct lbs_private *priv,
1035 struct cmd_ds_mesh_config *cmd,
1036 uint16_t action, uint16_t type)
1040 lbs_deb_enter(LBS_DEB_CMD);
1042 cmd->hdr.command = cpu_to_le16(CMD_MESH_CONFIG);
1043 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
1044 cmd->hdr.result = 0;
1046 cmd->type = cpu_to_le16(type);
1047 cmd->action = cpu_to_le16(action);
1049 ret = lbs_cmd_with_response(priv, CMD_MESH_CONFIG, cmd);
1051 lbs_deb_leave(LBS_DEB_CMD);
1055 int lbs_mesh_config_send(struct lbs_private *priv,
1056 struct cmd_ds_mesh_config *cmd,
1057 uint16_t action, uint16_t type)
1061 if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
1064 ret = __lbs_mesh_config_send(priv, cmd, action, type);
1068 /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
1069 * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
1070 * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
1071 * lbs_mesh_config_send.
1073 int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
1075 struct cmd_ds_mesh_config cmd;
1076 struct mrvl_meshie *ie;
1077 DECLARE_SSID_BUF(ssid);
1079 memset(&cmd, 0, sizeof(cmd));
1080 cmd.channel = cpu_to_le16(chan);
1081 ie = (struct mrvl_meshie *)cmd.data;
1084 case CMD_ACT_MESH_CONFIG_START:
1085 ie->id = WLAN_EID_GENERIC;
1086 ie->val.oui[0] = 0x00;
1087 ie->val.oui[1] = 0x50;
1088 ie->val.oui[2] = 0x43;
1089 ie->val.type = MARVELL_MESH_IE_TYPE;
1090 ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
1091 ie->val.version = MARVELL_MESH_IE_VERSION;
1092 ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
1093 ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
1094 ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
1095 ie->val.mesh_id_len = priv->mesh_ssid_len;
1096 memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
1097 ie->len = sizeof(struct mrvl_meshie_val) -
1098 IW_ESSID_MAX_SIZE + priv->mesh_ssid_len;
1099 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
1101 case CMD_ACT_MESH_CONFIG_STOP:
1106 lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
1107 action, priv->mesh_tlv, chan,
1108 print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
1110 return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
1113 static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
1114 struct cmd_ds_command *cmd,
1117 struct cmd_ds_802_11_beacon_control
1118 *bcn_ctrl = &cmd->params.bcn_ctrl;
1120 lbs_deb_enter(LBS_DEB_CMD);
1122 cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
1124 cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
1126 bcn_ctrl->action = cpu_to_le16(cmd_action);
1127 bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
1128 bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
1130 lbs_deb_leave(LBS_DEB_CMD);
1134 static void lbs_queue_cmd(struct lbs_private *priv,
1135 struct cmd_ctrl_node *cmdnode)
1137 unsigned long flags;
1140 lbs_deb_enter(LBS_DEB_HOST);
1143 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
1146 if (!cmdnode->cmdbuf->size) {
1147 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
1150 cmdnode->result = 0;
1152 /* Exit_PS command needs to be queued in the header always. */
1153 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
1154 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
1156 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1157 if (priv->psstate != PS_STATE_FULL_POWER)
1162 spin_lock_irqsave(&priv->driver_lock, flags);
1165 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
1167 list_add(&cmdnode->list, &priv->cmdpendingq);
1169 spin_unlock_irqrestore(&priv->driver_lock, flags);
1171 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
1172 le16_to_cpu(cmdnode->cmdbuf->command));
1175 lbs_deb_leave(LBS_DEB_HOST);
1178 static void lbs_submit_command(struct lbs_private *priv,
1179 struct cmd_ctrl_node *cmdnode)
1181 unsigned long flags;
1182 struct cmd_header *cmd;
1188 lbs_deb_enter(LBS_DEB_HOST);
1190 cmd = cmdnode->cmdbuf;
1192 spin_lock_irqsave(&priv->driver_lock, flags);
1193 priv->cur_cmd = cmdnode;
1194 priv->cur_cmd_retcode = 0;
1195 spin_unlock_irqrestore(&priv->driver_lock, flags);
1197 cmdsize = le16_to_cpu(cmd->size);
1198 command = le16_to_cpu(cmd->command);
1200 /* These commands take longer */
1201 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE ||
1202 command == CMD_802_11_AUTHENTICATE)
1205 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1206 command, le16_to_cpu(cmd->seqnum), cmdsize);
1207 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1209 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1212 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1213 /* Let the timer kick in and retry, and potentially reset
1214 the whole thing if the condition persists */
1218 /* Setup the timer after transmit command */
1219 mod_timer(&priv->command_timer, jiffies + timeo);
1221 lbs_deb_leave(LBS_DEB_HOST);
1225 * This function inserts command node to cmdfreeq
1226 * after cleans it. Requires priv->driver_lock held.
1228 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1229 struct cmd_ctrl_node *cmdnode)
1231 lbs_deb_enter(LBS_DEB_HOST);
1236 cmdnode->callback = NULL;
1237 cmdnode->callback_arg = 0;
1239 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1241 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1243 lbs_deb_leave(LBS_DEB_HOST);
1246 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1247 struct cmd_ctrl_node *ptempcmd)
1249 unsigned long flags;
1251 spin_lock_irqsave(&priv->driver_lock, flags);
1252 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1253 spin_unlock_irqrestore(&priv->driver_lock, flags);
1256 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1259 if (cmd == priv->cur_cmd)
1260 priv->cur_cmd_retcode = result;
1262 cmd->result = result;
1263 cmd->cmdwaitqwoken = 1;
1264 wake_up_interruptible(&cmd->cmdwait_q);
1266 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1267 __lbs_cleanup_and_insert_cmd(priv, cmd);
1268 priv->cur_cmd = NULL;
1271 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1273 struct cmd_ds_802_11_radio_control cmd;
1276 lbs_deb_enter(LBS_DEB_CMD);
1278 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1279 cmd.action = cpu_to_le16(CMD_ACT_SET);
1281 /* Only v8 and below support setting the preamble */
1282 if (priv->fwrelease < 0x09000000) {
1284 case RADIO_PREAMBLE_SHORT:
1285 if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
1288 case RADIO_PREAMBLE_AUTO:
1289 case RADIO_PREAMBLE_LONG:
1290 cmd.control = cpu_to_le16(preamble);
1298 cmd.control |= cpu_to_le16(0x1);
1300 cmd.control &= cpu_to_le16(~0x1);
1301 priv->txpower_cur = 0;
1304 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1305 radio_on ? "ON" : "OFF", preamble);
1307 priv->radio_on = radio_on;
1309 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1312 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1316 void lbs_set_mac_control(struct lbs_private *priv)
1318 struct cmd_ds_mac_control cmd;
1320 lbs_deb_enter(LBS_DEB_CMD);
1322 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1323 cmd.action = cpu_to_le16(priv->mac_control);
1326 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1328 lbs_deb_leave(LBS_DEB_CMD);
1332 * @brief This function prepare the command before send to firmware.
1334 * @param priv A pointer to struct lbs_private structure
1335 * @param cmd_no command number
1336 * @param cmd_action command action: GET or SET
1337 * @param wait_option wait option: wait response or not
1338 * @param cmd_oid cmd oid: treated as sub command
1339 * @param pdata_buf A pointer to informaion buffer
1342 int lbs_prepare_and_send_command(struct lbs_private *priv,
1345 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1348 struct cmd_ctrl_node *cmdnode;
1349 struct cmd_ds_command *cmdptr;
1350 unsigned long flags;
1352 lbs_deb_enter(LBS_DEB_HOST);
1355 lbs_deb_host("PREP_CMD: priv is NULL\n");
1360 if (priv->surpriseremoved) {
1361 lbs_deb_host("PREP_CMD: card removed\n");
1366 cmdnode = lbs_get_cmd_ctrl_node(priv);
1368 if (cmdnode == NULL) {
1369 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1371 /* Wake up main thread to execute next command */
1372 wake_up_interruptible(&priv->waitq);
1377 cmdnode->callback = NULL;
1378 cmdnode->callback_arg = (unsigned long)pdata_buf;
1380 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1382 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1384 /* Set sequence number, command and INT option */
1386 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1388 cmdptr->command = cpu_to_le16(cmd_no);
1392 case CMD_802_11_PS_MODE:
1393 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1396 case CMD_802_11_ASSOCIATE:
1397 case CMD_802_11_REASSOCIATE:
1398 ret = lbs_cmd_80211_associate(priv, cmdptr, pdata_buf);
1401 case CMD_802_11_AUTHENTICATE:
1402 ret = lbs_cmd_80211_authenticate(priv, cmdptr, pdata_buf);
1405 case CMD_MAC_REG_ACCESS:
1406 case CMD_BBP_REG_ACCESS:
1407 case CMD_RF_REG_ACCESS:
1408 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
1411 case CMD_802_11_MONITOR_MODE:
1412 ret = lbs_cmd_802_11_monitor_mode(cmdptr,
1413 cmd_action, pdata_buf);
1416 case CMD_802_11_RSSI:
1417 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1420 case CMD_802_11_SET_AFC:
1421 case CMD_802_11_GET_AFC:
1423 cmdptr->command = cpu_to_le16(cmd_no);
1424 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1427 memmove(&cmdptr->params.afc,
1428 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1433 case CMD_802_11D_DOMAIN_INFO:
1434 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr,
1435 cmd_no, cmd_action);
1438 case CMD_802_11_TPC_CFG:
1439 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1441 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1444 memmove(&cmdptr->params.tpccfg,
1445 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1449 case CMD_802_11_LED_GPIO_CTRL:
1451 struct mrvlietypes_ledgpio *gpio =
1452 (struct mrvlietypes_ledgpio*)
1453 cmdptr->params.ledgpio.data;
1455 memmove(&cmdptr->params.ledgpio,
1457 sizeof(struct cmd_ds_802_11_led_ctrl));
1460 cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
1462 #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
1464 cpu_to_le16(le16_to_cpu(gpio->header.len)
1466 + ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
1467 gpio->header.len = gpio->header.len;
1474 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1477 case CMD_FWT_ACCESS:
1478 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1482 cmdptr->command = cpu_to_le16(CMD_GET_TSF);
1483 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
1487 case CMD_802_11_BEACON_CTRL:
1488 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1491 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1496 /* return error, since the command preparation failed */
1498 lbs_deb_host("PREP_CMD: command preparation failed\n");
1499 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1504 cmdnode->cmdwaitqwoken = 0;
1506 lbs_queue_cmd(priv, cmdnode);
1507 wake_up_interruptible(&priv->waitq);
1509 if (wait_option & CMD_OPTION_WAITFORRSP) {
1510 lbs_deb_host("PREP_CMD: wait for response\n");
1512 wait_event_interruptible(cmdnode->cmdwait_q,
1513 cmdnode->cmdwaitqwoken);
1516 spin_lock_irqsave(&priv->driver_lock, flags);
1517 if (priv->cur_cmd_retcode) {
1518 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1519 priv->cur_cmd_retcode);
1520 priv->cur_cmd_retcode = 0;
1523 spin_unlock_irqrestore(&priv->driver_lock, flags);
1526 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1531 * @brief This function allocates the command buffer and link
1532 * it to command free queue.
1534 * @param priv A pointer to struct lbs_private structure
1537 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1542 struct cmd_ctrl_node *cmdarray;
1544 lbs_deb_enter(LBS_DEB_HOST);
1546 /* Allocate and initialize the command array */
1547 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1548 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1549 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1553 priv->cmd_array = cmdarray;
1555 /* Allocate and initialize each command buffer in the command array */
1556 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1557 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1558 if (!cmdarray[i].cmdbuf) {
1559 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1565 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1566 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1567 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1572 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1577 * @brief This function frees the command buffer.
1579 * @param priv A pointer to struct lbs_private structure
1582 int lbs_free_cmd_buffer(struct lbs_private *priv)
1584 struct cmd_ctrl_node *cmdarray;
1587 lbs_deb_enter(LBS_DEB_HOST);
1589 /* need to check if cmd array is allocated or not */
1590 if (priv->cmd_array == NULL) {
1591 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1595 cmdarray = priv->cmd_array;
1597 /* Release shared memory buffers */
1598 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1599 if (cmdarray[i].cmdbuf) {
1600 kfree(cmdarray[i].cmdbuf);
1601 cmdarray[i].cmdbuf = NULL;
1605 /* Release cmd_ctrl_node */
1606 if (priv->cmd_array) {
1607 kfree(priv->cmd_array);
1608 priv->cmd_array = NULL;
1612 lbs_deb_leave(LBS_DEB_HOST);
1617 * @brief This function gets a free command node if available in
1618 * command free queue.
1620 * @param priv A pointer to struct lbs_private structure
1621 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1623 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1625 struct cmd_ctrl_node *tempnode;
1626 unsigned long flags;
1628 lbs_deb_enter(LBS_DEB_HOST);
1633 spin_lock_irqsave(&priv->driver_lock, flags);
1635 if (!list_empty(&priv->cmdfreeq)) {
1636 tempnode = list_first_entry(&priv->cmdfreeq,
1637 struct cmd_ctrl_node, list);
1638 list_del(&tempnode->list);
1640 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1644 spin_unlock_irqrestore(&priv->driver_lock, flags);
1646 lbs_deb_leave(LBS_DEB_HOST);
1651 * @brief This function executes next command in command
1652 * pending queue. It will put fimware back to PS mode
1655 * @param priv A pointer to struct lbs_private structure
1658 int lbs_execute_next_command(struct lbs_private *priv)
1660 struct cmd_ctrl_node *cmdnode = NULL;
1661 struct cmd_header *cmd;
1662 unsigned long flags;
1665 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1666 * only caller to us is lbs_thread() and we get even when a
1667 * data packet is received */
1668 lbs_deb_enter(LBS_DEB_THREAD);
1670 spin_lock_irqsave(&priv->driver_lock, flags);
1672 if (priv->cur_cmd) {
1673 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1674 spin_unlock_irqrestore(&priv->driver_lock, flags);
1679 if (!list_empty(&priv->cmdpendingq)) {
1680 cmdnode = list_first_entry(&priv->cmdpendingq,
1681 struct cmd_ctrl_node, list);
1684 spin_unlock_irqrestore(&priv->driver_lock, flags);
1687 cmd = cmdnode->cmdbuf;
1689 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1690 if ((priv->psstate == PS_STATE_SLEEP) ||
1691 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1693 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1694 le16_to_cpu(cmd->command),
1699 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1700 "0x%04x in psstate %d\n",
1701 le16_to_cpu(cmd->command), priv->psstate);
1702 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1704 * 1. Non-PS command:
1705 * Queue it. set needtowakeup to TRUE if current state
1706 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1707 * 2. PS command but not Exit_PS:
1709 * 3. PS command Exit_PS:
1710 * Set needtowakeup to TRUE if current state is SLEEP,
1711 * otherwise send this command down to firmware
1714 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1715 /* Prepare to send Exit PS,
1716 * this non PS command will be sent later */
1717 if ((priv->psstate == PS_STATE_SLEEP)
1718 || (priv->psstate == PS_STATE_PRE_SLEEP)
1720 /* w/ new scheme, it will not reach here.
1721 since it is blocked in main_thread. */
1722 priv->needtowakeup = 1;
1724 lbs_ps_wakeup(priv, 0);
1730 * PS command. Ignore it if it is not Exit_PS.
1731 * otherwise send it down immediately.
1733 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1736 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1739 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1741 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1742 list_del(&cmdnode->list);
1743 spin_lock_irqsave(&priv->driver_lock, flags);
1744 lbs_complete_command(priv, cmdnode, 0);
1745 spin_unlock_irqrestore(&priv->driver_lock, flags);
1751 if ((priv->psstate == PS_STATE_SLEEP) ||
1752 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1754 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1755 list_del(&cmdnode->list);
1756 spin_lock_irqsave(&priv->driver_lock, flags);
1757 lbs_complete_command(priv, cmdnode, 0);
1758 spin_unlock_irqrestore(&priv->driver_lock, flags);
1759 priv->needtowakeup = 1;
1766 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1769 list_del(&cmdnode->list);
1770 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1771 le16_to_cpu(cmd->command));
1772 lbs_submit_command(priv, cmdnode);
1775 * check if in power save mode, if yes, put the device back
1778 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1779 (priv->psstate == PS_STATE_FULL_POWER) &&
1780 ((priv->connect_status == LBS_CONNECTED) ||
1781 (priv->mesh_connect_status == LBS_CONNECTED))) {
1782 if (priv->secinfo.WPAenabled ||
1783 priv->secinfo.WPA2enabled) {
1784 /* check for valid WPA group keys */
1785 if (priv->wpa_mcast_key.len ||
1786 priv->wpa_unicast_key.len) {
1788 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1789 " go back to PS_SLEEP");
1790 lbs_ps_sleep(priv, 0);
1794 "EXEC_NEXT_CMD: cmdpendingq empty, "
1795 "go back to PS_SLEEP");
1796 lbs_ps_sleep(priv, 0);
1803 lbs_deb_leave(LBS_DEB_THREAD);
1807 void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
1809 union iwreq_data iwrq;
1812 lbs_deb_enter(LBS_DEB_WEXT);
1814 memset(&iwrq, 0, sizeof(union iwreq_data));
1815 memset(buf, 0, sizeof(buf));
1817 snprintf(buf, sizeof(buf) - 1, "%s", str);
1819 iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
1821 /* Send Event to upper layer */
1822 lbs_deb_wext("event indication string %s\n", (char *)buf);
1823 lbs_deb_wext("event indication length %d\n", iwrq.data.length);
1824 lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
1826 wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1828 lbs_deb_leave(LBS_DEB_WEXT);
1831 static void lbs_send_confirmsleep(struct lbs_private *priv)
1833 unsigned long flags;
1836 lbs_deb_enter(LBS_DEB_HOST);
1837 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1838 sizeof(confirm_sleep));
1840 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1841 sizeof(confirm_sleep));
1843 lbs_pr_alert("confirm_sleep failed\n");
1847 spin_lock_irqsave(&priv->driver_lock, flags);
1849 /* We don't get a response on the sleep-confirmation */
1850 priv->dnld_sent = DNLD_RES_RECEIVED;
1852 /* If nothing to do, go back to sleep (?) */
1853 if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1854 priv->psstate = PS_STATE_SLEEP;
1856 spin_unlock_irqrestore(&priv->driver_lock, flags);
1859 lbs_deb_leave(LBS_DEB_HOST);
1862 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1864 lbs_deb_enter(LBS_DEB_HOST);
1867 * PS is currently supported only in Infrastructure mode
1868 * Remove this check if it is to be supported in IBSS mode also
1871 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1872 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1874 lbs_deb_leave(LBS_DEB_HOST);
1878 * @brief This function sends Exit_PS command to firmware.
1880 * @param priv A pointer to struct lbs_private structure
1881 * @param wait_option wait response or not
1884 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1888 lbs_deb_enter(LBS_DEB_HOST);
1890 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1892 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1894 wait_option, 0, &Localpsmode);
1896 lbs_deb_leave(LBS_DEB_HOST);
1900 * @brief This function checks condition and prepares to
1901 * send sleep confirm command to firmware if ok.
1903 * @param priv A pointer to struct lbs_private structure
1904 * @param psmode Power Saving mode
1907 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1909 unsigned long flags =0;
1912 lbs_deb_enter(LBS_DEB_HOST);
1914 spin_lock_irqsave(&priv->driver_lock, flags);
1915 if (priv->dnld_sent) {
1917 lbs_deb_host("dnld_sent was set\n");
1920 /* In-progress command? */
1921 if (priv->cur_cmd) {
1923 lbs_deb_host("cur_cmd was set\n");
1926 /* Pending events or command responses? */
1927 if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1929 lbs_deb_host("pending events or command responses\n");
1931 spin_unlock_irqrestore(&priv->driver_lock, flags);
1934 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1935 lbs_send_confirmsleep(priv);
1937 lbs_deb_host("sleep confirm has been delayed\n");
1940 lbs_deb_leave(LBS_DEB_HOST);
1945 * @brief Configures the transmission power control functionality.
1947 * @param priv A pointer to struct lbs_private structure
1948 * @param enable Transmission power control enable
1949 * @param p0 Power level when link quality is good (dBm).
1950 * @param p1 Power level when link quality is fair (dBm).
1951 * @param p2 Power level when link quality is poor (dBm).
1952 * @param usesnr Use Signal to Noise Ratio in TPC
1954 * @return 0 on success
1956 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1957 int8_t p2, int usesnr)
1959 struct cmd_ds_802_11_tpc_cfg cmd;
1962 memset(&cmd, 0, sizeof(cmd));
1963 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1964 cmd.action = cpu_to_le16(CMD_ACT_SET);
1965 cmd.enable = !!enable;
1966 cmd.usesnr = !!usesnr;
1971 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1977 * @brief Configures the power adaptation settings.
1979 * @param priv A pointer to struct lbs_private structure
1980 * @param enable Power adaptation enable
1981 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1982 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1983 * @param p2 Power level for 48 and 54 Mbps (dBm).
1985 * @return 0 on Success
1988 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1989 int8_t p1, int8_t p2)
1991 struct cmd_ds_802_11_pa_cfg cmd;
1994 memset(&cmd, 0, sizeof(cmd));
1995 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1996 cmd.action = cpu_to_le16(CMD_ACT_SET);
1997 cmd.enable = !!enable;
2002 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
2008 static struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
2009 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
2010 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2011 unsigned long callback_arg)
2013 struct cmd_ctrl_node *cmdnode;
2015 lbs_deb_enter(LBS_DEB_HOST);
2017 if (priv->surpriseremoved) {
2018 lbs_deb_host("PREP_CMD: card removed\n");
2019 cmdnode = ERR_PTR(-ENOENT);
2023 cmdnode = lbs_get_cmd_ctrl_node(priv);
2024 if (cmdnode == NULL) {
2025 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
2027 /* Wake up main thread to execute next command */
2028 wake_up_interruptible(&priv->waitq);
2029 cmdnode = ERR_PTR(-ENOBUFS);
2033 cmdnode->callback = callback;
2034 cmdnode->callback_arg = callback_arg;
2036 /* Copy the incoming command to the buffer */
2037 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
2039 /* Set sequence number, clean result, move to buffer */
2041 cmdnode->cmdbuf->command = cpu_to_le16(command);
2042 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
2043 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
2044 cmdnode->cmdbuf->result = 0;
2046 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
2048 cmdnode->cmdwaitqwoken = 0;
2049 lbs_queue_cmd(priv, cmdnode);
2050 wake_up_interruptible(&priv->waitq);
2053 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
2057 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
2058 struct cmd_header *in_cmd, int in_cmd_size)
2060 lbs_deb_enter(LBS_DEB_CMD);
2061 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2062 lbs_cmd_async_callback, 0);
2063 lbs_deb_leave(LBS_DEB_CMD);
2066 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
2067 struct cmd_header *in_cmd, int in_cmd_size,
2068 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
2069 unsigned long callback_arg)
2071 struct cmd_ctrl_node *cmdnode;
2072 unsigned long flags;
2075 lbs_deb_enter(LBS_DEB_HOST);
2077 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
2078 callback, callback_arg);
2079 if (IS_ERR(cmdnode)) {
2080 ret = PTR_ERR(cmdnode);
2085 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
2087 spin_lock_irqsave(&priv->driver_lock, flags);
2088 ret = cmdnode->result;
2090 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
2093 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
2094 spin_unlock_irqrestore(&priv->driver_lock, flags);
2097 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
2100 EXPORT_SYMBOL_GPL(__lbs_cmd);