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