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