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