Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / net / wireless / libertas / cmd.c
blob26e68326710b04bce2a9e4e08f42ddf1ba645ce2
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/hardirq.h>
7 #include <linux/kfifo.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/if_arp.h>
11 #include <linux/export.h>
13 #include "decl.h"
14 #include "cfg.h"
15 #include "cmd.h"
17 #define CAL_NF(nf) ((s32)(-(s32)(nf)))
18 #define CAL_RSSI(snr, nf) ((s32)((s32)(snr) + CAL_NF(nf)))
20 /**
21 * lbs_cmd_copyback - Simple callback that copies response back into command
23 * @priv: A pointer to &struct lbs_private structure
24 * @extra: A pointer to the original command structure for which
25 * 'resp' is a response
26 * @resp: A pointer to the command response
28 * returns: 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 * lbs_cmd_async_callback - Simple callback that ignores the result.
44 * Use this if you just want to send a command to the hardware, but don't
45 * care for the result.
47 * @priv: ignored
48 * @extra: ignored
49 * @resp: ignored
51 * returns: 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 * is_command_allowed_in_ps - tests if a command is allowed in Power Save mode
63 * @cmd: the command ID
65 * returns: 1 if allowed, 0 if not allowed
67 static u8 is_command_allowed_in_ps(u16 cmd)
69 switch (cmd) {
70 case CMD_802_11_RSSI:
71 return 1;
72 case CMD_802_11_HOST_SLEEP_CFG:
73 return 1;
74 default:
75 break;
77 return 0;
80 /**
81 * lbs_update_hw_spec - Updates the hardware details like MAC address
82 * and regulatory region
84 * @priv: A pointer to &struct lbs_private structure
86 * returns: 0 on success, error on failure
88 int lbs_update_hw_spec(struct lbs_private *priv)
90 struct cmd_ds_get_hw_spec cmd;
91 int ret = -1;
92 u32 i;
94 lbs_deb_enter(LBS_DEB_CMD);
96 memset(&cmd, 0, sizeof(cmd));
97 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
98 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
99 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
100 if (ret)
101 goto out;
103 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
105 /* The firmware release is in an interesting format: the patch
106 * level is in the most significant nibble ... so fix that: */
107 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
108 priv->fwrelease = (priv->fwrelease << 8) |
109 (priv->fwrelease >> 24 & 0xff);
111 /* Some firmware capabilities:
112 * CF card firmware 5.0.16p0: cap 0x00000303
113 * USB dongle firmware 5.110.17p2: cap 0x00000303
115 netdev_info(priv->dev, "%pM, fw %u.%u.%up%u, cap 0x%08x\n",
116 cmd.permanentaddr,
117 priv->fwrelease >> 24 & 0xff,
118 priv->fwrelease >> 16 & 0xff,
119 priv->fwrelease >> 8 & 0xff,
120 priv->fwrelease & 0xff,
121 priv->fwcapinfo);
122 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
123 cmd.hwifversion, cmd.version);
125 /* Clamp region code to 8-bit since FW spec indicates that it should
126 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
127 * returns non-zero high 8 bits here.
129 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
130 * need to check for this problem and handle it properly.
132 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
133 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
134 else
135 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
137 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
138 /* use the region code to search for the index */
139 if (priv->regioncode == lbs_region_code_to_index[i])
140 break;
143 /* if it's unidentified region code, use the default (USA) */
144 if (i >= MRVDRV_MAX_REGION_CODE) {
145 priv->regioncode = 0x10;
146 netdev_info(priv->dev,
147 "unidentified region code; using the default (USA)\n");
150 if (priv->current_addr[0] == 0xff)
151 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
153 if (!priv->copied_hwaddr) {
154 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
155 if (priv->mesh_dev)
156 memcpy(priv->mesh_dev->dev_addr,
157 priv->current_addr, ETH_ALEN);
158 priv->copied_hwaddr = 1;
161 out:
162 lbs_deb_leave(LBS_DEB_CMD);
163 return ret;
166 static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
167 struct cmd_header *resp)
169 lbs_deb_enter(LBS_DEB_CMD);
170 if (priv->is_host_sleep_activated) {
171 priv->is_host_sleep_configured = 0;
172 if (priv->psstate == PS_STATE_FULL_POWER) {
173 priv->is_host_sleep_activated = 0;
174 wake_up_interruptible(&priv->host_sleep_q);
176 } else {
177 priv->is_host_sleep_configured = 1;
179 lbs_deb_leave(LBS_DEB_CMD);
180 return 0;
183 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
184 struct wol_config *p_wol_config)
186 struct cmd_ds_host_sleep cmd_config;
187 int ret;
190 * Certain firmware versions do not support EHS_REMOVE_WAKEUP command
191 * and the card will return a failure. Since we need to be
192 * able to reset the mask, in those cases we set a 0 mask instead.
194 if (criteria == EHS_REMOVE_WAKEUP && !priv->ehs_remove_supported)
195 criteria = 0;
197 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
198 cmd_config.criteria = cpu_to_le32(criteria);
199 cmd_config.gpio = priv->wol_gpio;
200 cmd_config.gap = priv->wol_gap;
202 if (p_wol_config != NULL)
203 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
204 sizeof(struct wol_config));
205 else
206 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
208 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
209 le16_to_cpu(cmd_config.hdr.size),
210 lbs_ret_host_sleep_cfg, 0);
211 if (!ret) {
212 if (p_wol_config)
213 memcpy((uint8_t *) p_wol_config,
214 (uint8_t *)&cmd_config.wol_conf,
215 sizeof(struct wol_config));
216 } else {
217 netdev_info(priv->dev, "HOST_SLEEP_CFG failed %d\n", ret);
220 return ret;
222 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
225 * lbs_set_ps_mode - Sets the Power Save mode
227 * @priv: A pointer to &struct lbs_private structure
228 * @cmd_action: The Power Save operation (PS_MODE_ACTION_ENTER_PS or
229 * PS_MODE_ACTION_EXIT_PS)
230 * @block: Whether to block on a response or not
232 * returns: 0 on success, error on failure
234 int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
236 struct cmd_ds_802_11_ps_mode cmd;
237 int ret = 0;
239 lbs_deb_enter(LBS_DEB_CMD);
241 memset(&cmd, 0, sizeof(cmd));
242 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
243 cmd.action = cpu_to_le16(cmd_action);
245 if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
246 lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
247 cmd.multipledtim = cpu_to_le16(1); /* Default DTIM multiple */
248 } else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
249 lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
250 } else {
251 /* We don't handle CONFIRM_SLEEP here because it needs to
252 * be fastpathed to the firmware.
254 lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
255 ret = -EOPNOTSUPP;
256 goto out;
259 if (block)
260 ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
261 else
262 lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
264 out:
265 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
266 return ret;
269 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
270 struct sleep_params *sp)
272 struct cmd_ds_802_11_sleep_params cmd;
273 int ret;
275 lbs_deb_enter(LBS_DEB_CMD);
277 if (cmd_action == CMD_ACT_GET) {
278 memset(&cmd, 0, sizeof(cmd));
279 } else {
280 cmd.error = cpu_to_le16(sp->sp_error);
281 cmd.offset = cpu_to_le16(sp->sp_offset);
282 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
283 cmd.calcontrol = sp->sp_calcontrol;
284 cmd.externalsleepclk = sp->sp_extsleepclk;
285 cmd.reserved = cpu_to_le16(sp->sp_reserved);
287 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
288 cmd.action = cpu_to_le16(cmd_action);
290 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
292 if (!ret) {
293 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
294 "calcontrol 0x%x extsleepclk 0x%x\n",
295 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
296 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
297 cmd.externalsleepclk);
299 sp->sp_error = le16_to_cpu(cmd.error);
300 sp->sp_offset = le16_to_cpu(cmd.offset);
301 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
302 sp->sp_calcontrol = cmd.calcontrol;
303 sp->sp_extsleepclk = cmd.externalsleepclk;
304 sp->sp_reserved = le16_to_cpu(cmd.reserved);
307 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
308 return 0;
311 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
313 int ret = 0;
315 lbs_deb_enter(LBS_DEB_CMD);
317 if (priv->is_deep_sleep) {
318 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
319 !priv->is_deep_sleep, (10 * HZ))) {
320 netdev_err(priv->dev, "ds_awake_q: timer expired\n");
321 ret = -1;
325 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
326 return ret;
329 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
331 int ret = 0;
333 lbs_deb_enter(LBS_DEB_CMD);
335 if (deep_sleep) {
336 if (priv->is_deep_sleep != 1) {
337 lbs_deb_cmd("deep sleep: sleep\n");
338 BUG_ON(!priv->enter_deep_sleep);
339 ret = priv->enter_deep_sleep(priv);
340 if (!ret) {
341 netif_stop_queue(priv->dev);
342 netif_carrier_off(priv->dev);
344 } else {
345 netdev_err(priv->dev, "deep sleep: already enabled\n");
347 } else {
348 if (priv->is_deep_sleep) {
349 lbs_deb_cmd("deep sleep: wakeup\n");
350 BUG_ON(!priv->exit_deep_sleep);
351 ret = priv->exit_deep_sleep(priv);
352 if (!ret) {
353 ret = lbs_wait_for_ds_awake(priv);
354 if (ret)
355 netdev_err(priv->dev,
356 "deep sleep: wakeup failed\n");
361 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
362 return ret;
365 static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
366 unsigned long dummy,
367 struct cmd_header *cmd)
369 lbs_deb_enter(LBS_DEB_FW);
370 priv->is_host_sleep_activated = 1;
371 wake_up_interruptible(&priv->host_sleep_q);
372 lbs_deb_leave(LBS_DEB_FW);
373 return 0;
376 int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
378 struct cmd_header cmd;
379 int ret = 0;
380 uint32_t criteria = EHS_REMOVE_WAKEUP;
382 lbs_deb_enter(LBS_DEB_CMD);
384 if (host_sleep) {
385 if (priv->is_host_sleep_activated != 1) {
386 memset(&cmd, 0, sizeof(cmd));
387 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
388 (struct wol_config *)NULL);
389 if (ret) {
390 netdev_info(priv->dev,
391 "Host sleep configuration failed: %d\n",
392 ret);
393 return ret;
395 if (priv->psstate == PS_STATE_FULL_POWER) {
396 ret = __lbs_cmd(priv,
397 CMD_802_11_HOST_SLEEP_ACTIVATE,
398 &cmd,
399 sizeof(cmd),
400 lbs_ret_host_sleep_activate, 0);
401 if (ret)
402 netdev_info(priv->dev,
403 "HOST_SLEEP_ACTIVATE failed: %d\n",
404 ret);
407 if (!wait_event_interruptible_timeout(
408 priv->host_sleep_q,
409 priv->is_host_sleep_activated,
410 (10 * HZ))) {
411 netdev_err(priv->dev,
412 "host_sleep_q: timer expired\n");
413 ret = -1;
415 } else {
416 netdev_err(priv->dev, "host sleep: already enabled\n");
418 } else {
419 if (priv->is_host_sleep_activated)
420 ret = lbs_host_sleep_cfg(priv, criteria,
421 (struct wol_config *)NULL);
424 return ret;
428 * lbs_set_snmp_mib - Set an SNMP MIB value
430 * @priv: A pointer to &struct lbs_private structure
431 * @oid: The OID to set in the firmware
432 * @val: Value to set the OID to
434 * returns: 0 on success, error on failure
436 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
438 struct cmd_ds_802_11_snmp_mib cmd;
439 int ret;
441 lbs_deb_enter(LBS_DEB_CMD);
443 memset(&cmd, 0, sizeof (cmd));
444 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
445 cmd.action = cpu_to_le16(CMD_ACT_SET);
446 cmd.oid = cpu_to_le16((u16) oid);
448 switch (oid) {
449 case SNMP_MIB_OID_BSS_TYPE:
450 cmd.bufsize = cpu_to_le16(sizeof(u8));
451 cmd.value[0] = val;
452 break;
453 case SNMP_MIB_OID_11D_ENABLE:
454 case SNMP_MIB_OID_FRAG_THRESHOLD:
455 case SNMP_MIB_OID_RTS_THRESHOLD:
456 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
457 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
458 cmd.bufsize = cpu_to_le16(sizeof(u16));
459 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
460 break;
461 default:
462 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
463 ret = -EINVAL;
464 goto out;
467 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
468 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
470 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
472 out:
473 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
474 return ret;
478 * lbs_get_snmp_mib - Get an SNMP MIB value
480 * @priv: A pointer to &struct lbs_private structure
481 * @oid: The OID to retrieve from the firmware
482 * @out_val: Location for the returned value
484 * returns: 0 on success, error on failure
486 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
488 struct cmd_ds_802_11_snmp_mib cmd;
489 int ret;
491 lbs_deb_enter(LBS_DEB_CMD);
493 memset(&cmd, 0, sizeof (cmd));
494 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
495 cmd.action = cpu_to_le16(CMD_ACT_GET);
496 cmd.oid = cpu_to_le16(oid);
498 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
499 if (ret)
500 goto out;
502 switch (le16_to_cpu(cmd.bufsize)) {
503 case sizeof(u8):
504 *out_val = cmd.value[0];
505 break;
506 case sizeof(u16):
507 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
508 break;
509 default:
510 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
511 oid, le16_to_cpu(cmd.bufsize));
512 break;
515 out:
516 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
517 return ret;
521 * lbs_get_tx_power - Get the min, max, and current TX power
523 * @priv: A pointer to &struct lbs_private structure
524 * @curlevel: Current power level in dBm
525 * @minlevel: Minimum supported power level in dBm (optional)
526 * @maxlevel: Maximum supported power level in dBm (optional)
528 * returns: 0 on success, error on failure
530 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
531 s16 *maxlevel)
533 struct cmd_ds_802_11_rf_tx_power cmd;
534 int ret;
536 lbs_deb_enter(LBS_DEB_CMD);
538 memset(&cmd, 0, sizeof(cmd));
539 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
540 cmd.action = cpu_to_le16(CMD_ACT_GET);
542 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
543 if (ret == 0) {
544 *curlevel = le16_to_cpu(cmd.curlevel);
545 if (minlevel)
546 *minlevel = cmd.minlevel;
547 if (maxlevel)
548 *maxlevel = cmd.maxlevel;
551 lbs_deb_leave(LBS_DEB_CMD);
552 return ret;
556 * lbs_set_tx_power - Set the TX power
558 * @priv: A pointer to &struct lbs_private structure
559 * @dbm: The desired power level in dBm
561 * returns: 0 on success, error on failure
563 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
565 struct cmd_ds_802_11_rf_tx_power cmd;
566 int ret;
568 lbs_deb_enter(LBS_DEB_CMD);
570 memset(&cmd, 0, sizeof(cmd));
571 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
572 cmd.action = cpu_to_le16(CMD_ACT_SET);
573 cmd.curlevel = cpu_to_le16(dbm);
575 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
577 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
579 lbs_deb_leave(LBS_DEB_CMD);
580 return ret;
584 * lbs_set_monitor_mode - Enable or disable monitor mode
585 * (only implemented on OLPC usb8388 FW)
587 * @priv: A pointer to &struct lbs_private structure
588 * @enable: 1 to enable monitor mode, 0 to disable
590 * returns: 0 on success, error on failure
592 int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
594 struct cmd_ds_802_11_monitor_mode cmd;
595 int ret;
597 memset(&cmd, 0, sizeof(cmd));
598 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
599 cmd.action = cpu_to_le16(CMD_ACT_SET);
600 if (enable)
601 cmd.mode = cpu_to_le16(0x1);
603 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
605 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
606 if (ret == 0) {
607 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
608 ARPHRD_ETHER;
611 lbs_deb_leave(LBS_DEB_CMD);
612 return ret;
616 * lbs_get_channel - Get the radio channel
618 * @priv: A pointer to &struct lbs_private structure
620 * returns: The channel on success, error on failure
622 static int lbs_get_channel(struct lbs_private *priv)
624 struct cmd_ds_802_11_rf_channel cmd;
625 int ret = 0;
627 lbs_deb_enter(LBS_DEB_CMD);
629 memset(&cmd, 0, sizeof(cmd));
630 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
631 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
633 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
634 if (ret)
635 goto out;
637 ret = le16_to_cpu(cmd.channel);
638 lbs_deb_cmd("current radio channel is %d\n", ret);
640 out:
641 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
642 return ret;
645 int lbs_update_channel(struct lbs_private *priv)
647 int ret;
649 /* the channel in f/w could be out of sync; get the current channel */
650 lbs_deb_enter(LBS_DEB_ASSOC);
652 ret = lbs_get_channel(priv);
653 if (ret > 0) {
654 priv->channel = ret;
655 ret = 0;
657 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
658 return ret;
662 * lbs_set_channel - Set the radio channel
664 * @priv: A pointer to &struct lbs_private structure
665 * @channel: The desired channel, or 0 to clear a locked channel
667 * returns: 0 on success, error on failure
669 int lbs_set_channel(struct lbs_private *priv, u8 channel)
671 struct cmd_ds_802_11_rf_channel cmd;
672 #ifdef DEBUG
673 u8 old_channel = priv->channel;
674 #endif
675 int ret = 0;
677 lbs_deb_enter(LBS_DEB_CMD);
679 memset(&cmd, 0, sizeof(cmd));
680 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
681 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
682 cmd.channel = cpu_to_le16(channel);
684 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
685 if (ret)
686 goto out;
688 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
689 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
690 priv->channel);
692 out:
693 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
694 return ret;
698 * lbs_get_rssi - Get current RSSI and noise floor
700 * @priv: A pointer to &struct lbs_private structure
701 * @rssi: On successful return, signal level in mBm
702 * @nf: On successful return, Noise floor
704 * returns: The channel on success, error on failure
706 int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
708 struct cmd_ds_802_11_rssi cmd;
709 int ret = 0;
711 lbs_deb_enter(LBS_DEB_CMD);
713 BUG_ON(rssi == NULL);
714 BUG_ON(nf == NULL);
716 memset(&cmd, 0, sizeof(cmd));
717 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
718 /* Average SNR over last 8 beacons */
719 cmd.n_or_snr = cpu_to_le16(8);
721 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
722 if (ret == 0) {
723 *nf = CAL_NF(le16_to_cpu(cmd.nf));
724 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
727 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
728 return ret;
732 * lbs_set_11d_domain_info - Send regulatory and 802.11d domain information
733 * to the firmware
735 * @priv: pointer to &struct lbs_private
737 * returns: 0 on success, error code on failure
739 int lbs_set_11d_domain_info(struct lbs_private *priv)
741 struct wiphy *wiphy = priv->wdev->wiphy;
742 struct ieee80211_supported_band **bands = wiphy->bands;
743 struct cmd_ds_802_11d_domain_info cmd;
744 struct mrvl_ie_domain_param_set *domain = &cmd.domain;
745 struct ieee80211_country_ie_triplet *t;
746 enum ieee80211_band band;
747 struct ieee80211_channel *ch;
748 u8 num_triplet = 0;
749 u8 num_parsed_chan = 0;
750 u8 first_channel = 0, next_chan = 0, max_pwr = 0;
751 u8 i, flag = 0;
752 size_t triplet_size;
753 int ret = 0;
755 lbs_deb_enter(LBS_DEB_11D);
756 if (!priv->country_code[0])
757 goto out;
759 memset(&cmd, 0, sizeof(cmd));
760 cmd.action = cpu_to_le16(CMD_ACT_SET);
762 lbs_deb_11d("Setting country code '%c%c'\n",
763 priv->country_code[0], priv->country_code[1]);
765 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
767 /* Set country code */
768 domain->country_code[0] = priv->country_code[0];
769 domain->country_code[1] = priv->country_code[1];
770 domain->country_code[2] = ' ';
772 /* Now set up the channel triplets; firmware is somewhat picky here
773 * and doesn't validate channel numbers and spans; hence it would
774 * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
775 * the last 3 aren't valid channels, the driver is responsible for
776 * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
777 * etc.
779 for (band = 0;
780 (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
781 band++) {
783 if (!bands[band])
784 continue;
786 for (i = 0;
787 (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
788 i++) {
789 ch = &bands[band]->channels[i];
790 if (ch->flags & IEEE80211_CHAN_DISABLED)
791 continue;
793 if (!flag) {
794 flag = 1;
795 next_chan = first_channel = (u32) ch->hw_value;
796 max_pwr = ch->max_power;
797 num_parsed_chan = 1;
798 continue;
801 if ((ch->hw_value == next_chan + 1) &&
802 (ch->max_power == max_pwr)) {
803 /* Consolidate adjacent channels */
804 next_chan++;
805 num_parsed_chan++;
806 } else {
807 /* Add this triplet */
808 lbs_deb_11d("11D triplet (%d, %d, %d)\n",
809 first_channel, num_parsed_chan,
810 max_pwr);
811 t = &domain->triplet[num_triplet];
812 t->chans.first_channel = first_channel;
813 t->chans.num_channels = num_parsed_chan;
814 t->chans.max_power = max_pwr;
815 num_triplet++;
816 flag = 0;
820 if (flag) {
821 /* Add last triplet */
822 lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
823 num_parsed_chan, max_pwr);
824 t = &domain->triplet[num_triplet];
825 t->chans.first_channel = first_channel;
826 t->chans.num_channels = num_parsed_chan;
827 t->chans.max_power = max_pwr;
828 num_triplet++;
832 lbs_deb_11d("# triplets %d\n", num_triplet);
834 /* Set command header sizes */
835 triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
836 domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
837 triplet_size);
839 lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
840 (u8 *) &cmd.domain.country_code,
841 le16_to_cpu(domain->header.len));
843 cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
844 sizeof(cmd.action) +
845 sizeof(cmd.domain.header) +
846 sizeof(cmd.domain.country_code) +
847 triplet_size);
849 ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
851 out:
852 lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
853 return ret;
857 * lbs_get_reg - Read a MAC, Baseband, or RF register
859 * @priv: pointer to &struct lbs_private
860 * @reg: register command, one of CMD_MAC_REG_ACCESS,
861 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
862 * @offset: byte offset of the register to get
863 * @value: on success, the value of the register at 'offset'
865 * returns: 0 on success, error code on failure
867 int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
869 struct cmd_ds_reg_access cmd;
870 int ret = 0;
872 lbs_deb_enter(LBS_DEB_CMD);
874 BUG_ON(value == NULL);
876 memset(&cmd, 0, sizeof(cmd));
877 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
878 cmd.action = cpu_to_le16(CMD_ACT_GET);
879 cmd.offset = cpu_to_le16(offset);
881 if (reg != CMD_MAC_REG_ACCESS &&
882 reg != CMD_BBP_REG_ACCESS &&
883 reg != CMD_RF_REG_ACCESS) {
884 ret = -EINVAL;
885 goto out;
888 ret = lbs_cmd_with_response(priv, reg, &cmd);
889 if (!ret) {
890 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
891 *value = cmd.value.bbp_rf;
892 else if (reg == CMD_MAC_REG_ACCESS)
893 *value = le32_to_cpu(cmd.value.mac);
896 out:
897 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
898 return ret;
902 * lbs_set_reg - Write a MAC, Baseband, or RF register
904 * @priv: pointer to &struct lbs_private
905 * @reg: register command, one of CMD_MAC_REG_ACCESS,
906 * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
907 * @offset: byte offset of the register to set
908 * @value: the value to write to the register at 'offset'
910 * returns: 0 on success, error code on failure
912 int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
914 struct cmd_ds_reg_access cmd;
915 int ret = 0;
917 lbs_deb_enter(LBS_DEB_CMD);
919 memset(&cmd, 0, sizeof(cmd));
920 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
921 cmd.action = cpu_to_le16(CMD_ACT_SET);
922 cmd.offset = cpu_to_le16(offset);
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);
928 else {
929 ret = -EINVAL;
930 goto out;
933 ret = lbs_cmd_with_response(priv, reg, &cmd);
935 out:
936 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
937 return ret;
940 static void lbs_queue_cmd(struct lbs_private *priv,
941 struct cmd_ctrl_node *cmdnode)
943 unsigned long flags;
944 int addtail = 1;
946 lbs_deb_enter(LBS_DEB_HOST);
948 if (!cmdnode) {
949 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
950 goto done;
952 if (!cmdnode->cmdbuf->size) {
953 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
954 goto done;
956 cmdnode->result = 0;
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)
964 addtail = 0;
968 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
969 addtail = 0;
971 spin_lock_irqsave(&priv->driver_lock, flags);
973 if (addtail)
974 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
975 else
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));
983 done:
984 lbs_deb_leave(LBS_DEB_HOST);
987 static void lbs_submit_command(struct lbs_private *priv,
988 struct cmd_ctrl_node *cmdnode)
990 unsigned long flags;
991 struct cmd_header *cmd;
992 uint16_t cmdsize;
993 uint16_t command;
994 int timeo = 3 * HZ;
995 int ret;
997 lbs_deb_enter(LBS_DEB_HOST);
999 cmd = cmdnode->cmdbuf;
1001 spin_lock_irqsave(&priv->driver_lock, flags);
1002 priv->seqnum++;
1003 cmd->seqnum = cpu_to_le16(priv->seqnum);
1004 priv->cur_cmd = cmdnode;
1005 spin_unlock_irqrestore(&priv->driver_lock, flags);
1007 cmdsize = le16_to_cpu(cmd->size);
1008 command = le16_to_cpu(cmd->command);
1010 /* These commands take longer */
1011 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1012 timeo = 5 * HZ;
1014 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1015 command, le16_to_cpu(cmd->seqnum), cmdsize);
1016 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1018 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1020 if (ret) {
1021 netdev_info(priv->dev, "DNLD_CMD: hw_host_to_card failed: %d\n",
1022 ret);
1023 /* Reset dnld state machine, report failure */
1024 priv->dnld_sent = DNLD_RES_RECEIVED;
1025 lbs_complete_command(priv, cmdnode, ret);
1028 if (command == CMD_802_11_DEEP_SLEEP) {
1029 if (priv->is_auto_deep_sleep_enabled) {
1030 priv->wakeup_dev_required = 1;
1031 priv->dnld_sent = 0;
1033 priv->is_deep_sleep = 1;
1034 lbs_complete_command(priv, cmdnode, 0);
1035 } else {
1036 /* Setup the timer after transmit command */
1037 mod_timer(&priv->command_timer, jiffies + timeo);
1040 lbs_deb_leave(LBS_DEB_HOST);
1044 * This function inserts command node to cmdfreeq
1045 * after cleans it. Requires priv->driver_lock held.
1047 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1048 struct cmd_ctrl_node *cmdnode)
1050 lbs_deb_enter(LBS_DEB_HOST);
1052 if (!cmdnode)
1053 goto out;
1055 cmdnode->callback = NULL;
1056 cmdnode->callback_arg = 0;
1058 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1060 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1061 out:
1062 lbs_deb_leave(LBS_DEB_HOST);
1065 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1066 struct cmd_ctrl_node *ptempcmd)
1068 unsigned long flags;
1070 spin_lock_irqsave(&priv->driver_lock, flags);
1071 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1072 spin_unlock_irqrestore(&priv->driver_lock, flags);
1075 void __lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1076 int result)
1079 * Normally, commands are removed from cmdpendingq before being
1080 * submitted. However, we can arrive here on alternative codepaths
1081 * where the command is still pending. Make sure the command really
1082 * isn't part of a list at this point.
1084 list_del_init(&cmd->list);
1086 cmd->result = result;
1087 cmd->cmdwaitqwoken = 1;
1088 wake_up(&cmd->cmdwait_q);
1090 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1091 __lbs_cleanup_and_insert_cmd(priv, cmd);
1092 priv->cur_cmd = NULL;
1093 wake_up(&priv->waitq);
1096 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1097 int result)
1099 unsigned long flags;
1100 spin_lock_irqsave(&priv->driver_lock, flags);
1101 __lbs_complete_command(priv, cmd, result);
1102 spin_unlock_irqrestore(&priv->driver_lock, flags);
1105 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1107 struct cmd_ds_802_11_radio_control cmd;
1108 int ret = -EINVAL;
1110 lbs_deb_enter(LBS_DEB_CMD);
1112 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1113 cmd.action = cpu_to_le16(CMD_ACT_SET);
1115 /* Only v8 and below support setting the preamble */
1116 if (priv->fwrelease < 0x09000000) {
1117 switch (preamble) {
1118 case RADIO_PREAMBLE_SHORT:
1119 case RADIO_PREAMBLE_AUTO:
1120 case RADIO_PREAMBLE_LONG:
1121 cmd.control = cpu_to_le16(preamble);
1122 break;
1123 default:
1124 goto out;
1128 if (radio_on)
1129 cmd.control |= cpu_to_le16(0x1);
1130 else {
1131 cmd.control &= cpu_to_le16(~0x1);
1132 priv->txpower_cur = 0;
1135 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1136 radio_on ? "ON" : "OFF", preamble);
1138 priv->radio_on = radio_on;
1140 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1142 out:
1143 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1144 return ret;
1147 void lbs_set_mac_control(struct lbs_private *priv)
1149 struct cmd_ds_mac_control cmd;
1151 lbs_deb_enter(LBS_DEB_CMD);
1153 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1154 cmd.action = cpu_to_le16(priv->mac_control);
1155 cmd.reserved = 0;
1157 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1159 lbs_deb_leave(LBS_DEB_CMD);
1163 * lbs_allocate_cmd_buffer - allocates the command buffer and links
1164 * it to command free queue
1166 * @priv: A pointer to &struct lbs_private structure
1168 * returns: 0 for success or -1 on error
1170 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1172 int ret = 0;
1173 u32 bufsize;
1174 u32 i;
1175 struct cmd_ctrl_node *cmdarray;
1177 lbs_deb_enter(LBS_DEB_HOST);
1179 /* Allocate and initialize the command array */
1180 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1181 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1182 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1183 ret = -1;
1184 goto done;
1186 priv->cmd_array = cmdarray;
1188 /* Allocate and initialize each command buffer in the command array */
1189 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1190 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1191 if (!cmdarray[i].cmdbuf) {
1192 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1193 ret = -1;
1194 goto done;
1198 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1199 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1200 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1202 ret = 0;
1204 done:
1205 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1206 return ret;
1210 * lbs_free_cmd_buffer - free the command buffer
1212 * @priv: A pointer to &struct lbs_private structure
1214 * returns: 0 for success
1216 int lbs_free_cmd_buffer(struct lbs_private *priv)
1218 struct cmd_ctrl_node *cmdarray;
1219 unsigned int i;
1221 lbs_deb_enter(LBS_DEB_HOST);
1223 /* need to check if cmd array is allocated or not */
1224 if (priv->cmd_array == NULL) {
1225 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1226 goto done;
1229 cmdarray = priv->cmd_array;
1231 /* Release shared memory buffers */
1232 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1233 if (cmdarray[i].cmdbuf) {
1234 kfree(cmdarray[i].cmdbuf);
1235 cmdarray[i].cmdbuf = NULL;
1239 /* Release cmd_ctrl_node */
1240 if (priv->cmd_array) {
1241 kfree(priv->cmd_array);
1242 priv->cmd_array = NULL;
1245 done:
1246 lbs_deb_leave(LBS_DEB_HOST);
1247 return 0;
1251 * lbs_get_free_cmd_node - gets a free command node if available in
1252 * command free queue
1254 * @priv: A pointer to &struct lbs_private structure
1256 * returns: A pointer to &cmd_ctrl_node structure on success
1257 * or %NULL on error
1259 static struct cmd_ctrl_node *lbs_get_free_cmd_node(struct lbs_private *priv)
1261 struct cmd_ctrl_node *tempnode;
1262 unsigned long flags;
1264 lbs_deb_enter(LBS_DEB_HOST);
1266 if (!priv)
1267 return NULL;
1269 spin_lock_irqsave(&priv->driver_lock, flags);
1271 if (!list_empty(&priv->cmdfreeq)) {
1272 tempnode = list_first_entry(&priv->cmdfreeq,
1273 struct cmd_ctrl_node, list);
1274 list_del_init(&tempnode->list);
1275 } else {
1276 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1277 tempnode = NULL;
1280 spin_unlock_irqrestore(&priv->driver_lock, flags);
1282 lbs_deb_leave(LBS_DEB_HOST);
1283 return tempnode;
1287 * lbs_execute_next_command - execute next command in command
1288 * pending queue. Will put firmware back to PS mode if applicable.
1290 * @priv: A pointer to &struct lbs_private structure
1292 * returns: 0 on success or -1 on error
1294 int lbs_execute_next_command(struct lbs_private *priv)
1296 struct cmd_ctrl_node *cmdnode = NULL;
1297 struct cmd_header *cmd;
1298 unsigned long flags;
1299 int ret = 0;
1301 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1302 * only caller to us is lbs_thread() and we get even when a
1303 * data packet is received */
1304 lbs_deb_enter(LBS_DEB_THREAD);
1306 spin_lock_irqsave(&priv->driver_lock, flags);
1308 if (priv->cur_cmd) {
1309 netdev_alert(priv->dev,
1310 "EXEC_NEXT_CMD: already processing command!\n");
1311 spin_unlock_irqrestore(&priv->driver_lock, flags);
1312 ret = -1;
1313 goto done;
1316 if (!list_empty(&priv->cmdpendingq)) {
1317 cmdnode = list_first_entry(&priv->cmdpendingq,
1318 struct cmd_ctrl_node, list);
1321 spin_unlock_irqrestore(&priv->driver_lock, flags);
1323 if (cmdnode) {
1324 cmd = cmdnode->cmdbuf;
1326 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1327 if ((priv->psstate == PS_STATE_SLEEP) ||
1328 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1329 lbs_deb_host(
1330 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1331 le16_to_cpu(cmd->command),
1332 priv->psstate);
1333 ret = -1;
1334 goto done;
1336 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1337 "0x%04x in psstate %d\n",
1338 le16_to_cpu(cmd->command), priv->psstate);
1339 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1341 * 1. Non-PS command:
1342 * Queue it. set needtowakeup to TRUE if current state
1343 * is SLEEP, otherwise call send EXIT_PS.
1344 * 2. PS command but not EXIT_PS:
1345 * Ignore it.
1346 * 3. PS command EXIT_PS:
1347 * Set needtowakeup to TRUE if current state is SLEEP,
1348 * otherwise send this command down to firmware
1349 * immediately.
1351 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1352 /* Prepare to send Exit PS,
1353 * this non PS command will be sent later */
1354 if ((priv->psstate == PS_STATE_SLEEP)
1355 || (priv->psstate == PS_STATE_PRE_SLEEP)
1357 /* w/ new scheme, it will not reach here.
1358 since it is blocked in main_thread. */
1359 priv->needtowakeup = 1;
1360 } else {
1361 lbs_set_ps_mode(priv,
1362 PS_MODE_ACTION_EXIT_PS,
1363 false);
1366 ret = 0;
1367 goto done;
1368 } else {
1370 * PS command. Ignore it if it is not Exit_PS.
1371 * otherwise send it down immediately.
1373 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1375 lbs_deb_host(
1376 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1377 psm->action);
1378 if (psm->action !=
1379 cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
1380 lbs_deb_host(
1381 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1382 lbs_complete_command(priv, cmdnode, 0);
1384 ret = 0;
1385 goto done;
1388 if ((priv->psstate == PS_STATE_SLEEP) ||
1389 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1390 lbs_deb_host(
1391 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1392 lbs_complete_command(priv, cmdnode, 0);
1393 priv->needtowakeup = 1;
1395 ret = 0;
1396 goto done;
1399 lbs_deb_host(
1400 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1403 spin_lock_irqsave(&priv->driver_lock, flags);
1404 list_del_init(&cmdnode->list);
1405 spin_unlock_irqrestore(&priv->driver_lock, flags);
1406 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1407 le16_to_cpu(cmd->command));
1408 lbs_submit_command(priv, cmdnode);
1409 } else {
1411 * check if in power save mode, if yes, put the device back
1412 * to PS mode
1414 #ifdef TODO
1416 * This was the old code for libertas+wext. Someone that
1417 * understands this beast should re-code it in a sane way.
1419 * I actually don't understand why this is related to WPA
1420 * and to connection status, shouldn't powering should be
1421 * independ of such things?
1423 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1424 (priv->psstate == PS_STATE_FULL_POWER) &&
1425 ((priv->connect_status == LBS_CONNECTED) ||
1426 lbs_mesh_connected(priv))) {
1427 if (priv->secinfo.WPAenabled ||
1428 priv->secinfo.WPA2enabled) {
1429 /* check for valid WPA group keys */
1430 if (priv->wpa_mcast_key.len ||
1431 priv->wpa_unicast_key.len) {
1432 lbs_deb_host(
1433 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1434 " go back to PS_SLEEP");
1435 lbs_set_ps_mode(priv,
1436 PS_MODE_ACTION_ENTER_PS,
1437 false);
1439 } else {
1440 lbs_deb_host(
1441 "EXEC_NEXT_CMD: cmdpendingq empty, "
1442 "go back to PS_SLEEP");
1443 lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
1444 false);
1447 #endif
1450 ret = 0;
1451 done:
1452 lbs_deb_leave(LBS_DEB_THREAD);
1453 return ret;
1456 static void lbs_send_confirmsleep(struct lbs_private *priv)
1458 unsigned long flags;
1459 int ret;
1461 lbs_deb_enter(LBS_DEB_HOST);
1462 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1463 sizeof(confirm_sleep));
1465 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1466 sizeof(confirm_sleep));
1467 if (ret) {
1468 netdev_alert(priv->dev, "confirm_sleep failed\n");
1469 goto out;
1472 spin_lock_irqsave(&priv->driver_lock, flags);
1474 /* We don't get a response on the sleep-confirmation */
1475 priv->dnld_sent = DNLD_RES_RECEIVED;
1477 if (priv->is_host_sleep_configured) {
1478 priv->is_host_sleep_activated = 1;
1479 wake_up_interruptible(&priv->host_sleep_q);
1482 /* If nothing to do, go back to sleep (?) */
1483 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1484 priv->psstate = PS_STATE_SLEEP;
1486 spin_unlock_irqrestore(&priv->driver_lock, flags);
1488 out:
1489 lbs_deb_leave(LBS_DEB_HOST);
1493 * lbs_ps_confirm_sleep - checks condition and prepares to
1494 * send sleep confirm command to firmware if ok
1496 * @priv: A pointer to &struct lbs_private structure
1498 * returns: n/a
1500 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1502 unsigned long flags =0;
1503 int allowed = 1;
1505 lbs_deb_enter(LBS_DEB_HOST);
1507 spin_lock_irqsave(&priv->driver_lock, flags);
1508 if (priv->dnld_sent) {
1509 allowed = 0;
1510 lbs_deb_host("dnld_sent was set\n");
1513 /* In-progress command? */
1514 if (priv->cur_cmd) {
1515 allowed = 0;
1516 lbs_deb_host("cur_cmd was set\n");
1519 /* Pending events or command responses? */
1520 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1521 allowed = 0;
1522 lbs_deb_host("pending events or command responses\n");
1524 spin_unlock_irqrestore(&priv->driver_lock, flags);
1526 if (allowed) {
1527 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1528 lbs_send_confirmsleep(priv);
1529 } else {
1530 lbs_deb_host("sleep confirm has been delayed\n");
1533 lbs_deb_leave(LBS_DEB_HOST);
1538 * lbs_set_tpc_cfg - Configures the transmission power control functionality
1540 * @priv: A pointer to &struct lbs_private structure
1541 * @enable: Transmission power control enable
1542 * @p0: Power level when link quality is good (dBm).
1543 * @p1: Power level when link quality is fair (dBm).
1544 * @p2: Power level when link quality is poor (dBm).
1545 * @usesnr: Use Signal to Noise Ratio in TPC
1547 * returns: 0 on success
1549 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1550 int8_t p2, int usesnr)
1552 struct cmd_ds_802_11_tpc_cfg cmd;
1553 int ret;
1555 memset(&cmd, 0, sizeof(cmd));
1556 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1557 cmd.action = cpu_to_le16(CMD_ACT_SET);
1558 cmd.enable = !!enable;
1559 cmd.usesnr = !!usesnr;
1560 cmd.P0 = p0;
1561 cmd.P1 = p1;
1562 cmd.P2 = p2;
1564 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1566 return ret;
1570 * lbs_set_power_adapt_cfg - Configures the power adaptation settings
1572 * @priv: A pointer to &struct lbs_private structure
1573 * @enable: Power adaptation enable
1574 * @p0: Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1575 * @p1: Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1576 * @p2: Power level for 48 and 54 Mbps (dBm).
1578 * returns: 0 on Success
1581 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1582 int8_t p1, int8_t p2)
1584 struct cmd_ds_802_11_pa_cfg cmd;
1585 int ret;
1587 memset(&cmd, 0, sizeof(cmd));
1588 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1589 cmd.action = cpu_to_le16(CMD_ACT_SET);
1590 cmd.enable = !!enable;
1591 cmd.P0 = p0;
1592 cmd.P1 = p1;
1593 cmd.P2 = p2;
1595 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1597 return ret;
1601 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1602 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1603 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1604 unsigned long callback_arg)
1606 struct cmd_ctrl_node *cmdnode;
1608 lbs_deb_enter(LBS_DEB_HOST);
1610 if (priv->surpriseremoved) {
1611 lbs_deb_host("PREP_CMD: card removed\n");
1612 cmdnode = ERR_PTR(-ENOENT);
1613 goto done;
1616 /* No commands are allowed in Deep Sleep until we toggle the GPIO
1617 * to wake up the card and it has signaled that it's ready.
1619 if (!priv->is_auto_deep_sleep_enabled) {
1620 if (priv->is_deep_sleep) {
1621 lbs_deb_cmd("command not allowed in deep sleep\n");
1622 cmdnode = ERR_PTR(-EBUSY);
1623 goto done;
1627 cmdnode = lbs_get_free_cmd_node(priv);
1628 if (cmdnode == NULL) {
1629 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1631 /* Wake up main thread to execute next command */
1632 wake_up(&priv->waitq);
1633 cmdnode = ERR_PTR(-ENOBUFS);
1634 goto done;
1637 cmdnode->callback = callback;
1638 cmdnode->callback_arg = callback_arg;
1640 /* Copy the incoming command to the buffer */
1641 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1643 /* Set command, clean result, move to buffer */
1644 cmdnode->cmdbuf->command = cpu_to_le16(command);
1645 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1646 cmdnode->cmdbuf->result = 0;
1648 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1650 cmdnode->cmdwaitqwoken = 0;
1651 lbs_queue_cmd(priv, cmdnode);
1652 wake_up(&priv->waitq);
1654 done:
1655 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1656 return cmdnode;
1659 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1660 struct cmd_header *in_cmd, int in_cmd_size)
1662 lbs_deb_enter(LBS_DEB_CMD);
1663 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1664 lbs_cmd_async_callback, 0);
1665 lbs_deb_leave(LBS_DEB_CMD);
1668 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1669 struct cmd_header *in_cmd, int in_cmd_size,
1670 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1671 unsigned long callback_arg)
1673 struct cmd_ctrl_node *cmdnode;
1674 unsigned long flags;
1675 int ret = 0;
1677 lbs_deb_enter(LBS_DEB_HOST);
1679 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1680 callback, callback_arg);
1681 if (IS_ERR(cmdnode)) {
1682 ret = PTR_ERR(cmdnode);
1683 goto done;
1686 might_sleep();
1689 * Be careful with signals here. A signal may be received as the system
1690 * goes into suspend or resume. We do not want this to interrupt the
1691 * command, so we perform an uninterruptible sleep.
1693 wait_event(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1695 spin_lock_irqsave(&priv->driver_lock, flags);
1696 ret = cmdnode->result;
1697 if (ret)
1698 netdev_info(priv->dev, "PREP_CMD: command 0x%04x failed: %d\n",
1699 command, ret);
1701 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
1702 spin_unlock_irqrestore(&priv->driver_lock, flags);
1704 done:
1705 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1706 return ret;
1708 EXPORT_SYMBOL_GPL(__lbs_cmd);