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