libertas: TX packet is radiotap iff it comes from rtap_dev
[linux-2.6] / drivers / net / wireless / libertas / cmdresp.c
1 /**
2   * This file contains the handling of command
3   * responses as well as events generated by firmware.
4   */
5 #include <linux/delay.h>
6 #include <linux/if_arp.h>
7 #include <linux/netdevice.h>
8
9 #include <net/iw_handler.h>
10
11 #include "host.h"
12 #include "decl.h"
13 #include "defs.h"
14 #include "dev.h"
15 #include "join.h"
16 #include "wext.h"
17
18 /**
19  *  @brief This function handles disconnect event. it
20  *  reports disconnect to upper layer, clean tx/rx packets,
21  *  reset link state etc.
22  *
23  *  @param priv    A pointer to struct lbs_private structure
24  *  @return        n/a
25  */
26 void lbs_mac_event_disconnected(struct lbs_private *priv)
27 {
28         union iwreq_data wrqu;
29
30         if (priv->connect_status != LBS_CONNECTED)
31                 return;
32
33         lbs_deb_enter(LBS_DEB_ASSOC);
34
35         memset(wrqu.ap_addr.sa_data, 0x00, ETH_ALEN);
36         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
37
38         /*
39          * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
40          * It causes problem in the Supplicant
41          */
42
43         msleep_interruptible(1000);
44         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
45
46         /* Free Tx and Rx packets */
47         kfree_skb(priv->currenttxskb);
48         priv->currenttxskb = NULL;
49
50         /* report disconnect to upper layer */
51         netif_stop_queue(priv->dev);
52         netif_carrier_off(priv->dev);
53
54         /* reset SNR/NF/RSSI values */
55         memset(priv->SNR, 0x00, sizeof(priv->SNR));
56         memset(priv->NF, 0x00, sizeof(priv->NF));
57         memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
58         memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
59         memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
60         priv->nextSNRNF = 0;
61         priv->numSNRNF = 0;
62         priv->connect_status = LBS_DISCONNECTED;
63
64         /* Clear out associated SSID and BSSID since connection is
65          * no longer valid.
66          */
67         memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
68         memset(&priv->curbssparams.ssid, 0, IW_ESSID_MAX_SIZE);
69         priv->curbssparams.ssid_len = 0;
70
71         if (priv->psstate != PS_STATE_FULL_POWER) {
72                 /* make firmware to exit PS mode */
73                 lbs_deb_cmd("disconnected, so exit PS mode\n");
74                 lbs_ps_wakeup(priv, 0);
75         }
76         lbs_deb_leave(LBS_DEB_CMD);
77 }
78
79 /**
80  *  @brief This function handles MIC failure event.
81  *
82  *  @param priv    A pointer to struct lbs_private structure
83  *  @para  event   the event id
84  *  @return        n/a
85  */
86 static void handle_mic_failureevent(struct lbs_private *priv, u32 event)
87 {
88         char buf[50];
89
90         lbs_deb_enter(LBS_DEB_CMD);
91         memset(buf, 0, sizeof(buf));
92
93         sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");
94
95         if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
96                 strcat(buf, "unicast ");
97         } else {
98                 strcat(buf, "multicast ");
99         }
100
101         lbs_send_iwevcustom_event(priv, buf);
102         lbs_deb_leave(LBS_DEB_CMD);
103 }
104
105 static int lbs_ret_reg_access(struct lbs_private *priv,
106                                u16 type, struct cmd_ds_command *resp)
107 {
108         int ret = 0;
109
110         lbs_deb_enter(LBS_DEB_CMD);
111
112         switch (type) {
113         case CMD_RET(CMD_MAC_REG_ACCESS):
114                 {
115                         struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
116
117                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
118                         priv->offsetvalue.value = le32_to_cpu(reg->value);
119                         break;
120                 }
121
122         case CMD_RET(CMD_BBP_REG_ACCESS):
123                 {
124                         struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
125
126                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
127                         priv->offsetvalue.value = reg->value;
128                         break;
129                 }
130
131         case CMD_RET(CMD_RF_REG_ACCESS):
132                 {
133                         struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
134
135                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
136                         priv->offsetvalue.value = reg->value;
137                         break;
138                 }
139
140         default:
141                 ret = -1;
142         }
143
144         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
145         return ret;
146 }
147
148 static int lbs_ret_get_hw_spec(struct lbs_private *priv,
149                                 struct cmd_ds_command *resp)
150 {
151         u32 i;
152         struct cmd_ds_get_hw_spec *hwspec = &resp->params.hwspec;
153         int ret = 0;
154         DECLARE_MAC_BUF(mac);
155
156         lbs_deb_enter(LBS_DEB_CMD);
157
158         priv->fwcapinfo = le32_to_cpu(hwspec->fwcapinfo);
159
160         memcpy(priv->fwreleasenumber, hwspec->fwreleasenumber, 4);
161
162         lbs_deb_cmd("GET_HW_SPEC: firmware release %u.%u.%up%u\n",
163                     priv->fwreleasenumber[2], priv->fwreleasenumber[1],
164                     priv->fwreleasenumber[0], priv->fwreleasenumber[3]);
165         lbs_deb_cmd("GET_HW_SPEC: MAC addr %s\n",
166                     print_mac(mac, hwspec->permanentaddr));
167         lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
168                hwspec->hwifversion, hwspec->version);
169
170         /* Clamp region code to 8-bit since FW spec indicates that it should
171          * only ever be 8-bit, even though the field size is 16-bit.  Some firmware
172          * returns non-zero high 8 bits here.
173          */
174         priv->regioncode = le16_to_cpu(hwspec->regioncode) & 0xFF;
175
176         for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
177                 /* use the region code to search for the index */
178                 if (priv->regioncode == lbs_region_code_to_index[i]) {
179                         break;
180                 }
181         }
182
183         /* if it's unidentified region code, use the default (USA) */
184         if (i >= MRVDRV_MAX_REGION_CODE) {
185                 priv->regioncode = 0x10;
186                 lbs_pr_info("unidentified region code; using the default (USA)\n");
187         }
188
189         if (priv->current_addr[0] == 0xff)
190                 memmove(priv->current_addr, hwspec->permanentaddr, ETH_ALEN);
191
192         memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
193         if (priv->mesh_dev)
194                 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
195
196         if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
197                 ret = -1;
198                 goto done;
199         }
200
201         if (lbs_set_universaltable(priv, 0)) {
202                 ret = -1;
203                 goto done;
204         }
205
206 done:
207         lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
208         return ret;
209 }
210
211 static int lbs_ret_802_11_sleep_params(struct lbs_private *priv,
212                                         struct cmd_ds_command *resp)
213 {
214         struct cmd_ds_802_11_sleep_params *sp = &resp->params.sleep_params;
215
216         lbs_deb_enter(LBS_DEB_CMD);
217
218         lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, calcontrol 0x%x "
219                     "extsleepclk 0x%x\n", le16_to_cpu(sp->error),
220                     le16_to_cpu(sp->offset), le16_to_cpu(sp->stabletime),
221                     sp->calcontrol, sp->externalsleepclk);
222
223         priv->sp.sp_error = le16_to_cpu(sp->error);
224         priv->sp.sp_offset = le16_to_cpu(sp->offset);
225         priv->sp.sp_stabletime = le16_to_cpu(sp->stabletime);
226         priv->sp.sp_calcontrol = sp->calcontrol;
227         priv->sp.sp_extsleepclk = sp->externalsleepclk;
228         priv->sp.sp_reserved = le16_to_cpu(sp->reserved);
229
230         lbs_deb_enter(LBS_DEB_CMD);
231         return 0;
232 }
233
234 static int lbs_ret_802_11_stat(struct lbs_private *priv,
235                                 struct cmd_ds_command *resp)
236 {
237         lbs_deb_enter(LBS_DEB_CMD);
238 /*      currently priv->wlan802_11Stat is unused
239
240         struct cmd_ds_802_11_get_stat *p11Stat = &resp->params.gstat;
241
242         // TODO Convert it to Big endian befor copy
243         memcpy(&priv->wlan802_11Stat,
244                p11Stat, sizeof(struct cmd_ds_802_11_get_stat));
245 */
246         lbs_deb_leave(LBS_DEB_CMD);
247         return 0;
248 }
249
250 static int lbs_ret_802_11_snmp_mib(struct lbs_private *priv,
251                                     struct cmd_ds_command *resp)
252 {
253         struct cmd_ds_802_11_snmp_mib *smib = &resp->params.smib;
254         u16 oid = le16_to_cpu(smib->oid);
255         u16 querytype = le16_to_cpu(smib->querytype);
256
257         lbs_deb_enter(LBS_DEB_CMD);
258
259         lbs_deb_cmd("SNMP_RESP: oid 0x%x, querytype 0x%x\n", oid,
260                querytype);
261         lbs_deb_cmd("SNMP_RESP: Buf size %d\n", le16_to_cpu(smib->bufsize));
262
263         if (querytype == CMD_ACT_GET) {
264                 switch (oid) {
265                 case FRAGTHRESH_I:
266                         priv->fragthsd =
267                                 le16_to_cpu(*((__le16 *)(smib->value)));
268                         lbs_deb_cmd("SNMP_RESP: frag threshold %u\n",
269                                     priv->fragthsd);
270                         break;
271                 case RTSTHRESH_I:
272                         priv->rtsthsd =
273                                 le16_to_cpu(*((__le16 *)(smib->value)));
274                         lbs_deb_cmd("SNMP_RESP: rts threshold %u\n",
275                                     priv->rtsthsd);
276                         break;
277                 case SHORT_RETRYLIM_I:
278                         priv->txretrycount =
279                                 le16_to_cpu(*((__le16 *)(smib->value)));
280                         lbs_deb_cmd("SNMP_RESP: tx retry count %u\n",
281                                     priv->rtsthsd);
282                         break;
283                 default:
284                         break;
285                 }
286         }
287
288         lbs_deb_enter(LBS_DEB_CMD);
289         return 0;
290 }
291
292 static int lbs_ret_802_11_key_material(struct lbs_private *priv,
293                                         struct cmd_ds_command *resp)
294 {
295         struct cmd_ds_802_11_key_material *pkeymaterial =
296             &resp->params.keymaterial;
297         u16 action = le16_to_cpu(pkeymaterial->action);
298
299         lbs_deb_enter(LBS_DEB_CMD);
300
301         /* Copy the returned key to driver private data */
302         if (action == CMD_ACT_GET) {
303                 u8 * buf_ptr = (u8 *) &pkeymaterial->keyParamSet;
304                 u8 * resp_end = (u8 *) (resp + le16_to_cpu(resp->size));
305
306                 while (buf_ptr < resp_end) {
307                         struct MrvlIEtype_keyParamSet * pkeyparamset =
308                             (struct MrvlIEtype_keyParamSet *) buf_ptr;
309                         struct enc_key * pkey;
310                         u16 param_set_len = le16_to_cpu(pkeyparamset->length);
311                         u16 key_len = le16_to_cpu(pkeyparamset->keylen);
312                         u16 key_flags = le16_to_cpu(pkeyparamset->keyinfo);
313                         u16 key_type = le16_to_cpu(pkeyparamset->keytypeid);
314                         u8 * end;
315
316                         end = (u8 *) pkeyparamset + sizeof (pkeyparamset->type)
317                                                   + sizeof (pkeyparamset->length)
318                                                   + param_set_len;
319                         /* Make sure we don't access past the end of the IEs */
320                         if (end > resp_end)
321                                 break;
322
323                         if (key_flags & KEY_INFO_WPA_UNICAST)
324                                 pkey = &priv->wpa_unicast_key;
325                         else if (key_flags & KEY_INFO_WPA_MCAST)
326                                 pkey = &priv->wpa_mcast_key;
327                         else
328                                 break;
329
330                         /* Copy returned key into driver */
331                         memset(pkey, 0, sizeof(struct enc_key));
332                         if (key_len > sizeof(pkey->key))
333                                 break;
334                         pkey->type = key_type;
335                         pkey->flags = key_flags;
336                         pkey->len = key_len;
337                         memcpy(pkey->key, pkeyparamset->key, pkey->len);
338
339                         buf_ptr = end + 1;
340                 }
341         }
342
343         lbs_deb_enter(LBS_DEB_CMD);
344         return 0;
345 }
346
347 static int lbs_ret_802_11_mac_address(struct lbs_private *priv,
348                                        struct cmd_ds_command *resp)
349 {
350         struct cmd_ds_802_11_mac_address *macadd = &resp->params.macadd;
351
352         lbs_deb_enter(LBS_DEB_CMD);
353
354         memcpy(priv->current_addr, macadd->macadd, ETH_ALEN);
355
356         lbs_deb_enter(LBS_DEB_CMD);
357         return 0;
358 }
359
360 static int lbs_ret_802_11_rf_tx_power(struct lbs_private *priv,
361                                        struct cmd_ds_command *resp)
362 {
363         struct cmd_ds_802_11_rf_tx_power *rtp = &resp->params.txp;
364
365         lbs_deb_enter(LBS_DEB_CMD);
366
367         priv->txpowerlevel = le16_to_cpu(rtp->currentlevel);
368
369         lbs_deb_cmd("TX power currently %d\n", priv->txpowerlevel);
370
371         lbs_deb_leave(LBS_DEB_CMD);
372         return 0;
373 }
374
375 static int lbs_ret_802_11_rate_adapt_rateset(struct lbs_private *priv,
376                                               struct cmd_ds_command *resp)
377 {
378         struct cmd_ds_802_11_rate_adapt_rateset *rates = &resp->params.rateset;
379
380         lbs_deb_enter(LBS_DEB_CMD);
381
382         if (rates->action == CMD_ACT_GET) {
383                 priv->enablehwauto = le16_to_cpu(rates->enablehwauto);
384                 priv->ratebitmap = le16_to_cpu(rates->bitmap);
385         }
386
387         lbs_deb_leave(LBS_DEB_CMD);
388         return 0;
389 }
390
391 static int lbs_ret_802_11_data_rate(struct lbs_private *priv,
392                                      struct cmd_ds_command *resp)
393 {
394         struct cmd_ds_802_11_data_rate *pdatarate = &resp->params.drate;
395
396         lbs_deb_enter(LBS_DEB_CMD);
397
398         lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) pdatarate,
399                 sizeof(struct cmd_ds_802_11_data_rate));
400
401         /* FIXME: get actual rates FW can do if this command actually returns
402          * all data rates supported.
403          */
404         priv->cur_rate = lbs_fw_index_to_data_rate(pdatarate->rates[0]);
405         lbs_deb_cmd("DATA_RATE: current rate 0x%02x\n", priv->cur_rate);
406
407         lbs_deb_leave(LBS_DEB_CMD);
408         return 0;
409 }
410
411 static int lbs_ret_802_11_rf_channel(struct lbs_private *priv,
412                                       struct cmd_ds_command *resp)
413 {
414         struct cmd_ds_802_11_rf_channel *rfchannel = &resp->params.rfchannel;
415         u16 action = le16_to_cpu(rfchannel->action);
416         u16 newchannel = le16_to_cpu(rfchannel->currentchannel);
417
418         lbs_deb_enter(LBS_DEB_CMD);
419
420         if (action == CMD_OPT_802_11_RF_CHANNEL_GET
421             && priv->curbssparams.channel != newchannel) {
422                 lbs_deb_cmd("channel switch from %d to %d\n",
423                        priv->curbssparams.channel, newchannel);
424
425                 /* Update the channel again */
426                 priv->curbssparams.channel = newchannel;
427         }
428
429         lbs_deb_enter(LBS_DEB_CMD);
430         return 0;
431 }
432
433 static int lbs_ret_802_11_rssi(struct lbs_private *priv,
434                                 struct cmd_ds_command *resp)
435 {
436         struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
437
438         lbs_deb_enter(LBS_DEB_CMD);
439
440         /* store the non average value */
441         priv->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
442         priv->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
443
444         priv->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
445         priv->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
446
447         priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
448             CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
449                      priv->NF[TYPE_BEACON][TYPE_NOAVG]);
450
451         priv->RSSI[TYPE_BEACON][TYPE_AVG] =
452             CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
453                      priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
454
455         lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
456                priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
457                priv->RSSI[TYPE_BEACON][TYPE_AVG]);
458
459         lbs_deb_leave(LBS_DEB_CMD);
460         return 0;
461 }
462
463 static int lbs_ret_802_11_eeprom_access(struct lbs_private *priv,
464                                   struct cmd_ds_command *resp)
465 {
466         struct lbs_ioctl_regrdwr *pbuf;
467         pbuf = (struct lbs_ioctl_regrdwr *) priv->prdeeprom;
468
469         lbs_deb_enter_args(LBS_DEB_CMD, "len %d",
470                le16_to_cpu(resp->params.rdeeprom.bytecount));
471         if (pbuf->NOB < le16_to_cpu(resp->params.rdeeprom.bytecount)) {
472                 pbuf->NOB = 0;
473                 lbs_deb_cmd("EEPROM read length too big\n");
474                 return -1;
475         }
476         pbuf->NOB = le16_to_cpu(resp->params.rdeeprom.bytecount);
477         if (pbuf->NOB > 0) {
478
479                 memcpy(&pbuf->value, (u8 *) & resp->params.rdeeprom.value,
480                        le16_to_cpu(resp->params.rdeeprom.bytecount));
481                 lbs_deb_hex(LBS_DEB_CMD, "EEPROM", (char *)&pbuf->value,
482                         le16_to_cpu(resp->params.rdeeprom.bytecount));
483         }
484         lbs_deb_leave(LBS_DEB_CMD);
485         return 0;
486 }
487
488 static int lbs_ret_get_log(struct lbs_private *priv,
489                             struct cmd_ds_command *resp)
490 {
491         struct cmd_ds_802_11_get_log *logmessage = &resp->params.glog;
492
493         lbs_deb_enter(LBS_DEB_CMD);
494
495         /* Stored little-endian */
496         memcpy(&priv->logmsg, logmessage, sizeof(struct cmd_ds_802_11_get_log));
497
498         lbs_deb_leave(LBS_DEB_CMD);
499         return 0;
500 }
501
502 static int lbs_ret_802_11_enable_rsn(struct lbs_private *priv,
503                                           struct cmd_ds_command *resp)
504 {
505         struct cmd_ds_802_11_enable_rsn *enable_rsn = &resp->params.enbrsn;
506         u32 * pdata_buf = priv->cur_cmd->pdata_buf;
507
508         lbs_deb_enter(LBS_DEB_CMD);
509
510         if (enable_rsn->action == cpu_to_le16(CMD_ACT_GET)) {
511                 if (pdata_buf)
512                         *pdata_buf = (u32) le16_to_cpu(enable_rsn->enable);
513         }
514
515         lbs_deb_leave(LBS_DEB_CMD);
516         return 0;
517 }
518
519 static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
520                                         struct cmd_ds_command *resp)
521 {
522         struct cmd_ds_802_11_beacon_control *bcn_ctrl =
523             &resp->params.bcn_ctrl;
524
525         lbs_deb_enter(LBS_DEB_CMD);
526
527         if (bcn_ctrl->action == CMD_ACT_GET) {
528                 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
529                 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
530         }
531
532         lbs_deb_enter(LBS_DEB_CMD);
533         return 0;
534 }
535
536 static int lbs_ret_802_11_subscribe_event(struct lbs_private *priv,
537         struct cmd_ds_command *resp)
538 {
539         struct cmd_ds_802_11_subscribe_event *cmd_event =
540                 &resp->params.subscribe_event;
541         struct cmd_ds_802_11_subscribe_event *dst_event =
542                 priv->cur_cmd->pdata_buf;
543
544         lbs_deb_enter(LBS_DEB_CMD);
545
546         if (dst_event->action == cpu_to_le16(CMD_ACT_GET)) {
547                 dst_event->events = cmd_event->events;
548                 memcpy(dst_event->tlv, cmd_event->tlv, sizeof(dst_event->tlv));
549         }
550
551         lbs_deb_leave(LBS_DEB_CMD);
552         return 0;
553 }
554
555 static inline int handle_cmd_response(u16 respcmd,
556                                       struct cmd_ds_command *resp,
557                                       struct lbs_private *priv)
558 {
559         int ret = 0;
560         unsigned long flags;
561
562         lbs_deb_enter(LBS_DEB_HOST);
563
564         switch (respcmd) {
565         case CMD_RET(CMD_MAC_REG_ACCESS):
566         case CMD_RET(CMD_BBP_REG_ACCESS):
567         case CMD_RET(CMD_RF_REG_ACCESS):
568                 ret = lbs_ret_reg_access(priv, respcmd, resp);
569                 break;
570
571         case CMD_RET(CMD_GET_HW_SPEC):
572                 ret = lbs_ret_get_hw_spec(priv, resp);
573                 break;
574
575         case CMD_RET(CMD_802_11_SCAN):
576                 ret = lbs_ret_80211_scan(priv, resp);
577                 break;
578
579         case CMD_RET(CMD_802_11_GET_LOG):
580                 ret = lbs_ret_get_log(priv, resp);
581                 break;
582
583         case CMD_RET_802_11_ASSOCIATE:
584         case CMD_RET(CMD_802_11_ASSOCIATE):
585         case CMD_RET(CMD_802_11_REASSOCIATE):
586                 ret = lbs_ret_80211_associate(priv, resp);
587                 break;
588
589         case CMD_RET(CMD_802_11_DISASSOCIATE):
590         case CMD_RET(CMD_802_11_DEAUTHENTICATE):
591                 ret = lbs_ret_80211_disassociate(priv, resp);
592                 break;
593
594         case CMD_RET(CMD_802_11_AD_HOC_START):
595         case CMD_RET(CMD_802_11_AD_HOC_JOIN):
596                 ret = lbs_ret_80211_ad_hoc_start(priv, resp);
597                 break;
598
599         case CMD_RET(CMD_802_11_GET_STAT):
600                 ret = lbs_ret_802_11_stat(priv, resp);
601                 break;
602
603         case CMD_RET(CMD_802_11_SNMP_MIB):
604                 ret = lbs_ret_802_11_snmp_mib(priv, resp);
605                 break;
606
607         case CMD_RET(CMD_802_11_RF_TX_POWER):
608                 ret = lbs_ret_802_11_rf_tx_power(priv, resp);
609                 break;
610
611         case CMD_RET(CMD_802_11_SET_AFC):
612         case CMD_RET(CMD_802_11_GET_AFC):
613                 spin_lock_irqsave(&priv->driver_lock, flags);
614                 memmove(priv->cur_cmd->pdata_buf, &resp->params.afc,
615                         sizeof(struct cmd_ds_802_11_afc));
616                 spin_unlock_irqrestore(&priv->driver_lock, flags);
617
618                 break;
619
620         case CMD_RET(CMD_MAC_MULTICAST_ADR):
621         case CMD_RET(CMD_MAC_CONTROL):
622         case CMD_RET(CMD_802_11_SET_WEP):
623         case CMD_RET(CMD_802_11_RESET):
624         case CMD_RET(CMD_802_11_AUTHENTICATE):
625         case CMD_RET(CMD_802_11_RADIO_CONTROL):
626         case CMD_RET(CMD_802_11_BEACON_STOP):
627                 break;
628
629         case CMD_RET(CMD_802_11_ENABLE_RSN):
630                 ret = lbs_ret_802_11_enable_rsn(priv, resp);
631                 break;
632
633         case CMD_RET(CMD_802_11_DATA_RATE):
634                 ret = lbs_ret_802_11_data_rate(priv, resp);
635                 break;
636         case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
637                 ret = lbs_ret_802_11_rate_adapt_rateset(priv, resp);
638                 break;
639         case CMD_RET(CMD_802_11_RF_CHANNEL):
640                 ret = lbs_ret_802_11_rf_channel(priv, resp);
641                 break;
642
643         case CMD_RET(CMD_802_11_RSSI):
644                 ret = lbs_ret_802_11_rssi(priv, resp);
645                 break;
646
647         case CMD_RET(CMD_802_11_MAC_ADDRESS):
648                 ret = lbs_ret_802_11_mac_address(priv, resp);
649                 break;
650
651         case CMD_RET(CMD_802_11_AD_HOC_STOP):
652                 ret = lbs_ret_80211_ad_hoc_stop(priv, resp);
653                 break;
654
655         case CMD_RET(CMD_802_11_KEY_MATERIAL):
656                 ret = lbs_ret_802_11_key_material(priv, resp);
657                 break;
658
659         case CMD_RET(CMD_802_11_EEPROM_ACCESS):
660                 ret = lbs_ret_802_11_eeprom_access(priv, resp);
661                 break;
662
663         case CMD_RET(CMD_802_11D_DOMAIN_INFO):
664                 ret = lbs_ret_802_11d_domain_info(priv, resp);
665                 break;
666
667         case CMD_RET(CMD_802_11_SLEEP_PARAMS):
668                 ret = lbs_ret_802_11_sleep_params(priv, resp);
669                 break;
670         case CMD_RET(CMD_802_11_INACTIVITY_TIMEOUT):
671                 spin_lock_irqsave(&priv->driver_lock, flags);
672                 *((u16 *) priv->cur_cmd->pdata_buf) =
673                     le16_to_cpu(resp->params.inactivity_timeout.timeout);
674                 spin_unlock_irqrestore(&priv->driver_lock, flags);
675                 break;
676
677         case CMD_RET(CMD_802_11_TPC_CFG):
678                 spin_lock_irqsave(&priv->driver_lock, flags);
679                 memmove(priv->cur_cmd->pdata_buf, &resp->params.tpccfg,
680                         sizeof(struct cmd_ds_802_11_tpc_cfg));
681                 spin_unlock_irqrestore(&priv->driver_lock, flags);
682                 break;
683         case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
684                 spin_lock_irqsave(&priv->driver_lock, flags);
685                 memmove(priv->cur_cmd->pdata_buf, &resp->params.ledgpio,
686                         sizeof(struct cmd_ds_802_11_led_ctrl));
687                 spin_unlock_irqrestore(&priv->driver_lock, flags);
688                 break;
689         case CMD_RET(CMD_802_11_SUBSCRIBE_EVENT):
690                 ret = lbs_ret_802_11_subscribe_event(priv, resp);
691                 break;
692
693         case CMD_RET(CMD_802_11_PWR_CFG):
694                 spin_lock_irqsave(&priv->driver_lock, flags);
695                 memmove(priv->cur_cmd->pdata_buf, &resp->params.pwrcfg,
696                         sizeof(struct cmd_ds_802_11_pwr_cfg));
697                 spin_unlock_irqrestore(&priv->driver_lock, flags);
698
699                 break;
700
701         case CMD_RET(CMD_GET_TSF):
702                 spin_lock_irqsave(&priv->driver_lock, flags);
703                 memcpy(priv->cur_cmd->pdata_buf,
704                        &resp->params.gettsf.tsfvalue, sizeof(u64));
705                 spin_unlock_irqrestore(&priv->driver_lock, flags);
706                 break;
707         case CMD_RET(CMD_BT_ACCESS):
708                 spin_lock_irqsave(&priv->driver_lock, flags);
709                 if (priv->cur_cmd->pdata_buf)
710                         memcpy(priv->cur_cmd->pdata_buf,
711                                &resp->params.bt.addr1, 2 * ETH_ALEN);
712                 spin_unlock_irqrestore(&priv->driver_lock, flags);
713                 break;
714         case CMD_RET(CMD_FWT_ACCESS):
715                 spin_lock_irqsave(&priv->driver_lock, flags);
716                 if (priv->cur_cmd->pdata_buf)
717                         memcpy(priv->cur_cmd->pdata_buf, &resp->params.fwt,
718                                sizeof(resp->params.fwt));
719                 spin_unlock_irqrestore(&priv->driver_lock, flags);
720                 break;
721         case CMD_RET(CMD_MESH_ACCESS):
722                 if (priv->cur_cmd->pdata_buf)
723                         memcpy(priv->cur_cmd->pdata_buf, &resp->params.mesh,
724                                sizeof(resp->params.mesh));
725                 break;
726         case CMD_RET(CMD_802_11_BEACON_CTRL):
727                 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
728                 break;
729
730         default:
731                 lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
732                             resp->command);
733                 break;
734         }
735         lbs_deb_leave(LBS_DEB_HOST);
736         return ret;
737 }
738
739 int lbs_process_rx_command(struct lbs_private *priv)
740 {
741         u16 respcmd;
742         struct cmd_ds_command *resp;
743         int ret = 0;
744         ulong flags;
745         u16 result;
746
747         lbs_deb_enter(LBS_DEB_HOST);
748
749         /* Now we got response from FW, cancel the command timer */
750         del_timer(&priv->command_timer);
751
752         mutex_lock(&priv->lock);
753         spin_lock_irqsave(&priv->driver_lock, flags);
754
755         if (!priv->cur_cmd) {
756                 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
757                 ret = -1;
758                 spin_unlock_irqrestore(&priv->driver_lock, flags);
759                 goto done;
760         }
761         resp = (struct cmd_ds_command *)(priv->cur_cmd->bufvirtualaddr);
762
763         respcmd = le16_to_cpu(resp->command);
764         result = le16_to_cpu(resp->result);
765
766         lbs_deb_host("CMD_RESP: response 0x%04x, size %d, jiffies %lu\n",
767                 respcmd, priv->upld_len, jiffies);
768         lbs_deb_hex(LBS_DEB_HOST, "CMD_RESP", priv->cur_cmd->bufvirtualaddr,
769                     priv->upld_len);
770
771         if (!(respcmd & 0x8000)) {
772                 lbs_deb_host("invalid response!\n");
773                 priv->cur_cmd_retcode = -1;
774                 __lbs_cleanup_and_insert_cmd(priv, priv->cur_cmd);
775                 priv->cur_cmd = NULL;
776                 spin_unlock_irqrestore(&priv->driver_lock, flags);
777                 ret = -1;
778                 goto done;
779         }
780
781         /* Store the response code to cur_cmd_retcode. */
782         priv->cur_cmd_retcode = result;
783
784         if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
785                 struct cmd_ds_802_11_ps_mode *psmode = &resp->params.psmode;
786                 u16 action = le16_to_cpu(psmode->action);
787
788                 lbs_deb_host(
789                        "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
790                        result, action);
791
792                 if (result) {
793                         lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
794                                     result);
795                         /*
796                          * We should not re-try enter-ps command in
797                          * ad-hoc mode. It takes place in
798                          * lbs_execute_next_command().
799                          */
800                         if (priv->mode == IW_MODE_ADHOC &&
801                             action == CMD_SUBCMD_ENTER_PS)
802                                 priv->psmode = LBS802_11POWERMODECAM;
803                 } else if (action == CMD_SUBCMD_ENTER_PS) {
804                         priv->needtowakeup = 0;
805                         priv->psstate = PS_STATE_AWAKE;
806
807                         lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
808                         if (priv->connect_status != LBS_CONNECTED) {
809                                 /*
810                                  * When Deauth Event received before Enter_PS command
811                                  * response, We need to wake up the firmware.
812                                  */
813                                 lbs_deb_host(
814                                        "disconnected, invoking lbs_ps_wakeup\n");
815
816                                 spin_unlock_irqrestore(&priv->driver_lock, flags);
817                                 mutex_unlock(&priv->lock);
818                                 lbs_ps_wakeup(priv, 0);
819                                 mutex_lock(&priv->lock);
820                                 spin_lock_irqsave(&priv->driver_lock, flags);
821                         }
822                 } else if (action == CMD_SUBCMD_EXIT_PS) {
823                         priv->needtowakeup = 0;
824                         priv->psstate = PS_STATE_FULL_POWER;
825                         lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
826                 } else {
827                         lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
828                 }
829
830                 __lbs_cleanup_and_insert_cmd(priv, priv->cur_cmd);
831                 priv->cur_cmd = NULL;
832                 spin_unlock_irqrestore(&priv->driver_lock, flags);
833
834                 ret = 0;
835                 goto done;
836         }
837
838         /* If the command is not successful, cleanup and return failure */
839         if ((result != 0 || !(respcmd & 0x8000))) {
840                 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
841                        result, respcmd);
842                 /*
843                  * Handling errors here
844                  */
845                 switch (respcmd) {
846                 case CMD_RET(CMD_GET_HW_SPEC):
847                 case CMD_RET(CMD_802_11_RESET):
848                         lbs_deb_host("CMD_RESP: reset failed\n");
849                         break;
850
851                 }
852
853                 __lbs_cleanup_and_insert_cmd(priv, priv->cur_cmd);
854                 priv->cur_cmd = NULL;
855                 spin_unlock_irqrestore(&priv->driver_lock, flags);
856
857                 ret = -1;
858                 goto done;
859         }
860
861         spin_unlock_irqrestore(&priv->driver_lock, flags);
862
863         if (priv->cur_cmd && priv->cur_cmd->callback)
864                 ret = priv->cur_cmd->callback(respcmd, resp, priv);
865         else
866                 ret = handle_cmd_response(respcmd, resp, priv);
867
868         spin_lock_irqsave(&priv->driver_lock, flags);
869
870         if (priv->cur_cmd) {
871                 /* Clean up and Put current command back to cmdfreeq */
872                 __lbs_cleanup_and_insert_cmd(priv, priv->cur_cmd);
873                 priv->cur_cmd = NULL;
874         }
875         spin_unlock_irqrestore(&priv->driver_lock, flags);
876
877 done:
878         mutex_unlock(&priv->lock);
879         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
880         return ret;
881 }
882
883 int lbs_process_event(struct lbs_private *priv)
884 {
885         int ret = 0;
886         u32 eventcause;
887
888         lbs_deb_enter(LBS_DEB_CMD);
889
890         spin_lock_irq(&priv->driver_lock);
891         eventcause = priv->eventcause >> SBI_EVENT_CAUSE_SHIFT;
892         spin_unlock_irq(&priv->driver_lock);
893
894         lbs_deb_cmd("event cause %d\n", eventcause);
895
896         switch (eventcause) {
897         case MACREG_INT_CODE_LINK_SENSED:
898                 lbs_deb_cmd("EVENT: MACREG_INT_CODE_LINK_SENSED\n");
899                 break;
900
901         case MACREG_INT_CODE_DEAUTHENTICATED:
902                 lbs_deb_cmd("EVENT: deauthenticated\n");
903                 lbs_mac_event_disconnected(priv);
904                 break;
905
906         case MACREG_INT_CODE_DISASSOCIATED:
907                 lbs_deb_cmd("EVENT: disassociated\n");
908                 lbs_mac_event_disconnected(priv);
909                 break;
910
911         case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
912                 lbs_deb_cmd("EVENT: link lost\n");
913                 lbs_mac_event_disconnected(priv);
914                 break;
915
916         case MACREG_INT_CODE_PS_SLEEP:
917                 lbs_deb_cmd("EVENT: sleep\n");
918
919                 /* handle unexpected PS SLEEP event */
920                 if (priv->psstate == PS_STATE_FULL_POWER) {
921                         lbs_deb_cmd(
922                                "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
923                         break;
924                 }
925                 priv->psstate = PS_STATE_PRE_SLEEP;
926
927                 lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
928
929                 break;
930
931         case MACREG_INT_CODE_PS_AWAKE:
932                 lbs_deb_cmd("EVENT: awake\n");
933
934                 /* handle unexpected PS AWAKE event */
935                 if (priv->psstate == PS_STATE_FULL_POWER) {
936                         lbs_deb_cmd(
937                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
938                         break;
939                 }
940
941                 priv->psstate = PS_STATE_AWAKE;
942
943                 if (priv->needtowakeup) {
944                         /*
945                          * wait for the command processing to finish
946                          * before resuming sending
947                          * priv->needtowakeup will be set to FALSE
948                          * in lbs_ps_wakeup()
949                          */
950                         lbs_deb_cmd("waking up ...\n");
951                         lbs_ps_wakeup(priv, 0);
952                 }
953                 break;
954
955         case MACREG_INT_CODE_MIC_ERR_UNICAST:
956                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
957                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
958                 break;
959
960         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
961                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
962                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
963                 break;
964         case MACREG_INT_CODE_MIB_CHANGED:
965         case MACREG_INT_CODE_INIT_DONE:
966                 break;
967
968         case MACREG_INT_CODE_ADHOC_BCN_LOST:
969                 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
970                 break;
971
972         case MACREG_INT_CODE_RSSI_LOW:
973                 lbs_pr_alert("EVENT: rssi low\n");
974                 break;
975         case MACREG_INT_CODE_SNR_LOW:
976                 lbs_pr_alert("EVENT: snr low\n");
977                 break;
978         case MACREG_INT_CODE_MAX_FAIL:
979                 lbs_pr_alert("EVENT: max fail\n");
980                 break;
981         case MACREG_INT_CODE_RSSI_HIGH:
982                 lbs_pr_alert("EVENT: rssi high\n");
983                 break;
984         case MACREG_INT_CODE_SNR_HIGH:
985                 lbs_pr_alert("EVENT: snr high\n");
986                 break;
987
988         case MACREG_INT_CODE_MESH_AUTO_STARTED:
989                 /* Ignore spurious autostart events if autostart is disabled */
990                 if (!priv->mesh_autostart_enabled) {
991                         lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
992                         break;
993                 }
994                 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
995                 priv->mesh_connect_status = LBS_CONNECTED;
996                 if (priv->mesh_open == 1) {
997                         netif_wake_queue(priv->mesh_dev);
998                         netif_carrier_on(priv->mesh_dev);
999                 }
1000                 priv->mode = IW_MODE_ADHOC;
1001                 schedule_work(&priv->sync_channel);
1002                 break;
1003
1004         default:
1005                 lbs_pr_alert("EVENT: unknown event id %d\n", eventcause);
1006                 break;
1007         }
1008
1009         spin_lock_irq(&priv->driver_lock);
1010         priv->eventcause = 0;
1011         spin_unlock_irq(&priv->driver_lock);
1012
1013         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1014         return ret;
1015 }