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