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