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