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