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