- patches.apparmor/remove_suid_new_case_in_2.6.22.diff: Merge fix.
[linux-flexiantxendom0-3.2.10.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 "host.h"
8 #include "hostcmd.h"
9 #include "sbi.h"
10 #include "decl.h"
11 #include "defs.h"
12 #include "dev.h"
13 #include "join.h"
14 #include "wext.h"
15
16 static void cleanup_cmdnode(struct cmd_ctrl_node *ptempnode);
17
18 static u16 commands_allowed_in_ps[] = {
19         cmd_802_11_rssi,
20 };
21
22 /**
23  *  @brief This function checks if the commans is allowed
24  *  in PS mode not.
25  *
26  *  @param command the command ID
27  *  @return        TRUE or FALSE
28  */
29 static u8 is_command_allowed_in_ps(u16 command)
30 {
31         int count = sizeof(commands_allowed_in_ps)
32             / sizeof(commands_allowed_in_ps[0]);
33         int i;
34
35         for (i = 0; i < count; i++) {
36                 if (command == cpu_to_le16(commands_allowed_in_ps[i]))
37                         return 1;
38         }
39
40         return 0;
41 }
42
43 static int wlan_cmd_hw_spec(wlan_private * priv, struct cmd_ds_command *cmd)
44 {
45         struct cmd_ds_get_hw_spec *hwspec = &cmd->params.hwspec;
46
47         ENTER();
48
49         cmd->command = cpu_to_le16(cmd_get_hw_spec);
50         cmd->size =
51             cpu_to_le16(sizeof(struct cmd_ds_get_hw_spec) + S_DS_GEN);
52         memcpy(hwspec->permanentaddr, priv->adapter->current_addr, ETH_ALEN);
53
54         LEAVE();
55         return 0;
56 }
57
58 static int wlan_cmd_802_11_ps_mode(wlan_private * priv,
59                                    struct cmd_ds_command *cmd,
60                                    u16 cmd_action)
61 {
62         struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
63         u16 action = cmd_action;
64         wlan_adapter *adapter = priv->adapter;
65
66         ENTER();
67
68         cmd->command = cpu_to_le16(cmd_802_11_ps_mode);
69         cmd->size =
70             cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
71                              S_DS_GEN);
72         psm->action = cpu_to_le16(cmd_action);
73         psm->multipledtim = 0;
74         switch (action) {
75         case cmd_subcmd_enter_ps:
76                 lbs_pr_debug(1, "PS command:" "SubCode- Enter PS\n");
77                 lbs_pr_debug(1, "locallisteninterval = %d\n",
78                        adapter->locallisteninterval);
79
80                 psm->locallisteninterval =
81                     cpu_to_le16(adapter->locallisteninterval);
82                 psm->nullpktinterval =
83                     cpu_to_le16(adapter->nullpktinterval);
84                 psm->multipledtim =
85                     cpu_to_le16(priv->adapter->multipledtim);
86                 break;
87
88         case cmd_subcmd_exit_ps:
89                 lbs_pr_debug(1, "PS command:" "SubCode- Exit PS\n");
90                 break;
91
92         case cmd_subcmd_sleep_confirmed:
93                 lbs_pr_debug(1, "PS command: SubCode- sleep confirm\n");
94                 break;
95
96         default:
97                 break;
98         }
99
100         LEAVE();
101         return 0;
102 }
103
104 static int wlan_cmd_802_11_inactivity_timeout(wlan_private * priv,
105                                               struct cmd_ds_command *cmd,
106                                               u16 cmd_action, void *pdata_buf)
107 {
108         u16 *timeout = pdata_buf;
109
110         cmd->command = cpu_to_le16(cmd_802_11_inactivity_timeout);
111         cmd->size =
112             cpu_to_le16(sizeof(struct cmd_ds_802_11_inactivity_timeout)
113                              + S_DS_GEN);
114
115         cmd->params.inactivity_timeout.action = cpu_to_le16(cmd_action);
116
117         if (cmd_action)
118                 cmd->params.inactivity_timeout.timeout =
119                     cpu_to_le16(*timeout);
120         else
121                 cmd->params.inactivity_timeout.timeout = 0;
122
123         return 0;
124 }
125
126 static int wlan_cmd_802_11_sleep_params(wlan_private * priv,
127                                         struct cmd_ds_command *cmd,
128                                         u16 cmd_action)
129 {
130         wlan_adapter *adapter = priv->adapter;
131         struct cmd_ds_802_11_sleep_params *sp = &cmd->params.sleep_params;
132
133         ENTER();
134
135         cmd->size =
136             cpu_to_le16((sizeof(struct cmd_ds_802_11_sleep_params)) +
137                              S_DS_GEN);
138         cmd->command = cpu_to_le16(cmd_802_11_sleep_params);
139
140         if (cmd_action == cmd_act_get) {
141                 memset(&adapter->sp, 0, sizeof(struct sleep_params));
142                 memset(sp, 0, sizeof(struct cmd_ds_802_11_sleep_params));
143                 sp->action = cpu_to_le16(cmd_action);
144         } else if (cmd_action == cmd_act_set) {
145                 sp->action = cpu_to_le16(cmd_action);
146                 sp->error = cpu_to_le16(adapter->sp.sp_error);
147                 sp->offset = cpu_to_le16(adapter->sp.sp_offset);
148                 sp->stabletime = cpu_to_le16(adapter->sp.sp_stabletime);
149                 sp->calcontrol = (u8) adapter->sp.sp_calcontrol;
150                 sp->externalsleepclk = (u8) adapter->sp.sp_extsleepclk;
151                 sp->reserved = cpu_to_le16(adapter->sp.sp_reserved);
152         }
153
154         LEAVE();
155         return 0;
156 }
157
158 static int wlan_cmd_802_11_set_wep(wlan_private * priv,
159                                    struct cmd_ds_command *cmd,
160                                    u32 cmd_act,
161                                    void * pdata_buf)
162 {
163         struct cmd_ds_802_11_set_wep *wep = &cmd->params.wep;
164         wlan_adapter *adapter = priv->adapter;
165         int ret = 0;
166         struct assoc_request * assoc_req = pdata_buf;
167
168         ENTER();
169
170         cmd->command = cpu_to_le16(cmd_802_11_set_wep);
171         cmd->size = cpu_to_le16((sizeof(struct cmd_ds_802_11_set_wep))
172                                      + S_DS_GEN);
173
174         if (cmd_act == cmd_act_add) {
175                 int i;
176
177                 if (!assoc_req) {
178                         lbs_pr_debug(1, "Invalid association request!");
179                         ret = -1;
180                         goto done;
181                 }
182
183                 wep->action = cpu_to_le16(cmd_act_add);
184
185                 /* default tx key index */
186                 wep->keyindex = cpu_to_le16((u16)
187                                                  (assoc_req->wep_tx_keyidx &
188                                                  (u32)cmd_WEP_KEY_INDEX_MASK));
189
190                 lbs_pr_debug(1, "Tx key Index: %u\n", wep->keyindex);
191
192                 /* Copy key types and material to host command structure */
193                 for (i = 0; i < 4; i++) {
194                         struct WLAN_802_11_KEY * pkey = &assoc_req->wep_keys[i];
195
196                         switch (pkey->len) {
197                         case KEY_LEN_WEP_40:
198                                 wep->keytype[i] = cmd_type_wep_40_bit;
199                                 memmove(&wep->keymaterial[i], pkey->key,
200                                         pkey->len);
201                                 break;
202                         case KEY_LEN_WEP_104:
203                                 wep->keytype[i] = cmd_type_wep_104_bit;
204                                 memmove(&wep->keymaterial[i], pkey->key,
205                                         pkey->len);
206                                 break;
207                         case 0:
208                                 break;
209                         default:
210                                 lbs_pr_debug(1, "Invalid WEP key %d length of %d\n",
211                                        i, pkey->len);
212                                 ret = -1;
213                                 goto done;
214                                 break;
215                         }
216                 }
217         } else if (cmd_act == cmd_act_remove) {
218                 /* ACT_REMOVE clears _all_ WEP keys */
219                 wep->action = cpu_to_le16(cmd_act_remove);
220
221                 /* default tx key index */
222                 wep->keyindex = cpu_to_le16((u16)
223                                                  (adapter->wep_tx_keyidx &
224                                                  (u32)cmd_WEP_KEY_INDEX_MASK));
225         }
226
227         ret = 0;
228
229 done:
230         LEAVE();
231         return ret;
232 }
233
234 static int wlan_cmd_802_11_enable_rsn(wlan_private * priv,
235                                       struct cmd_ds_command *cmd,
236                                       u16 cmd_action)
237 {
238         struct cmd_ds_802_11_enable_rsn *penableRSN = &cmd->params.enbrsn;
239         wlan_adapter *adapter = priv->adapter;
240
241         cmd->command = cpu_to_le16(cmd_802_11_enable_rsn);
242         cmd->size =
243             cpu_to_le16(sizeof(struct cmd_ds_802_11_enable_rsn) +
244                              S_DS_GEN);
245         penableRSN->action = cpu_to_le16(cmd_action);
246         if (adapter->secinfo.WPAenabled || adapter->secinfo.WPA2enabled) {
247                 penableRSN->enable = cpu_to_le16(cmd_enable_rsn);
248         } else {
249                 penableRSN->enable = cpu_to_le16(cmd_disable_rsn);
250         }
251
252         return 0;
253 }
254
255
256 static void set_one_wpa_key(struct MrvlIEtype_keyParamSet * pkeyparamset,
257                             struct WLAN_802_11_KEY * pkey)
258 {
259         pkeyparamset->keytypeid = cpu_to_le16(pkey->type);
260
261         if (pkey->flags & KEY_INFO_WPA_ENABLED) {
262                 pkeyparamset->keyinfo = cpu_to_le16(KEY_INFO_WPA_ENABLED);
263         } else {
264                 pkeyparamset->keyinfo = cpu_to_le16(!KEY_INFO_WPA_ENABLED);
265         }
266
267         if (pkey->flags & KEY_INFO_WPA_UNICAST) {
268                 pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
269         } else if (pkey->flags & KEY_INFO_WPA_MCAST) {
270                 pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
271         }
272
273         pkeyparamset->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
274         pkeyparamset->keylen = cpu_to_le16(pkey->len);
275         memcpy(pkeyparamset->key, pkey->key, pkey->len);
276         pkeyparamset->length = cpu_to_le16(  sizeof(pkeyparamset->keytypeid)
277                                                 + sizeof(pkeyparamset->keyinfo)
278                                                 + sizeof(pkeyparamset->keylen)
279                                                 + sizeof(pkeyparamset->key));
280 }
281
282 static int wlan_cmd_802_11_key_material(wlan_private * priv,
283                                         struct cmd_ds_command *cmd,
284                                         u16 cmd_action,
285                                         u32 cmd_oid, void *pdata_buf)
286 {
287         wlan_adapter *adapter = priv->adapter;
288         struct cmd_ds_802_11_key_material *pkeymaterial =
289             &cmd->params.keymaterial;
290         int ret = 0;
291         int index = 0;
292
293         ENTER();
294
295         cmd->command = cpu_to_le16(cmd_802_11_key_material);
296         pkeymaterial->action = cpu_to_le16(cmd_action);
297
298         if (cmd_action == cmd_act_get) {
299                 cmd->size = cpu_to_le16(  S_DS_GEN
300                                              + sizeof (pkeymaterial->action));
301                 ret = 0;
302                 goto done;
303         }
304
305         memset(&pkeymaterial->keyParamSet, 0, sizeof(pkeymaterial->keyParamSet));
306
307         if (adapter->wpa_unicast_key.len) {
308                 set_one_wpa_key(&pkeymaterial->keyParamSet[index],
309                                 &adapter->wpa_unicast_key);
310                 index++;
311         }
312
313         if (adapter->wpa_mcast_key.len) {
314                 set_one_wpa_key(&pkeymaterial->keyParamSet[index],
315                                 &adapter->wpa_mcast_key);
316                 index++;
317         }
318
319         cmd->size = cpu_to_le16(  S_DS_GEN
320                                      + sizeof (pkeymaterial->action)
321                                      + index * sizeof(struct MrvlIEtype_keyParamSet));
322
323         ret = 0;
324
325 done:
326         LEAVE();
327         return ret;
328 }
329
330 static int wlan_cmd_802_11_reset(wlan_private * priv,
331                                  struct cmd_ds_command *cmd, int cmd_action)
332 {
333         struct cmd_ds_802_11_reset *reset = &cmd->params.reset;
334
335         cmd->command = cpu_to_le16(cmd_802_11_reset);
336         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
337         reset->action = cpu_to_le16(cmd_action);
338
339         return 0;
340 }
341
342 static int wlan_cmd_802_11_get_log(wlan_private * priv,
343                                    struct cmd_ds_command *cmd)
344 {
345         cmd->command = cpu_to_le16(cmd_802_11_get_log);
346         cmd->size =
347                 cpu_to_le16(sizeof(struct cmd_ds_802_11_get_log) + S_DS_GEN);
348
349         return 0;
350 }
351
352 static int wlan_cmd_802_11_get_stat(wlan_private * priv,
353                                     struct cmd_ds_command *cmd)
354 {
355         cmd->command = cpu_to_le16(cmd_802_11_get_stat);
356         cmd->size =
357             cpu_to_le16(sizeof(struct cmd_ds_802_11_get_stat) +
358                              S_DS_GEN);
359
360         return 0;
361 }
362
363 static int wlan_cmd_802_11_snmp_mib(wlan_private * priv,
364                                     struct cmd_ds_command *cmd,
365                                     int cmd_action,
366                                     int cmd_oid, void *pdata_buf)
367 {
368         struct cmd_ds_802_11_snmp_mib *pSNMPMIB = &cmd->params.smib;
369         wlan_adapter *adapter = priv->adapter;
370         u8 ucTemp;
371
372         ENTER();
373
374         lbs_pr_debug(1, "SNMP_CMD: cmd_oid = 0x%x\n", cmd_oid);
375
376         cmd->command = cpu_to_le16(cmd_802_11_snmp_mib);
377         cmd->size =
378             cpu_to_le16(sizeof(struct cmd_ds_802_11_snmp_mib) +
379                              S_DS_GEN);
380
381         switch (cmd_oid) {
382         case OID_802_11_INFRASTRUCTURE_MODE:
383         {
384                 u8 mode = (u8) (size_t) pdata_buf;
385                 pSNMPMIB->querytype = cpu_to_le16(cmd_act_set);
386                 pSNMPMIB->oid = cpu_to_le16((u16) desired_bsstype_i);
387                 pSNMPMIB->bufsize = sizeof(u8);
388                 if (mode == IW_MODE_ADHOC) {
389                         ucTemp = SNMP_MIB_VALUE_ADHOC;
390                 } else {
391                         /* Infra and Auto modes */
392                         ucTemp = SNMP_MIB_VALUE_INFRA;
393                 }
394
395                 memmove(pSNMPMIB->value, &ucTemp, sizeof(u8));
396
397                 break;
398         }
399
400         case OID_802_11D_ENABLE:
401                 {
402                         u32 ulTemp;
403
404                         pSNMPMIB->oid = cpu_to_le16((u16) dot11d_i);
405
406                         if (cmd_action == cmd_act_set) {
407                                 pSNMPMIB->querytype = cmd_act_set;
408                                 pSNMPMIB->bufsize = sizeof(u16);
409                                 ulTemp = *(u32 *)pdata_buf;
410                                 *((unsigned short *)(pSNMPMIB->value)) =
411                                     cpu_to_le16((u16) ulTemp);
412                         }
413                         break;
414                 }
415
416         case OID_802_11_FRAGMENTATION_THRESHOLD:
417                 {
418                         u32 ulTemp;
419
420                         pSNMPMIB->oid = cpu_to_le16((u16) fragthresh_i);
421
422                         if (cmd_action == cmd_act_get) {
423                                 pSNMPMIB->querytype =
424                                     cpu_to_le16(cmd_act_get);
425                         } else if (cmd_action == cmd_act_set) {
426                                 pSNMPMIB->querytype =
427                                     cpu_to_le16(cmd_act_set);
428                                 pSNMPMIB->bufsize =
429                                     cpu_to_le16(sizeof(u16));
430                                 ulTemp = *((u32 *) pdata_buf);
431                                 *((unsigned short *)(pSNMPMIB->value)) =
432                                     cpu_to_le16((u16) ulTemp);
433
434                         }
435
436                         break;
437                 }
438
439         case OID_802_11_RTS_THRESHOLD:
440                 {
441
442                         u32 ulTemp;
443                         pSNMPMIB->oid = le16_to_cpu((u16) rtsthresh_i);
444
445                         if (cmd_action == cmd_act_get) {
446                                 pSNMPMIB->querytype =
447                                     cpu_to_le16(cmd_act_get);
448                         } else if (cmd_action == cmd_act_set) {
449                                 pSNMPMIB->querytype =
450                                     cpu_to_le16(cmd_act_set);
451                                 pSNMPMIB->bufsize =
452                                     cpu_to_le16(sizeof(u16));
453                                 ulTemp = *((u32 *)
454                                            pdata_buf);
455                                 *(unsigned short *)(pSNMPMIB->value) =
456                                     cpu_to_le16((u16) ulTemp);
457
458                         }
459                         break;
460                 }
461         case OID_802_11_TX_RETRYCOUNT:
462                 pSNMPMIB->oid = cpu_to_le16((u16) short_retrylim_i);
463
464                 if (cmd_action == cmd_act_get) {
465                         pSNMPMIB->querytype =
466                             cpu_to_le16(cmd_act_get);
467                 } else if (cmd_action == cmd_act_set) {
468                         pSNMPMIB->querytype =
469                             cpu_to_le16(cmd_act_set);
470                         pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
471                         *((unsigned short *)(pSNMPMIB->value)) =
472                             cpu_to_le16((u16) adapter->txretrycount);
473                 }
474
475                 break;
476         default:
477                 break;
478         }
479
480         lbs_pr_debug(1,
481                "SNMP_CMD: command=0x%x, size=0x%x, seqnum=0x%x, result=0x%x\n",
482                cmd->command, cmd->size, cmd->seqnum, cmd->result);
483
484         lbs_pr_debug(1,
485                "SNMP_CMD: action=0x%x, oid=0x%x, oidsize=0x%x, value=0x%x\n",
486                pSNMPMIB->querytype, pSNMPMIB->oid, pSNMPMIB->bufsize,
487                *(u16 *) pSNMPMIB->value);
488
489         LEAVE();
490         return 0;
491 }
492
493 static int wlan_cmd_802_11_radio_control(wlan_private * priv,
494                                          struct cmd_ds_command *cmd,
495                                          int cmd_action)
496 {
497         wlan_adapter *adapter = priv->adapter;
498         struct cmd_ds_802_11_radio_control *pradiocontrol =
499             &cmd->params.radio;
500
501         ENTER();
502
503         cmd->size =
504             cpu_to_le16((sizeof(struct cmd_ds_802_11_radio_control)) +
505                              S_DS_GEN);
506         cmd->command = cpu_to_le16(cmd_802_11_radio_control);
507
508         pradiocontrol->action = cpu_to_le16(cmd_action);
509
510         switch (adapter->preamble) {
511         case cmd_type_short_preamble:
512                 pradiocontrol->control = cpu_to_le16(SET_SHORT_PREAMBLE);
513                 break;
514
515         case cmd_type_long_preamble:
516                 pradiocontrol->control = cpu_to_le16(SET_LONG_PREAMBLE);
517                 break;
518
519         case cmd_type_auto_preamble:
520         default:
521                 pradiocontrol->control = cpu_to_le16(SET_AUTO_PREAMBLE);
522                 break;
523         }
524
525         if (adapter->radioon)
526                 pradiocontrol->control |= cpu_to_le16(TURN_ON_RF);
527         else
528                 pradiocontrol->control &= cpu_to_le16(~TURN_ON_RF);
529
530         LEAVE();
531         return 0;
532 }
533
534 static int wlan_cmd_802_11_rf_tx_power(wlan_private * priv,
535                                        struct cmd_ds_command *cmd,
536                                        u16 cmd_action, void *pdata_buf)
537 {
538
539         struct cmd_ds_802_11_rf_tx_power *prtp = &cmd->params.txp;
540
541         ENTER();
542
543         cmd->size =
544             cpu_to_le16((sizeof(struct cmd_ds_802_11_rf_tx_power)) +
545                              S_DS_GEN);
546         cmd->command = cpu_to_le16(cmd_802_11_rf_tx_power);
547         prtp->action = cmd_action;
548
549         lbs_pr_debug(1, "RF_TX_POWER_CMD: size:%d cmd:0x%x Act:%d\n", cmd->size,
550                cmd->command, prtp->action);
551
552         switch (cmd_action) {
553         case cmd_act_tx_power_opt_get:
554                 prtp->action = cpu_to_le16(cmd_act_get);
555                 prtp->currentlevel = 0;
556                 break;
557
558         case cmd_act_tx_power_opt_set_high:
559                 prtp->action = cpu_to_le16(cmd_act_set);
560                 prtp->currentlevel =
561                     cpu_to_le16(cmd_act_tx_power_index_high);
562                 break;
563
564         case cmd_act_tx_power_opt_set_mid:
565                 prtp->action = cpu_to_le16(cmd_act_set);
566                 prtp->currentlevel =
567                     cpu_to_le16(cmd_act_tx_power_index_mid);
568                 break;
569
570         case cmd_act_tx_power_opt_set_low:
571                 prtp->action = cpu_to_le16(cmd_act_set);
572                 prtp->currentlevel = cpu_to_le16(*((u16 *) pdata_buf));
573                 break;
574         }
575         LEAVE();
576         return 0;
577 }
578
579 static int wlan_cmd_802_11_rf_antenna(wlan_private * priv,
580                                       struct cmd_ds_command *cmd,
581                                       u16 cmd_action, void *pdata_buf)
582 {
583         struct cmd_ds_802_11_rf_antenna *rant = &cmd->params.rant;
584
585         cmd->command = cpu_to_le16(cmd_802_11_rf_antenna);
586         cmd->size =
587             cpu_to_le16(sizeof(struct cmd_ds_802_11_rf_antenna) +
588                              S_DS_GEN);
589
590         rant->action = cpu_to_le16(cmd_action);
591         if ((cmd_action == cmd_act_set_rx) ||
592             (cmd_action == cmd_act_set_tx)) {
593                 rant->antennamode =
594                     cpu_to_le16((u16) (*(u32 *) pdata_buf));
595         }
596
597         return 0;
598 }
599
600 static int wlan_cmd_802_11_rate_adapt_rateset(wlan_private * priv,
601                                               struct cmd_ds_command *cmd,
602                                               u16 cmd_action)
603 {
604         struct cmd_ds_802_11_rate_adapt_rateset
605         *rateadapt = &cmd->params.rateset;
606         wlan_adapter *adapter = priv->adapter;
607
608         cmd->size =
609             cpu_to_le16(sizeof(struct cmd_ds_802_11_rate_adapt_rateset)
610                              + S_DS_GEN);
611         cmd->command = cpu_to_le16(cmd_802_11_rate_adapt_rateset);
612
613         ENTER();
614
615         rateadapt->action = cmd_action;
616         rateadapt->enablehwauto = adapter->enablehwauto;
617         rateadapt->bitmap = adapter->ratebitmap;
618
619         LEAVE();
620         return 0;
621 }
622
623 static int wlan_cmd_802_11_data_rate(wlan_private * priv,
624                                      struct cmd_ds_command *cmd,
625                                      u16 cmd_action)
626 {
627         struct cmd_ds_802_11_data_rate *pdatarate = &cmd->params.drate;
628         wlan_adapter *adapter = priv->adapter;
629         u16 action = cmd_action;
630
631         ENTER();
632
633         cmd->size =
634             cpu_to_le16(sizeof(struct cmd_ds_802_11_data_rate) +
635                              S_DS_GEN);
636
637         cmd->command = cpu_to_le16(cmd_802_11_data_rate);
638
639         memset(pdatarate, 0, sizeof(struct cmd_ds_802_11_data_rate));
640
641         pdatarate->action = cpu_to_le16(cmd_action);
642
643         if (action == cmd_act_set_tx_fix_rate) {
644                 pdatarate->datarate[0] = libertas_data_rate_to_index(adapter->datarate);
645                 lbs_pr_debug(1, "Setting FW for fixed rate 0x%02X\n",
646                        adapter->datarate);
647         } else if (action == cmd_act_set_tx_auto) {
648                 lbs_pr_debug(1, "Setting FW for AUTO rate\n");
649         }
650
651         LEAVE();
652         return 0;
653 }
654
655 static int wlan_cmd_mac_multicast_adr(wlan_private * priv,
656                                       struct cmd_ds_command *cmd,
657                                       u16 cmd_action)
658 {
659         struct cmd_ds_mac_multicast_adr *pMCastAdr = &cmd->params.madr;
660         wlan_adapter *adapter = priv->adapter;
661
662         cmd->size =
663             cpu_to_le16(sizeof(struct cmd_ds_mac_multicast_adr) +
664                              S_DS_GEN);
665         cmd->command = cpu_to_le16(cmd_mac_multicast_adr);
666
667         pMCastAdr->action = cpu_to_le16(cmd_action);
668         pMCastAdr->nr_of_adrs =
669             cpu_to_le16((u16) adapter->nr_of_multicastmacaddr);
670         memcpy(pMCastAdr->maclist, adapter->multicastlist,
671                adapter->nr_of_multicastmacaddr * ETH_ALEN);
672
673         return 0;
674 }
675
676 static int wlan_cmd_802_11_rf_channel(wlan_private * priv,
677                                       struct cmd_ds_command *cmd,
678                                       int option, void *pdata_buf)
679 {
680         struct cmd_ds_802_11_rf_channel *rfchan = &cmd->params.rfchannel;
681
682         cmd->command = cpu_to_le16(cmd_802_11_rf_channel);
683         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rf_channel)
684                                      + S_DS_GEN);
685
686         if (option == cmd_opt_802_11_rf_channel_set) {
687                 rfchan->currentchannel = cpu_to_le16(*((u16 *) pdata_buf));
688         }
689
690         rfchan->action = cpu_to_le16(option);
691
692         return 0;
693 }
694
695 static int wlan_cmd_802_11_rssi(wlan_private * priv,
696                                 struct cmd_ds_command *cmd)
697 {
698         wlan_adapter *adapter = priv->adapter;
699
700         cmd->command = cpu_to_le16(cmd_802_11_rssi);
701         cmd->size =
702             cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
703         cmd->params.rssi.N = priv->adapter->bcn_avg_factor;
704
705         /* reset Beacon SNR/NF/RSSI values */
706         adapter->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
707         adapter->SNR[TYPE_BEACON][TYPE_AVG] = 0;
708         adapter->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
709         adapter->NF[TYPE_BEACON][TYPE_AVG] = 0;
710         adapter->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
711         adapter->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
712
713         return 0;
714 }
715
716 static int wlan_cmd_reg_access(wlan_private * priv,
717                                struct cmd_ds_command *cmdptr,
718                                u8 cmd_action, void *pdata_buf)
719 {
720         struct wlan_offset_value *offval;
721
722         ENTER();
723
724         offval = (struct wlan_offset_value *)pdata_buf;
725
726         switch (cmdptr->command) {
727         case cmd_mac_reg_access:
728                 {
729                         struct cmd_ds_mac_reg_access *macreg;
730
731                         cmdptr->size =
732                             cpu_to_le16(sizeof
733                                              (struct cmd_ds_mac_reg_access)
734                                              + S_DS_GEN);
735                         macreg =
736                             (struct cmd_ds_mac_reg_access *)&cmdptr->params.
737                             macreg;
738
739                         macreg->action = cpu_to_le16(cmd_action);
740                         macreg->offset = cpu_to_le16((u16) offval->offset);
741                         macreg->value = cpu_to_le32(offval->value);
742
743                         break;
744                 }
745
746         case cmd_bbp_reg_access:
747                 {
748                         struct cmd_ds_bbp_reg_access *bbpreg;
749
750                         cmdptr->size =
751                             cpu_to_le16(sizeof
752                                              (struct cmd_ds_bbp_reg_access)
753                                              + S_DS_GEN);
754                         bbpreg =
755                             (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
756                             bbpreg;
757
758                         bbpreg->action = cpu_to_le16(cmd_action);
759                         bbpreg->offset = cpu_to_le16((u16) offval->offset);
760                         bbpreg->value = (u8) offval->value;
761
762                         break;
763                 }
764
765         case cmd_rf_reg_access:
766                 {
767                         struct cmd_ds_rf_reg_access *rfreg;
768
769                         cmdptr->size =
770                             cpu_to_le16(sizeof
771                                              (struct cmd_ds_rf_reg_access) +
772                                              S_DS_GEN);
773                         rfreg =
774                             (struct cmd_ds_rf_reg_access *)&cmdptr->params.
775                             rfreg;
776
777                         rfreg->action = cpu_to_le16(cmd_action);
778                         rfreg->offset = cpu_to_le16((u16) offval->offset);
779                         rfreg->value = (u8) offval->value;
780
781                         break;
782                 }
783
784         default:
785                 break;
786         }
787
788         LEAVE();
789         return 0;
790 }
791
792 static int wlan_cmd_802_11_mac_address(wlan_private * priv,
793                                        struct cmd_ds_command *cmd,
794                                        u16 cmd_action)
795 {
796         wlan_adapter *adapter = priv->adapter;
797
798         cmd->command = cpu_to_le16(cmd_802_11_mac_address);
799         cmd->size =
800             cpu_to_le16(sizeof(struct cmd_ds_802_11_mac_address) +
801                              S_DS_GEN);
802         cmd->result = 0;
803
804         cmd->params.macadd.action = cpu_to_le16(cmd_action);
805
806         if (cmd_action == cmd_act_set) {
807                 memcpy(cmd->params.macadd.macadd,
808                        adapter->current_addr, ETH_ALEN);
809                 lbs_dbg_hex("SET_CMD: MAC ADDRESS-", adapter->current_addr, 6);
810         }
811
812         return 0;
813 }
814
815 static int wlan_cmd_802_11_eeprom_access(wlan_private * priv,
816                                          struct cmd_ds_command *cmd,
817                                          int cmd_action, void *pdata_buf)
818 {
819         struct wlan_ioctl_regrdwr *ea = pdata_buf;
820
821         ENTER();
822
823         cmd->command = cpu_to_le16(cmd_802_11_eeprom_access);
824         cmd->size =
825             cpu_to_le16(sizeof(struct cmd_ds_802_11_eeprom_access) +
826                              S_DS_GEN);
827         cmd->result = 0;
828
829         cmd->params.rdeeprom.action = cpu_to_le16(ea->action);
830         cmd->params.rdeeprom.offset = cpu_to_le16(ea->offset);
831         cmd->params.rdeeprom.bytecount = cpu_to_le16(ea->NOB);
832         cmd->params.rdeeprom.value = 0;
833
834         return 0;
835 }
836
837 static int wlan_cmd_bt_access(wlan_private * priv,
838                                struct cmd_ds_command *cmd,
839                                u16 cmd_action, void *pdata_buf)
840 {
841         struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
842         lbs_pr_debug(1, "BT CMD(%d)\n", cmd_action);
843
844         cmd->command = cpu_to_le16(cmd_bt_access);
845         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access)
846                                      + S_DS_GEN);
847         cmd->result = 0;
848         bt_access->action = cpu_to_le16(cmd_action);
849
850         switch (cmd_action) {
851         case cmd_act_bt_access_add:
852                 memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
853                 lbs_dbg_hex("BT_ADD: blinded mac address-", bt_access->addr1, 6);
854                 break;
855         case cmd_act_bt_access_del:
856                 memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
857                 lbs_dbg_hex("BT_DEL: blinded mac address-", bt_access->addr1, 6);
858                 break;
859         case cmd_act_bt_access_list:
860                 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
861                 break;
862         case cmd_act_bt_access_reset:
863                 break;
864         default:
865                 break;
866         }
867         return 0;
868 }
869
870 static int wlan_cmd_fwt_access(wlan_private * priv,
871                                struct cmd_ds_command *cmd,
872                                u16 cmd_action, void *pdata_buf)
873 {
874         struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
875         lbs_pr_debug(1, "FWT CMD(%d)\n", cmd_action);
876
877         cmd->command = cpu_to_le16(cmd_fwt_access);
878         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access)
879                                      + S_DS_GEN);
880         cmd->result = 0;
881
882         if (pdata_buf)
883                 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
884         else
885                 memset(fwt_access, 0, sizeof(*fwt_access));
886
887         fwt_access->action = cpu_to_le16(cmd_action);
888
889         return 0;
890 }
891
892 static int wlan_cmd_mesh_access(wlan_private * priv,
893                                 struct cmd_ds_command *cmd,
894                                 u16 cmd_action, void *pdata_buf)
895 {
896         struct cmd_ds_mesh_access *mesh_access = &cmd->params.mesh;
897         lbs_pr_debug(1, "FWT CMD(%d)\n", cmd_action);
898
899         cmd->command = cpu_to_le16(cmd_mesh_access);
900         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mesh_access)
901                                      + S_DS_GEN);
902         cmd->result = 0;
903
904         if (pdata_buf)
905                 memcpy(mesh_access, pdata_buf, sizeof(*mesh_access));
906         else
907                 memset(mesh_access, 0, sizeof(*mesh_access));
908
909         mesh_access->action = cpu_to_le16(cmd_action);
910
911         return 0;
912 }
913
914 void libertas_queue_cmd(wlan_adapter * adapter, struct cmd_ctrl_node *cmdnode, u8 addtail)
915 {
916         unsigned long flags;
917         struct cmd_ds_command *cmdptr;
918
919         ENTER();
920
921         if (!cmdnode) {
922                 lbs_pr_debug(1, "QUEUE_CMD: cmdnode is NULL\n");
923                 goto done;
924         }
925
926         cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
927         if (!cmdptr) {
928                 lbs_pr_debug(1, "QUEUE_CMD: cmdptr is NULL\n");
929                 goto done;
930         }
931
932         /* Exit_PS command needs to be queued in the header always. */
933         if (cmdptr->command == cmd_802_11_ps_mode) {
934                 struct cmd_ds_802_11_ps_mode *psm = &cmdptr->params.psmode;
935                 if (psm->action == cmd_subcmd_exit_ps) {
936                         if (adapter->psstate != PS_STATE_FULL_POWER)
937                                 addtail = 0;
938                 }
939         }
940
941         spin_lock_irqsave(&adapter->driver_lock, flags);
942
943         if (addtail)
944                 list_add_tail((struct list_head *)cmdnode,
945                               &adapter->cmdpendingq);
946         else
947                 list_add((struct list_head *)cmdnode, &adapter->cmdpendingq);
948
949         spin_unlock_irqrestore(&adapter->driver_lock, flags);
950
951         lbs_pr_debug(1, "QUEUE_CMD: Inserted node=%p, cmd=0x%x in cmdpendingq\n",
952                cmdnode,
953                ((struct cmd_ds_gen*)cmdnode->bufvirtualaddr)->command);
954
955 done:
956         LEAVE();
957         return;
958 }
959
960 /*
961  * TODO: Fix the issue when DownloadcommandToStation is being called the
962  * second time when the command timesout. All the cmdptr->xxx are in little
963  * endian and therefore all the comparissions will fail.
964  * For now - we are not performing the endian conversion the second time - but
965  * for PS and DEEP_SLEEP we need to worry
966  */
967 static int DownloadcommandToStation(wlan_private * priv,
968                                     struct cmd_ctrl_node *cmdnode)
969 {
970         unsigned long flags;
971         struct cmd_ds_command *cmdptr;
972         wlan_adapter *adapter = priv->adapter;
973         int ret = 0;
974         u16 cmdsize;
975         u16 command;
976
977         ENTER();
978
979         if (!adapter || !cmdnode) {
980                 lbs_pr_debug(1, "DNLD_CMD: adapter = %p, cmdnode = %p\n",
981                        adapter, cmdnode);
982                 if (cmdnode) {
983                         spin_lock_irqsave(&adapter->driver_lock, flags);
984                         __libertas_cleanup_and_insert_cmd(priv, cmdnode);
985                         spin_unlock_irqrestore(&adapter->driver_lock, flags);
986                 }
987                 ret = -1;
988                 goto done;
989         }
990
991         cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
992
993
994         spin_lock_irqsave(&adapter->driver_lock, flags);
995         if (!cmdptr || !cmdptr->size) {
996                 lbs_pr_debug(1, "DNLD_CMD: cmdptr is Null or cmd size is Zero, "
997                        "Not sending\n");
998                 __libertas_cleanup_and_insert_cmd(priv, cmdnode);
999                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
1000                 ret = -1;
1001                 goto done;
1002         }
1003
1004         adapter->cur_cmd = cmdnode;
1005         adapter->cur_cmd_retcode = 0;
1006         spin_unlock_irqrestore(&adapter->driver_lock, flags);
1007         lbs_pr_debug(1, "DNLD_CMD:: Before download, size of cmd = %d\n",
1008                cmdptr->size);
1009
1010         cmdsize = cmdptr->size;
1011
1012         command = cpu_to_le16(cmdptr->command);
1013
1014         cmdnode->cmdwaitqwoken = 0;
1015         cmdsize = cpu_to_le16(cmdsize);
1016
1017         ret = libertas_sbi_host_to_card(priv, MVMS_CMD, (u8 *) cmdptr, cmdsize);
1018
1019         if (ret != 0) {
1020                 lbs_pr_debug(1, "DNLD_CMD: Host to Card failed\n");
1021                 spin_lock_irqsave(&adapter->driver_lock, flags);
1022                 __libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
1023                 adapter->cur_cmd = NULL;
1024                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
1025                 ret = -1;
1026                 goto done;
1027         }
1028
1029         lbs_pr_debug(1, "DNLD_CMD: Sent command 0x%x @ %lu\n", command, jiffies);
1030         lbs_dbg_hex("DNLD_CMD: command", cmdnode->bufvirtualaddr, cmdsize);
1031
1032         /* Setup the timer after transmit command */
1033         if (command == cmd_802_11_scan
1034             || command == cmd_802_11_authenticate
1035             || command == cmd_802_11_associate)
1036                 mod_timer(&adapter->command_timer, jiffies + (10*HZ));
1037         else
1038                 mod_timer(&adapter->command_timer, jiffies + (5*HZ));
1039
1040         ret = 0;
1041
1042       done:
1043         LEAVE();
1044         return ret;
1045 }
1046
1047 static int wlan_cmd_mac_control(wlan_private * priv,
1048                                 struct cmd_ds_command *cmd)
1049 {
1050         struct cmd_ds_mac_control *mac = &cmd->params.macctrl;
1051
1052         ENTER();
1053
1054         cmd->command = cpu_to_le16(cmd_mac_control);
1055         cmd->size =
1056             cpu_to_le16(sizeof(struct cmd_ds_mac_control) + S_DS_GEN);
1057         mac->action = cpu_to_le16(priv->adapter->currentpacketfilter);
1058
1059         lbs_pr_debug(1, "wlan_cmd_mac_control(): action=0x%X size=%d\n",
1060                mac->action, cmd->size);
1061
1062         LEAVE();
1063         return 0;
1064 }
1065
1066 /**
1067  *  This function inserts command node to cmdfreeq
1068  *  after cleans it. Requires adapter->driver_lock held.
1069  */
1070 void __libertas_cleanup_and_insert_cmd(wlan_private * priv, struct cmd_ctrl_node *ptempcmd)
1071 {
1072         wlan_adapter *adapter = priv->adapter;
1073
1074         if (!ptempcmd)
1075                 goto done;
1076
1077         cleanup_cmdnode(ptempcmd);
1078         list_add_tail((struct list_head *)ptempcmd, &adapter->cmdfreeq);
1079 done:
1080         return;
1081 }
1082
1083 void libertas_cleanup_and_insert_cmd(wlan_private * priv, struct cmd_ctrl_node *ptempcmd)
1084 {
1085         unsigned long flags;
1086
1087         spin_lock_irqsave(&priv->adapter->driver_lock, flags);
1088         __libertas_cleanup_and_insert_cmd(priv, ptempcmd);
1089         spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
1090 }
1091
1092 int libertas_set_radio_control(wlan_private * priv)
1093 {
1094         int ret = 0;
1095
1096         ENTER();
1097
1098         ret = libertas_prepare_and_send_command(priv,
1099                                     cmd_802_11_radio_control,
1100                                     cmd_act_set,
1101                                     cmd_option_waitforrsp, 0, NULL);
1102
1103         lbs_pr_debug(1, "RADIO_SET: on or off: 0x%X, preamble = 0x%X\n",
1104                priv->adapter->radioon, priv->adapter->preamble);
1105
1106         LEAVE();
1107         return ret;
1108 }
1109
1110 int libertas_set_mac_packet_filter(wlan_private * priv)
1111 {
1112         int ret = 0;
1113
1114         ENTER();
1115
1116         lbs_pr_debug(1, "libertas_set_mac_packet_filter value = %x\n",
1117                priv->adapter->currentpacketfilter);
1118
1119         /* Send MAC control command to station */
1120         ret = libertas_prepare_and_send_command(priv,
1121                                     cmd_mac_control, 0, 0, 0, NULL);
1122
1123         LEAVE();
1124         return ret;
1125 }
1126
1127 /**
1128  *  @brief This function prepare the command before send to firmware.
1129  *
1130  *  @param priv         A pointer to wlan_private structure
1131  *  @param cmd_no       command number
1132  *  @param cmd_action   command action: GET or SET
1133  *  @param wait_option  wait option: wait response or not
1134  *  @param cmd_oid      cmd oid: treated as sub command
1135  *  @param pdata_buf    A pointer to informaion buffer
1136  *  @return             0 or -1
1137  */
1138 int libertas_prepare_and_send_command(wlan_private * priv,
1139                           u16 cmd_no,
1140                           u16 cmd_action,
1141                           u16 wait_option, u32 cmd_oid, void *pdata_buf)
1142 {
1143         int ret = 0;
1144         wlan_adapter *adapter = priv->adapter;
1145         struct cmd_ctrl_node *cmdnode;
1146         struct cmd_ds_command *cmdptr;
1147         unsigned long flags;
1148
1149         ENTER();
1150
1151         if (!adapter) {
1152                 lbs_pr_debug(1, "PREP_CMD: adapter is Null\n");
1153                 ret = -1;
1154                 goto done;
1155         }
1156
1157         if (adapter->surpriseremoved) {
1158                 lbs_pr_debug(1, "PREP_CMD: Card is Removed\n");
1159                 ret = -1;
1160                 goto done;
1161         }
1162
1163         cmdnode = libertas_get_free_cmd_ctrl_node(priv);
1164
1165         if (cmdnode == NULL) {
1166                 lbs_pr_debug(1, "PREP_CMD: No free cmdnode\n");
1167
1168                 /* Wake up main thread to execute next command */
1169                 wake_up_interruptible(&priv->mainthread.waitq);
1170                 ret = -1;
1171                 goto done;
1172         }
1173
1174         libertas_set_cmd_ctrl_node(priv, cmdnode, cmd_oid, wait_option, pdata_buf);
1175
1176         cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
1177
1178         lbs_pr_debug(1, "PREP_CMD: Val of cmd ptr=%p, command=0x%X\n",
1179                cmdptr, cmd_no);
1180
1181         if (!cmdptr) {
1182                 lbs_pr_debug(1, "PREP_CMD: bufvirtualaddr of cmdnode is NULL\n");
1183                 libertas_cleanup_and_insert_cmd(priv, cmdnode);
1184                 ret = -1;
1185                 goto done;
1186         }
1187
1188         /* Set sequence number, command and INT option */
1189         adapter->seqnum++;
1190         cmdptr->seqnum = cpu_to_le16(adapter->seqnum);
1191
1192         cmdptr->command = cmd_no;
1193         cmdptr->result = 0;
1194
1195         switch (cmd_no) {
1196         case cmd_get_hw_spec:
1197                 ret = wlan_cmd_hw_spec(priv, cmdptr);
1198                 break;
1199         case cmd_802_11_ps_mode:
1200                 ret = wlan_cmd_802_11_ps_mode(priv, cmdptr, cmd_action);
1201                 break;
1202
1203         case cmd_802_11_scan:
1204                 ret = libertas_cmd_80211_scan(priv, cmdptr, pdata_buf);
1205                 break;
1206
1207         case cmd_mac_control:
1208                 ret = wlan_cmd_mac_control(priv, cmdptr);
1209                 break;
1210
1211         case cmd_802_11_associate:
1212         case cmd_802_11_reassociate:
1213                 ret = libertas_cmd_80211_associate(priv, cmdptr, pdata_buf);
1214                 break;
1215
1216         case cmd_802_11_deauthenticate:
1217                 ret = libertas_cmd_80211_deauthenticate(priv, cmdptr);
1218                 break;
1219
1220         case cmd_802_11_set_wep:
1221                 ret = wlan_cmd_802_11_set_wep(priv, cmdptr, cmd_action, pdata_buf);
1222                 break;
1223
1224         case cmd_802_11_ad_hoc_start:
1225                 ret = libertas_cmd_80211_ad_hoc_start(priv, cmdptr, pdata_buf);
1226                 break;
1227         case cmd_code_dnld:
1228                 break;
1229
1230         case cmd_802_11_reset:
1231                 ret = wlan_cmd_802_11_reset(priv, cmdptr, cmd_action);
1232                 break;
1233
1234         case cmd_802_11_get_log:
1235                 ret = wlan_cmd_802_11_get_log(priv, cmdptr);
1236                 break;
1237
1238         case cmd_802_11_authenticate:
1239                 ret = libertas_cmd_80211_authenticate(priv, cmdptr, pdata_buf);
1240                 break;
1241
1242         case cmd_802_11_get_stat:
1243                 ret = wlan_cmd_802_11_get_stat(priv, cmdptr);
1244                 break;
1245
1246         case cmd_802_11_snmp_mib:
1247                 ret = wlan_cmd_802_11_snmp_mib(priv, cmdptr,
1248                                                cmd_action, cmd_oid, pdata_buf);
1249                 break;
1250
1251         case cmd_mac_reg_access:
1252         case cmd_bbp_reg_access:
1253         case cmd_rf_reg_access:
1254                 ret = wlan_cmd_reg_access(priv, cmdptr, cmd_action, pdata_buf);
1255                 break;
1256
1257         case cmd_802_11_rf_channel:
1258                 ret = wlan_cmd_802_11_rf_channel(priv, cmdptr,
1259                                                  cmd_action, pdata_buf);
1260                 break;
1261
1262         case cmd_802_11_rf_tx_power:
1263                 ret = wlan_cmd_802_11_rf_tx_power(priv, cmdptr,
1264                                                   cmd_action, pdata_buf);
1265                 break;
1266
1267         case cmd_802_11_radio_control:
1268                 ret = wlan_cmd_802_11_radio_control(priv, cmdptr, cmd_action);
1269                 break;
1270
1271         case cmd_802_11_rf_antenna:
1272                 ret = wlan_cmd_802_11_rf_antenna(priv, cmdptr,
1273                                                  cmd_action, pdata_buf);
1274                 break;
1275
1276         case cmd_802_11_data_rate:
1277                 ret = wlan_cmd_802_11_data_rate(priv, cmdptr, cmd_action);
1278                 break;
1279         case cmd_802_11_rate_adapt_rateset:
1280                 ret = wlan_cmd_802_11_rate_adapt_rateset(priv,
1281                                                          cmdptr, cmd_action);
1282                 break;
1283
1284         case cmd_mac_multicast_adr:
1285                 ret = wlan_cmd_mac_multicast_adr(priv, cmdptr, cmd_action);
1286                 break;
1287
1288         case cmd_802_11_ad_hoc_join:
1289                 ret = libertas_cmd_80211_ad_hoc_join(priv, cmdptr, pdata_buf);
1290                 break;
1291
1292         case cmd_802_11_rssi:
1293                 ret = wlan_cmd_802_11_rssi(priv, cmdptr);
1294                 break;
1295
1296         case cmd_802_11_ad_hoc_stop:
1297                 ret = libertas_cmd_80211_ad_hoc_stop(priv, cmdptr);
1298                 break;
1299
1300         case cmd_802_11_enable_rsn:
1301                 ret = wlan_cmd_802_11_enable_rsn(priv, cmdptr, cmd_action);
1302                 break;
1303
1304         case cmd_802_11_key_material:
1305                 ret = wlan_cmd_802_11_key_material(priv, cmdptr,
1306                                                    cmd_action, cmd_oid,
1307                                                    pdata_buf);
1308                 break;
1309
1310         case cmd_802_11_pairwise_tsc:
1311                 break;
1312         case cmd_802_11_group_tsc:
1313                 break;
1314
1315         case cmd_802_11_mac_address:
1316                 ret = wlan_cmd_802_11_mac_address(priv, cmdptr, cmd_action);
1317                 break;
1318
1319         case cmd_802_11_eeprom_access:
1320                 ret = wlan_cmd_802_11_eeprom_access(priv, cmdptr,
1321                                                     cmd_action, pdata_buf);
1322                 break;
1323
1324         case cmd_802_11_set_afc:
1325         case cmd_802_11_get_afc:
1326
1327                 cmdptr->command = cpu_to_le16(cmd_no);
1328                 cmdptr->size =
1329                     cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1330                                      S_DS_GEN);
1331
1332                 memmove(&cmdptr->params.afc,
1333                         pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1334
1335                 ret = 0;
1336                 goto done;
1337
1338         case cmd_802_11d_domain_info:
1339                 ret = libertas_cmd_802_11d_domain_info(priv, cmdptr,
1340                                                    cmd_no, cmd_action);
1341                 break;
1342
1343         case cmd_802_11_sleep_params:
1344                 ret = wlan_cmd_802_11_sleep_params(priv, cmdptr, cmd_action);
1345                 break;
1346         case cmd_802_11_inactivity_timeout:
1347                 ret = wlan_cmd_802_11_inactivity_timeout(priv, cmdptr,
1348                                                          cmd_action, pdata_buf);
1349                 libertas_set_cmd_ctrl_node(priv, cmdnode, 0, 0, pdata_buf);
1350                 break;
1351
1352         case cmd_802_11_tpc_cfg:
1353                 cmdptr->command = cpu_to_le16(cmd_802_11_tpc_cfg);
1354                 cmdptr->size =
1355                     cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1356                                      S_DS_GEN);
1357
1358                 memmove(&cmdptr->params.tpccfg,
1359                         pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1360
1361                 ret = 0;
1362                 break;
1363         case cmd_802_11_led_gpio_ctrl:
1364                 {
1365                         struct mrvlietypes_ledgpio *gpio =
1366                             (struct mrvlietypes_ledgpio*)
1367                             cmdptr->params.ledgpio.data;
1368
1369                         memmove(&cmdptr->params.ledgpio,
1370                                 pdata_buf,
1371                                 sizeof(struct cmd_ds_802_11_led_ctrl));
1372
1373                         cmdptr->command =
1374                             cpu_to_le16(cmd_802_11_led_gpio_ctrl);
1375
1376 #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
1377                         cmdptr->size =
1378                             cpu_to_le16(gpio->header.len + S_DS_GEN +
1379                                              ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
1380                         gpio->header.len = cpu_to_le16(gpio->header.len);
1381
1382                         ret = 0;
1383                         break;
1384                 }
1385         case cmd_802_11_pwr_cfg:
1386                 cmdptr->command = cpu_to_le16(cmd_802_11_pwr_cfg);
1387                 cmdptr->size =
1388                     cpu_to_le16(sizeof(struct cmd_ds_802_11_pwr_cfg) +
1389                                      S_DS_GEN);
1390                 memmove(&cmdptr->params.pwrcfg, pdata_buf,
1391                         sizeof(struct cmd_ds_802_11_pwr_cfg));
1392
1393                 ret = 0;
1394                 break;
1395         case cmd_bt_access:
1396                 ret = wlan_cmd_bt_access(priv, cmdptr, cmd_action, pdata_buf);
1397                 break;
1398
1399         case cmd_fwt_access:
1400                 ret = wlan_cmd_fwt_access(priv, cmdptr, cmd_action, pdata_buf);
1401                 break;
1402
1403         case cmd_mesh_access:
1404                 ret = wlan_cmd_mesh_access(priv, cmdptr, cmd_action, pdata_buf);
1405                 break;
1406
1407         case cmd_get_tsf:
1408                 cmdptr->command = cpu_to_le16(cmd_get_tsf);
1409                 cmdptr->size =
1410                     cpu_to_le16(sizeof(struct cmd_ds_get_tsf)
1411                                      + S_DS_GEN);
1412                 ret = 0;
1413                 break;
1414         case cmd_802_11_tx_rate_query:
1415                 cmdptr->command =
1416                     cpu_to_le16(cmd_802_11_tx_rate_query);
1417                 cmdptr->size =
1418                     cpu_to_le16(sizeof(struct cmd_tx_rate_query) +
1419                                      S_DS_GEN);
1420                 adapter->txrate = 0;
1421                 ret = 0;
1422                 break;
1423         default:
1424                 lbs_pr_debug(1, "PREP_CMD: unknown command- %#x\n", cmd_no);
1425                 ret = -1;
1426                 break;
1427         }
1428
1429         /* return error, since the command preparation failed */
1430         if (ret != 0) {
1431                 lbs_pr_debug(1, "PREP_CMD: command preparation failed\n");
1432                 libertas_cleanup_and_insert_cmd(priv, cmdnode);
1433                 ret = -1;
1434                 goto done;
1435         }
1436
1437         cmdnode->cmdwaitqwoken = 0;
1438
1439         libertas_queue_cmd(adapter, cmdnode, 1);
1440         adapter->nr_cmd_pending++;
1441         wake_up_interruptible(&priv->mainthread.waitq);
1442
1443         if (wait_option & cmd_option_waitforrsp) {
1444                 lbs_pr_debug(1, "PREP_CMD: Wait for CMD response\n");
1445                 might_sleep();
1446                 wait_event_interruptible(cmdnode->cmdwait_q,
1447                                          cmdnode->cmdwaitqwoken);
1448         }
1449
1450         spin_lock_irqsave(&adapter->driver_lock, flags);
1451         if (adapter->cur_cmd_retcode) {
1452                 lbs_pr_debug(1, "PREP_CMD: command failed with return code=%d\n",
1453                        adapter->cur_cmd_retcode);
1454                 adapter->cur_cmd_retcode = 0;
1455                 ret = -1;
1456         }
1457         spin_unlock_irqrestore(&adapter->driver_lock, flags);
1458
1459 done:
1460         LEAVE();
1461         return ret;
1462 }
1463
1464 /**
1465  *  @brief This function allocates the command buffer and link
1466  *  it to command free queue.
1467  *
1468  *  @param priv         A pointer to wlan_private structure
1469  *  @return             0 or -1
1470  */
1471 int libertas_allocate_cmd_buffer(wlan_private * priv)
1472 {
1473         int ret = 0;
1474         u32 ulbufsize;
1475         u32 i;
1476         struct cmd_ctrl_node *tempcmd_array;
1477         u8 *ptempvirtualaddr;
1478         wlan_adapter *adapter = priv->adapter;
1479
1480         ENTER();
1481
1482         /* Allocate and initialize cmdCtrlNode */
1483         ulbufsize = sizeof(struct cmd_ctrl_node) * MRVDRV_NUM_OF_CMD_BUFFER;
1484
1485         if (!(tempcmd_array = kmalloc(ulbufsize, GFP_KERNEL))) {
1486                 lbs_pr_debug(1,
1487                        "ALLOC_CMD_BUF: failed to allocate tempcmd_array\n");
1488                 ret = -1;
1489                 goto done;
1490         }
1491
1492         adapter->cmd_array = tempcmd_array;
1493         memset(adapter->cmd_array, 0, ulbufsize);
1494
1495         /* Allocate and initialize command buffers */
1496         ulbufsize = MRVDRV_SIZE_OF_CMD_BUFFER;
1497         for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1498                 if (!(ptempvirtualaddr = kmalloc(ulbufsize, GFP_KERNEL))) {
1499                         lbs_pr_debug(1,
1500                                "ALLOC_CMD_BUF: ptempvirtualaddr: out of memory\n");
1501                         ret = -1;
1502                         goto done;
1503                 }
1504
1505                 memset(ptempvirtualaddr, 0, ulbufsize);
1506
1507                 /* Update command buffer virtual */
1508                 tempcmd_array[i].bufvirtualaddr = ptempvirtualaddr;
1509         }
1510
1511         for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1512                 init_waitqueue_head(&tempcmd_array[i].cmdwait_q);
1513                 libertas_cleanup_and_insert_cmd(priv, &tempcmd_array[i]);
1514         }
1515
1516         ret = 0;
1517       done:
1518         LEAVE();
1519         return ret;
1520 }
1521
1522 /**
1523  *  @brief This function frees the command buffer.
1524  *
1525  *  @param priv         A pointer to wlan_private structure
1526  *  @return             0 or -1
1527  */
1528 int libertas_free_cmd_buffer(wlan_private * priv)
1529 {
1530         u32 ulbufsize;
1531         unsigned int i;
1532         struct cmd_ctrl_node *tempcmd_array;
1533         wlan_adapter *adapter = priv->adapter;
1534
1535         ENTER();
1536
1537         /* need to check if cmd array is allocated or not */
1538         if (adapter->cmd_array == NULL) {
1539                 lbs_pr_debug(1, "FREE_CMD_BUF: cmd_array is Null\n");
1540                 goto done;
1541         }
1542
1543         tempcmd_array = adapter->cmd_array;
1544
1545         /* Release shared memory buffers */
1546         ulbufsize = MRVDRV_SIZE_OF_CMD_BUFFER;
1547         for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1548                 if (tempcmd_array[i].bufvirtualaddr) {
1549                         lbs_pr_debug(1, "Free all the array\n");
1550                         kfree(tempcmd_array[i].bufvirtualaddr);
1551                         tempcmd_array[i].bufvirtualaddr = NULL;
1552                 }
1553         }
1554
1555         /* Release cmd_ctrl_node */
1556         if (adapter->cmd_array) {
1557                 lbs_pr_debug(1, "Free cmd_array\n");
1558                 kfree(adapter->cmd_array);
1559                 adapter->cmd_array = NULL;
1560         }
1561
1562 done:
1563         LEAVE();
1564         return 0;
1565 }
1566
1567 /**
1568  *  @brief This function gets a free command node if available in
1569  *  command free queue.
1570  *
1571  *  @param priv         A pointer to wlan_private structure
1572  *  @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1573  */
1574 struct cmd_ctrl_node *libertas_get_free_cmd_ctrl_node(wlan_private * priv)
1575 {
1576         struct cmd_ctrl_node *tempnode;
1577         wlan_adapter *adapter = priv->adapter;
1578         unsigned long flags;
1579
1580         if (!adapter)
1581                 return NULL;
1582
1583         spin_lock_irqsave(&adapter->driver_lock, flags);
1584
1585         if (!list_empty(&adapter->cmdfreeq)) {
1586                 tempnode = (struct cmd_ctrl_node *)adapter->cmdfreeq.next;
1587                 list_del((struct list_head *)tempnode);
1588         } else {
1589                 lbs_pr_debug(1, "GET_CMD_NODE: cmd_ctrl_node is not available\n");
1590                 tempnode = NULL;
1591         }
1592
1593         spin_unlock_irqrestore(&adapter->driver_lock, flags);
1594
1595         if (tempnode) {
1596                 lbs_pr_debug(3, "GET_CMD_NODE: cmdCtrlNode available\n");
1597                 lbs_pr_debug(3, "GET_CMD_NODE: cmdCtrlNode Address = %p\n",
1598                        tempnode);
1599                 cleanup_cmdnode(tempnode);
1600         }
1601
1602         return tempnode;
1603 }
1604
1605 /**
1606  *  @brief This function cleans command node.
1607  *
1608  *  @param ptempnode    A pointer to cmdCtrlNode structure
1609  *  @return             n/a
1610  */
1611 static void cleanup_cmdnode(struct cmd_ctrl_node *ptempnode)
1612 {
1613         if (!ptempnode)
1614                 return;
1615         ptempnode->cmdwaitqwoken = 1;
1616         wake_up_interruptible(&ptempnode->cmdwait_q);
1617         ptempnode->status = 0;
1618         ptempnode->cmd_oid = (u32) 0;
1619         ptempnode->wait_option = 0;
1620         ptempnode->pdata_buf = NULL;
1621
1622         if (ptempnode->bufvirtualaddr != NULL)
1623                 memset(ptempnode->bufvirtualaddr, 0, MRVDRV_SIZE_OF_CMD_BUFFER);
1624         return;
1625 }
1626
1627 /**
1628  *  @brief This function initializes the command node.
1629  *
1630  *  @param priv         A pointer to wlan_private structure
1631  *  @param ptempnode    A pointer to cmd_ctrl_node structure
1632  *  @param cmd_oid      cmd oid: treated as sub command
1633  *  @param wait_option  wait option: wait response or not
1634  *  @param pdata_buf    A pointer to informaion buffer
1635  *  @return             0 or -1
1636  */
1637 void libertas_set_cmd_ctrl_node(wlan_private * priv,
1638                     struct cmd_ctrl_node *ptempnode,
1639                     u32 cmd_oid, u16 wait_option, void *pdata_buf)
1640 {
1641         ENTER();
1642
1643         if (!ptempnode)
1644                 return;
1645
1646         ptempnode->cmd_oid = cmd_oid;
1647         ptempnode->wait_option = wait_option;
1648         ptempnode->pdata_buf = pdata_buf;
1649
1650         LEAVE();
1651 }
1652
1653 /**
1654  *  @brief This function executes next command in command
1655  *  pending queue. It will put fimware back to PS mode
1656  *  if applicable.
1657  *
1658  *  @param priv     A pointer to wlan_private structure
1659  *  @return        0 or -1
1660  */
1661 int libertas_execute_next_command(wlan_private * priv)
1662 {
1663         wlan_adapter *adapter = priv->adapter;
1664         struct cmd_ctrl_node *cmdnode = NULL;
1665         struct cmd_ds_command *cmdptr;
1666         unsigned long flags;
1667         int ret = 0;
1668
1669         lbs_pr_debug(1, "libertas_execute_next_command\n");
1670
1671         spin_lock_irqsave(&adapter->driver_lock, flags);
1672
1673         if (adapter->cur_cmd) {
1674                 lbs_pr_alert( "EXEC_NEXT_CMD: there is command in processing!\n");
1675                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
1676                 ret = -1;
1677                 goto done;
1678         }
1679
1680         if (!list_empty(&adapter->cmdpendingq)) {
1681                 cmdnode = (struct cmd_ctrl_node *)
1682                     adapter->cmdpendingq.next;
1683         }
1684
1685         spin_unlock_irqrestore(&adapter->driver_lock, flags);
1686
1687         if (cmdnode) {
1688                 lbs_pr_debug(1,
1689                        "EXEC_NEXT_CMD: Got next command from cmdpendingq\n");
1690                 cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
1691
1692                 if (is_command_allowed_in_ps(cmdptr->command)) {
1693                         if ((adapter->psstate == PS_STATE_SLEEP)
1694                             || (adapter->psstate == PS_STATE_PRE_SLEEP)
1695                             ) {
1696                                 lbs_pr_debug(1,
1697                                        "EXEC_NEXT_CMD: Cannot send cmd 0x%x in psstate %d\n",
1698                                        cmdptr->command, adapter->psstate);
1699                                 ret = -1;
1700                                 goto done;
1701                         }
1702                         lbs_pr_debug(1, "EXEC_NEXT_CMD: OK to send command "
1703                                "0x%x in psstate %d\n",
1704                                cmdptr->command, adapter->psstate);
1705                 } else if (adapter->psstate != PS_STATE_FULL_POWER) {
1706                         /*
1707                          * 1. Non-PS command:
1708                          * Queue it. set needtowakeup to TRUE if current state
1709                          * is SLEEP, otherwise call libertas_ps_wakeup to send Exit_PS.
1710                          * 2. PS command but not Exit_PS:
1711                          * Ignore it.
1712                          * 3. PS command Exit_PS:
1713                          * Set needtowakeup to TRUE if current state is SLEEP,
1714                          * otherwise send this command down to firmware
1715                          * immediately.
1716                          */
1717                         if (cmdptr->command !=
1718                             cpu_to_le16(cmd_802_11_ps_mode)) {
1719                                 /*  Prepare to send Exit PS,
1720                                  *  this non PS command will be sent later */
1721                                 if ((adapter->psstate == PS_STATE_SLEEP)
1722                                     || (adapter->psstate == PS_STATE_PRE_SLEEP)
1723                                     ) {
1724                                         /* w/ new scheme, it will not reach here.
1725                                            since it is blocked in main_thread. */
1726                                         adapter->needtowakeup = 1;
1727                                 } else
1728                                         libertas_ps_wakeup(priv, 0);
1729
1730                                 ret = 0;
1731                                 goto done;
1732                         } else {
1733                                 /*
1734                                  * PS command. Ignore it if it is not Exit_PS.
1735                                  * otherwise send it down immediately.
1736                                  */
1737                                 struct cmd_ds_802_11_ps_mode *psm =
1738                                     &cmdptr->params.psmode;
1739
1740                                 lbs_pr_debug(1,
1741                                        "EXEC_NEXT_CMD: PS cmd- action=0x%x\n",
1742                                        psm->action);
1743                                 if (psm->action !=
1744                                     cpu_to_le16(cmd_subcmd_exit_ps)) {
1745                                         lbs_pr_debug(1,
1746                                                "EXEC_NEXT_CMD: Ignore Enter PS cmd\n");
1747                                         list_del((struct list_head *)cmdnode);
1748                                         libertas_cleanup_and_insert_cmd(priv, cmdnode);
1749
1750                                         ret = 0;
1751                                         goto done;
1752                                 }
1753
1754                                 if ((adapter->psstate == PS_STATE_SLEEP)
1755                                     || (adapter->psstate == PS_STATE_PRE_SLEEP)
1756                                     ) {
1757                                         lbs_pr_debug(1,
1758                                                "EXEC_NEXT_CMD: Ignore ExitPS cmd in sleep\n");
1759                                         list_del((struct list_head *)cmdnode);
1760                                         libertas_cleanup_and_insert_cmd(priv, cmdnode);
1761                                         adapter->needtowakeup = 1;
1762
1763                                         ret = 0;
1764                                         goto done;
1765                                 }
1766
1767                                 lbs_pr_debug(1,
1768                                        "EXEC_NEXT_CMD: Sending Exit_PS down...\n");
1769                         }
1770                 }
1771                 list_del((struct list_head *)cmdnode);
1772                 lbs_pr_debug(1, "EXEC_NEXT_CMD: Sending 0x%04X command\n",
1773                        cmdptr->command);
1774                 DownloadcommandToStation(priv, cmdnode);
1775         } else {
1776                 /*
1777                  * check if in power save mode, if yes, put the device back
1778                  * to PS mode
1779                  */
1780                 if ((adapter->psmode != wlan802_11powermodecam) &&
1781                     (adapter->psstate == PS_STATE_FULL_POWER) &&
1782                     (adapter->connect_status == libertas_connected)) {
1783                         if (adapter->secinfo.WPAenabled
1784                             || adapter->secinfo.WPA2enabled) {
1785                                 /* check for valid WPA group keys */
1786                                 if (adapter->wpa_mcast_key.len
1787                                     || adapter->wpa_unicast_key.len) {
1788                                         lbs_pr_debug(1,
1789                                                "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1790                                                " go back to PS_SLEEP");
1791                                         libertas_ps_sleep(priv, 0);
1792                                 }
1793                         } else {
1794                                 lbs_pr_debug(1,
1795                                        "EXEC_NEXT_CMD: command PendQ is empty,"
1796                                        " go back to PS_SLEEP");
1797                                 libertas_ps_sleep(priv, 0);
1798                         }
1799                 }
1800         }
1801
1802         ret = 0;
1803 done:
1804         return ret;
1805 }
1806
1807 void libertas_send_iwevcustom_event(wlan_private * priv, s8 * str)
1808 {
1809         union iwreq_data iwrq;
1810         u8 buf[50];
1811
1812         ENTER();
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_pr_debug(1, "Event Indication string = %s\n",
1823                (char *)buf);
1824         lbs_pr_debug(1, "Event Indication String length = %d\n", iwrq.data.length);
1825
1826         lbs_pr_debug(1, "Sending wireless event IWEVCUSTOM for %s\n", str);
1827         wireless_send_event(priv->wlan_dev.netdev, IWEVCUSTOM, &iwrq, buf);
1828
1829         LEAVE();
1830         return;
1831 }
1832
1833 static int sendconfirmsleep(wlan_private * priv, u8 * cmdptr, u16 size)
1834 {
1835         unsigned long flags;
1836         wlan_adapter *adapter = priv->adapter;
1837         int ret = 0;
1838
1839         ENTER();
1840
1841         lbs_pr_debug(1, "SEND_SLEEPC_CMD: Before download, size of cmd = %d\n",
1842                size);
1843
1844         lbs_dbg_hex("SEND_SLEEPC_CMD: Sleep confirm command", cmdptr, size);
1845
1846         ret = libertas_sbi_host_to_card(priv, MVMS_CMD, cmdptr, size);
1847         priv->wlan_dev.dnld_sent = DNLD_RES_RECEIVED;
1848
1849         spin_lock_irqsave(&adapter->driver_lock, flags);
1850         if (adapter->intcounter || adapter->currenttxskb)
1851                 lbs_pr_debug(1, "SEND_SLEEPC_CMD: intcounter=%d currenttxskb=%p\n",
1852                        adapter->intcounter, adapter->currenttxskb);
1853         spin_unlock_irqrestore(&adapter->driver_lock, flags);
1854
1855         if (ret) {
1856                 lbs_pr_alert(
1857                        "SEND_SLEEPC_CMD: Host to Card failed for Confirm Sleep\n");
1858         } else {
1859                 spin_lock_irqsave(&adapter->driver_lock, flags);
1860                 if (!adapter->intcounter) {
1861                         adapter->psstate = PS_STATE_SLEEP;
1862                 } else {
1863                         lbs_pr_debug(1, "SEND_SLEEPC_CMD: After sent,IntC=%d\n",
1864                                adapter->intcounter);
1865                 }
1866                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
1867
1868                 lbs_pr_debug(1, "SEND_SLEEPC_CMD: Sent Confirm Sleep command\n");
1869                 lbs_pr_debug(1, "+");
1870         }
1871
1872         LEAVE();
1873         return ret;
1874 }
1875
1876 void libertas_ps_sleep(wlan_private * priv, int wait_option)
1877 {
1878
1879         ENTER();
1880
1881         /*
1882          * PS is currently supported only in Infrastructure mode
1883          * Remove this check if it is to be supported in IBSS mode also
1884          */
1885
1886         libertas_prepare_and_send_command(priv, cmd_802_11_ps_mode,
1887                               cmd_subcmd_enter_ps, wait_option, 0, NULL);
1888
1889         LEAVE();
1890         return;
1891 }
1892
1893 /**
1894  *  @brief This function sends Eixt_PS command to firmware.
1895  *
1896  *  @param priv         A pointer to wlan_private structure
1897  *  @param wait_option  wait response or not
1898  *  @return             n/a
1899  */
1900 void libertas_ps_wakeup(wlan_private * priv, int wait_option)
1901 {
1902         enum WLAN_802_11_POWER_MODE Localpsmode;
1903
1904         ENTER();
1905
1906         Localpsmode = wlan802_11powermodecam;
1907
1908         lbs_pr_debug(1, "Exit_PS: Localpsmode = %d\n", Localpsmode);
1909
1910         libertas_prepare_and_send_command(priv, cmd_802_11_ps_mode,
1911                               cmd_subcmd_exit_ps,
1912                               wait_option, 0, &Localpsmode);
1913
1914         LEAVE();
1915         return;
1916 }
1917
1918 /**
1919  *  @brief This function checks condition and prepares to
1920  *  send sleep confirm command to firmware if ok.
1921  *
1922  *  @param priv         A pointer to wlan_private structure
1923  *  @param psmode       Power Saving mode
1924  *  @return             n/a
1925  */
1926 void libertas_ps_confirm_sleep(wlan_private * priv, u16 psmode)
1927 {
1928         unsigned long flags =0;
1929         wlan_adapter *adapter = priv->adapter;
1930         u8 allowed = 1;
1931
1932         ENTER();
1933
1934         if (priv->wlan_dev.dnld_sent) {
1935                 allowed = 0;
1936                 lbs_pr_debug(1, "D");
1937         }
1938
1939         spin_lock_irqsave(&adapter->driver_lock, flags);
1940         if (adapter->cur_cmd) {
1941                 allowed = 0;
1942                 lbs_pr_debug(1, "C");
1943         }
1944         if (adapter->intcounter > 0) {
1945                 allowed = 0;
1946                 lbs_pr_debug(1, "I%d", adapter->intcounter);
1947         }
1948         spin_unlock_irqrestore(&adapter->driver_lock, flags);
1949
1950         if (allowed) {
1951                 lbs_pr_debug(1, "Sending libertas_ps_confirm_sleep\n");
1952                 sendconfirmsleep(priv, (u8 *) & adapter->libertas_ps_confirm_sleep,
1953                                  sizeof(struct PS_CMD_ConfirmSleep));
1954         } else {
1955                 lbs_pr_debug(1, "Sleep Confirm has been delayed\n");
1956         }
1957
1958         LEAVE();
1959 }