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