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