]> git.kernelconcepts.de Git - mv-sheeva.git/blob - drivers/net/wireless/libertas/cmd.c
libertas: convert DEEP_SLEEP timer to a direct command
[mv-sheeva.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 /**
234  *  @brief Sets the Power Save mode
235  *
236  *  @param priv         A pointer to struct lbs_private structure
237  *  @param cmd_action   The Power Save operation (PS_MODE_ACTION_ENTER_PS or
238  *                         PS_MODE_ACTION_EXIT_PS)
239  *  @param block        Whether to block on a response or not
240  *
241  *  @return             0 on success, error on failure
242  */
243 int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
244 {
245         struct cmd_ds_802_11_ps_mode cmd;
246         int ret = 0;
247
248         lbs_deb_enter(LBS_DEB_CMD);
249
250         memset(&cmd, 0, sizeof(cmd));
251         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
252         cmd.action = cpu_to_le16(cmd_action);
253
254         if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
255                 lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
256                 cmd.multipledtim = cpu_to_le16(1);  /* Default DTIM multiple */
257         } else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
258                 lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
259         } else {
260                 /* We don't handle CONFIRM_SLEEP here because it needs to
261                  * be fastpathed to the firmware.
262                  */
263                 lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
264                 ret = -EOPNOTSUPP;
265                 goto out;
266         }
267
268         if (block)
269                 ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
270         else
271                 lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
272
273 out:
274         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
275         return ret;
276 }
277
278 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
279                                 struct sleep_params *sp)
280 {
281         struct cmd_ds_802_11_sleep_params cmd;
282         int ret;
283
284         lbs_deb_enter(LBS_DEB_CMD);
285
286         if (cmd_action == CMD_ACT_GET) {
287                 memset(&cmd, 0, sizeof(cmd));
288         } else {
289                 cmd.error = cpu_to_le16(sp->sp_error);
290                 cmd.offset = cpu_to_le16(sp->sp_offset);
291                 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
292                 cmd.calcontrol = sp->sp_calcontrol;
293                 cmd.externalsleepclk = sp->sp_extsleepclk;
294                 cmd.reserved = cpu_to_le16(sp->sp_reserved);
295         }
296         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
297         cmd.action = cpu_to_le16(cmd_action);
298
299         ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
300
301         if (!ret) {
302                 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
303                             "calcontrol 0x%x extsleepclk 0x%x\n",
304                             le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
305                             le16_to_cpu(cmd.stabletime), cmd.calcontrol,
306                             cmd.externalsleepclk);
307
308                 sp->sp_error = le16_to_cpu(cmd.error);
309                 sp->sp_offset = le16_to_cpu(cmd.offset);
310                 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
311                 sp->sp_calcontrol = cmd.calcontrol;
312                 sp->sp_extsleepclk = cmd.externalsleepclk;
313                 sp->sp_reserved = le16_to_cpu(cmd.reserved);
314         }
315
316         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
317         return 0;
318 }
319
320 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
321 {
322         int ret = 0;
323
324         lbs_deb_enter(LBS_DEB_CMD);
325
326         if (priv->is_deep_sleep) {
327                 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
328                                         !priv->is_deep_sleep, (10 * HZ))) {
329                         lbs_pr_err("ds_awake_q: timer expired\n");
330                         ret = -1;
331                 }
332         }
333
334         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
335         return ret;
336 }
337
338 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
339 {
340         int ret =  0;
341
342         lbs_deb_enter(LBS_DEB_CMD);
343
344         if (deep_sleep) {
345                 if (priv->is_deep_sleep != 1) {
346                         lbs_deb_cmd("deep sleep: sleep\n");
347                         BUG_ON(!priv->enter_deep_sleep);
348                         ret = priv->enter_deep_sleep(priv);
349                         if (!ret) {
350                                 netif_stop_queue(priv->dev);
351                                 netif_carrier_off(priv->dev);
352                         }
353                 } else {
354                         lbs_pr_err("deep sleep: already enabled\n");
355                 }
356         } else {
357                 if (priv->is_deep_sleep) {
358                         lbs_deb_cmd("deep sleep: wakeup\n");
359                         BUG_ON(!priv->exit_deep_sleep);
360                         ret = priv->exit_deep_sleep(priv);
361                         if (!ret) {
362                                 ret = lbs_wait_for_ds_awake(priv);
363                                 if (ret)
364                                         lbs_pr_err("deep sleep: wakeup"
365                                                         "failed\n");
366                         }
367                 }
368         }
369
370         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
371         return ret;
372 }
373
374 static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
375                 unsigned long dummy,
376                 struct cmd_header *cmd)
377 {
378         lbs_deb_enter(LBS_DEB_FW);
379         priv->is_host_sleep_activated = 1;
380         wake_up_interruptible(&priv->host_sleep_q);
381         lbs_deb_leave(LBS_DEB_FW);
382         return 0;
383 }
384
385 int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
386 {
387         struct cmd_header cmd;
388         int ret = 0;
389         uint32_t criteria = EHS_REMOVE_WAKEUP;
390
391         lbs_deb_enter(LBS_DEB_CMD);
392
393         if (host_sleep) {
394                 if (priv->is_host_sleep_activated != 1) {
395                         memset(&cmd, 0, sizeof(cmd));
396                         ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
397                                         (struct wol_config *)NULL);
398                         if (ret) {
399                                 lbs_pr_info("Host sleep configuration failed: "
400                                                 "%d\n", ret);
401                                 return ret;
402                         }
403                         if (priv->psstate == PS_STATE_FULL_POWER) {
404                                 ret = __lbs_cmd(priv,
405                                                 CMD_802_11_HOST_SLEEP_ACTIVATE,
406                                                 &cmd,
407                                                 sizeof(cmd),
408                                                 lbs_ret_host_sleep_activate, 0);
409                                 if (ret)
410                                         lbs_pr_info("HOST_SLEEP_ACTIVATE "
411                                                         "failed: %d\n", ret);
412                         }
413
414                         if (!wait_event_interruptible_timeout(
415                                                 priv->host_sleep_q,
416                                                 priv->is_host_sleep_activated,
417                                                 (10 * HZ))) {
418                                 lbs_pr_err("host_sleep_q: timer expired\n");
419                                 ret = -1;
420                         }
421                 } else {
422                         lbs_pr_err("host sleep: already enabled\n");
423                 }
424         } else {
425                 if (priv->is_host_sleep_activated)
426                         ret = lbs_host_sleep_cfg(priv, criteria,
427                                         (struct wol_config *)NULL);
428         }
429
430         return ret;
431 }
432
433 /**
434  *  @brief Set an SNMP MIB value
435  *
436  *  @param priv         A pointer to struct lbs_private structure
437  *  @param oid          The OID to set in the firmware
438  *  @param val          Value to set the OID to
439  *
440  *  @return             0 on success, error on failure
441  */
442 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
443 {
444         struct cmd_ds_802_11_snmp_mib cmd;
445         int ret;
446
447         lbs_deb_enter(LBS_DEB_CMD);
448
449         memset(&cmd, 0, sizeof (cmd));
450         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
451         cmd.action = cpu_to_le16(CMD_ACT_SET);
452         cmd.oid = cpu_to_le16((u16) oid);
453
454         switch (oid) {
455         case SNMP_MIB_OID_BSS_TYPE:
456                 cmd.bufsize = cpu_to_le16(sizeof(u8));
457                 cmd.value[0] = val;
458                 break;
459         case SNMP_MIB_OID_11D_ENABLE:
460         case SNMP_MIB_OID_FRAG_THRESHOLD:
461         case SNMP_MIB_OID_RTS_THRESHOLD:
462         case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
463         case SNMP_MIB_OID_LONG_RETRY_LIMIT:
464                 cmd.bufsize = cpu_to_le16(sizeof(u16));
465                 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
466                 break;
467         default:
468                 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
469                 ret = -EINVAL;
470                 goto out;
471         }
472
473         lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
474                     le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
475
476         ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
477
478 out:
479         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
480         return ret;
481 }
482
483 /**
484  *  @brief Get an SNMP MIB value
485  *
486  *  @param priv         A pointer to struct lbs_private structure
487  *  @param oid          The OID to retrieve from the firmware
488  *  @param out_val      Location for the returned value
489  *
490  *  @return             0 on success, error on failure
491  */
492 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
493 {
494         struct cmd_ds_802_11_snmp_mib cmd;
495         int ret;
496
497         lbs_deb_enter(LBS_DEB_CMD);
498
499         memset(&cmd, 0, sizeof (cmd));
500         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
501         cmd.action = cpu_to_le16(CMD_ACT_GET);
502         cmd.oid = cpu_to_le16(oid);
503
504         ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
505         if (ret)
506                 goto out;
507
508         switch (le16_to_cpu(cmd.bufsize)) {
509         case sizeof(u8):
510                 *out_val = cmd.value[0];
511                 break;
512         case sizeof(u16):
513                 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
514                 break;
515         default:
516                 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
517                             oid, le16_to_cpu(cmd.bufsize));
518                 break;
519         }
520
521 out:
522         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
523         return ret;
524 }
525
526 /**
527  *  @brief Get the min, max, and current TX power
528  *
529  *  @param priv         A pointer to struct lbs_private structure
530  *  @param curlevel     Current power level in dBm
531  *  @param minlevel     Minimum supported power level in dBm (optional)
532  *  @param maxlevel     Maximum supported power level in dBm (optional)
533  *
534  *  @return             0 on success, error on failure
535  */
536 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
537                      s16 *maxlevel)
538 {
539         struct cmd_ds_802_11_rf_tx_power cmd;
540         int ret;
541
542         lbs_deb_enter(LBS_DEB_CMD);
543
544         memset(&cmd, 0, sizeof(cmd));
545         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
546         cmd.action = cpu_to_le16(CMD_ACT_GET);
547
548         ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
549         if (ret == 0) {
550                 *curlevel = le16_to_cpu(cmd.curlevel);
551                 if (minlevel)
552                         *minlevel = cmd.minlevel;
553                 if (maxlevel)
554                         *maxlevel = cmd.maxlevel;
555         }
556
557         lbs_deb_leave(LBS_DEB_CMD);
558         return ret;
559 }
560
561 /**
562  *  @brief Set the TX power
563  *
564  *  @param priv         A pointer to struct lbs_private structure
565  *  @param dbm          The desired power level in dBm
566  *
567  *  @return             0 on success, error on failure
568  */
569 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
570 {
571         struct cmd_ds_802_11_rf_tx_power cmd;
572         int ret;
573
574         lbs_deb_enter(LBS_DEB_CMD);
575
576         memset(&cmd, 0, sizeof(cmd));
577         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
578         cmd.action = cpu_to_le16(CMD_ACT_SET);
579         cmd.curlevel = cpu_to_le16(dbm);
580
581         lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
582
583         ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
584
585         lbs_deb_leave(LBS_DEB_CMD);
586         return ret;
587 }
588
589 /**
590  *  @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
591  *
592  *  @param priv        A pointer to struct lbs_private structure
593  *  @param enable      1 to enable monitor mode, 0 to disable
594  *
595  *  @return            0 on success, error on failure
596  */
597 int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
598 {
599         struct cmd_ds_802_11_monitor_mode cmd;
600         int ret;
601
602         memset(&cmd, 0, sizeof(cmd));
603         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
604         cmd.action = cpu_to_le16(CMD_ACT_SET);
605         if (enable)
606                 cmd.mode = cpu_to_le16(0x1);
607
608         lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
609
610         ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
611         if (ret == 0) {
612                 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
613                                                 ARPHRD_ETHER;
614         }
615
616         lbs_deb_leave(LBS_DEB_CMD);
617         return ret;
618 }
619
620 /**
621  *  @brief Get the radio channel
622  *
623  *  @param priv         A pointer to struct lbs_private structure
624  *
625  *  @return             The channel on success, error on failure
626  */
627 static int lbs_get_channel(struct lbs_private *priv)
628 {
629         struct cmd_ds_802_11_rf_channel cmd;
630         int ret = 0;
631
632         lbs_deb_enter(LBS_DEB_CMD);
633
634         memset(&cmd, 0, sizeof(cmd));
635         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
636         cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
637
638         ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
639         if (ret)
640                 goto out;
641
642         ret = le16_to_cpu(cmd.channel);
643         lbs_deb_cmd("current radio channel is %d\n", ret);
644
645 out:
646         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
647         return ret;
648 }
649
650 int lbs_update_channel(struct lbs_private *priv)
651 {
652         int ret;
653
654         /* the channel in f/w could be out of sync; get the current channel */
655         lbs_deb_enter(LBS_DEB_ASSOC);
656
657         ret = lbs_get_channel(priv);
658         if (ret > 0) {
659                 priv->channel = ret;
660                 ret = 0;
661         }
662         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
663         return ret;
664 }
665
666 /**
667  *  @brief Set the radio channel
668  *
669  *  @param priv         A pointer to struct lbs_private structure
670  *  @param channel      The desired channel, or 0 to clear a locked channel
671  *
672  *  @return             0 on success, error on failure
673  */
674 int lbs_set_channel(struct lbs_private *priv, u8 channel)
675 {
676         struct cmd_ds_802_11_rf_channel cmd;
677 #ifdef DEBUG
678         u8 old_channel = priv->channel;
679 #endif
680         int ret = 0;
681
682         lbs_deb_enter(LBS_DEB_CMD);
683
684         memset(&cmd, 0, sizeof(cmd));
685         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
686         cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
687         cmd.channel = cpu_to_le16(channel);
688
689         ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
690         if (ret)
691                 goto out;
692
693         priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
694         lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
695                 priv->channel);
696
697 out:
698         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
699         return ret;
700 }
701
702 /**
703  *  @brief Get current RSSI and noise floor
704  *
705  *  @param priv         A pointer to struct lbs_private structure
706  *  @param rssi         On successful return, signal level in mBm
707  *
708  *  @return             The channel on success, error on failure
709  */
710 int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
711 {
712         struct cmd_ds_802_11_rssi cmd;
713         int ret = 0;
714
715         lbs_deb_enter(LBS_DEB_CMD);
716
717         BUG_ON(rssi == NULL);
718         BUG_ON(nf == NULL);
719
720         memset(&cmd, 0, sizeof(cmd));
721         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
722         /* Average SNR over last 8 beacons */
723         cmd.n_or_snr = cpu_to_le16(8);
724
725         ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
726         if (ret == 0) {
727                 *nf = CAL_NF(le16_to_cpu(cmd.nf));
728                 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
729         }
730
731         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
732         return ret;
733 }
734
735 /**
736  *  @brief Send regulatory and 802.11d domain information to the firmware
737  *
738  *  @param priv         pointer to struct lbs_private
739  *  @param request      cfg80211 regulatory request structure
740  *  @param bands        the device's supported bands and channels
741  *
742  *  @return             0 on success, error code on failure
743 */
744 int lbs_set_11d_domain_info(struct lbs_private *priv,
745                             struct regulatory_request *request,
746                             struct ieee80211_supported_band **bands)
747 {
748         struct cmd_ds_802_11d_domain_info cmd;
749         struct mrvl_ie_domain_param_set *domain = &cmd.domain;
750         struct ieee80211_country_ie_triplet *t;
751         enum ieee80211_band band;
752         struct ieee80211_channel *ch;
753         u8 num_triplet = 0;
754         u8 num_parsed_chan = 0;
755         u8 first_channel = 0, next_chan = 0, max_pwr = 0;
756         u8 i, flag = 0;
757         size_t triplet_size;
758         int ret;
759
760         lbs_deb_enter(LBS_DEB_11D);
761
762         memset(&cmd, 0, sizeof(cmd));
763         cmd.action = cpu_to_le16(CMD_ACT_SET);
764
765         lbs_deb_11d("Setting country code '%c%c'\n",
766                     request->alpha2[0], request->alpha2[1]);
767
768         domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
769
770         /* Set country code */
771         domain->country_code[0] = request->alpha2[0];
772         domain->country_code[1] = request->alpha2[1];
773         domain->country_code[2] = ' ';
774
775         /* Now set up the channel triplets; firmware is somewhat picky here
776          * and doesn't validate channel numbers and spans; hence it would
777          * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39.  Since
778          * the last 3 aren't valid channels, the driver is responsible for
779          * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
780          * etc.
781          */
782         for (band = 0;
783              (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
784              band++) {
785
786                 if (!bands[band])
787                         continue;
788
789                 for (i = 0;
790                      (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
791                      i++) {
792                         ch = &bands[band]->channels[i];
793                         if (ch->flags & IEEE80211_CHAN_DISABLED)
794                                 continue;
795
796                         if (!flag) {
797                                 flag = 1;
798                                 next_chan = first_channel = (u32) ch->hw_value;
799                                 max_pwr = ch->max_power;
800                                 num_parsed_chan = 1;
801                                 continue;
802                         }
803
804                         if ((ch->hw_value == next_chan + 1) &&
805                                         (ch->max_power == max_pwr)) {
806                                 /* Consolidate adjacent channels */
807                                 next_chan++;
808                                 num_parsed_chan++;
809                         } else {
810                                 /* Add this triplet */
811                                 lbs_deb_11d("11D triplet (%d, %d, %d)\n",
812                                         first_channel, num_parsed_chan,
813                                         max_pwr);
814                                 t = &domain->triplet[num_triplet];
815                                 t->chans.first_channel = first_channel;
816                                 t->chans.num_channels = num_parsed_chan;
817                                 t->chans.max_power = max_pwr;
818                                 num_triplet++;
819                                 flag = 0;
820                         }
821                 }
822
823                 if (flag) {
824                         /* Add last triplet */
825                         lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
826                                 num_parsed_chan, max_pwr);
827                         t = &domain->triplet[num_triplet];
828                         t->chans.first_channel = first_channel;
829                         t->chans.num_channels = num_parsed_chan;
830                         t->chans.max_power = max_pwr;
831                         num_triplet++;
832                 }
833         }
834
835         lbs_deb_11d("# triplets %d\n", num_triplet);
836
837         /* Set command header sizes */
838         triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
839         domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
840                                         triplet_size);
841
842         lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
843                         (u8 *) &cmd.domain.country_code,
844                         le16_to_cpu(domain->header.len));
845
846         cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
847                                    sizeof(cmd.action) +
848                                    sizeof(cmd.domain.header) +
849                                    sizeof(cmd.domain.country_code) +
850                                    triplet_size);
851
852         ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
853
854         lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
855         return ret;
856 }
857
858 /**
859  *  @brief Read a MAC, Baseband, or RF register
860  *
861  *  @param priv         pointer to struct lbs_private
862  *  @param cmd          register command, one of CMD_MAC_REG_ACCESS,
863  *                        CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
864  *  @param offset       byte offset of the register to get
865  *  @param value        on success, the value of the register at 'offset'
866  *
867  *  @return             0 on success, error code on failure
868 */
869 int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
870 {
871         struct cmd_ds_reg_access cmd;
872         int ret = 0;
873
874         lbs_deb_enter(LBS_DEB_CMD);
875
876         BUG_ON(value == NULL);
877
878         memset(&cmd, 0, sizeof(cmd));
879         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
880         cmd.action = cpu_to_le16(CMD_ACT_GET);
881
882         if (reg != CMD_MAC_REG_ACCESS &&
883             reg != CMD_BBP_REG_ACCESS &&
884             reg != CMD_RF_REG_ACCESS) {
885                 ret = -EINVAL;
886                 goto out;
887         }
888
889         ret = lbs_cmd_with_response(priv, reg, &cmd);
890         if (ret) {
891                 if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
892                         *value = cmd.value.bbp_rf;
893                 else if (reg == CMD_MAC_REG_ACCESS)
894                         *value = le32_to_cpu(cmd.value.mac);
895         }
896
897 out:
898         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
899         return ret;
900 }
901
902 /**
903  *  @brief Write a MAC, Baseband, or RF register
904  *
905  *  @param priv         pointer to struct lbs_private
906  *  @param cmd          register command, one of CMD_MAC_REG_ACCESS,
907  *                        CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
908  *  @param offset       byte offset of the register to set
909  *  @param value        the value to write to the register at 'offset'
910  *
911  *  @return             0 on success, error code on failure
912 */
913 int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
914 {
915         struct cmd_ds_reg_access cmd;
916         int ret = 0;
917
918         lbs_deb_enter(LBS_DEB_CMD);
919
920         memset(&cmd, 0, sizeof(cmd));
921         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
922         cmd.action = cpu_to_le16(CMD_ACT_SET);
923
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;
931         }
932
933         ret = lbs_cmd_with_response(priv, reg, &cmd);
934
935 out:
936         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
937         return ret;
938 }
939
940 static void lbs_queue_cmd(struct lbs_private *priv,
941                           struct cmd_ctrl_node *cmdnode)
942 {
943         unsigned long flags;
944         int addtail = 1;
945
946         lbs_deb_enter(LBS_DEB_HOST);
947
948         if (!cmdnode) {
949                 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
950                 goto done;
951         }
952         if (!cmdnode->cmdbuf->size) {
953                 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
954                 goto done;
955         }
956         cmdnode->result = 0;
957
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;
961
962                 if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
963                         if (priv->psstate != PS_STATE_FULL_POWER)
964                                 addtail = 0;
965                 }
966         }
967
968         if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
969                 addtail = 0;
970
971         spin_lock_irqsave(&priv->driver_lock, flags);
972
973         if (addtail)
974                 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
975         else
976                 list_add(&cmdnode->list, &priv->cmdpendingq);
977
978         spin_unlock_irqrestore(&priv->driver_lock, flags);
979
980         lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
981                      le16_to_cpu(cmdnode->cmdbuf->command));
982
983 done:
984         lbs_deb_leave(LBS_DEB_HOST);
985 }
986
987 static void lbs_submit_command(struct lbs_private *priv,
988                                struct cmd_ctrl_node *cmdnode)
989 {
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;
996
997         lbs_deb_enter(LBS_DEB_HOST);
998
999         cmd = cmdnode->cmdbuf;
1000
1001         spin_lock_irqsave(&priv->driver_lock, flags);
1002         priv->cur_cmd = cmdnode;
1003         priv->cur_cmd_retcode = 0;
1004         spin_unlock_irqrestore(&priv->driver_lock, flags);
1005
1006         cmdsize = le16_to_cpu(cmd->size);
1007         command = le16_to_cpu(cmd->command);
1008
1009         /* These commands take longer */
1010         if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
1011                 timeo = 5 * HZ;
1012
1013         lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
1014                      command, le16_to_cpu(cmd->seqnum), cmdsize);
1015         lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
1016
1017         ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
1018
1019         if (ret) {
1020                 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
1021                 /* Let the timer kick in and retry, and potentially reset
1022                    the whole thing if the condition persists */
1023                 timeo = HZ/4;
1024         }
1025
1026         if (command == CMD_802_11_DEEP_SLEEP) {
1027                 if (priv->is_auto_deep_sleep_enabled) {
1028                         priv->wakeup_dev_required = 1;
1029                         priv->dnld_sent = 0;
1030                 }
1031                 priv->is_deep_sleep = 1;
1032                 lbs_complete_command(priv, cmdnode, 0);
1033         } else {
1034                 /* Setup the timer after transmit command */
1035                 mod_timer(&priv->command_timer, jiffies + timeo);
1036         }
1037
1038         lbs_deb_leave(LBS_DEB_HOST);
1039 }
1040
1041 /**
1042  *  This function inserts command node to cmdfreeq
1043  *  after cleans it. Requires priv->driver_lock held.
1044  */
1045 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1046                                          struct cmd_ctrl_node *cmdnode)
1047 {
1048         lbs_deb_enter(LBS_DEB_HOST);
1049
1050         if (!cmdnode)
1051                 goto out;
1052
1053         cmdnode->callback = NULL;
1054         cmdnode->callback_arg = 0;
1055
1056         memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
1057
1058         list_add_tail(&cmdnode->list, &priv->cmdfreeq);
1059  out:
1060         lbs_deb_leave(LBS_DEB_HOST);
1061 }
1062
1063 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
1064         struct cmd_ctrl_node *ptempcmd)
1065 {
1066         unsigned long flags;
1067
1068         spin_lock_irqsave(&priv->driver_lock, flags);
1069         __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1070         spin_unlock_irqrestore(&priv->driver_lock, flags);
1071 }
1072
1073 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
1074                           int result)
1075 {
1076         if (cmd == priv->cur_cmd)
1077                 priv->cur_cmd_retcode = result;
1078
1079         cmd->result = result;
1080         cmd->cmdwaitqwoken = 1;
1081         wake_up_interruptible(&cmd->cmdwait_q);
1082
1083         if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
1084                 __lbs_cleanup_and_insert_cmd(priv, cmd);
1085         priv->cur_cmd = NULL;
1086 }
1087
1088 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
1089 {
1090         struct cmd_ds_802_11_radio_control cmd;
1091         int ret = -EINVAL;
1092
1093         lbs_deb_enter(LBS_DEB_CMD);
1094
1095         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1096         cmd.action = cpu_to_le16(CMD_ACT_SET);
1097
1098         /* Only v8 and below support setting the preamble */
1099         if (priv->fwrelease < 0x09000000) {
1100                 switch (preamble) {
1101                 case RADIO_PREAMBLE_SHORT:
1102                 case RADIO_PREAMBLE_AUTO:
1103                 case RADIO_PREAMBLE_LONG:
1104                         cmd.control = cpu_to_le16(preamble);
1105                         break;
1106                 default:
1107                         goto out;
1108                 }
1109         }
1110
1111         if (radio_on)
1112                 cmd.control |= cpu_to_le16(0x1);
1113         else {
1114                 cmd.control &= cpu_to_le16(~0x1);
1115                 priv->txpower_cur = 0;
1116         }
1117
1118         lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1119                     radio_on ? "ON" : "OFF", preamble);
1120
1121         priv->radio_on = radio_on;
1122
1123         ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1124
1125 out:
1126         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1127         return ret;
1128 }
1129
1130 void lbs_set_mac_control(struct lbs_private *priv)
1131 {
1132         struct cmd_ds_mac_control cmd;
1133
1134         lbs_deb_enter(LBS_DEB_CMD);
1135
1136         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1137         cmd.action = cpu_to_le16(priv->mac_control);
1138         cmd.reserved = 0;
1139
1140         lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1141
1142         lbs_deb_leave(LBS_DEB_CMD);
1143 }
1144
1145 /**
1146  *  @brief This function prepare the command before send to firmware.
1147  *
1148  *  @param priv         A pointer to struct lbs_private structure
1149  *  @param cmd_no       command number
1150  *  @param cmd_action   command action: GET or SET
1151  *  @param wait_option  wait option: wait response or not
1152  *  @param cmd_oid      cmd oid: treated as sub command
1153  *  @param pdata_buf    A pointer to informaion buffer
1154  *  @return             0 or -1
1155  */
1156 int lbs_prepare_and_send_command(struct lbs_private *priv,
1157                           u16 cmd_no,
1158                           u16 cmd_action,
1159                           u16 wait_option, u32 cmd_oid, void *pdata_buf)
1160 {
1161         int ret = 0;
1162         struct cmd_ctrl_node *cmdnode;
1163         struct cmd_header *cmdptr;
1164         unsigned long flags;
1165
1166         lbs_deb_enter(LBS_DEB_HOST);
1167
1168         if (!priv) {
1169                 lbs_deb_host("PREP_CMD: priv is NULL\n");
1170                 ret = -1;
1171                 goto done;
1172         }
1173
1174         if (priv->surpriseremoved) {
1175                 lbs_deb_host("PREP_CMD: card removed\n");
1176                 ret = -1;
1177                 goto done;
1178         }
1179
1180         if (!lbs_is_cmd_allowed(priv)) {
1181                 ret = -EBUSY;
1182                 goto done;
1183         }
1184
1185         cmdnode = lbs_get_cmd_ctrl_node(priv);
1186
1187         if (cmdnode == NULL) {
1188                 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1189
1190                 /* Wake up main thread to execute next command */
1191                 wake_up_interruptible(&priv->waitq);
1192                 ret = -1;
1193                 goto done;
1194         }
1195
1196         cmdnode->callback = NULL;
1197         cmdnode->callback_arg = (unsigned long)pdata_buf;
1198
1199         cmdptr = (struct cmd_header *)cmdnode->cmdbuf;
1200
1201         lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1202
1203         /* Set sequence number, command and INT option */
1204         priv->seqnum++;
1205         cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1206
1207         cmdptr->command = cpu_to_le16(cmd_no);
1208         cmdptr->result = 0;
1209
1210         switch (cmd_no) {
1211         default:
1212                 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1213                 ret = -1;
1214                 break;
1215         }
1216
1217         /* return error, since the command preparation failed */
1218         if (ret != 0) {
1219                 lbs_deb_host("PREP_CMD: command preparation failed\n");
1220                 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1221                 ret = -1;
1222                 goto done;
1223         }
1224
1225         cmdnode->cmdwaitqwoken = 0;
1226
1227         lbs_queue_cmd(priv, cmdnode);
1228         wake_up_interruptible(&priv->waitq);
1229
1230         if (wait_option & CMD_OPTION_WAITFORRSP) {
1231                 lbs_deb_host("PREP_CMD: wait for response\n");
1232                 might_sleep();
1233                 wait_event_interruptible(cmdnode->cmdwait_q,
1234                                          cmdnode->cmdwaitqwoken);
1235         }
1236
1237         spin_lock_irqsave(&priv->driver_lock, flags);
1238         if (priv->cur_cmd_retcode) {
1239                 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1240                        priv->cur_cmd_retcode);
1241                 priv->cur_cmd_retcode = 0;
1242                 ret = -1;
1243         }
1244         spin_unlock_irqrestore(&priv->driver_lock, flags);
1245
1246 done:
1247         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1248         return ret;
1249 }
1250
1251 /**
1252  *  @brief This function allocates the command buffer and link
1253  *  it to command free queue.
1254  *
1255  *  @param priv         A pointer to struct lbs_private structure
1256  *  @return             0 or -1
1257  */
1258 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1259 {
1260         int ret = 0;
1261         u32 bufsize;
1262         u32 i;
1263         struct cmd_ctrl_node *cmdarray;
1264
1265         lbs_deb_enter(LBS_DEB_HOST);
1266
1267         /* Allocate and initialize the command array */
1268         bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1269         if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1270                 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1271                 ret = -1;
1272                 goto done;
1273         }
1274         priv->cmd_array = cmdarray;
1275
1276         /* Allocate and initialize each command buffer in the command array */
1277         for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1278                 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1279                 if (!cmdarray[i].cmdbuf) {
1280                         lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1281                         ret = -1;
1282                         goto done;
1283                 }
1284         }
1285
1286         for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1287                 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1288                 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1289         }
1290         ret = 0;
1291
1292 done:
1293         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1294         return ret;
1295 }
1296
1297 /**
1298  *  @brief This function frees the command buffer.
1299  *
1300  *  @param priv         A pointer to struct lbs_private structure
1301  *  @return             0 or -1
1302  */
1303 int lbs_free_cmd_buffer(struct lbs_private *priv)
1304 {
1305         struct cmd_ctrl_node *cmdarray;
1306         unsigned int i;
1307
1308         lbs_deb_enter(LBS_DEB_HOST);
1309
1310         /* need to check if cmd array is allocated or not */
1311         if (priv->cmd_array == NULL) {
1312                 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1313                 goto done;
1314         }
1315
1316         cmdarray = priv->cmd_array;
1317
1318         /* Release shared memory buffers */
1319         for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1320                 if (cmdarray[i].cmdbuf) {
1321                         kfree(cmdarray[i].cmdbuf);
1322                         cmdarray[i].cmdbuf = NULL;
1323                 }
1324         }
1325
1326         /* Release cmd_ctrl_node */
1327         if (priv->cmd_array) {
1328                 kfree(priv->cmd_array);
1329                 priv->cmd_array = NULL;
1330         }
1331
1332 done:
1333         lbs_deb_leave(LBS_DEB_HOST);
1334         return 0;
1335 }
1336
1337 /**
1338  *  @brief This function gets a free command node if available in
1339  *  command free queue.
1340  *
1341  *  @param priv         A pointer to struct lbs_private structure
1342  *  @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1343  */
1344 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1345 {
1346         struct cmd_ctrl_node *tempnode;
1347         unsigned long flags;
1348
1349         lbs_deb_enter(LBS_DEB_HOST);
1350
1351         if (!priv)
1352                 return NULL;
1353
1354         spin_lock_irqsave(&priv->driver_lock, flags);
1355
1356         if (!list_empty(&priv->cmdfreeq)) {
1357                 tempnode = list_first_entry(&priv->cmdfreeq,
1358                                             struct cmd_ctrl_node, list);
1359                 list_del(&tempnode->list);
1360         } else {
1361                 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1362                 tempnode = NULL;
1363         }
1364
1365         spin_unlock_irqrestore(&priv->driver_lock, flags);
1366
1367         lbs_deb_leave(LBS_DEB_HOST);
1368         return tempnode;
1369 }
1370
1371 /**
1372  *  @brief This function executes next command in command
1373  *  pending queue. It will put firmware back to PS mode
1374  *  if applicable.
1375  *
1376  *  @param priv     A pointer to struct lbs_private structure
1377  *  @return        0 or -1
1378  */
1379 int lbs_execute_next_command(struct lbs_private *priv)
1380 {
1381         struct cmd_ctrl_node *cmdnode = NULL;
1382         struct cmd_header *cmd;
1383         unsigned long flags;
1384         int ret = 0;
1385
1386         /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1387          * only caller to us is lbs_thread() and we get even when a
1388          * data packet is received */
1389         lbs_deb_enter(LBS_DEB_THREAD);
1390
1391         spin_lock_irqsave(&priv->driver_lock, flags);
1392
1393         if (priv->cur_cmd) {
1394                 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1395                 spin_unlock_irqrestore(&priv->driver_lock, flags);
1396                 ret = -1;
1397                 goto done;
1398         }
1399
1400         if (!list_empty(&priv->cmdpendingq)) {
1401                 cmdnode = list_first_entry(&priv->cmdpendingq,
1402                                            struct cmd_ctrl_node, list);
1403         }
1404
1405         spin_unlock_irqrestore(&priv->driver_lock, flags);
1406
1407         if (cmdnode) {
1408                 cmd = cmdnode->cmdbuf;
1409
1410                 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1411                         if ((priv->psstate == PS_STATE_SLEEP) ||
1412                             (priv->psstate == PS_STATE_PRE_SLEEP)) {
1413                                 lbs_deb_host(
1414                                        "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1415                                        le16_to_cpu(cmd->command),
1416                                        priv->psstate);
1417                                 ret = -1;
1418                                 goto done;
1419                         }
1420                         lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1421                                      "0x%04x in psstate %d\n",
1422                                      le16_to_cpu(cmd->command), priv->psstate);
1423                 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1424                         /*
1425                          * 1. Non-PS command:
1426                          * Queue it. set needtowakeup to TRUE if current state
1427                          * is SLEEP, otherwise call send EXIT_PS.
1428                          * 2. PS command but not EXIT_PS:
1429                          * Ignore it.
1430                          * 3. PS command EXIT_PS:
1431                          * Set needtowakeup to TRUE if current state is SLEEP,
1432                          * otherwise send this command down to firmware
1433                          * immediately.
1434                          */
1435                         if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1436                                 /*  Prepare to send Exit PS,
1437                                  *  this non PS command will be sent later */
1438                                 if ((priv->psstate == PS_STATE_SLEEP)
1439                                     || (priv->psstate == PS_STATE_PRE_SLEEP)
1440                                     ) {
1441                                         /* w/ new scheme, it will not reach here.
1442                                            since it is blocked in main_thread. */
1443                                         priv->needtowakeup = 1;
1444                                 } else {
1445                                         lbs_set_ps_mode(priv,
1446                                                         PS_MODE_ACTION_EXIT_PS,
1447                                                         false);
1448                                 }
1449
1450                                 ret = 0;
1451                                 goto done;
1452                         } else {
1453                                 /*
1454                                  * PS command. Ignore it if it is not Exit_PS.
1455                                  * otherwise send it down immediately.
1456                                  */
1457                                 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1458
1459                                 lbs_deb_host(
1460                                        "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1461                                        psm->action);
1462                                 if (psm->action !=
1463                                     cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
1464                                         lbs_deb_host(
1465                                                "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1466                                         list_del(&cmdnode->list);
1467                                         spin_lock_irqsave(&priv->driver_lock, flags);
1468                                         lbs_complete_command(priv, cmdnode, 0);
1469                                         spin_unlock_irqrestore(&priv->driver_lock, flags);
1470
1471                                         ret = 0;
1472                                         goto done;
1473                                 }
1474
1475                                 if ((priv->psstate == PS_STATE_SLEEP) ||
1476                                     (priv->psstate == PS_STATE_PRE_SLEEP)) {
1477                                         lbs_deb_host(
1478                                                "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1479                                         list_del(&cmdnode->list);
1480                                         spin_lock_irqsave(&priv->driver_lock, flags);
1481                                         lbs_complete_command(priv, cmdnode, 0);
1482                                         spin_unlock_irqrestore(&priv->driver_lock, flags);
1483                                         priv->needtowakeup = 1;
1484
1485                                         ret = 0;
1486                                         goto done;
1487                                 }
1488
1489                                 lbs_deb_host(
1490                                        "EXEC_NEXT_CMD: sending EXIT_PS\n");
1491                         }
1492                 }
1493                 list_del(&cmdnode->list);
1494                 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1495                             le16_to_cpu(cmd->command));
1496                 lbs_submit_command(priv, cmdnode);
1497         } else {
1498                 /*
1499                  * check if in power save mode, if yes, put the device back
1500                  * to PS mode
1501                  */
1502 #ifdef TODO
1503                 /*
1504                  * This was the old code for libertas+wext. Someone that
1505                  * understands this beast should re-code it in a sane way.
1506                  *
1507                  * I actually don't understand why this is related to WPA
1508                  * and to connection status, shouldn't powering should be
1509                  * independ of such things?
1510                  */
1511                 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1512                     (priv->psstate == PS_STATE_FULL_POWER) &&
1513                     ((priv->connect_status == LBS_CONNECTED) ||
1514                     lbs_mesh_connected(priv))) {
1515                         if (priv->secinfo.WPAenabled ||
1516                             priv->secinfo.WPA2enabled) {
1517                                 /* check for valid WPA group keys */
1518                                 if (priv->wpa_mcast_key.len ||
1519                                     priv->wpa_unicast_key.len) {
1520                                         lbs_deb_host(
1521                                                "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1522                                                " go back to PS_SLEEP");
1523                                         lbs_set_ps_mode(priv,
1524                                                         PS_MODE_ACTION_ENTER_PS,
1525                                                         false);
1526                                 }
1527                         } else {
1528                                 lbs_deb_host(
1529                                        "EXEC_NEXT_CMD: cmdpendingq empty, "
1530                                        "go back to PS_SLEEP");
1531                                 lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
1532                                                 false);
1533                         }
1534                 }
1535 #endif
1536         }
1537
1538         ret = 0;
1539 done:
1540         lbs_deb_leave(LBS_DEB_THREAD);
1541         return ret;
1542 }
1543
1544 static void lbs_send_confirmsleep(struct lbs_private *priv)
1545 {
1546         unsigned long flags;
1547         int ret;
1548
1549         lbs_deb_enter(LBS_DEB_HOST);
1550         lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1551                 sizeof(confirm_sleep));
1552
1553         ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1554                 sizeof(confirm_sleep));
1555         if (ret) {
1556                 lbs_pr_alert("confirm_sleep failed\n");
1557                 goto out;
1558         }
1559
1560         spin_lock_irqsave(&priv->driver_lock, flags);
1561
1562         /* We don't get a response on the sleep-confirmation */
1563         priv->dnld_sent = DNLD_RES_RECEIVED;
1564
1565         if (priv->is_host_sleep_configured) {
1566                 priv->is_host_sleep_activated = 1;
1567                 wake_up_interruptible(&priv->host_sleep_q);
1568         }
1569
1570         /* If nothing to do, go back to sleep (?) */
1571         if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1572                 priv->psstate = PS_STATE_SLEEP;
1573
1574         spin_unlock_irqrestore(&priv->driver_lock, flags);
1575
1576 out:
1577         lbs_deb_leave(LBS_DEB_HOST);
1578 }
1579
1580 /**
1581  *  @brief This function checks condition and prepares to
1582  *  send sleep confirm command to firmware if ok.
1583  *
1584  *  @param priv         A pointer to struct lbs_private structure
1585  *  @param psmode       Power Saving mode
1586  *  @return             n/a
1587  */
1588 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1589 {
1590         unsigned long flags =0;
1591         int allowed = 1;
1592
1593         lbs_deb_enter(LBS_DEB_HOST);
1594
1595         spin_lock_irqsave(&priv->driver_lock, flags);
1596         if (priv->dnld_sent) {
1597                 allowed = 0;
1598                 lbs_deb_host("dnld_sent was set\n");
1599         }
1600
1601         /* In-progress command? */
1602         if (priv->cur_cmd) {
1603                 allowed = 0;
1604                 lbs_deb_host("cur_cmd was set\n");
1605         }
1606
1607         /* Pending events or command responses? */
1608         if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1609                 allowed = 0;
1610                 lbs_deb_host("pending events or command responses\n");
1611         }
1612         spin_unlock_irqrestore(&priv->driver_lock, flags);
1613
1614         if (allowed) {
1615                 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1616                 lbs_send_confirmsleep(priv);
1617         } else {
1618                 lbs_deb_host("sleep confirm has been delayed\n");
1619         }
1620
1621         lbs_deb_leave(LBS_DEB_HOST);
1622 }
1623
1624
1625 /**
1626  * @brief Configures the transmission power control functionality.
1627  *
1628  * @param priv          A pointer to struct lbs_private structure
1629  * @param enable        Transmission power control enable
1630  * @param p0            Power level when link quality is good (dBm).
1631  * @param p1            Power level when link quality is fair (dBm).
1632  * @param p2            Power level when link quality is poor (dBm).
1633  * @param usesnr        Use Signal to Noise Ratio in TPC
1634  *
1635  * @return 0 on success
1636  */
1637 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1638                 int8_t p2, int usesnr)
1639 {
1640         struct cmd_ds_802_11_tpc_cfg cmd;
1641         int ret;
1642
1643         memset(&cmd, 0, sizeof(cmd));
1644         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1645         cmd.action = cpu_to_le16(CMD_ACT_SET);
1646         cmd.enable = !!enable;
1647         cmd.usesnr = !!usesnr;
1648         cmd.P0 = p0;
1649         cmd.P1 = p1;
1650         cmd.P2 = p2;
1651
1652         ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1653
1654         return ret;
1655 }
1656
1657 /**
1658  * @brief Configures the power adaptation settings.
1659  *
1660  * @param priv          A pointer to struct lbs_private structure
1661  * @param enable        Power adaptation enable
1662  * @param p0            Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1663  * @param p1            Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1664  * @param p2            Power level for 48 and 54 Mbps (dBm).
1665  *
1666  * @return 0 on Success
1667  */
1668
1669 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1670                 int8_t p1, int8_t p2)
1671 {
1672         struct cmd_ds_802_11_pa_cfg cmd;
1673         int ret;
1674
1675         memset(&cmd, 0, sizeof(cmd));
1676         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1677         cmd.action = cpu_to_le16(CMD_ACT_SET);
1678         cmd.enable = !!enable;
1679         cmd.P0 = p0;
1680         cmd.P1 = p1;
1681         cmd.P2 = p2;
1682
1683         ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1684
1685         return ret;
1686 }
1687
1688
1689 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1690         uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1691         int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1692         unsigned long callback_arg)
1693 {
1694         struct cmd_ctrl_node *cmdnode;
1695
1696         lbs_deb_enter(LBS_DEB_HOST);
1697
1698         if (priv->surpriseremoved) {
1699                 lbs_deb_host("PREP_CMD: card removed\n");
1700                 cmdnode = ERR_PTR(-ENOENT);
1701                 goto done;
1702         }
1703
1704         if (!lbs_is_cmd_allowed(priv)) {
1705                 cmdnode = ERR_PTR(-EBUSY);
1706                 goto done;
1707         }
1708
1709         cmdnode = lbs_get_cmd_ctrl_node(priv);
1710         if (cmdnode == NULL) {
1711                 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1712
1713                 /* Wake up main thread to execute next command */
1714                 wake_up_interruptible(&priv->waitq);
1715                 cmdnode = ERR_PTR(-ENOBUFS);
1716                 goto done;
1717         }
1718
1719         cmdnode->callback = callback;
1720         cmdnode->callback_arg = callback_arg;
1721
1722         /* Copy the incoming command to the buffer */
1723         memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1724
1725         /* Set sequence number, clean result, move to buffer */
1726         priv->seqnum++;
1727         cmdnode->cmdbuf->command = cpu_to_le16(command);
1728         cmdnode->cmdbuf->size    = cpu_to_le16(in_cmd_size);
1729         cmdnode->cmdbuf->seqnum  = cpu_to_le16(priv->seqnum);
1730         cmdnode->cmdbuf->result  = 0;
1731
1732         lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1733
1734         cmdnode->cmdwaitqwoken = 0;
1735         lbs_queue_cmd(priv, cmdnode);
1736         wake_up_interruptible(&priv->waitq);
1737
1738  done:
1739         lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1740         return cmdnode;
1741 }
1742
1743 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1744         struct cmd_header *in_cmd, int in_cmd_size)
1745 {
1746         lbs_deb_enter(LBS_DEB_CMD);
1747         __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1748                 lbs_cmd_async_callback, 0);
1749         lbs_deb_leave(LBS_DEB_CMD);
1750 }
1751
1752 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1753               struct cmd_header *in_cmd, int in_cmd_size,
1754               int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1755               unsigned long callback_arg)
1756 {
1757         struct cmd_ctrl_node *cmdnode;
1758         unsigned long flags;
1759         int ret = 0;
1760
1761         lbs_deb_enter(LBS_DEB_HOST);
1762
1763         cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1764                                   callback, callback_arg);
1765         if (IS_ERR(cmdnode)) {
1766                 ret = PTR_ERR(cmdnode);
1767                 goto done;
1768         }
1769
1770         might_sleep();
1771         wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1772
1773         spin_lock_irqsave(&priv->driver_lock, flags);
1774         ret = cmdnode->result;
1775         if (ret)
1776                 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1777                             command, ret);
1778
1779         __lbs_cleanup_and_insert_cmd(priv, cmdnode);
1780         spin_unlock_irqrestore(&priv->driver_lock, flags);
1781
1782 done:
1783         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1784         return ret;
1785 }
1786 EXPORT_SYMBOL_GPL(__lbs_cmd);