libertas: remove unused indirect TPC_CFG command leftovers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / libertas / cmd.c
blob76a892eeef104bee777bc77a22d2ea58a8ae3402
1 /**
2 * This file contains the handling of command.
3 * It prepares command and sends it to firmware when it is ready.
4 */
6 #include <linux/kfifo.h>
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/if_arp.h>
11 #include "decl.h"
12 #include "cfg.h"
13 #include "cmd.h"
15 #define CAL_NF(nf) ((s32)(-(s32)(nf)))
16 #define CAL_RSSI(snr, nf) ((s32)((s32)(snr) + CAL_NF(nf)))
18 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
20 /**
21 * @brief Simple callback that copies response back into command
23 * @param priv A pointer to struct lbs_private structure
24 * @param extra A pointer to the original command structure for which
25 * 'resp' is a response
26 * @param resp A pointer to the command response
28 * @return 0 on success, error on failure
30 int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
31 struct cmd_header *resp)
33 struct cmd_header *buf = (void *)extra;
34 uint16_t copy_len;
36 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
37 memcpy(buf, resp, copy_len);
38 return 0;
40 EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
42 /**
43 * @brief Simple callback that ignores the result. Use this if
44 * you just want to send a command to the hardware, but don't
45 * care for the result.
47 * @param priv ignored
48 * @param extra ignored
49 * @param resp ignored
51 * @return 0 for success
53 static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
54 struct cmd_header *resp)
56 return 0;
60 /**
61 * @brief Checks whether a command is allowed in Power Save mode
63 * @param command the command ID
64 * @return 1 if allowed, 0 if not allowed
66 static u8 is_command_allowed_in_ps(u16 cmd)
68 switch (cmd) {
69 case CMD_802_11_RSSI:
70 return 1;
71 case CMD_802_11_HOST_SLEEP_CFG:
72 return 1;
73 default:
74 break;
76 return 0;
79 /**
80 * @brief This function checks if the command is allowed.
82 * @param priv A pointer to lbs_private structure
83 * @return allowed or not allowed.
86 static int lbs_is_cmd_allowed(struct lbs_private *priv)
88 int ret = 1;
90 lbs_deb_enter(LBS_DEB_CMD);
92 if (!priv->is_auto_deep_sleep_enabled) {
93 if (priv->is_deep_sleep) {
94 lbs_deb_cmd("command not allowed in deep sleep\n");
95 ret = 0;
99 lbs_deb_leave(LBS_DEB_CMD);
100 return ret;
104 * @brief Updates the hardware details like MAC address and regulatory region
106 * @param priv A pointer to struct lbs_private structure
108 * @return 0 on success, error on failure
110 int lbs_update_hw_spec(struct lbs_private *priv)
112 struct cmd_ds_get_hw_spec cmd;
113 int ret = -1;
114 u32 i;
116 lbs_deb_enter(LBS_DEB_CMD);
118 memset(&cmd, 0, sizeof(cmd));
119 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
120 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
121 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
122 if (ret)
123 goto out;
125 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
127 /* The firmware release is in an interesting format: the patch
128 * level is in the most significant nibble ... so fix that: */
129 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
130 priv->fwrelease = (priv->fwrelease << 8) |
131 (priv->fwrelease >> 24 & 0xff);
133 /* Some firmware capabilities:
134 * CF card firmware 5.0.16p0: cap 0x00000303
135 * USB dongle firmware 5.110.17p2: cap 0x00000303
137 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
138 cmd.permanentaddr,
139 priv->fwrelease >> 24 & 0xff,
140 priv->fwrelease >> 16 & 0xff,
141 priv->fwrelease >> 8 & 0xff,
142 priv->fwrelease & 0xff,
143 priv->fwcapinfo);
144 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
145 cmd.hwifversion, cmd.version);
147 /* Clamp region code to 8-bit since FW spec indicates that it should
148 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
149 * returns non-zero high 8 bits here.
151 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
152 * need to check for this problem and handle it properly.
154 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
155 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
156 else
157 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
159 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
160 /* use the region code to search for the index */
161 if (priv->regioncode == lbs_region_code_to_index[i])
162 break;
165 /* if it's unidentified region code, use the default (USA) */
166 if (i >= MRVDRV_MAX_REGION_CODE) {
167 priv->regioncode = 0x10;
168 lbs_pr_info("unidentified region code; using the default (USA)\n");
171 if (priv->current_addr[0] == 0xff)
172 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
174 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
175 if (priv->mesh_dev)
176 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
178 out:
179 lbs_deb_leave(LBS_DEB_CMD);
180 return ret;
183 static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
184 struct cmd_header *resp)
186 lbs_deb_enter(LBS_DEB_CMD);
187 if (priv->is_host_sleep_activated) {
188 priv->is_host_sleep_configured = 0;
189 if (priv->psstate == PS_STATE_FULL_POWER) {
190 priv->is_host_sleep_activated = 0;
191 wake_up_interruptible(&priv->host_sleep_q);
193 } else {
194 priv->is_host_sleep_configured = 1;
196 lbs_deb_leave(LBS_DEB_CMD);
197 return 0;
200 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
201 struct wol_config *p_wol_config)
203 struct cmd_ds_host_sleep cmd_config;
204 int ret;
206 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
207 cmd_config.criteria = cpu_to_le32(criteria);
208 cmd_config.gpio = priv->wol_gpio;
209 cmd_config.gap = priv->wol_gap;
211 if (p_wol_config != NULL)
212 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
213 sizeof(struct wol_config));
214 else
215 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
217 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
218 le16_to_cpu(cmd_config.hdr.size),
219 lbs_ret_host_sleep_cfg, 0);
220 if (!ret) {
221 if (p_wol_config)
222 memcpy((uint8_t *) p_wol_config,
223 (uint8_t *)&cmd_config.wol_conf,
224 sizeof(struct wol_config));
225 } else {
226 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
229 return ret;
231 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
233 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
234 u16 cmd_action)
236 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
238 lbs_deb_enter(LBS_DEB_CMD);
240 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
241 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
242 sizeof(struct cmd_header));
243 psm->action = cpu_to_le16(cmd_action);
244 psm->multipledtim = 0;
245 switch (cmd_action) {
246 case CMD_SUBCMD_ENTER_PS:
247 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
249 psm->locallisteninterval = 0;
250 psm->nullpktinterval = 0;
251 psm->multipledtim =
252 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
253 break;
255 case CMD_SUBCMD_EXIT_PS:
256 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
257 break;
259 case CMD_SUBCMD_SLEEP_CONFIRMED:
260 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
261 break;
263 default:
264 break;
267 lbs_deb_leave(LBS_DEB_CMD);
268 return 0;
271 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
272 struct sleep_params *sp)
274 struct cmd_ds_802_11_sleep_params cmd;
275 int ret;
277 lbs_deb_enter(LBS_DEB_CMD);
279 if (cmd_action == CMD_ACT_GET) {
280 memset(&cmd, 0, sizeof(cmd));
281 } else {
282 cmd.error = cpu_to_le16(sp->sp_error);
283 cmd.offset = cpu_to_le16(sp->sp_offset);
284 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
285 cmd.calcontrol = sp->sp_calcontrol;
286 cmd.externalsleepclk = sp->sp_extsleepclk;
287 cmd.reserved = cpu_to_le16(sp->sp_reserved);
289 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
290 cmd.action = cpu_to_le16(cmd_action);
292 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
294 if (!ret) {
295 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
296 "calcontrol 0x%x extsleepclk 0x%x\n",
297 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
298 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
299 cmd.externalsleepclk);
301 sp->sp_error = le16_to_cpu(cmd.error);
302 sp->sp_offset = le16_to_cpu(cmd.offset);
303 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
304 sp->sp_calcontrol = cmd.calcontrol;
305 sp->sp_extsleepclk = cmd.externalsleepclk;
306 sp->sp_reserved = le16_to_cpu(cmd.reserved);
309 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
310 return 0;
313 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
315 int ret = 0;
317 lbs_deb_enter(LBS_DEB_CMD);
319 if (priv->is_deep_sleep) {
320 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
321 !priv->is_deep_sleep, (10 * HZ))) {
322 lbs_pr_err("ds_awake_q: timer expired\n");
323 ret = -1;
327 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
328 return ret;
331 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
333 int ret = 0;
335 lbs_deb_enter(LBS_DEB_CMD);
337 if (deep_sleep) {
338 if (priv->is_deep_sleep != 1) {
339 lbs_deb_cmd("deep sleep: sleep\n");
340 BUG_ON(!priv->enter_deep_sleep);
341 ret = priv->enter_deep_sleep(priv);
342 if (!ret) {
343 netif_stop_queue(priv->dev);
344 netif_carrier_off(priv->dev);
346 } else {
347 lbs_pr_err("deep sleep: already enabled\n");
349 } else {
350 if (priv->is_deep_sleep) {
351 lbs_deb_cmd("deep sleep: wakeup\n");
352 BUG_ON(!priv->exit_deep_sleep);
353 ret = priv->exit_deep_sleep(priv);
354 if (!ret) {
355 ret = lbs_wait_for_ds_awake(priv);
356 if (ret)
357 lbs_pr_err("deep sleep: wakeup"
358 "failed\n");
363 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
364 return ret;
367 static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
368 unsigned long dummy,
369 struct cmd_header *cmd)
371 lbs_deb_enter(LBS_DEB_FW);
372 priv->is_host_sleep_activated = 1;
373 wake_up_interruptible(&priv->host_sleep_q);
374 lbs_deb_leave(LBS_DEB_FW);
375 return 0;
378 int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
380 struct cmd_header cmd;
381 int ret = 0;
382 uint32_t criteria = EHS_REMOVE_WAKEUP;
384 lbs_deb_enter(LBS_DEB_CMD);
386 if (host_sleep) {
387 if (priv->is_host_sleep_activated != 1) {
388 memset(&cmd, 0, sizeof(cmd));
389 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
390 (struct wol_config *)NULL);
391 if (ret) {
392 lbs_pr_info("Host sleep configuration failed: "
393 "%d\n", ret);
394 return ret;
396 if (priv->psstate == PS_STATE_FULL_POWER) {
397 ret = __lbs_cmd(priv,
398 CMD_802_11_HOST_SLEEP_ACTIVATE,
399 &cmd,
400 sizeof(cmd),
401 lbs_ret_host_sleep_activate, 0);
402 if (ret)
403 lbs_pr_info("HOST_SLEEP_ACTIVATE "
404 "failed: %d\n", ret);
407 if (!wait_event_interruptible_timeout(
408 priv->host_sleep_q,
409 priv->is_host_sleep_activated,
410 (10 * HZ))) {
411 lbs_pr_err("host_sleep_q: timer expired\n");
412 ret = -1;
414 } else {
415 lbs_pr_err("host sleep: already enabled\n");
417 } else {
418 if (priv->is_host_sleep_activated)
419 ret = lbs_host_sleep_cfg(priv, criteria,
420 (struct wol_config *)NULL);
423 return ret;
427 * @brief Set an SNMP MIB value
429 * @param priv A pointer to struct lbs_private structure
430 * @param oid The OID to set in the firmware
431 * @param val Value to set the OID to
433 * @return 0 on success, error on failure
435 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
437 struct cmd_ds_802_11_snmp_mib cmd;
438 int ret;
440 lbs_deb_enter(LBS_DEB_CMD);
442 memset(&cmd, 0, sizeof (cmd));
443 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
444 cmd.action = cpu_to_le16(CMD_ACT_SET);
445 cmd.oid = cpu_to_le16((u16) oid);
447 switch (oid) {
448 case SNMP_MIB_OID_BSS_TYPE:
449 cmd.bufsize = cpu_to_le16(sizeof(u8));
450 cmd.value[0] = val;
451 break;
452 case SNMP_MIB_OID_11D_ENABLE:
453 case SNMP_MIB_OID_FRAG_THRESHOLD:
454 case SNMP_MIB_OID_RTS_THRESHOLD:
455 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
456 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
457 cmd.bufsize = cpu_to_le16(sizeof(u16));
458 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
459 break;
460 default:
461 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
462 ret = -EINVAL;
463 goto out;
466 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
467 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
469 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
471 out:
472 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
473 return ret;
477 * @brief Get an SNMP MIB value
479 * @param priv A pointer to struct lbs_private structure
480 * @param oid The OID to retrieve from the firmware
481 * @param out_val Location for the returned value
483 * @return 0 on success, error on failure
485 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
487 struct cmd_ds_802_11_snmp_mib cmd;
488 int ret;
490 lbs_deb_enter(LBS_DEB_CMD);
492 memset(&cmd, 0, sizeof (cmd));
493 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
494 cmd.action = cpu_to_le16(CMD_ACT_GET);
495 cmd.oid = cpu_to_le16(oid);
497 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
498 if (ret)
499 goto out;
501 switch (le16_to_cpu(cmd.bufsize)) {
502 case sizeof(u8):
503 *out_val = cmd.value[0];
504 break;
505 case sizeof(u16):
506 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
507 break;
508 default:
509 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
510 oid, le16_to_cpu(cmd.bufsize));
511 break;
514 out:
515 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
516 return ret;
520 * @brief Get the min, max, and current TX power
522 * @param priv A pointer to struct lbs_private structure
523 * @param curlevel Current power level in dBm
524 * @param minlevel Minimum supported power level in dBm (optional)
525 * @param maxlevel Maximum supported power level in dBm (optional)
527 * @return 0 on success, error on failure
529 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
530 s16 *maxlevel)
532 struct cmd_ds_802_11_rf_tx_power cmd;
533 int ret;
535 lbs_deb_enter(LBS_DEB_CMD);
537 memset(&cmd, 0, sizeof(cmd));
538 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
539 cmd.action = cpu_to_le16(CMD_ACT_GET);
541 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
542 if (ret == 0) {
543 *curlevel = le16_to_cpu(cmd.curlevel);
544 if (minlevel)
545 *minlevel = cmd.minlevel;
546 if (maxlevel)
547 *maxlevel = cmd.maxlevel;
550 lbs_deb_leave(LBS_DEB_CMD);
551 return ret;
555 * @brief Set the TX power
557 * @param priv A pointer to struct lbs_private structure
558 * @param dbm The desired power level in dBm
560 * @return 0 on success, error on failure
562 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
564 struct cmd_ds_802_11_rf_tx_power cmd;
565 int ret;
567 lbs_deb_enter(LBS_DEB_CMD);
569 memset(&cmd, 0, sizeof(cmd));
570 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
571 cmd.action = cpu_to_le16(CMD_ACT_SET);
572 cmd.curlevel = cpu_to_le16(dbm);
574 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
576 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
578 lbs_deb_leave(LBS_DEB_CMD);
579 return ret;
583 * @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
585 * @param priv A pointer to struct lbs_private structure
586 * @param enable 1 to enable monitor mode, 0 to disable
588 * @return 0 on success, error on failure
590 int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
592 struct cmd_ds_802_11_monitor_mode cmd;
593 int ret;
595 memset(&cmd, 0, sizeof(cmd));
596 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
597 cmd.action = cpu_to_le16(CMD_ACT_SET);
598 if (enable)
599 cmd.mode = cpu_to_le16(0x1);
601 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
603 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
604 if (ret == 0) {
605 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
606 ARPHRD_ETHER;
609 lbs_deb_leave(LBS_DEB_CMD);
610 return ret;
614 * @brief Get the radio channel
616 * @param priv A pointer to struct lbs_private structure
618 * @return The channel on success, error on failure
620 static int lbs_get_channel(struct lbs_private *priv)
622 struct cmd_ds_802_11_rf_channel cmd;
623 int ret = 0;
625 lbs_deb_enter(LBS_DEB_CMD);
627 memset(&cmd, 0, sizeof(cmd));
628 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
629 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
631 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
632 if (ret)
633 goto out;
635 ret = le16_to_cpu(cmd.channel);
636 lbs_deb_cmd("current radio channel is %d\n", ret);
638 out:
639 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
640 return ret;
643 int lbs_update_channel(struct lbs_private *priv)
645 int ret;
647 /* the channel in f/w could be out of sync; get the current channel */
648 lbs_deb_enter(LBS_DEB_ASSOC);
650 ret = lbs_get_channel(priv);
651 if (ret > 0) {
652 priv->channel = ret;
653 ret = 0;
655 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
656 return ret;
660 * @brief Set the radio channel
662 * @param priv A pointer to struct lbs_private structure
663 * @param channel The desired channel, or 0 to clear a locked channel
665 * @return 0 on success, error on failure
667 int lbs_set_channel(struct lbs_private *priv, u8 channel)
669 struct cmd_ds_802_11_rf_channel cmd;
670 #ifdef DEBUG
671 u8 old_channel = priv->channel;
672 #endif
673 int ret = 0;
675 lbs_deb_enter(LBS_DEB_CMD);
677 memset(&cmd, 0, sizeof(cmd));
678 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
679 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
680 cmd.channel = cpu_to_le16(channel);
682 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
683 if (ret)
684 goto out;
686 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
687 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
688 priv->channel);
690 out:
691 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
692 return ret;
696 * @brief Get current RSSI and noise floor
698 * @param priv A pointer to struct lbs_private structure
699 * @param rssi On successful return, signal level in mBm
701 * @return The channel on success, error on failure
703 int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
705 struct cmd_ds_802_11_rssi cmd;
706 int ret = 0;
708 lbs_deb_enter(LBS_DEB_CMD);
710 BUG_ON(rssi == NULL);
711 BUG_ON(nf == NULL);
713 memset(&cmd, 0, sizeof(cmd));
714 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
715 /* Average SNR over last 8 beacons */
716 cmd.n_or_snr = cpu_to_le16(8);
718 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
719 if (ret == 0) {
720 *nf = CAL_NF(le16_to_cpu(cmd.nf));
721 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
724 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
725 return ret;
729 * @brief Send regulatory and 802.11d domain information to the firmware
731 * @param priv pointer to struct lbs_private
732 * @param request cfg80211 regulatory request structure
733 * @param bands the device's supported bands and channels
735 * @return 0 on success, error code on failure
737 int lbs_set_11d_domain_info(struct lbs_private *priv,
738 struct regulatory_request *request,
739 struct ieee80211_supported_band **bands)
741 struct cmd_ds_802_11d_domain_info cmd;
742 struct mrvl_ie_domain_param_set *domain = &cmd.domain;
743 struct ieee80211_country_ie_triplet *t;
744 enum ieee80211_band band;
745 struct ieee80211_channel *ch;
746 u8 num_triplet = 0;
747 u8 num_parsed_chan = 0;
748 u8 first_channel = 0, next_chan = 0, max_pwr = 0;
749 u8 i, flag = 0;
750 size_t triplet_size;
751 int ret;
753 lbs_deb_enter(LBS_DEB_11D);
755 memset(&cmd, 0, sizeof(cmd));
756 cmd.action = cpu_to_le16(CMD_ACT_SET);
758 lbs_deb_11d("Setting country code '%c%c'\n",
759 request->alpha2[0], request->alpha2[1]);
761 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
763 /* Set country code */
764 domain->country_code[0] = request->alpha2[0];
765 domain->country_code[1] = request->alpha2[1];
766 domain->country_code[2] = ' ';
768 /* Now set up the channel triplets; firmware is somewhat picky here
769 * and doesn't validate channel numbers and spans; hence it would
770 * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
771 * the last 3 aren't valid channels, the driver is responsible for
772 * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
773 * etc.
775 for (band = 0;
776 (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
777 band++) {
779 if (!bands[band])
780 continue;
782 for (i = 0;
783 (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
784 i++) {
785 ch = &bands[band]->channels[i];
786 if (ch->flags & IEEE80211_CHAN_DISABLED)
787 continue;
789 if (!flag) {
790 flag = 1;
791 next_chan = first_channel = (u32) ch->hw_value;
792 max_pwr = ch->max_power;
793 num_parsed_chan = 1;
794 continue;
797 if ((ch->hw_value == next_chan + 1) &&
798 (ch->max_power == max_pwr)) {
799 /* Consolidate adjacent channels */
800 next_chan++;
801 num_parsed_chan++;
802 } else {
803 /* Add this triplet */
804 lbs_deb_11d("11D triplet (%d, %d, %d)\n",
805 first_channel, num_parsed_chan,
806 max_pwr);
807 t = &domain->triplet[num_triplet];
808 t->chans.first_channel = first_channel;
809 t->chans.num_channels = num_parsed_chan;
810 t->chans.max_power = max_pwr;
811 num_triplet++;
812 flag = 0;
816 if (flag) {
817 /* Add last triplet */
818 lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
819 num_parsed_chan, max_pwr);
820 t = &domain->triplet[num_triplet];
821 t->chans.first_channel = first_channel;
822 t->chans.num_channels = num_parsed_chan;
823 t->chans.max_power = max_pwr;
824 num_triplet++;
828 lbs_deb_11d("# triplets %d\n", num_triplet);
830 /* Set command header sizes */
831 triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
832 domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
833 triplet_size);
835 lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
836 (u8 *) &cmd.domain.country_code,
837 le16_to_cpu(domain->header.len));
839 cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
840 sizeof(cmd.action) +
841 sizeof(cmd.domain.header) +
842 sizeof(cmd.domain.country_code) +
843 triplet_size);
845 ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
847 lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
848 return ret;
851 static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
852 u8 cmd_action, void *pdata_buf)
854 struct lbs_offset_value *offval;
856 lbs_deb_enter(LBS_DEB_CMD);
858 offval = (struct lbs_offset_value *)pdata_buf;
860 switch (le16_to_cpu(cmdptr->command)) {
861 case CMD_MAC_REG_ACCESS:
863 struct cmd_ds_mac_reg_access *macreg;
865 cmdptr->size =
866 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
867 + sizeof(struct cmd_header));
868 macreg =
869 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
870 macreg;
872 macreg->action = cpu_to_le16(cmd_action);
873 macreg->offset = cpu_to_le16((u16) offval->offset);
874 macreg->value = cpu_to_le32(offval->value);
876 break;
879 case CMD_BBP_REG_ACCESS:
881 struct cmd_ds_bbp_reg_access *bbpreg;
883 cmdptr->size =
884 cpu_to_le16(sizeof
885 (struct cmd_ds_bbp_reg_access)
886 + sizeof(struct cmd_header));
887 bbpreg =
888 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
889 bbpreg;
891 bbpreg->action = cpu_to_le16(cmd_action);
892 bbpreg->offset = cpu_to_le16((u16) offval->offset);
893 bbpreg->value = (u8) offval->value;
895 break;
898 case CMD_RF_REG_ACCESS:
900 struct cmd_ds_rf_reg_access *rfreg;
902 cmdptr->size =
903 cpu_to_le16(sizeof
904 (struct cmd_ds_rf_reg_access) +
905 sizeof(struct cmd_header));
906 rfreg =
907 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
908 rfreg;
910 rfreg->action = cpu_to_le16(cmd_action);
911 rfreg->offset = cpu_to_le16((u16) offval->offset);
912 rfreg->value = (u8) offval->value;
914 break;
917 default:
918 break;
921 lbs_deb_leave(LBS_DEB_CMD);
922 return 0;
925 static void lbs_queue_cmd(struct lbs_private *priv,
926 struct cmd_ctrl_node *cmdnode)
928 unsigned long flags;
929 int addtail = 1;
931 lbs_deb_enter(LBS_DEB_HOST);
933 if (!cmdnode) {
934 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
935 goto done;
937 if (!cmdnode->cmdbuf->size) {
938 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
939 goto done;
941 cmdnode->result = 0;
943 /* Exit_PS command needs to be queued in the header always. */
944 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
945 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
947 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
948 if (priv->psstate != PS_STATE_FULL_POWER)
949 addtail = 0;
953 if (le16_to_cpu(cmdnode->cmdbuf->command) ==
954 CMD_802_11_WAKEUP_CONFIRM)
955 addtail = 0;
957 spin_lock_irqsave(&priv->driver_lock, flags);
959 if (addtail)
960 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
961 else
962 list_add(&cmdnode->list, &priv->cmdpendingq);
964 spin_unlock_irqrestore(&priv->driver_lock, flags);
966 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
967 le16_to_cpu(cmdnode->cmdbuf->command));
969 done:
970 lbs_deb_leave(LBS_DEB_HOST);
973 static void lbs_submit_command(struct lbs_private *priv,
974 struct cmd_ctrl_node *cmdnode)
976 unsigned long flags;
977 struct cmd_header *cmd;
978 uint16_t cmdsize;
979 uint16_t command;
980 int timeo = 3 * HZ;
981 int ret;
983 lbs_deb_enter(LBS_DEB_HOST);
985 cmd = cmdnode->cmdbuf;
987 spin_lock_irqsave(&priv->driver_lock, flags);
988 priv->cur_cmd = cmdnode;
989 priv->cur_cmd_retcode = 0;
990 spin_unlock_irqrestore(&priv->driver_lock, flags);
992 cmdsize = le16_to_cpu(cmd->size);
993 command = le16_to_cpu(cmd->command);
995 /* These commands take longer */
996 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
997 timeo = 5 * HZ;
999 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1000 command, le16_to_cpu(cmd->seqnum), cmdsize);
1001 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1003 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1005 if (ret) {
1006 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1007 /* Let the timer kick in and retry, and potentially reset
1008 the whole thing if the condition persists */
1009 timeo = HZ/4;
1012 if (command == CMD_802_11_DEEP_SLEEP) {
1013 if (priv->is_auto_deep_sleep_enabled) {
1014 priv->wakeup_dev_required = 1;
1015 priv->dnld_sent = 0;
1017 priv->is_deep_sleep = 1;
1018 lbs_complete_command(priv, cmdnode, 0);
1019 } else {
1020 /* Setup the timer after transmit command */
1021 mod_timer(&priv->command_timer, jiffies + timeo);
1024 lbs_deb_leave(LBS_DEB_HOST);
1028 * This function inserts command node to cmdfreeq
1029 * after cleans it. Requires priv->driver_lock held.
1031 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1032 struct cmd_ctrl_node *cmdnode)
1034 lbs_deb_enter(LBS_DEB_HOST);
1036 if (!cmdnode)
1037 goto out;
1039 cmdnode->callback = NULL;
1040 cmdnode->callback_arg = 0;
1042 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1044 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1045 out:
1046 lbs_deb_leave(LBS_DEB_HOST);
1049 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1050 struct cmd_ctrl_node *ptempcmd)
1052 unsigned long flags;
1054 spin_lock_irqsave(&priv->driver_lock, flags);
1055 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1056 spin_unlock_irqrestore(&priv->driver_lock, flags);
1059 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1060 int result)
1062 if (cmd == priv->cur_cmd)
1063 priv->cur_cmd_retcode = result;
1065 cmd->result = result;
1066 cmd->cmdwaitqwoken = 1;
1067 wake_up_interruptible(&cmd->cmdwait_q);
1069 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1070 __lbs_cleanup_and_insert_cmd(priv, cmd);
1071 priv->cur_cmd = NULL;
1074 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1076 struct cmd_ds_802_11_radio_control cmd;
1077 int ret = -EINVAL;
1079 lbs_deb_enter(LBS_DEB_CMD);
1081 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1082 cmd.action = cpu_to_le16(CMD_ACT_SET);
1084 /* Only v8 and below support setting the preamble */
1085 if (priv->fwrelease < 0x09000000) {
1086 switch (preamble) {
1087 case RADIO_PREAMBLE_SHORT:
1088 case RADIO_PREAMBLE_AUTO:
1089 case RADIO_PREAMBLE_LONG:
1090 cmd.control = cpu_to_le16(preamble);
1091 break;
1092 default:
1093 goto out;
1097 if (radio_on)
1098 cmd.control |= cpu_to_le16(0x1);
1099 else {
1100 cmd.control &= cpu_to_le16(~0x1);
1101 priv->txpower_cur = 0;
1104 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1105 radio_on ? "ON" : "OFF", preamble);
1107 priv->radio_on = radio_on;
1109 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1111 out:
1112 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1113 return ret;
1116 void lbs_set_mac_control(struct lbs_private *priv)
1118 struct cmd_ds_mac_control cmd;
1120 lbs_deb_enter(LBS_DEB_CMD);
1122 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1123 cmd.action = cpu_to_le16(priv->mac_control);
1124 cmd.reserved = 0;
1126 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1128 lbs_deb_leave(LBS_DEB_CMD);
1132 * @brief This function prepare the command before send to firmware.
1134 * @param priv A pointer to struct lbs_private structure
1135 * @param cmd_no command number
1136 * @param cmd_action command action: GET or SET
1137 * @param wait_option wait option: wait response or not
1138 * @param cmd_oid cmd oid: treated as sub command
1139 * @param pdata_buf A pointer to informaion buffer
1140 * @return 0 or -1
1142 int lbs_prepare_and_send_command(struct lbs_private *priv,
1143 u16 cmd_no,
1144 u16 cmd_action,
1145 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1147 int ret = 0;
1148 struct cmd_ctrl_node *cmdnode;
1149 struct cmd_ds_command *cmdptr;
1150 unsigned long flags;
1152 lbs_deb_enter(LBS_DEB_HOST);
1154 if (!priv) {
1155 lbs_deb_host("PREP_CMD: priv is NULL\n");
1156 ret = -1;
1157 goto done;
1160 if (priv->surpriseremoved) {
1161 lbs_deb_host("PREP_CMD: card removed\n");
1162 ret = -1;
1163 goto done;
1166 if (!lbs_is_cmd_allowed(priv)) {
1167 ret = -EBUSY;
1168 goto done;
1171 cmdnode = lbs_get_cmd_ctrl_node(priv);
1173 if (cmdnode == NULL) {
1174 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1176 /* Wake up main thread to execute next command */
1177 wake_up_interruptible(&priv->waitq);
1178 ret = -1;
1179 goto done;
1182 cmdnode->callback = NULL;
1183 cmdnode->callback_arg = (unsigned long)pdata_buf;
1185 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1187 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1189 /* Set sequence number, command and INT option */
1190 priv->seqnum++;
1191 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1193 cmdptr->command = cpu_to_le16(cmd_no);
1194 cmdptr->result = 0;
1196 switch (cmd_no) {
1197 case CMD_802_11_PS_MODE:
1198 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1199 break;
1201 case CMD_MAC_REG_ACCESS:
1202 case CMD_BBP_REG_ACCESS:
1203 case CMD_RF_REG_ACCESS:
1204 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
1205 break;
1207 case CMD_802_11_SET_AFC:
1208 case CMD_802_11_GET_AFC:
1210 cmdptr->command = cpu_to_le16(cmd_no);
1211 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1212 sizeof(struct cmd_header));
1214 memmove(&cmdptr->params.afc,
1215 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1217 ret = 0;
1218 goto done;
1220 #ifdef CONFIG_LIBERTAS_MESH
1222 case CMD_BT_ACCESS:
1223 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1224 break;
1226 case CMD_FWT_ACCESS:
1227 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1228 break;
1230 #endif
1232 case CMD_802_11_BEACON_CTRL:
1233 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1234 break;
1235 case CMD_802_11_DEEP_SLEEP:
1236 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
1237 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
1238 break;
1239 default:
1240 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1241 ret = -1;
1242 break;
1245 /* return error, since the command preparation failed */
1246 if (ret != 0) {
1247 lbs_deb_host("PREP_CMD: command preparation failed\n");
1248 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1249 ret = -1;
1250 goto done;
1253 cmdnode->cmdwaitqwoken = 0;
1255 lbs_queue_cmd(priv, cmdnode);
1256 wake_up_interruptible(&priv->waitq);
1258 if (wait_option & CMD_OPTION_WAITFORRSP) {
1259 lbs_deb_host("PREP_CMD: wait for response\n");
1260 might_sleep();
1261 wait_event_interruptible(cmdnode->cmdwait_q,
1262 cmdnode->cmdwaitqwoken);
1265 spin_lock_irqsave(&priv->driver_lock, flags);
1266 if (priv->cur_cmd_retcode) {
1267 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1268 priv->cur_cmd_retcode);
1269 priv->cur_cmd_retcode = 0;
1270 ret = -1;
1272 spin_unlock_irqrestore(&priv->driver_lock, flags);
1274 done:
1275 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1276 return ret;
1280 * @brief This function allocates the command buffer and link
1281 * it to command free queue.
1283 * @param priv A pointer to struct lbs_private structure
1284 * @return 0 or -1
1286 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1288 int ret = 0;
1289 u32 bufsize;
1290 u32 i;
1291 struct cmd_ctrl_node *cmdarray;
1293 lbs_deb_enter(LBS_DEB_HOST);
1295 /* Allocate and initialize the command array */
1296 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1297 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1298 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1299 ret = -1;
1300 goto done;
1302 priv->cmd_array = cmdarray;
1304 /* Allocate and initialize each command buffer in the command array */
1305 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1306 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1307 if (!cmdarray[i].cmdbuf) {
1308 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1309 ret = -1;
1310 goto done;
1314 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1315 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1316 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1318 ret = 0;
1320 done:
1321 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1322 return ret;
1326 * @brief This function frees the command buffer.
1328 * @param priv A pointer to struct lbs_private structure
1329 * @return 0 or -1
1331 int lbs_free_cmd_buffer(struct lbs_private *priv)
1333 struct cmd_ctrl_node *cmdarray;
1334 unsigned int i;
1336 lbs_deb_enter(LBS_DEB_HOST);
1338 /* need to check if cmd array is allocated or not */
1339 if (priv->cmd_array == NULL) {
1340 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1341 goto done;
1344 cmdarray = priv->cmd_array;
1346 /* Release shared memory buffers */
1347 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1348 if (cmdarray[i].cmdbuf) {
1349 kfree(cmdarray[i].cmdbuf);
1350 cmdarray[i].cmdbuf = NULL;
1354 /* Release cmd_ctrl_node */
1355 if (priv->cmd_array) {
1356 kfree(priv->cmd_array);
1357 priv->cmd_array = NULL;
1360 done:
1361 lbs_deb_leave(LBS_DEB_HOST);
1362 return 0;
1366 * @brief This function gets a free command node if available in
1367 * command free queue.
1369 * @param priv A pointer to struct lbs_private structure
1370 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1372 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1374 struct cmd_ctrl_node *tempnode;
1375 unsigned long flags;
1377 lbs_deb_enter(LBS_DEB_HOST);
1379 if (!priv)
1380 return NULL;
1382 spin_lock_irqsave(&priv->driver_lock, flags);
1384 if (!list_empty(&priv->cmdfreeq)) {
1385 tempnode = list_first_entry(&priv->cmdfreeq,
1386 struct cmd_ctrl_node, list);
1387 list_del(&tempnode->list);
1388 } else {
1389 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1390 tempnode = NULL;
1393 spin_unlock_irqrestore(&priv->driver_lock, flags);
1395 lbs_deb_leave(LBS_DEB_HOST);
1396 return tempnode;
1400 * @brief This function executes next command in command
1401 * pending queue. It will put firmware back to PS mode
1402 * if applicable.
1404 * @param priv A pointer to struct lbs_private structure
1405 * @return 0 or -1
1407 int lbs_execute_next_command(struct lbs_private *priv)
1409 struct cmd_ctrl_node *cmdnode = NULL;
1410 struct cmd_header *cmd;
1411 unsigned long flags;
1412 int ret = 0;
1414 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1415 * only caller to us is lbs_thread() and we get even when a
1416 * data packet is received */
1417 lbs_deb_enter(LBS_DEB_THREAD);
1419 spin_lock_irqsave(&priv->driver_lock, flags);
1421 if (priv->cur_cmd) {
1422 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1423 spin_unlock_irqrestore(&priv->driver_lock, flags);
1424 ret = -1;
1425 goto done;
1428 if (!list_empty(&priv->cmdpendingq)) {
1429 cmdnode = list_first_entry(&priv->cmdpendingq,
1430 struct cmd_ctrl_node, list);
1433 spin_unlock_irqrestore(&priv->driver_lock, flags);
1435 if (cmdnode) {
1436 cmd = cmdnode->cmdbuf;
1438 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1439 if ((priv->psstate == PS_STATE_SLEEP) ||
1440 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1441 lbs_deb_host(
1442 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1443 le16_to_cpu(cmd->command),
1444 priv->psstate);
1445 ret = -1;
1446 goto done;
1448 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1449 "0x%04x in psstate %d\n",
1450 le16_to_cpu(cmd->command), priv->psstate);
1451 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1453 * 1. Non-PS command:
1454 * Queue it. set needtowakeup to TRUE if current state
1455 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1456 * 2. PS command but not Exit_PS:
1457 * Ignore it.
1458 * 3. PS command Exit_PS:
1459 * Set needtowakeup to TRUE if current state is SLEEP,
1460 * otherwise send this command down to firmware
1461 * immediately.
1463 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1464 /* Prepare to send Exit PS,
1465 * this non PS command will be sent later */
1466 if ((priv->psstate == PS_STATE_SLEEP)
1467 || (priv->psstate == PS_STATE_PRE_SLEEP)
1469 /* w/ new scheme, it will not reach here.
1470 since it is blocked in main_thread. */
1471 priv->needtowakeup = 1;
1472 } else
1473 lbs_ps_wakeup(priv, 0);
1475 ret = 0;
1476 goto done;
1477 } else {
1479 * PS command. Ignore it if it is not Exit_PS.
1480 * otherwise send it down immediately.
1482 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1484 lbs_deb_host(
1485 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1486 psm->action);
1487 if (psm->action !=
1488 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1489 lbs_deb_host(
1490 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1491 list_del(&cmdnode->list);
1492 spin_lock_irqsave(&priv->driver_lock, flags);
1493 lbs_complete_command(priv, cmdnode, 0);
1494 spin_unlock_irqrestore(&priv->driver_lock, flags);
1496 ret = 0;
1497 goto done;
1500 if ((priv->psstate == PS_STATE_SLEEP) ||
1501 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1502 lbs_deb_host(
1503 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1504 list_del(&cmdnode->list);
1505 spin_lock_irqsave(&priv->driver_lock, flags);
1506 lbs_complete_command(priv, cmdnode, 0);
1507 spin_unlock_irqrestore(&priv->driver_lock, flags);
1508 priv->needtowakeup = 1;
1510 ret = 0;
1511 goto done;
1514 lbs_deb_host(
1515 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1518 list_del(&cmdnode->list);
1519 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1520 le16_to_cpu(cmd->command));
1521 lbs_submit_command(priv, cmdnode);
1522 } else {
1524 * check if in power save mode, if yes, put the device back
1525 * to PS mode
1527 #ifdef TODO
1529 * This was the old code for libertas+wext. Someone that
1530 * understands this beast should re-code it in a sane way.
1532 * I actually don't understand why this is related to WPA
1533 * and to connection status, shouldn't powering should be
1534 * independ of such things?
1536 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1537 (priv->psstate == PS_STATE_FULL_POWER) &&
1538 ((priv->connect_status == LBS_CONNECTED) ||
1539 lbs_mesh_connected(priv))) {
1540 if (priv->secinfo.WPAenabled ||
1541 priv->secinfo.WPA2enabled) {
1542 /* check for valid WPA group keys */
1543 if (priv->wpa_mcast_key.len ||
1544 priv->wpa_unicast_key.len) {
1545 lbs_deb_host(
1546 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1547 " go back to PS_SLEEP");
1548 lbs_ps_sleep(priv, 0);
1550 } else {
1551 lbs_deb_host(
1552 "EXEC_NEXT_CMD: cmdpendingq empty, "
1553 "go back to PS_SLEEP");
1554 lbs_ps_sleep(priv, 0);
1557 #endif
1560 ret = 0;
1561 done:
1562 lbs_deb_leave(LBS_DEB_THREAD);
1563 return ret;
1566 static void lbs_send_confirmsleep(struct lbs_private *priv)
1568 unsigned long flags;
1569 int ret;
1571 lbs_deb_enter(LBS_DEB_HOST);
1572 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1573 sizeof(confirm_sleep));
1575 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1576 sizeof(confirm_sleep));
1577 if (ret) {
1578 lbs_pr_alert("confirm_sleep failed\n");
1579 goto out;
1582 spin_lock_irqsave(&priv->driver_lock, flags);
1584 /* We don't get a response on the sleep-confirmation */
1585 priv->dnld_sent = DNLD_RES_RECEIVED;
1587 if (priv->is_host_sleep_configured) {
1588 priv->is_host_sleep_activated = 1;
1589 wake_up_interruptible(&priv->host_sleep_q);
1592 /* If nothing to do, go back to sleep (?) */
1593 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1594 priv->psstate = PS_STATE_SLEEP;
1596 spin_unlock_irqrestore(&priv->driver_lock, flags);
1598 out:
1599 lbs_deb_leave(LBS_DEB_HOST);
1602 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1604 lbs_deb_enter(LBS_DEB_HOST);
1607 * PS is currently supported only in Infrastructure mode
1608 * Remove this check if it is to be supported in IBSS mode also
1611 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1612 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1614 lbs_deb_leave(LBS_DEB_HOST);
1618 * @brief This function sends Exit_PS command to firmware.
1620 * @param priv A pointer to struct lbs_private structure
1621 * @param wait_option wait response or not
1622 * @return n/a
1624 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1626 __le32 Localpsmode;
1628 lbs_deb_enter(LBS_DEB_HOST);
1630 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1632 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1633 CMD_SUBCMD_EXIT_PS,
1634 wait_option, 0, &Localpsmode);
1636 lbs_deb_leave(LBS_DEB_HOST);
1640 * @brief This function checks condition and prepares to
1641 * send sleep confirm command to firmware if ok.
1643 * @param priv A pointer to struct lbs_private structure
1644 * @param psmode Power Saving mode
1645 * @return n/a
1647 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1649 unsigned long flags =0;
1650 int allowed = 1;
1652 lbs_deb_enter(LBS_DEB_HOST);
1654 spin_lock_irqsave(&priv->driver_lock, flags);
1655 if (priv->dnld_sent) {
1656 allowed = 0;
1657 lbs_deb_host("dnld_sent was set\n");
1660 /* In-progress command? */
1661 if (priv->cur_cmd) {
1662 allowed = 0;
1663 lbs_deb_host("cur_cmd was set\n");
1666 /* Pending events or command responses? */
1667 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1668 allowed = 0;
1669 lbs_deb_host("pending events or command responses\n");
1671 spin_unlock_irqrestore(&priv->driver_lock, flags);
1673 if (allowed) {
1674 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1675 lbs_send_confirmsleep(priv);
1676 } else {
1677 lbs_deb_host("sleep confirm has been delayed\n");
1680 lbs_deb_leave(LBS_DEB_HOST);
1685 * @brief Configures the transmission power control functionality.
1687 * @param priv A pointer to struct lbs_private structure
1688 * @param enable Transmission power control enable
1689 * @param p0 Power level when link quality is good (dBm).
1690 * @param p1 Power level when link quality is fair (dBm).
1691 * @param p2 Power level when link quality is poor (dBm).
1692 * @param usesnr Use Signal to Noise Ratio in TPC
1694 * @return 0 on success
1696 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1697 int8_t p2, int usesnr)
1699 struct cmd_ds_802_11_tpc_cfg cmd;
1700 int ret;
1702 memset(&cmd, 0, sizeof(cmd));
1703 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1704 cmd.action = cpu_to_le16(CMD_ACT_SET);
1705 cmd.enable = !!enable;
1706 cmd.usesnr = !!usesnr;
1707 cmd.P0 = p0;
1708 cmd.P1 = p1;
1709 cmd.P2 = p2;
1711 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1713 return ret;
1717 * @brief Configures the power adaptation settings.
1719 * @param priv A pointer to struct lbs_private structure
1720 * @param enable Power adaptation enable
1721 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1722 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1723 * @param p2 Power level for 48 and 54 Mbps (dBm).
1725 * @return 0 on Success
1728 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1729 int8_t p1, int8_t p2)
1731 struct cmd_ds_802_11_pa_cfg cmd;
1732 int ret;
1734 memset(&cmd, 0, sizeof(cmd));
1735 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1736 cmd.action = cpu_to_le16(CMD_ACT_SET);
1737 cmd.enable = !!enable;
1738 cmd.P0 = p0;
1739 cmd.P1 = p1;
1740 cmd.P2 = p2;
1742 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1744 return ret;
1748 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1749 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1750 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1751 unsigned long callback_arg)
1753 struct cmd_ctrl_node *cmdnode;
1755 lbs_deb_enter(LBS_DEB_HOST);
1757 if (priv->surpriseremoved) {
1758 lbs_deb_host("PREP_CMD: card removed\n");
1759 cmdnode = ERR_PTR(-ENOENT);
1760 goto done;
1763 if (!lbs_is_cmd_allowed(priv)) {
1764 cmdnode = ERR_PTR(-EBUSY);
1765 goto done;
1768 cmdnode = lbs_get_cmd_ctrl_node(priv);
1769 if (cmdnode == NULL) {
1770 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1772 /* Wake up main thread to execute next command */
1773 wake_up_interruptible(&priv->waitq);
1774 cmdnode = ERR_PTR(-ENOBUFS);
1775 goto done;
1778 cmdnode->callback = callback;
1779 cmdnode->callback_arg = callback_arg;
1781 /* Copy the incoming command to the buffer */
1782 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1784 /* Set sequence number, clean result, move to buffer */
1785 priv->seqnum++;
1786 cmdnode->cmdbuf->command = cpu_to_le16(command);
1787 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1788 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1789 cmdnode->cmdbuf->result = 0;
1791 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1793 cmdnode->cmdwaitqwoken = 0;
1794 lbs_queue_cmd(priv, cmdnode);
1795 wake_up_interruptible(&priv->waitq);
1797 done:
1798 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1799 return cmdnode;
1802 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1803 struct cmd_header *in_cmd, int in_cmd_size)
1805 lbs_deb_enter(LBS_DEB_CMD);
1806 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1807 lbs_cmd_async_callback, 0);
1808 lbs_deb_leave(LBS_DEB_CMD);
1811 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1812 struct cmd_header *in_cmd, int in_cmd_size,
1813 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1814 unsigned long callback_arg)
1816 struct cmd_ctrl_node *cmdnode;
1817 unsigned long flags;
1818 int ret = 0;
1820 lbs_deb_enter(LBS_DEB_HOST);
1822 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1823 callback, callback_arg);
1824 if (IS_ERR(cmdnode)) {
1825 ret = PTR_ERR(cmdnode);
1826 goto done;
1829 might_sleep();
1830 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1832 spin_lock_irqsave(&priv->driver_lock, flags);
1833 ret = cmdnode->result;
1834 if (ret)
1835 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1836 command, ret);
1838 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
1839 spin_unlock_irqrestore(&priv->driver_lock, flags);
1841 done:
1842 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1843 return ret;
1845 EXPORT_SYMBOL_GPL(__lbs_cmd);