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