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