2 * This file contains the handling of command.
3 * It prepares command and sends it to firmware when it is ready.
6 #include <linux/kfifo.h>
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/if_arp.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);
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;
36 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
37 memcpy(buf, resp, copy_len);
40 EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
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.
48 * @param extra 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)
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)
71 case CMD_802_11_HOST_SLEEP_CFG:
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)
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");
99 lbs_deb_leave(LBS_DEB_CMD);
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;
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);
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",
139 priv->fwrelease >> 24 & 0xff,
140 priv->fwrelease >> 16 & 0xff,
141 priv->fwrelease >> 8 & 0xff,
142 priv->fwrelease & 0xff,
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;
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])
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);
176 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
179 lbs_deb_leave(LBS_DEB_CMD);
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);
194 priv->is_host_sleep_configured = 1;
196 lbs_deb_leave(LBS_DEB_CMD);
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;
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));
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);
222 memcpy((uint8_t *) p_wol_config,
223 (uint8_t *)&cmd_config.wol_conf,
224 sizeof(struct wol_config));
226 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
231 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
234 * @brief Sets the Power Save mode
236 * @param priv A pointer to struct lbs_private structure
237 * @param cmd_action The Power Save operation (PS_MODE_ACTION_ENTER_PS or
238 * PS_MODE_ACTION_EXIT_PS)
239 * @param block Whether to block on a response or not
241 * @return 0 on success, error on failure
243 int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
245 struct cmd_ds_802_11_ps_mode cmd;
248 lbs_deb_enter(LBS_DEB_CMD);
250 memset(&cmd, 0, sizeof(cmd));
251 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
252 cmd.action = cpu_to_le16(cmd_action);
254 if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
255 lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
256 cmd.multipledtim = cpu_to_le16(1); /* Default DTIM multiple */
257 } else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
258 lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
260 /* We don't handle CONFIRM_SLEEP here because it needs to
261 * be fastpathed to the firmware.
263 lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
269 ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
271 lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
274 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
278 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
279 struct sleep_params *sp)
281 struct cmd_ds_802_11_sleep_params cmd;
284 lbs_deb_enter(LBS_DEB_CMD);
286 if (cmd_action == CMD_ACT_GET) {
287 memset(&cmd, 0, sizeof(cmd));
289 cmd.error = cpu_to_le16(sp->sp_error);
290 cmd.offset = cpu_to_le16(sp->sp_offset);
291 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
292 cmd.calcontrol = sp->sp_calcontrol;
293 cmd.externalsleepclk = sp->sp_extsleepclk;
294 cmd.reserved = cpu_to_le16(sp->sp_reserved);
296 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
297 cmd.action = cpu_to_le16(cmd_action);
299 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
302 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
303 "calcontrol 0x%x extsleepclk 0x%x\n",
304 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
305 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
306 cmd.externalsleepclk);
308 sp->sp_error = le16_to_cpu(cmd.error);
309 sp->sp_offset = le16_to_cpu(cmd.offset);
310 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
311 sp->sp_calcontrol = cmd.calcontrol;
312 sp->sp_extsleepclk = cmd.externalsleepclk;
313 sp->sp_reserved = le16_to_cpu(cmd.reserved);
316 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
320 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
324 lbs_deb_enter(LBS_DEB_CMD);
326 if (priv->is_deep_sleep) {
327 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
328 !priv->is_deep_sleep, (10 * HZ))) {
329 lbs_pr_err("ds_awake_q: timer expired\n");
334 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
338 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
342 lbs_deb_enter(LBS_DEB_CMD);
345 if (priv->is_deep_sleep != 1) {
346 lbs_deb_cmd("deep sleep: sleep\n");
347 BUG_ON(!priv->enter_deep_sleep);
348 ret = priv->enter_deep_sleep(priv);
350 netif_stop_queue(priv->dev);
351 netif_carrier_off(priv->dev);
354 lbs_pr_err("deep sleep: already enabled\n");
357 if (priv->is_deep_sleep) {
358 lbs_deb_cmd("deep sleep: wakeup\n");
359 BUG_ON(!priv->exit_deep_sleep);
360 ret = priv->exit_deep_sleep(priv);
362 ret = lbs_wait_for_ds_awake(priv);
364 lbs_pr_err("deep sleep: wakeup"
370 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
374 static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
376 struct cmd_header *cmd)
378 lbs_deb_enter(LBS_DEB_FW);
379 priv->is_host_sleep_activated = 1;
380 wake_up_interruptible(&priv->host_sleep_q);
381 lbs_deb_leave(LBS_DEB_FW);
385 int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
387 struct cmd_header cmd;
389 uint32_t criteria = EHS_REMOVE_WAKEUP;
391 lbs_deb_enter(LBS_DEB_CMD);
394 if (priv->is_host_sleep_activated != 1) {
395 memset(&cmd, 0, sizeof(cmd));
396 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
397 (struct wol_config *)NULL);
399 lbs_pr_info("Host sleep configuration failed: "
403 if (priv->psstate == PS_STATE_FULL_POWER) {
404 ret = __lbs_cmd(priv,
405 CMD_802_11_HOST_SLEEP_ACTIVATE,
408 lbs_ret_host_sleep_activate, 0);
410 lbs_pr_info("HOST_SLEEP_ACTIVATE "
411 "failed: %d\n", ret);
414 if (!wait_event_interruptible_timeout(
416 priv->is_host_sleep_activated,
418 lbs_pr_err("host_sleep_q: timer expired\n");
422 lbs_pr_err("host sleep: already enabled\n");
425 if (priv->is_host_sleep_activated)
426 ret = lbs_host_sleep_cfg(priv, criteria,
427 (struct wol_config *)NULL);
434 * @brief Set an SNMP MIB value
436 * @param priv A pointer to struct lbs_private structure
437 * @param oid The OID to set in the firmware
438 * @param val Value to set the OID to
440 * @return 0 on success, error on failure
442 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
444 struct cmd_ds_802_11_snmp_mib cmd;
447 lbs_deb_enter(LBS_DEB_CMD);
449 memset(&cmd, 0, sizeof (cmd));
450 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
451 cmd.action = cpu_to_le16(CMD_ACT_SET);
452 cmd.oid = cpu_to_le16((u16) oid);
455 case SNMP_MIB_OID_BSS_TYPE:
456 cmd.bufsize = cpu_to_le16(sizeof(u8));
459 case SNMP_MIB_OID_11D_ENABLE:
460 case SNMP_MIB_OID_FRAG_THRESHOLD:
461 case SNMP_MIB_OID_RTS_THRESHOLD:
462 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
463 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
464 cmd.bufsize = cpu_to_le16(sizeof(u16));
465 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
468 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
473 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
474 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
476 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
479 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
484 * @brief Get an SNMP MIB value
486 * @param priv A pointer to struct lbs_private structure
487 * @param oid The OID to retrieve from the firmware
488 * @param out_val Location for the returned value
490 * @return 0 on success, error on failure
492 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
494 struct cmd_ds_802_11_snmp_mib cmd;
497 lbs_deb_enter(LBS_DEB_CMD);
499 memset(&cmd, 0, sizeof (cmd));
500 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
501 cmd.action = cpu_to_le16(CMD_ACT_GET);
502 cmd.oid = cpu_to_le16(oid);
504 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
508 switch (le16_to_cpu(cmd.bufsize)) {
510 *out_val = cmd.value[0];
513 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
516 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
517 oid, le16_to_cpu(cmd.bufsize));
522 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
527 * @brief Get the min, max, and current TX power
529 * @param priv A pointer to struct lbs_private structure
530 * @param curlevel Current power level in dBm
531 * @param minlevel Minimum supported power level in dBm (optional)
532 * @param maxlevel Maximum supported power level in dBm (optional)
534 * @return 0 on success, error on failure
536 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
539 struct cmd_ds_802_11_rf_tx_power cmd;
542 lbs_deb_enter(LBS_DEB_CMD);
544 memset(&cmd, 0, sizeof(cmd));
545 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
546 cmd.action = cpu_to_le16(CMD_ACT_GET);
548 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
550 *curlevel = le16_to_cpu(cmd.curlevel);
552 *minlevel = cmd.minlevel;
554 *maxlevel = cmd.maxlevel;
557 lbs_deb_leave(LBS_DEB_CMD);
562 * @brief Set the TX power
564 * @param priv A pointer to struct lbs_private structure
565 * @param dbm The desired power level in dBm
567 * @return 0 on success, error on failure
569 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
571 struct cmd_ds_802_11_rf_tx_power cmd;
574 lbs_deb_enter(LBS_DEB_CMD);
576 memset(&cmd, 0, sizeof(cmd));
577 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
578 cmd.action = cpu_to_le16(CMD_ACT_SET);
579 cmd.curlevel = cpu_to_le16(dbm);
581 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
583 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
585 lbs_deb_leave(LBS_DEB_CMD);
590 * @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
592 * @param priv A pointer to struct lbs_private structure
593 * @param enable 1 to enable monitor mode, 0 to disable
595 * @return 0 on success, error on failure
597 int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
599 struct cmd_ds_802_11_monitor_mode cmd;
602 memset(&cmd, 0, sizeof(cmd));
603 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
604 cmd.action = cpu_to_le16(CMD_ACT_SET);
606 cmd.mode = cpu_to_le16(0x1);
608 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
610 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
612 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
616 lbs_deb_leave(LBS_DEB_CMD);
621 * @brief Get the radio channel
623 * @param priv A pointer to struct lbs_private structure
625 * @return The channel on success, error on failure
627 static int lbs_get_channel(struct lbs_private *priv)
629 struct cmd_ds_802_11_rf_channel cmd;
632 lbs_deb_enter(LBS_DEB_CMD);
634 memset(&cmd, 0, sizeof(cmd));
635 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
636 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
638 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
642 ret = le16_to_cpu(cmd.channel);
643 lbs_deb_cmd("current radio channel is %d\n", ret);
646 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
650 int lbs_update_channel(struct lbs_private *priv)
654 /* the channel in f/w could be out of sync; get the current channel */
655 lbs_deb_enter(LBS_DEB_ASSOC);
657 ret = lbs_get_channel(priv);
662 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
667 * @brief Set the radio channel
669 * @param priv A pointer to struct lbs_private structure
670 * @param channel The desired channel, or 0 to clear a locked channel
672 * @return 0 on success, error on failure
674 int lbs_set_channel(struct lbs_private *priv, u8 channel)
676 struct cmd_ds_802_11_rf_channel cmd;
678 u8 old_channel = priv->channel;
682 lbs_deb_enter(LBS_DEB_CMD);
684 memset(&cmd, 0, sizeof(cmd));
685 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
686 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
687 cmd.channel = cpu_to_le16(channel);
689 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
693 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
694 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
698 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
703 * @brief Get current RSSI and noise floor
705 * @param priv A pointer to struct lbs_private structure
706 * @param rssi On successful return, signal level in mBm
708 * @return The channel on success, error on failure
710 int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
712 struct cmd_ds_802_11_rssi cmd;
715 lbs_deb_enter(LBS_DEB_CMD);
717 BUG_ON(rssi == NULL);
720 memset(&cmd, 0, sizeof(cmd));
721 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
722 /* Average SNR over last 8 beacons */
723 cmd.n_or_snr = cpu_to_le16(8);
725 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
727 *nf = CAL_NF(le16_to_cpu(cmd.nf));
728 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
731 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
736 * @brief Send regulatory and 802.11d domain information to the firmware
738 * @param priv pointer to struct lbs_private
739 * @param request cfg80211 regulatory request structure
740 * @param bands the device's supported bands and channels
742 * @return 0 on success, error code on failure
744 int lbs_set_11d_domain_info(struct lbs_private *priv,
745 struct regulatory_request *request,
746 struct ieee80211_supported_band **bands)
748 struct cmd_ds_802_11d_domain_info cmd;
749 struct mrvl_ie_domain_param_set *domain = &cmd.domain;
750 struct ieee80211_country_ie_triplet *t;
751 enum ieee80211_band band;
752 struct ieee80211_channel *ch;
754 u8 num_parsed_chan = 0;
755 u8 first_channel = 0, next_chan = 0, max_pwr = 0;
760 lbs_deb_enter(LBS_DEB_11D);
762 memset(&cmd, 0, sizeof(cmd));
763 cmd.action = cpu_to_le16(CMD_ACT_SET);
765 lbs_deb_11d("Setting country code '%c%c'\n",
766 request->alpha2[0], request->alpha2[1]);
768 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
770 /* Set country code */
771 domain->country_code[0] = request->alpha2[0];
772 domain->country_code[1] = request->alpha2[1];
773 domain->country_code[2] = ' ';
775 /* Now set up the channel triplets; firmware is somewhat picky here
776 * and doesn't validate channel numbers and spans; hence it would
777 * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
778 * the last 3 aren't valid channels, the driver is responsible for
779 * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
783 (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
790 (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
792 ch = &bands[band]->channels[i];
793 if (ch->flags & IEEE80211_CHAN_DISABLED)
798 next_chan = first_channel = (u32) ch->hw_value;
799 max_pwr = ch->max_power;
804 if ((ch->hw_value == next_chan + 1) &&
805 (ch->max_power == max_pwr)) {
806 /* Consolidate adjacent channels */
810 /* Add this triplet */
811 lbs_deb_11d("11D triplet (%d, %d, %d)\n",
812 first_channel, num_parsed_chan,
814 t = &domain->triplet[num_triplet];
815 t->chans.first_channel = first_channel;
816 t->chans.num_channels = num_parsed_chan;
817 t->chans.max_power = max_pwr;
824 /* Add last triplet */
825 lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
826 num_parsed_chan, max_pwr);
827 t = &domain->triplet[num_triplet];
828 t->chans.first_channel = first_channel;
829 t->chans.num_channels = num_parsed_chan;
830 t->chans.max_power = max_pwr;
835 lbs_deb_11d("# triplets %d\n", num_triplet);
837 /* Set command header sizes */
838 triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
839 domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
842 lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
843 (u8 *) &cmd.domain.country_code,
844 le16_to_cpu(domain->header.len));
846 cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
848 sizeof(cmd.domain.header) +
849 sizeof(cmd.domain.country_code) +
852 ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
854 lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
859 * @brief Read a MAC, Baseband, or RF register
861 * @param priv pointer to struct lbs_private
862 * @param cmd register command, one of CMD_MAC_REG_ACCESS,
863 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
864 * @param offset byte offset of the register to get
865 * @param value on success, the value of the register at 'offset'
867 * @return 0 on success, error code on failure
869 int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
871 struct cmd_ds_reg_access cmd;
874 lbs_deb_enter(LBS_DEB_CMD);
876 BUG_ON(value == NULL);
878 memset(&cmd, 0, sizeof(cmd));
879 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
880 cmd.action = cpu_to_le16(CMD_ACT_GET);
882 if (reg != CMD_MAC_REG_ACCESS &&
883 reg != CMD_BBP_REG_ACCESS &&
884 reg != CMD_RF_REG_ACCESS) {
889 ret = lbs_cmd_with_response(priv, reg, &cmd);
891 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
892 *value = cmd.value.bbp_rf;
893 else if (reg == CMD_MAC_REG_ACCESS)
894 *value = le32_to_cpu(cmd.value.mac);
898 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
903 * @brief Write a MAC, Baseband, or RF register
905 * @param priv pointer to struct lbs_private
906 * @param cmd register command, one of CMD_MAC_REG_ACCESS,
907 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
908 * @param offset byte offset of the register to set
909 * @param value the value to write to the register at 'offset'
911 * @return 0 on success, error code on failure
913 int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
915 struct cmd_ds_reg_access cmd;
918 lbs_deb_enter(LBS_DEB_CMD);
920 memset(&cmd, 0, sizeof(cmd));
921 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
922 cmd.action = cpu_to_le16(CMD_ACT_SET);
924 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
925 cmd.value.bbp_rf = (u8) (value & 0xFF);
926 else if (reg == CMD_MAC_REG_ACCESS)
927 cmd.value.mac = cpu_to_le32(value);
933 ret = lbs_cmd_with_response(priv, reg, &cmd);
936 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
940 static void lbs_queue_cmd(struct lbs_private *priv,
941 struct cmd_ctrl_node *cmdnode)
946 lbs_deb_enter(LBS_DEB_HOST);
949 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
952 if (!cmdnode->cmdbuf->size) {
953 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
958 /* Exit_PS command needs to be queued in the header always. */
959 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
960 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf;
962 if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
963 if (priv->psstate != PS_STATE_FULL_POWER)
968 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
971 spin_lock_irqsave(&priv->driver_lock, flags);
974 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
976 list_add(&cmdnode->list, &priv->cmdpendingq);
978 spin_unlock_irqrestore(&priv->driver_lock, flags);
980 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
981 le16_to_cpu(cmdnode->cmdbuf->command));
984 lbs_deb_leave(LBS_DEB_HOST);
987 static void lbs_submit_command(struct lbs_private *priv,
988 struct cmd_ctrl_node *cmdnode)
991 struct cmd_header *cmd;
997 lbs_deb_enter(LBS_DEB_HOST);
999 cmd = cmdnode->cmdbuf;
1001 spin_lock_irqsave(&priv->driver_lock, flags);
1002 priv->cur_cmd = cmdnode;
1003 priv->cur_cmd_retcode = 0;
1004 spin_unlock_irqrestore(&priv->driver_lock, flags);
1006 cmdsize = le16_to_cpu(cmd->size);
1007 command = le16_to_cpu(cmd->command);
1009 /* These commands take longer */
1010 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1013 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1014 command, le16_to_cpu(cmd->seqnum), cmdsize);
1015 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1017 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1020 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1021 /* Let the timer kick in and retry, and potentially reset
1022 the whole thing if the condition persists */
1026 if (command == CMD_802_11_DEEP_SLEEP) {
1027 if (priv->is_auto_deep_sleep_enabled) {
1028 priv->wakeup_dev_required = 1;
1029 priv->dnld_sent = 0;
1031 priv->is_deep_sleep = 1;
1032 lbs_complete_command(priv, cmdnode, 0);
1034 /* Setup the timer after transmit command */
1035 mod_timer(&priv->command_timer, jiffies + timeo);
1038 lbs_deb_leave(LBS_DEB_HOST);
1042 * This function inserts command node to cmdfreeq
1043 * after cleans it. Requires priv->driver_lock held.
1045 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1046 struct cmd_ctrl_node *cmdnode)
1048 lbs_deb_enter(LBS_DEB_HOST);
1053 cmdnode->callback = NULL;
1054 cmdnode->callback_arg = 0;
1056 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1058 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1060 lbs_deb_leave(LBS_DEB_HOST);
1063 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1064 struct cmd_ctrl_node *ptempcmd)
1066 unsigned long flags;
1068 spin_lock_irqsave(&priv->driver_lock, flags);
1069 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1070 spin_unlock_irqrestore(&priv->driver_lock, flags);
1073 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1076 if (cmd == priv->cur_cmd)
1077 priv->cur_cmd_retcode = result;
1079 cmd->result = result;
1080 cmd->cmdwaitqwoken = 1;
1081 wake_up_interruptible(&cmd->cmdwait_q);
1083 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1084 __lbs_cleanup_and_insert_cmd(priv, cmd);
1085 priv->cur_cmd = NULL;
1088 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1090 struct cmd_ds_802_11_radio_control cmd;
1093 lbs_deb_enter(LBS_DEB_CMD);
1095 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1096 cmd.action = cpu_to_le16(CMD_ACT_SET);
1098 /* Only v8 and below support setting the preamble */
1099 if (priv->fwrelease < 0x09000000) {
1101 case RADIO_PREAMBLE_SHORT:
1102 case RADIO_PREAMBLE_AUTO:
1103 case RADIO_PREAMBLE_LONG:
1104 cmd.control = cpu_to_le16(preamble);
1112 cmd.control |= cpu_to_le16(0x1);
1114 cmd.control &= cpu_to_le16(~0x1);
1115 priv->txpower_cur = 0;
1118 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1119 radio_on ? "ON" : "OFF", preamble);
1121 priv->radio_on = radio_on;
1123 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1126 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1130 void lbs_set_mac_control(struct lbs_private *priv)
1132 struct cmd_ds_mac_control cmd;
1134 lbs_deb_enter(LBS_DEB_CMD);
1136 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1137 cmd.action = cpu_to_le16(priv->mac_control);
1140 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1142 lbs_deb_leave(LBS_DEB_CMD);
1146 * @brief This function prepare the command before send to firmware.
1148 * @param priv A pointer to struct lbs_private structure
1149 * @param cmd_no command number
1150 * @param cmd_action command action: GET or SET
1151 * @param wait_option wait option: wait response or not
1152 * @param cmd_oid cmd oid: treated as sub command
1153 * @param pdata_buf A pointer to informaion buffer
1156 int lbs_prepare_and_send_command(struct lbs_private *priv,
1159 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1162 struct cmd_ctrl_node *cmdnode;
1163 struct cmd_header *cmdptr;
1164 unsigned long flags;
1166 lbs_deb_enter(LBS_DEB_HOST);
1169 lbs_deb_host("PREP_CMD: priv is NULL\n");
1174 if (priv->surpriseremoved) {
1175 lbs_deb_host("PREP_CMD: card removed\n");
1180 if (!lbs_is_cmd_allowed(priv)) {
1185 cmdnode = lbs_get_cmd_ctrl_node(priv);
1187 if (cmdnode == NULL) {
1188 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1190 /* Wake up main thread to execute next command */
1191 wake_up_interruptible(&priv->waitq);
1196 cmdnode->callback = NULL;
1197 cmdnode->callback_arg = (unsigned long)pdata_buf;
1199 cmdptr = (struct cmd_header *)cmdnode->cmdbuf;
1201 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1203 /* Set sequence number, command and INT option */
1205 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1207 cmdptr->command = cpu_to_le16(cmd_no);
1212 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1217 /* return error, since the command preparation failed */
1219 lbs_deb_host("PREP_CMD: command preparation failed\n");
1220 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1225 cmdnode->cmdwaitqwoken = 0;
1227 lbs_queue_cmd(priv, cmdnode);
1228 wake_up_interruptible(&priv->waitq);
1230 if (wait_option & CMD_OPTION_WAITFORRSP) {
1231 lbs_deb_host("PREP_CMD: wait for response\n");
1233 wait_event_interruptible(cmdnode->cmdwait_q,
1234 cmdnode->cmdwaitqwoken);
1237 spin_lock_irqsave(&priv->driver_lock, flags);
1238 if (priv->cur_cmd_retcode) {
1239 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1240 priv->cur_cmd_retcode);
1241 priv->cur_cmd_retcode = 0;
1244 spin_unlock_irqrestore(&priv->driver_lock, flags);
1247 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1252 * @brief This function allocates the command buffer and link
1253 * it to command free queue.
1255 * @param priv A pointer to struct lbs_private structure
1258 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1263 struct cmd_ctrl_node *cmdarray;
1265 lbs_deb_enter(LBS_DEB_HOST);
1267 /* Allocate and initialize the command array */
1268 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1269 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1270 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1274 priv->cmd_array = cmdarray;
1276 /* Allocate and initialize each command buffer in the command array */
1277 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1278 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1279 if (!cmdarray[i].cmdbuf) {
1280 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1286 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1287 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1288 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1293 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1298 * @brief This function frees the command buffer.
1300 * @param priv A pointer to struct lbs_private structure
1303 int lbs_free_cmd_buffer(struct lbs_private *priv)
1305 struct cmd_ctrl_node *cmdarray;
1308 lbs_deb_enter(LBS_DEB_HOST);
1310 /* need to check if cmd array is allocated or not */
1311 if (priv->cmd_array == NULL) {
1312 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1316 cmdarray = priv->cmd_array;
1318 /* Release shared memory buffers */
1319 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1320 if (cmdarray[i].cmdbuf) {
1321 kfree(cmdarray[i].cmdbuf);
1322 cmdarray[i].cmdbuf = NULL;
1326 /* Release cmd_ctrl_node */
1327 if (priv->cmd_array) {
1328 kfree(priv->cmd_array);
1329 priv->cmd_array = NULL;
1333 lbs_deb_leave(LBS_DEB_HOST);
1338 * @brief This function gets a free command node if available in
1339 * command free queue.
1341 * @param priv A pointer to struct lbs_private structure
1342 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1344 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1346 struct cmd_ctrl_node *tempnode;
1347 unsigned long flags;
1349 lbs_deb_enter(LBS_DEB_HOST);
1354 spin_lock_irqsave(&priv->driver_lock, flags);
1356 if (!list_empty(&priv->cmdfreeq)) {
1357 tempnode = list_first_entry(&priv->cmdfreeq,
1358 struct cmd_ctrl_node, list);
1359 list_del(&tempnode->list);
1361 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1365 spin_unlock_irqrestore(&priv->driver_lock, flags);
1367 lbs_deb_leave(LBS_DEB_HOST);
1372 * @brief This function executes next command in command
1373 * pending queue. It will put firmware back to PS mode
1376 * @param priv A pointer to struct lbs_private structure
1379 int lbs_execute_next_command(struct lbs_private *priv)
1381 struct cmd_ctrl_node *cmdnode = NULL;
1382 struct cmd_header *cmd;
1383 unsigned long flags;
1386 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1387 * only caller to us is lbs_thread() and we get even when a
1388 * data packet is received */
1389 lbs_deb_enter(LBS_DEB_THREAD);
1391 spin_lock_irqsave(&priv->driver_lock, flags);
1393 if (priv->cur_cmd) {
1394 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1395 spin_unlock_irqrestore(&priv->driver_lock, flags);
1400 if (!list_empty(&priv->cmdpendingq)) {
1401 cmdnode = list_first_entry(&priv->cmdpendingq,
1402 struct cmd_ctrl_node, list);
1405 spin_unlock_irqrestore(&priv->driver_lock, flags);
1408 cmd = cmdnode->cmdbuf;
1410 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1411 if ((priv->psstate == PS_STATE_SLEEP) ||
1412 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1414 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1415 le16_to_cpu(cmd->command),
1420 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1421 "0x%04x in psstate %d\n",
1422 le16_to_cpu(cmd->command), priv->psstate);
1423 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1425 * 1. Non-PS command:
1426 * Queue it. set needtowakeup to TRUE if current state
1427 * is SLEEP, otherwise call send EXIT_PS.
1428 * 2. PS command but not EXIT_PS:
1430 * 3. PS command EXIT_PS:
1431 * Set needtowakeup to TRUE if current state is SLEEP,
1432 * otherwise send this command down to firmware
1435 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1436 /* Prepare to send Exit PS,
1437 * this non PS command will be sent later */
1438 if ((priv->psstate == PS_STATE_SLEEP)
1439 || (priv->psstate == PS_STATE_PRE_SLEEP)
1441 /* w/ new scheme, it will not reach here.
1442 since it is blocked in main_thread. */
1443 priv->needtowakeup = 1;
1445 lbs_set_ps_mode(priv,
1446 PS_MODE_ACTION_EXIT_PS,
1454 * PS command. Ignore it if it is not Exit_PS.
1455 * otherwise send it down immediately.
1457 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1460 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1463 cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
1465 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1466 list_del(&cmdnode->list);
1467 spin_lock_irqsave(&priv->driver_lock, flags);
1468 lbs_complete_command(priv, cmdnode, 0);
1469 spin_unlock_irqrestore(&priv->driver_lock, flags);
1475 if ((priv->psstate == PS_STATE_SLEEP) ||
1476 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1478 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1479 list_del(&cmdnode->list);
1480 spin_lock_irqsave(&priv->driver_lock, flags);
1481 lbs_complete_command(priv, cmdnode, 0);
1482 spin_unlock_irqrestore(&priv->driver_lock, flags);
1483 priv->needtowakeup = 1;
1490 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1493 list_del(&cmdnode->list);
1494 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1495 le16_to_cpu(cmd->command));
1496 lbs_submit_command(priv, cmdnode);
1499 * check if in power save mode, if yes, put the device back
1504 * This was the old code for libertas+wext. Someone that
1505 * understands this beast should re-code it in a sane way.
1507 * I actually don't understand why this is related to WPA
1508 * and to connection status, shouldn't powering should be
1509 * independ of such things?
1511 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1512 (priv->psstate == PS_STATE_FULL_POWER) &&
1513 ((priv->connect_status == LBS_CONNECTED) ||
1514 lbs_mesh_connected(priv))) {
1515 if (priv->secinfo.WPAenabled ||
1516 priv->secinfo.WPA2enabled) {
1517 /* check for valid WPA group keys */
1518 if (priv->wpa_mcast_key.len ||
1519 priv->wpa_unicast_key.len) {
1521 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1522 " go back to PS_SLEEP");
1523 lbs_set_ps_mode(priv,
1524 PS_MODE_ACTION_ENTER_PS,
1529 "EXEC_NEXT_CMD: cmdpendingq empty, "
1530 "go back to PS_SLEEP");
1531 lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
1540 lbs_deb_leave(LBS_DEB_THREAD);
1544 static void lbs_send_confirmsleep(struct lbs_private *priv)
1546 unsigned long flags;
1549 lbs_deb_enter(LBS_DEB_HOST);
1550 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1551 sizeof(confirm_sleep));
1553 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1554 sizeof(confirm_sleep));
1556 lbs_pr_alert("confirm_sleep failed\n");
1560 spin_lock_irqsave(&priv->driver_lock, flags);
1562 /* We don't get a response on the sleep-confirmation */
1563 priv->dnld_sent = DNLD_RES_RECEIVED;
1565 if (priv->is_host_sleep_configured) {
1566 priv->is_host_sleep_activated = 1;
1567 wake_up_interruptible(&priv->host_sleep_q);
1570 /* If nothing to do, go back to sleep (?) */
1571 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1572 priv->psstate = PS_STATE_SLEEP;
1574 spin_unlock_irqrestore(&priv->driver_lock, flags);
1577 lbs_deb_leave(LBS_DEB_HOST);
1581 * @brief This function checks condition and prepares to
1582 * send sleep confirm command to firmware if ok.
1584 * @param priv A pointer to struct lbs_private structure
1585 * @param psmode Power Saving mode
1588 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1590 unsigned long flags =0;
1593 lbs_deb_enter(LBS_DEB_HOST);
1595 spin_lock_irqsave(&priv->driver_lock, flags);
1596 if (priv->dnld_sent) {
1598 lbs_deb_host("dnld_sent was set\n");
1601 /* In-progress command? */
1602 if (priv->cur_cmd) {
1604 lbs_deb_host("cur_cmd was set\n");
1607 /* Pending events or command responses? */
1608 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1610 lbs_deb_host("pending events or command responses\n");
1612 spin_unlock_irqrestore(&priv->driver_lock, flags);
1615 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1616 lbs_send_confirmsleep(priv);
1618 lbs_deb_host("sleep confirm has been delayed\n");
1621 lbs_deb_leave(LBS_DEB_HOST);
1626 * @brief Configures the transmission power control functionality.
1628 * @param priv A pointer to struct lbs_private structure
1629 * @param enable Transmission power control enable
1630 * @param p0 Power level when link quality is good (dBm).
1631 * @param p1 Power level when link quality is fair (dBm).
1632 * @param p2 Power level when link quality is poor (dBm).
1633 * @param usesnr Use Signal to Noise Ratio in TPC
1635 * @return 0 on success
1637 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1638 int8_t p2, int usesnr)
1640 struct cmd_ds_802_11_tpc_cfg cmd;
1643 memset(&cmd, 0, sizeof(cmd));
1644 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1645 cmd.action = cpu_to_le16(CMD_ACT_SET);
1646 cmd.enable = !!enable;
1647 cmd.usesnr = !!usesnr;
1652 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1658 * @brief Configures the power adaptation settings.
1660 * @param priv A pointer to struct lbs_private structure
1661 * @param enable Power adaptation enable
1662 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1663 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1664 * @param p2 Power level for 48 and 54 Mbps (dBm).
1666 * @return 0 on Success
1669 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1670 int8_t p1, int8_t p2)
1672 struct cmd_ds_802_11_pa_cfg cmd;
1675 memset(&cmd, 0, sizeof(cmd));
1676 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1677 cmd.action = cpu_to_le16(CMD_ACT_SET);
1678 cmd.enable = !!enable;
1683 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1689 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1690 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1691 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1692 unsigned long callback_arg)
1694 struct cmd_ctrl_node *cmdnode;
1696 lbs_deb_enter(LBS_DEB_HOST);
1698 if (priv->surpriseremoved) {
1699 lbs_deb_host("PREP_CMD: card removed\n");
1700 cmdnode = ERR_PTR(-ENOENT);
1704 if (!lbs_is_cmd_allowed(priv)) {
1705 cmdnode = ERR_PTR(-EBUSY);
1709 cmdnode = lbs_get_cmd_ctrl_node(priv);
1710 if (cmdnode == NULL) {
1711 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1713 /* Wake up main thread to execute next command */
1714 wake_up_interruptible(&priv->waitq);
1715 cmdnode = ERR_PTR(-ENOBUFS);
1719 cmdnode->callback = callback;
1720 cmdnode->callback_arg = callback_arg;
1722 /* Copy the incoming command to the buffer */
1723 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1725 /* Set sequence number, clean result, move to buffer */
1727 cmdnode->cmdbuf->command = cpu_to_le16(command);
1728 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1729 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1730 cmdnode->cmdbuf->result = 0;
1732 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1734 cmdnode->cmdwaitqwoken = 0;
1735 lbs_queue_cmd(priv, cmdnode);
1736 wake_up_interruptible(&priv->waitq);
1739 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1743 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1744 struct cmd_header *in_cmd, int in_cmd_size)
1746 lbs_deb_enter(LBS_DEB_CMD);
1747 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1748 lbs_cmd_async_callback, 0);
1749 lbs_deb_leave(LBS_DEB_CMD);
1752 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1753 struct cmd_header *in_cmd, int in_cmd_size,
1754 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1755 unsigned long callback_arg)
1757 struct cmd_ctrl_node *cmdnode;
1758 unsigned long flags;
1761 lbs_deb_enter(LBS_DEB_HOST);
1763 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1764 callback, callback_arg);
1765 if (IS_ERR(cmdnode)) {
1766 ret = PTR_ERR(cmdnode);
1771 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1773 spin_lock_irqsave(&priv->driver_lock, flags);
1774 ret = cmdnode->result;
1776 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1779 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
1780 spin_unlock_irqrestore(&priv->driver_lock, flags);
1783 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1786 EXPORT_SYMBOL_GPL(__lbs_cmd);