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