]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wireless/libertas/cmd.c
libertas: convert Mesh Blinding Table access to a direct command
[net-next-2.6.git] / drivers / net / wireless / libertas / cmd.c
1 /**
2   * This file contains the handling of command.
3   * It prepares command and sends it to firmware when it is ready.
4   */
5
6 #include <linux/kfifo.h>
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/if_arp.h>
10
11 #include "decl.h"
12 #include "cfg.h"
13 #include "cmd.h"
14
15 #define CAL_NF(nf)              ((s32)(-(s32)(nf)))
16 #define CAL_RSSI(snr, nf)       ((s32)((s32)(snr) + CAL_NF(nf)))
17
18 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
19
20 /**
21  *  @brief Simple callback that copies response back into command
22  *
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
27  *
28  *  @return             0 on success, error on failure
29  */
30 int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
31                      struct cmd_header *resp)
32 {
33         struct cmd_header *buf = (void *)extra;
34         uint16_t copy_len;
35
36         copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
37         memcpy(buf, resp, copy_len);
38         return 0;
39 }
40 EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
41
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.
46  *
47  *  @param priv         ignored
48  *  @param extra        ignored
49  *  @param resp         ignored
50  *
51  *  @return             0 for success
52  */
53 static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
54                      struct cmd_header *resp)
55 {
56         return 0;
57 }
58
59
60 /**
61  *  @brief Checks whether a command is allowed in Power Save mode
62  *
63  *  @param command the command ID
64  *  @return        1 if allowed, 0 if not allowed
65  */
66 static u8 is_command_allowed_in_ps(u16 cmd)
67 {
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;
75         }
76         return 0;
77 }
78
79 /**
80  *  @brief This function checks if the command is allowed.
81  *
82  *  @param priv         A pointer to lbs_private structure
83  *  @return             allowed or not allowed.
84  */
85
86 static int lbs_is_cmd_allowed(struct lbs_private *priv)
87 {
88         int ret = 1;
89
90         lbs_deb_enter(LBS_DEB_CMD);
91
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;
96                 }
97         }
98
99         lbs_deb_leave(LBS_DEB_CMD);
100         return ret;
101 }
102
103 /**
104  *  @brief Updates the hardware details like MAC address and regulatory region
105  *
106  *  @param priv         A pointer to struct lbs_private structure
107  *
108  *  @return             0 on success, error on failure
109  */
110 int lbs_update_hw_spec(struct lbs_private *priv)
111 {
112         struct cmd_ds_get_hw_spec cmd;
113         int ret = -1;
114         u32 i;
115
116         lbs_deb_enter(LBS_DEB_CMD);
117
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;
124
125         priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
126
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);
132
133         /* Some firmware capabilities:
134          * CF card    firmware 5.0.16p0:   cap 0x00000303
135          * USB dongle firmware 5.110.17p2: cap 0x00000303
136          */
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);
146
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.
150          *
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.
153          */
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;
158
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;
163         }
164
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");
169         }
170
171         if (priv->current_addr[0] == 0xff)
172                 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
173
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);
177
178 out:
179         lbs_deb_leave(LBS_DEB_CMD);
180         return ret;
181 }
182
183 static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
184                         struct cmd_header *resp)
185 {
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);
192                 }
193         } else {
194                 priv->is_host_sleep_configured = 1;
195         }
196         lbs_deb_leave(LBS_DEB_CMD);
197         return 0;
198 }
199
200 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
201                 struct wol_config *p_wol_config)
202 {
203         struct cmd_ds_host_sleep cmd_config;
204         int ret;
205
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;
210
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;
216
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);
227         }
228
229         return ret;
230 }
231 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
232
233 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
234                                    u16 cmd_action)
235 {
236         struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
237
238         lbs_deb_enter(LBS_DEB_CMD);
239
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");
248
249                 psm->locallisteninterval = 0;
250                 psm->nullpktinterval = 0;
251                 psm->multipledtim =
252                     cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
253                 break;
254
255         case CMD_SUBCMD_EXIT_PS:
256                 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
257                 break;
258
259         case CMD_SUBCMD_SLEEP_CONFIRMED:
260                 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
261                 break;
262
263         default:
264                 break;
265         }
266
267         lbs_deb_leave(LBS_DEB_CMD);
268         return 0;
269 }
270
271 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
272                                 struct sleep_params *sp)
273 {
274         struct cmd_ds_802_11_sleep_params cmd;
275         int ret;
276
277         lbs_deb_enter(LBS_DEB_CMD);
278
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);
288         }
289         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
290         cmd.action = cpu_to_le16(cmd_action);
291
292         ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
293
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);
300
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);
307         }
308
309         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
310         return 0;
311 }
312
313 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
314 {
315         int ret = 0;
316
317         lbs_deb_enter(LBS_DEB_CMD);
318
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;
324                 }
325         }
326
327         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
328         return ret;
329 }
330
331 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
332 {
333         int ret =  0;
334
335         lbs_deb_enter(LBS_DEB_CMD);
336
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);
345                         }
346                 } else {
347                         lbs_pr_err("deep sleep: already enabled\n");
348                 }
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");
359                         }
360                 }
361         }
362
363         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
364         return ret;
365 }
366
367 static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
368                 unsigned long dummy,
369                 struct cmd_header *cmd)
370 {
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;
376 }
377
378 int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
379 {
380         struct cmd_header cmd;
381         int ret = 0;
382         uint32_t criteria = EHS_REMOVE_WAKEUP;
383
384         lbs_deb_enter(LBS_DEB_CMD);
385
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;
395                         }
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);
405                         }
406
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;
413                         }
414                 } else {
415                         lbs_pr_err("host sleep: already enabled\n");
416                 }
417         } else {
418                 if (priv->is_host_sleep_activated)
419                         ret = lbs_host_sleep_cfg(priv, criteria,
420                                         (struct wol_config *)NULL);
421         }
422
423         return ret;
424 }
425
426 /**
427  *  @brief Set an SNMP MIB value
428  *
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
432  *
433  *  @return             0 on success, error on failure
434  */
435 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
436 {
437         struct cmd_ds_802_11_snmp_mib cmd;
438         int ret;
439
440         lbs_deb_enter(LBS_DEB_CMD);
441
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);
446
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;
464         }
465
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);
468
469         ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
470
471 out:
472         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
473         return ret;
474 }
475
476 /**
477  *  @brief Get an SNMP MIB value
478  *
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
482  *
483  *  @return             0 on success, error on failure
484  */
485 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
486 {
487         struct cmd_ds_802_11_snmp_mib cmd;
488         int ret;
489
490         lbs_deb_enter(LBS_DEB_CMD);
491
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);
496
497         ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
498         if (ret)
499                 goto out;
500
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;
512         }
513
514 out:
515         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
516         return ret;
517 }
518
519 /**
520  *  @brief Get the min, max, and current TX power
521  *
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)
526  *
527  *  @return             0 on success, error on failure
528  */
529 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
530                      s16 *maxlevel)
531 {
532         struct cmd_ds_802_11_rf_tx_power cmd;
533         int ret;
534
535         lbs_deb_enter(LBS_DEB_CMD);
536
537         memset(&cmd, 0, sizeof(cmd));
538         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
539         cmd.action = cpu_to_le16(CMD_ACT_GET);
540
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;
548         }
549
550         lbs_deb_leave(LBS_DEB_CMD);
551         return ret;
552 }
553
554 /**
555  *  @brief Set the TX power
556  *
557  *  @param priv         A pointer to struct lbs_private structure
558  *  @param dbm          The desired power level in dBm
559  *
560  *  @return             0 on success, error on failure
561  */
562 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
563 {
564         struct cmd_ds_802_11_rf_tx_power cmd;
565         int ret;
566
567         lbs_deb_enter(LBS_DEB_CMD);
568
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);
573
574         lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
575
576         ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
577
578         lbs_deb_leave(LBS_DEB_CMD);
579         return ret;
580 }
581
582 /**
583  *  @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
584  *
585  *  @param priv        A pointer to struct lbs_private structure
586  *  @param enable      1 to enable monitor mode, 0 to disable
587  *
588  *  @return            0 on success, error on failure
589  */
590 int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
591 {
592         struct cmd_ds_802_11_monitor_mode cmd;
593         int ret;
594
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);
600
601         lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
602
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;
607         }
608
609         lbs_deb_leave(LBS_DEB_CMD);
610         return ret;
611 }
612
613 /**
614  *  @brief Get the radio channel
615  *
616  *  @param priv         A pointer to struct lbs_private structure
617  *
618  *  @return             The channel on success, error on failure
619  */
620 static int lbs_get_channel(struct lbs_private *priv)
621 {
622         struct cmd_ds_802_11_rf_channel cmd;
623         int ret = 0;
624
625         lbs_deb_enter(LBS_DEB_CMD);
626
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);
630
631         ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
632         if (ret)
633                 goto out;
634
635         ret = le16_to_cpu(cmd.channel);
636         lbs_deb_cmd("current radio channel is %d\n", ret);
637
638 out:
639         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
640         return ret;
641 }
642
643 int lbs_update_channel(struct lbs_private *priv)
644 {
645         int ret;
646
647         /* the channel in f/w could be out of sync; get the current channel */
648         lbs_deb_enter(LBS_DEB_ASSOC);
649
650         ret = lbs_get_channel(priv);
651         if (ret > 0) {
652                 priv->channel = ret;
653                 ret = 0;
654         }
655         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
656         return ret;
657 }
658
659 /**
660  *  @brief Set the radio channel
661  *
662  *  @param priv         A pointer to struct lbs_private structure
663  *  @param channel      The desired channel, or 0 to clear a locked channel
664  *
665  *  @return             0 on success, error on failure
666  */
667 int lbs_set_channel(struct lbs_private *priv, u8 channel)
668 {
669         struct cmd_ds_802_11_rf_channel cmd;
670 #ifdef DEBUG
671         u8 old_channel = priv->channel;
672 #endif
673         int ret = 0;
674
675         lbs_deb_enter(LBS_DEB_CMD);
676
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);
681
682         ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
683         if (ret)
684                 goto out;
685
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);
689
690 out:
691         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
692         return ret;
693 }
694
695 /**
696  *  @brief Get current RSSI and noise floor
697  *
698  *  @param priv         A pointer to struct lbs_private structure
699  *  @param rssi         On successful return, signal level in mBm
700  *
701  *  @return             The channel on success, error on failure
702  */
703 int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
704 {
705         struct cmd_ds_802_11_rssi cmd;
706         int ret = 0;
707
708         lbs_deb_enter(LBS_DEB_CMD);
709
710         BUG_ON(rssi == NULL);
711         BUG_ON(nf == NULL);
712
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);
717
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));
722         }
723
724         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
725         return ret;
726 }
727
728 /**
729  *  @brief Send regulatory and 802.11d domain information to the firmware
730  *
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
734  *
735  *  @return             0 on success, error code on failure
736 */
737 int lbs_set_11d_domain_info(struct lbs_private *priv,
738                             struct regulatory_request *request,
739                             struct ieee80211_supported_band **bands)
740 {
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;
752
753         lbs_deb_enter(LBS_DEB_11D);
754
755         memset(&cmd, 0, sizeof(cmd));
756         cmd.action = cpu_to_le16(CMD_ACT_SET);
757
758         lbs_deb_11d("Setting country code '%c%c'\n",
759                     request->alpha2[0], request->alpha2[1]);
760
761         domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
762
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] = ' ';
767
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.
774          */
775         for (band = 0;
776              (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
777              band++) {
778
779                 if (!bands[band])
780                         continue;
781
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;
788
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;
795                         }
796
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;
813                         }
814                 }
815
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++;
825                 }
826         }
827
828         lbs_deb_11d("# triplets %d\n", num_triplet);
829
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);
834
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));
838
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);
844
845         ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
846
847         lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
848         return ret;
849 }
850
851 /**
852  *  @brief Read a MAC, Baseband, or RF register
853  *
854  *  @param priv         pointer to struct lbs_private
855  *  @param cmd          register command, one of CMD_MAC_REG_ACCESS,
856  *                        CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
857  *  @param offset       byte offset of the register to get
858  *  @param value        on success, the value of the register at 'offset'
859  *
860  *  @return             0 on success, error code on failure
861 */
862 int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
863 {
864         struct cmd_ds_reg_access cmd;
865         int ret = 0;
866
867         lbs_deb_enter(LBS_DEB_CMD);
868
869         BUG_ON(value == NULL);
870
871         memset(&cmd, 0, sizeof(cmd));
872         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
873         cmd.action = cpu_to_le16(CMD_ACT_GET);
874
875         if (reg != CMD_MAC_REG_ACCESS &&
876             reg != CMD_BBP_REG_ACCESS &&
877             reg != CMD_RF_REG_ACCESS) {
878                 ret = -EINVAL;
879                 goto out;
880         }
881
882         ret = lbs_cmd_with_response(priv, reg, &cmd);
883         if (ret) {
884                 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
885                         *value = cmd.value.bbp_rf;
886                 else if (reg == CMD_MAC_REG_ACCESS)
887                         *value = le32_to_cpu(cmd.value.mac);
888         }
889
890 out:
891         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
892         return ret;
893 }
894
895 /**
896  *  @brief Write a MAC, Baseband, or RF register
897  *
898  *  @param priv         pointer to struct lbs_private
899  *  @param cmd          register command, one of CMD_MAC_REG_ACCESS,
900  *                        CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
901  *  @param offset       byte offset of the register to set
902  *  @param value        the value to write to the register at 'offset'
903  *
904  *  @return             0 on success, error code on failure
905 */
906 int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
907 {
908         struct cmd_ds_reg_access cmd;
909         int ret = 0;
910
911         lbs_deb_enter(LBS_DEB_CMD);
912
913         memset(&cmd, 0, sizeof(cmd));
914         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
915         cmd.action = cpu_to_le16(CMD_ACT_SET);
916
917         if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
918                 cmd.value.bbp_rf = (u8) (value & 0xFF);
919         else if (reg == CMD_MAC_REG_ACCESS)
920                 cmd.value.mac = cpu_to_le32(value);
921         else {
922                 ret = -EINVAL;
923                 goto out;
924         }
925
926         ret = lbs_cmd_with_response(priv, reg, &cmd);
927
928 out:
929         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
930         return ret;
931 }
932
933 static void lbs_queue_cmd(struct lbs_private *priv,
934                           struct cmd_ctrl_node *cmdnode)
935 {
936         unsigned long flags;
937         int addtail = 1;
938
939         lbs_deb_enter(LBS_DEB_HOST);
940
941         if (!cmdnode) {
942                 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
943                 goto done;
944         }
945         if (!cmdnode->cmdbuf->size) {
946                 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
947                 goto done;
948         }
949         cmdnode->result = 0;
950
951         /* Exit_PS command needs to be queued in the header always. */
952         if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
953                 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
954
955                 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
956                         if (priv->psstate != PS_STATE_FULL_POWER)
957                                 addtail = 0;
958                 }
959         }
960
961         if (le16_to_cpu(cmdnode->cmdbuf->command) ==
962                         CMD_802_11_WAKEUP_CONFIRM)
963                 addtail = 0;
964
965         spin_lock_irqsave(&priv->driver_lock, flags);
966
967         if (addtail)
968                 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
969         else
970                 list_add(&cmdnode->list, &priv->cmdpendingq);
971
972         spin_unlock_irqrestore(&priv->driver_lock, flags);
973
974         lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
975                      le16_to_cpu(cmdnode->cmdbuf->command));
976
977 done:
978         lbs_deb_leave(LBS_DEB_HOST);
979 }
980
981 static void lbs_submit_command(struct lbs_private *priv,
982                                struct cmd_ctrl_node *cmdnode)
983 {
984         unsigned long flags;
985         struct cmd_header *cmd;
986         uint16_t cmdsize;
987         uint16_t command;
988         int timeo = 3 * HZ;
989         int ret;
990
991         lbs_deb_enter(LBS_DEB_HOST);
992
993         cmd = cmdnode->cmdbuf;
994
995         spin_lock_irqsave(&priv->driver_lock, flags);
996         priv->cur_cmd = cmdnode;
997         priv->cur_cmd_retcode = 0;
998         spin_unlock_irqrestore(&priv->driver_lock, flags);
999
1000         cmdsize = le16_to_cpu(cmd->size);
1001         command = le16_to_cpu(cmd->command);
1002
1003         /* These commands take longer */
1004         if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1005                 timeo = 5 * HZ;
1006
1007         lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1008                      command, le16_to_cpu(cmd->seqnum), cmdsize);
1009         lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1010
1011         ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1012
1013         if (ret) {
1014                 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1015                 /* Let the timer kick in and retry, and potentially reset
1016                    the whole thing if the condition persists */
1017                 timeo = HZ/4;
1018         }
1019
1020         if (command == CMD_802_11_DEEP_SLEEP) {
1021                 if (priv->is_auto_deep_sleep_enabled) {
1022                         priv->wakeup_dev_required = 1;
1023                         priv->dnld_sent = 0;
1024                 }
1025                 priv->is_deep_sleep = 1;
1026                 lbs_complete_command(priv, cmdnode, 0);
1027         } else {
1028                 /* Setup the timer after transmit command */
1029                 mod_timer(&priv->command_timer, jiffies + timeo);
1030         }
1031
1032         lbs_deb_leave(LBS_DEB_HOST);
1033 }
1034
1035 /**
1036  *  This function inserts command node to cmdfreeq
1037  *  after cleans it. Requires priv->driver_lock held.
1038  */
1039 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1040                                          struct cmd_ctrl_node *cmdnode)
1041 {
1042         lbs_deb_enter(LBS_DEB_HOST);
1043
1044         if (!cmdnode)
1045                 goto out;
1046
1047         cmdnode->callback = NULL;
1048         cmdnode->callback_arg = 0;
1049
1050         memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1051
1052         list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1053  out:
1054         lbs_deb_leave(LBS_DEB_HOST);
1055 }
1056
1057 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1058         struct cmd_ctrl_node *ptempcmd)
1059 {
1060         unsigned long flags;
1061
1062         spin_lock_irqsave(&priv->driver_lock, flags);
1063         __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1064         spin_unlock_irqrestore(&priv->driver_lock, flags);
1065 }
1066
1067 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1068                           int result)
1069 {
1070         if (cmd == priv->cur_cmd)
1071                 priv->cur_cmd_retcode = result;
1072
1073         cmd->result = result;
1074         cmd->cmdwaitqwoken = 1;
1075         wake_up_interruptible(&cmd->cmdwait_q);
1076
1077         if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1078                 __lbs_cleanup_and_insert_cmd(priv, cmd);
1079         priv->cur_cmd = NULL;
1080 }
1081
1082 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1083 {
1084         struct cmd_ds_802_11_radio_control cmd;
1085         int ret = -EINVAL;
1086
1087         lbs_deb_enter(LBS_DEB_CMD);
1088
1089         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1090         cmd.action = cpu_to_le16(CMD_ACT_SET);
1091
1092         /* Only v8 and below support setting the preamble */
1093         if (priv->fwrelease < 0x09000000) {
1094                 switch (preamble) {
1095                 case RADIO_PREAMBLE_SHORT:
1096                 case RADIO_PREAMBLE_AUTO:
1097                 case RADIO_PREAMBLE_LONG:
1098                         cmd.control = cpu_to_le16(preamble);
1099                         break;
1100                 default:
1101                         goto out;
1102                 }
1103         }
1104
1105         if (radio_on)
1106                 cmd.control |= cpu_to_le16(0x1);
1107         else {
1108                 cmd.control &= cpu_to_le16(~0x1);
1109                 priv->txpower_cur = 0;
1110         }
1111
1112         lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1113                     radio_on ? "ON" : "OFF", preamble);
1114
1115         priv->radio_on = radio_on;
1116
1117         ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1118
1119 out:
1120         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1121         return ret;
1122 }
1123
1124 void lbs_set_mac_control(struct lbs_private *priv)
1125 {
1126         struct cmd_ds_mac_control cmd;
1127
1128         lbs_deb_enter(LBS_DEB_CMD);
1129
1130         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1131         cmd.action = cpu_to_le16(priv->mac_control);
1132         cmd.reserved = 0;
1133
1134         lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1135
1136         lbs_deb_leave(LBS_DEB_CMD);
1137 }
1138
1139 /**
1140  *  @brief This function prepare the command before send to firmware.
1141  *
1142  *  @param priv         A pointer to struct lbs_private structure
1143  *  @param cmd_no       command number
1144  *  @param cmd_action   command action: GET or SET
1145  *  @param wait_option  wait option: wait response or not
1146  *  @param cmd_oid      cmd oid: treated as sub command
1147  *  @param pdata_buf    A pointer to informaion buffer
1148  *  @return             0 or -1
1149  */
1150 int lbs_prepare_and_send_command(struct lbs_private *priv,
1151                           u16 cmd_no,
1152                           u16 cmd_action,
1153                           u16 wait_option, u32 cmd_oid, void *pdata_buf)
1154 {
1155         int ret = 0;
1156         struct cmd_ctrl_node *cmdnode;
1157         struct cmd_ds_command *cmdptr;
1158         unsigned long flags;
1159
1160         lbs_deb_enter(LBS_DEB_HOST);
1161
1162         if (!priv) {
1163                 lbs_deb_host("PREP_CMD: priv is NULL\n");
1164                 ret = -1;
1165                 goto done;
1166         }
1167
1168         if (priv->surpriseremoved) {
1169                 lbs_deb_host("PREP_CMD: card removed\n");
1170                 ret = -1;
1171                 goto done;
1172         }
1173
1174         if (!lbs_is_cmd_allowed(priv)) {
1175                 ret = -EBUSY;
1176                 goto done;
1177         }
1178
1179         cmdnode = lbs_get_cmd_ctrl_node(priv);
1180
1181         if (cmdnode == NULL) {
1182                 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1183
1184                 /* Wake up main thread to execute next command */
1185                 wake_up_interruptible(&priv->waitq);
1186                 ret = -1;
1187                 goto done;
1188         }
1189
1190         cmdnode->callback = NULL;
1191         cmdnode->callback_arg = (unsigned long)pdata_buf;
1192
1193         cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1194
1195         lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1196
1197         /* Set sequence number, command and INT option */
1198         priv->seqnum++;
1199         cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1200
1201         cmdptr->command = cpu_to_le16(cmd_no);
1202         cmdptr->result = 0;
1203
1204         switch (cmd_no) {
1205         case CMD_802_11_PS_MODE:
1206                 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1207                 break;
1208
1209 #ifdef CONFIG_LIBERTAS_MESH
1210
1211         case CMD_FWT_ACCESS:
1212                 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1213                 break;
1214
1215 #endif
1216
1217         case CMD_802_11_DEEP_SLEEP:
1218                 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
1219                 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
1220                 break;
1221         default:
1222                 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1223                 ret = -1;
1224                 break;
1225         }
1226
1227         /* return error, since the command preparation failed */
1228         if (ret != 0) {
1229                 lbs_deb_host("PREP_CMD: command preparation failed\n");
1230                 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1231                 ret = -1;
1232                 goto done;
1233         }
1234
1235         cmdnode->cmdwaitqwoken = 0;
1236
1237         lbs_queue_cmd(priv, cmdnode);
1238         wake_up_interruptible(&priv->waitq);
1239
1240         if (wait_option & CMD_OPTION_WAITFORRSP) {
1241                 lbs_deb_host("PREP_CMD: wait for response\n");
1242                 might_sleep();
1243                 wait_event_interruptible(cmdnode->cmdwait_q,
1244                                          cmdnode->cmdwaitqwoken);
1245         }
1246
1247         spin_lock_irqsave(&priv->driver_lock, flags);
1248         if (priv->cur_cmd_retcode) {
1249                 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1250                        priv->cur_cmd_retcode);
1251                 priv->cur_cmd_retcode = 0;
1252                 ret = -1;
1253         }
1254         spin_unlock_irqrestore(&priv->driver_lock, flags);
1255
1256 done:
1257         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1258         return ret;
1259 }
1260
1261 /**
1262  *  @brief This function allocates the command buffer and link
1263  *  it to command free queue.
1264  *
1265  *  @param priv         A pointer to struct lbs_private structure
1266  *  @return             0 or -1
1267  */
1268 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1269 {
1270         int ret = 0;
1271         u32 bufsize;
1272         u32 i;
1273         struct cmd_ctrl_node *cmdarray;
1274
1275         lbs_deb_enter(LBS_DEB_HOST);
1276
1277         /* Allocate and initialize the command array */
1278         bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1279         if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1280                 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1281                 ret = -1;
1282                 goto done;
1283         }
1284         priv->cmd_array = cmdarray;
1285
1286         /* Allocate and initialize each command buffer in the command array */
1287         for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1288                 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1289                 if (!cmdarray[i].cmdbuf) {
1290                         lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1291                         ret = -1;
1292                         goto done;
1293                 }
1294         }
1295
1296         for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1297                 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1298                 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1299         }
1300         ret = 0;
1301
1302 done:
1303         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1304         return ret;
1305 }
1306
1307 /**
1308  *  @brief This function frees the command buffer.
1309  *
1310  *  @param priv         A pointer to struct lbs_private structure
1311  *  @return             0 or -1
1312  */
1313 int lbs_free_cmd_buffer(struct lbs_private *priv)
1314 {
1315         struct cmd_ctrl_node *cmdarray;
1316         unsigned int i;
1317
1318         lbs_deb_enter(LBS_DEB_HOST);
1319
1320         /* need to check if cmd array is allocated or not */
1321         if (priv->cmd_array == NULL) {
1322                 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1323                 goto done;
1324         }
1325
1326         cmdarray = priv->cmd_array;
1327
1328         /* Release shared memory buffers */
1329         for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1330                 if (cmdarray[i].cmdbuf) {
1331                         kfree(cmdarray[i].cmdbuf);
1332                         cmdarray[i].cmdbuf = NULL;
1333                 }
1334         }
1335
1336         /* Release cmd_ctrl_node */
1337         if (priv->cmd_array) {
1338                 kfree(priv->cmd_array);
1339                 priv->cmd_array = NULL;
1340         }
1341
1342 done:
1343         lbs_deb_leave(LBS_DEB_HOST);
1344         return 0;
1345 }
1346
1347 /**
1348  *  @brief This function gets a free command node if available in
1349  *  command free queue.
1350  *
1351  *  @param priv         A pointer to struct lbs_private structure
1352  *  @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1353  */
1354 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1355 {
1356         struct cmd_ctrl_node *tempnode;
1357         unsigned long flags;
1358
1359         lbs_deb_enter(LBS_DEB_HOST);
1360
1361         if (!priv)
1362                 return NULL;
1363
1364         spin_lock_irqsave(&priv->driver_lock, flags);
1365
1366         if (!list_empty(&priv->cmdfreeq)) {
1367                 tempnode = list_first_entry(&priv->cmdfreeq,
1368                                             struct cmd_ctrl_node, list);
1369                 list_del(&tempnode->list);
1370         } else {
1371                 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1372                 tempnode = NULL;
1373         }
1374
1375         spin_unlock_irqrestore(&priv->driver_lock, flags);
1376
1377         lbs_deb_leave(LBS_DEB_HOST);
1378         return tempnode;
1379 }
1380
1381 /**
1382  *  @brief This function executes next command in command
1383  *  pending queue. It will put firmware back to PS mode
1384  *  if applicable.
1385  *
1386  *  @param priv     A pointer to struct lbs_private structure
1387  *  @return        0 or -1
1388  */
1389 int lbs_execute_next_command(struct lbs_private *priv)
1390 {
1391         struct cmd_ctrl_node *cmdnode = NULL;
1392         struct cmd_header *cmd;
1393         unsigned long flags;
1394         int ret = 0;
1395
1396         /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1397          * only caller to us is lbs_thread() and we get even when a
1398          * data packet is received */
1399         lbs_deb_enter(LBS_DEB_THREAD);
1400
1401         spin_lock_irqsave(&priv->driver_lock, flags);
1402
1403         if (priv->cur_cmd) {
1404                 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1405                 spin_unlock_irqrestore(&priv->driver_lock, flags);
1406                 ret = -1;
1407                 goto done;
1408         }
1409
1410         if (!list_empty(&priv->cmdpendingq)) {
1411                 cmdnode = list_first_entry(&priv->cmdpendingq,
1412                                            struct cmd_ctrl_node, list);
1413         }
1414
1415         spin_unlock_irqrestore(&priv->driver_lock, flags);
1416
1417         if (cmdnode) {
1418                 cmd = cmdnode->cmdbuf;
1419
1420                 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1421                         if ((priv->psstate == PS_STATE_SLEEP) ||
1422                             (priv->psstate == PS_STATE_PRE_SLEEP)) {
1423                                 lbs_deb_host(
1424                                        "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1425                                        le16_to_cpu(cmd->command),
1426                                        priv->psstate);
1427                                 ret = -1;
1428                                 goto done;
1429                         }
1430                         lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1431                                      "0x%04x in psstate %d\n",
1432                                      le16_to_cpu(cmd->command), priv->psstate);
1433                 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1434                         /*
1435                          * 1. Non-PS command:
1436                          * Queue it. set needtowakeup to TRUE if current state
1437                          * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1438                          * 2. PS command but not Exit_PS:
1439                          * Ignore it.
1440                          * 3. PS command Exit_PS:
1441                          * Set needtowakeup to TRUE if current state is SLEEP,
1442                          * otherwise send this command down to firmware
1443                          * immediately.
1444                          */
1445                         if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1446                                 /*  Prepare to send Exit PS,
1447                                  *  this non PS command will be sent later */
1448                                 if ((priv->psstate == PS_STATE_SLEEP)
1449                                     || (priv->psstate == PS_STATE_PRE_SLEEP)
1450                                     ) {
1451                                         /* w/ new scheme, it will not reach here.
1452                                            since it is blocked in main_thread. */
1453                                         priv->needtowakeup = 1;
1454                                 } else
1455                                         lbs_ps_wakeup(priv, 0);
1456
1457                                 ret = 0;
1458                                 goto done;
1459                         } else {
1460                                 /*
1461                                  * PS command. Ignore it if it is not Exit_PS.
1462                                  * otherwise send it down immediately.
1463                                  */
1464                                 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1465
1466                                 lbs_deb_host(
1467                                        "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1468                                        psm->action);
1469                                 if (psm->action !=
1470                                     cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1471                                         lbs_deb_host(
1472                                                "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1473                                         list_del(&cmdnode->list);
1474                                         spin_lock_irqsave(&priv->driver_lock, flags);
1475                                         lbs_complete_command(priv, cmdnode, 0);
1476                                         spin_unlock_irqrestore(&priv->driver_lock, flags);
1477
1478                                         ret = 0;
1479                                         goto done;
1480                                 }
1481
1482                                 if ((priv->psstate == PS_STATE_SLEEP) ||
1483                                     (priv->psstate == PS_STATE_PRE_SLEEP)) {
1484                                         lbs_deb_host(
1485                                                "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1486                                         list_del(&cmdnode->list);
1487                                         spin_lock_irqsave(&priv->driver_lock, flags);
1488                                         lbs_complete_command(priv, cmdnode, 0);
1489                                         spin_unlock_irqrestore(&priv->driver_lock, flags);
1490                                         priv->needtowakeup = 1;
1491
1492                                         ret = 0;
1493                                         goto done;
1494                                 }
1495
1496                                 lbs_deb_host(
1497                                        "EXEC_NEXT_CMD: sending EXIT_PS\n");
1498                         }
1499                 }
1500                 list_del(&cmdnode->list);
1501                 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1502                             le16_to_cpu(cmd->command));
1503                 lbs_submit_command(priv, cmdnode);
1504         } else {
1505                 /*
1506                  * check if in power save mode, if yes, put the device back
1507                  * to PS mode
1508                  */
1509 #ifdef TODO
1510                 /*
1511                  * This was the old code for libertas+wext. Someone that
1512                  * understands this beast should re-code it in a sane way.
1513                  *
1514                  * I actually don't understand why this is related to WPA
1515                  * and to connection status, shouldn't powering should be
1516                  * independ of such things?
1517                  */
1518                 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1519                     (priv->psstate == PS_STATE_FULL_POWER) &&
1520                     ((priv->connect_status == LBS_CONNECTED) ||
1521                     lbs_mesh_connected(priv))) {
1522                         if (priv->secinfo.WPAenabled ||
1523                             priv->secinfo.WPA2enabled) {
1524                                 /* check for valid WPA group keys */
1525                                 if (priv->wpa_mcast_key.len ||
1526                                     priv->wpa_unicast_key.len) {
1527                                         lbs_deb_host(
1528                                                "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1529                                                " go back to PS_SLEEP");
1530                                         lbs_ps_sleep(priv, 0);
1531                                 }
1532                         } else {
1533                                 lbs_deb_host(
1534                                        "EXEC_NEXT_CMD: cmdpendingq empty, "
1535                                        "go back to PS_SLEEP");
1536                                 lbs_ps_sleep(priv, 0);
1537                         }
1538                 }
1539 #endif
1540         }
1541
1542         ret = 0;
1543 done:
1544         lbs_deb_leave(LBS_DEB_THREAD);
1545         return ret;
1546 }
1547
1548 static void lbs_send_confirmsleep(struct lbs_private *priv)
1549 {
1550         unsigned long flags;
1551         int ret;
1552
1553         lbs_deb_enter(LBS_DEB_HOST);
1554         lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1555                 sizeof(confirm_sleep));
1556
1557         ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1558                 sizeof(confirm_sleep));
1559         if (ret) {
1560                 lbs_pr_alert("confirm_sleep failed\n");
1561                 goto out;
1562         }
1563
1564         spin_lock_irqsave(&priv->driver_lock, flags);
1565
1566         /* We don't get a response on the sleep-confirmation */
1567         priv->dnld_sent = DNLD_RES_RECEIVED;
1568
1569         if (priv->is_host_sleep_configured) {
1570                 priv->is_host_sleep_activated = 1;
1571                 wake_up_interruptible(&priv->host_sleep_q);
1572         }
1573
1574         /* If nothing to do, go back to sleep (?) */
1575         if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1576                 priv->psstate = PS_STATE_SLEEP;
1577
1578         spin_unlock_irqrestore(&priv->driver_lock, flags);
1579
1580 out:
1581         lbs_deb_leave(LBS_DEB_HOST);
1582 }
1583
1584 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1585 {
1586         lbs_deb_enter(LBS_DEB_HOST);
1587
1588         /*
1589          * PS is currently supported only in Infrastructure mode
1590          * Remove this check if it is to be supported in IBSS mode also
1591          */
1592
1593         lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1594                               CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1595
1596         lbs_deb_leave(LBS_DEB_HOST);
1597 }
1598
1599 /**
1600  *  @brief This function sends Exit_PS command to firmware.
1601  *
1602  *  @param priv         A pointer to struct lbs_private structure
1603  *  @param wait_option  wait response or not
1604  *  @return             n/a
1605  */
1606 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1607 {
1608         __le32 Localpsmode;
1609
1610         lbs_deb_enter(LBS_DEB_HOST);
1611
1612         Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1613
1614         lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1615                               CMD_SUBCMD_EXIT_PS,
1616                               wait_option, 0, &Localpsmode);
1617
1618         lbs_deb_leave(LBS_DEB_HOST);
1619 }
1620
1621 /**
1622  *  @brief This function checks condition and prepares to
1623  *  send sleep confirm command to firmware if ok.
1624  *
1625  *  @param priv         A pointer to struct lbs_private structure
1626  *  @param psmode       Power Saving mode
1627  *  @return             n/a
1628  */
1629 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1630 {
1631         unsigned long flags =0;
1632         int allowed = 1;
1633
1634         lbs_deb_enter(LBS_DEB_HOST);
1635
1636         spin_lock_irqsave(&priv->driver_lock, flags);
1637         if (priv->dnld_sent) {
1638                 allowed = 0;
1639                 lbs_deb_host("dnld_sent was set\n");
1640         }
1641
1642         /* In-progress command? */
1643         if (priv->cur_cmd) {
1644                 allowed = 0;
1645                 lbs_deb_host("cur_cmd was set\n");
1646         }
1647
1648         /* Pending events or command responses? */
1649         if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1650                 allowed = 0;
1651                 lbs_deb_host("pending events or command responses\n");
1652         }
1653         spin_unlock_irqrestore(&priv->driver_lock, flags);
1654
1655         if (allowed) {
1656                 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1657                 lbs_send_confirmsleep(priv);
1658         } else {
1659                 lbs_deb_host("sleep confirm has been delayed\n");
1660         }
1661
1662         lbs_deb_leave(LBS_DEB_HOST);
1663 }
1664
1665
1666 /**
1667  * @brief Configures the transmission power control functionality.
1668  *
1669  * @param priv          A pointer to struct lbs_private structure
1670  * @param enable        Transmission power control enable
1671  * @param p0            Power level when link quality is good (dBm).
1672  * @param p1            Power level when link quality is fair (dBm).
1673  * @param p2            Power level when link quality is poor (dBm).
1674  * @param usesnr        Use Signal to Noise Ratio in TPC
1675  *
1676  * @return 0 on success
1677  */
1678 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1679                 int8_t p2, int usesnr)
1680 {
1681         struct cmd_ds_802_11_tpc_cfg cmd;
1682         int ret;
1683
1684         memset(&cmd, 0, sizeof(cmd));
1685         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1686         cmd.action = cpu_to_le16(CMD_ACT_SET);
1687         cmd.enable = !!enable;
1688         cmd.usesnr = !!usesnr;
1689         cmd.P0 = p0;
1690         cmd.P1 = p1;
1691         cmd.P2 = p2;
1692
1693         ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1694
1695         return ret;
1696 }
1697
1698 /**
1699  * @brief Configures the power adaptation settings.
1700  *
1701  * @param priv          A pointer to struct lbs_private structure
1702  * @param enable        Power adaptation enable
1703  * @param p0            Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1704  * @param p1            Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1705  * @param p2            Power level for 48 and 54 Mbps (dBm).
1706  *
1707  * @return 0 on Success
1708  */
1709
1710 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1711                 int8_t p1, int8_t p2)
1712 {
1713         struct cmd_ds_802_11_pa_cfg cmd;
1714         int ret;
1715
1716         memset(&cmd, 0, sizeof(cmd));
1717         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1718         cmd.action = cpu_to_le16(CMD_ACT_SET);
1719         cmd.enable = !!enable;
1720         cmd.P0 = p0;
1721         cmd.P1 = p1;
1722         cmd.P2 = p2;
1723
1724         ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1725
1726         return ret;
1727 }
1728
1729
1730 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1731         uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1732         int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1733         unsigned long callback_arg)
1734 {
1735         struct cmd_ctrl_node *cmdnode;
1736
1737         lbs_deb_enter(LBS_DEB_HOST);
1738
1739         if (priv->surpriseremoved) {
1740                 lbs_deb_host("PREP_CMD: card removed\n");
1741                 cmdnode = ERR_PTR(-ENOENT);
1742                 goto done;
1743         }
1744
1745         if (!lbs_is_cmd_allowed(priv)) {
1746                 cmdnode = ERR_PTR(-EBUSY);
1747                 goto done;
1748         }
1749
1750         cmdnode = lbs_get_cmd_ctrl_node(priv);
1751         if (cmdnode == NULL) {
1752                 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1753
1754                 /* Wake up main thread to execute next command */
1755                 wake_up_interruptible(&priv->waitq);
1756                 cmdnode = ERR_PTR(-ENOBUFS);
1757                 goto done;
1758         }
1759
1760         cmdnode->callback = callback;
1761         cmdnode->callback_arg = callback_arg;
1762
1763         /* Copy the incoming command to the buffer */
1764         memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1765
1766         /* Set sequence number, clean result, move to buffer */
1767         priv->seqnum++;
1768         cmdnode->cmdbuf->command = cpu_to_le16(command);
1769         cmdnode->cmdbuf->size    = cpu_to_le16(in_cmd_size);
1770         cmdnode->cmdbuf->seqnum  = cpu_to_le16(priv->seqnum);
1771         cmdnode->cmdbuf->result  = 0;
1772
1773         lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1774
1775         cmdnode->cmdwaitqwoken = 0;
1776         lbs_queue_cmd(priv, cmdnode);
1777         wake_up_interruptible(&priv->waitq);
1778
1779  done:
1780         lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1781         return cmdnode;
1782 }
1783
1784 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1785         struct cmd_header *in_cmd, int in_cmd_size)
1786 {
1787         lbs_deb_enter(LBS_DEB_CMD);
1788         __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1789                 lbs_cmd_async_callback, 0);
1790         lbs_deb_leave(LBS_DEB_CMD);
1791 }
1792
1793 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1794               struct cmd_header *in_cmd, int in_cmd_size,
1795               int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1796               unsigned long callback_arg)
1797 {
1798         struct cmd_ctrl_node *cmdnode;
1799         unsigned long flags;
1800         int ret = 0;
1801
1802         lbs_deb_enter(LBS_DEB_HOST);
1803
1804         cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1805                                   callback, callback_arg);
1806         if (IS_ERR(cmdnode)) {
1807                 ret = PTR_ERR(cmdnode);
1808                 goto done;
1809         }
1810
1811         might_sleep();
1812         wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1813
1814         spin_lock_irqsave(&priv->driver_lock, flags);
1815         ret = cmdnode->result;
1816         if (ret)
1817                 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1818                             command, ret);
1819
1820         __lbs_cleanup_and_insert_cmd(priv, cmdnode);
1821         spin_unlock_irqrestore(&priv->driver_lock, flags);
1822
1823 done:
1824         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1825         return ret;
1826 }
1827 EXPORT_SYMBOL_GPL(__lbs_cmd);