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