libertas: misc power saving adjusts
[linux-3.10.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         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         /* report disconnect to upper layer */
47         netif_stop_queue(priv->dev);
48         netif_carrier_off(priv->dev);
49
50         /* Free Tx and Rx packets */
51         kfree_skb(priv->currenttxskb);
52         priv->currenttxskb = NULL;
53         priv->tx_pending_len = 0;
54
55         /* reset SNR/NF/RSSI values */
56         memset(priv->SNR, 0x00, sizeof(priv->SNR));
57         memset(priv->NF, 0x00, sizeof(priv->NF));
58         memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
59         memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
60         memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
61         priv->nextSNRNF = 0;
62         priv->numSNRNF = 0;
63         priv->connect_status = LBS_DISCONNECTED;
64
65         /* Clear out associated SSID and BSSID since connection is
66          * no longer valid.
67          */
68         memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
69         memset(&priv->curbssparams.ssid, 0, IW_ESSID_MAX_SIZE);
70         priv->curbssparams.ssid_len = 0;
71
72         if (priv->psstate != PS_STATE_FULL_POWER) {
73                 /* make firmware to exit PS mode */
74                 lbs_deb_cmd("disconnected, so exit PS mode\n");
75                 lbs_ps_wakeup(priv, 0);
76         }
77         lbs_deb_leave(LBS_DEB_ASSOC);
78 }
79
80 /**
81  *  @brief This function handles MIC failure event.
82  *
83  *  @param priv    A pointer to struct lbs_private structure
84  *  @para  event   the event id
85  *  @return        n/a
86  */
87 static void handle_mic_failureevent(struct lbs_private *priv, u32 event)
88 {
89         char buf[50];
90
91         lbs_deb_enter(LBS_DEB_CMD);
92         memset(buf, 0, sizeof(buf));
93
94         sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");
95
96         if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
97                 strcat(buf, "unicast ");
98         } else {
99                 strcat(buf, "multicast ");
100         }
101
102         lbs_send_iwevcustom_event(priv, buf);
103         lbs_deb_leave(LBS_DEB_CMD);
104 }
105
106 static int lbs_ret_reg_access(struct lbs_private *priv,
107                                u16 type, struct cmd_ds_command *resp)
108 {
109         int ret = 0;
110
111         lbs_deb_enter(LBS_DEB_CMD);
112
113         switch (type) {
114         case CMD_RET(CMD_MAC_REG_ACCESS):
115                 {
116                         struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
117
118                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
119                         priv->offsetvalue.value = le32_to_cpu(reg->value);
120                         break;
121                 }
122
123         case CMD_RET(CMD_BBP_REG_ACCESS):
124                 {
125                         struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
126
127                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
128                         priv->offsetvalue.value = reg->value;
129                         break;
130                 }
131
132         case CMD_RET(CMD_RF_REG_ACCESS):
133                 {
134                         struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
135
136                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
137                         priv->offsetvalue.value = reg->value;
138                         break;
139                 }
140
141         default:
142                 ret = -1;
143         }
144
145         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
146         return ret;
147 }
148
149 static int lbs_ret_802_11_stat(struct lbs_private *priv,
150                                 struct cmd_ds_command *resp)
151 {
152         lbs_deb_enter(LBS_DEB_CMD);
153 /*      currently priv->wlan802_11Stat is unused
154
155         struct cmd_ds_802_11_get_stat *p11Stat = &resp->params.gstat;
156
157         // TODO Convert it to Big endian befor copy
158         memcpy(&priv->wlan802_11Stat,
159                p11Stat, sizeof(struct cmd_ds_802_11_get_stat));
160 */
161         lbs_deb_leave(LBS_DEB_CMD);
162         return 0;
163 }
164
165 static int lbs_ret_802_11_snmp_mib(struct lbs_private *priv,
166                                     struct cmd_ds_command *resp)
167 {
168         struct cmd_ds_802_11_snmp_mib *smib = &resp->params.smib;
169         u16 oid = le16_to_cpu(smib->oid);
170         u16 querytype = le16_to_cpu(smib->querytype);
171
172         lbs_deb_enter(LBS_DEB_CMD);
173
174         lbs_deb_cmd("SNMP_RESP: oid 0x%x, querytype 0x%x\n", oid,
175                querytype);
176         lbs_deb_cmd("SNMP_RESP: Buf size %d\n", le16_to_cpu(smib->bufsize));
177
178         if (querytype == CMD_ACT_GET) {
179                 switch (oid) {
180                 case FRAGTHRESH_I:
181                         priv->fragthsd =
182                                 le16_to_cpu(*((__le16 *)(smib->value)));
183                         lbs_deb_cmd("SNMP_RESP: frag threshold %u\n",
184                                     priv->fragthsd);
185                         break;
186                 case RTSTHRESH_I:
187                         priv->rtsthsd =
188                                 le16_to_cpu(*((__le16 *)(smib->value)));
189                         lbs_deb_cmd("SNMP_RESP: rts threshold %u\n",
190                                     priv->rtsthsd);
191                         break;
192                 case SHORT_RETRYLIM_I:
193                         priv->txretrycount =
194                                 le16_to_cpu(*((__le16 *)(smib->value)));
195                         lbs_deb_cmd("SNMP_RESP: tx retry count %u\n",
196                                     priv->rtsthsd);
197                         break;
198                 default:
199                         break;
200                 }
201         }
202
203         lbs_deb_enter(LBS_DEB_CMD);
204         return 0;
205 }
206
207 static int lbs_ret_802_11_mac_address(struct lbs_private *priv,
208                                        struct cmd_ds_command *resp)
209 {
210         struct cmd_ds_802_11_mac_address *macadd = &resp->params.macadd;
211
212         lbs_deb_enter(LBS_DEB_CMD);
213
214         memcpy(priv->current_addr, macadd->macadd, ETH_ALEN);
215
216         lbs_deb_enter(LBS_DEB_CMD);
217         return 0;
218 }
219
220 static int lbs_ret_802_11_rf_tx_power(struct lbs_private *priv,
221                                        struct cmd_ds_command *resp)
222 {
223         struct cmd_ds_802_11_rf_tx_power *rtp = &resp->params.txp;
224
225         lbs_deb_enter(LBS_DEB_CMD);
226
227         priv->txpowerlevel = le16_to_cpu(rtp->currentlevel);
228
229         lbs_deb_cmd("TX power currently %d\n", priv->txpowerlevel);
230
231         lbs_deb_leave(LBS_DEB_CMD);
232         return 0;
233 }
234
235 static int lbs_ret_802_11_rate_adapt_rateset(struct lbs_private *priv,
236                                               struct cmd_ds_command *resp)
237 {
238         struct cmd_ds_802_11_rate_adapt_rateset *rates = &resp->params.rateset;
239
240         lbs_deb_enter(LBS_DEB_CMD);
241
242         if (rates->action == CMD_ACT_GET) {
243                 priv->enablehwauto = le16_to_cpu(rates->enablehwauto);
244                 priv->ratebitmap = le16_to_cpu(rates->bitmap);
245         }
246
247         lbs_deb_leave(LBS_DEB_CMD);
248         return 0;
249 }
250
251 static int lbs_ret_802_11_rssi(struct lbs_private *priv,
252                                 struct cmd_ds_command *resp)
253 {
254         struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
255
256         lbs_deb_enter(LBS_DEB_CMD);
257
258         /* store the non average value */
259         priv->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
260         priv->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
261
262         priv->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
263         priv->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
264
265         priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
266             CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
267                      priv->NF[TYPE_BEACON][TYPE_NOAVG]);
268
269         priv->RSSI[TYPE_BEACON][TYPE_AVG] =
270             CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
271                      priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
272
273         lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
274                priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
275                priv->RSSI[TYPE_BEACON][TYPE_AVG]);
276
277         lbs_deb_leave(LBS_DEB_CMD);
278         return 0;
279 }
280
281 static int lbs_ret_802_11_eeprom_access(struct lbs_private *priv,
282                                   struct cmd_ds_command *resp)
283 {
284         struct lbs_ioctl_regrdwr *pbuf;
285         pbuf = (struct lbs_ioctl_regrdwr *) priv->prdeeprom;
286
287         lbs_deb_enter_args(LBS_DEB_CMD, "len %d",
288                le16_to_cpu(resp->params.rdeeprom.bytecount));
289         if (pbuf->NOB < le16_to_cpu(resp->params.rdeeprom.bytecount)) {
290                 pbuf->NOB = 0;
291                 lbs_deb_cmd("EEPROM read length too big\n");
292                 return -1;
293         }
294         pbuf->NOB = le16_to_cpu(resp->params.rdeeprom.bytecount);
295         if (pbuf->NOB > 0) {
296
297                 memcpy(&pbuf->value, (u8 *) & resp->params.rdeeprom.value,
298                        le16_to_cpu(resp->params.rdeeprom.bytecount));
299                 lbs_deb_hex(LBS_DEB_CMD, "EEPROM", (char *)&pbuf->value,
300                         le16_to_cpu(resp->params.rdeeprom.bytecount));
301         }
302         lbs_deb_leave(LBS_DEB_CMD);
303         return 0;
304 }
305
306 static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
307                                         struct cmd_ds_command *resp)
308 {
309         struct cmd_ds_802_11_beacon_control *bcn_ctrl =
310             &resp->params.bcn_ctrl;
311
312         lbs_deb_enter(LBS_DEB_CMD);
313
314         if (bcn_ctrl->action == CMD_ACT_GET) {
315                 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
316                 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
317         }
318
319         lbs_deb_enter(LBS_DEB_CMD);
320         return 0;
321 }
322
323 static inline int handle_cmd_response(struct lbs_private *priv,
324                                       unsigned long dummy,
325                                       struct cmd_header *cmd_response)
326 {
327         struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
328         int ret = 0;
329         unsigned long flags;
330         uint16_t respcmd = le16_to_cpu(resp->command);
331
332         lbs_deb_enter(LBS_DEB_HOST);
333
334         switch (respcmd) {
335         case CMD_RET(CMD_MAC_REG_ACCESS):
336         case CMD_RET(CMD_BBP_REG_ACCESS):
337         case CMD_RET(CMD_RF_REG_ACCESS):
338                 ret = lbs_ret_reg_access(priv, respcmd, resp);
339                 break;
340
341         case CMD_RET_802_11_ASSOCIATE:
342         case CMD_RET(CMD_802_11_ASSOCIATE):
343         case CMD_RET(CMD_802_11_REASSOCIATE):
344                 ret = lbs_ret_80211_associate(priv, resp);
345                 break;
346
347         case CMD_RET(CMD_802_11_DISASSOCIATE):
348         case CMD_RET(CMD_802_11_DEAUTHENTICATE):
349                 ret = lbs_ret_80211_disassociate(priv, resp);
350                 break;
351
352         case CMD_RET(CMD_802_11_AD_HOC_START):
353         case CMD_RET(CMD_802_11_AD_HOC_JOIN):
354                 ret = lbs_ret_80211_ad_hoc_start(priv, resp);
355                 break;
356
357         case CMD_RET(CMD_802_11_GET_STAT):
358                 ret = lbs_ret_802_11_stat(priv, resp);
359                 break;
360
361         case CMD_RET(CMD_802_11_SNMP_MIB):
362                 ret = lbs_ret_802_11_snmp_mib(priv, resp);
363                 break;
364
365         case CMD_RET(CMD_802_11_RF_TX_POWER):
366                 ret = lbs_ret_802_11_rf_tx_power(priv, resp);
367                 break;
368
369         case CMD_RET(CMD_802_11_SET_AFC):
370         case CMD_RET(CMD_802_11_GET_AFC):
371                 spin_lock_irqsave(&priv->driver_lock, flags);
372                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
373                         sizeof(struct cmd_ds_802_11_afc));
374                 spin_unlock_irqrestore(&priv->driver_lock, flags);
375
376                 break;
377
378         case CMD_RET(CMD_MAC_MULTICAST_ADR):
379         case CMD_RET(CMD_802_11_RESET):
380         case CMD_RET(CMD_802_11_AUTHENTICATE):
381         case CMD_RET(CMD_802_11_BEACON_STOP):
382                 break;
383
384         case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
385                 ret = lbs_ret_802_11_rate_adapt_rateset(priv, resp);
386                 break;
387
388         case CMD_RET(CMD_802_11_RSSI):
389                 ret = lbs_ret_802_11_rssi(priv, resp);
390                 break;
391
392         case CMD_RET(CMD_802_11_MAC_ADDRESS):
393                 ret = lbs_ret_802_11_mac_address(priv, resp);
394                 break;
395
396         case CMD_RET(CMD_802_11_AD_HOC_STOP):
397                 ret = lbs_ret_80211_ad_hoc_stop(priv, resp);
398                 break;
399
400         case CMD_RET(CMD_802_11_EEPROM_ACCESS):
401                 ret = lbs_ret_802_11_eeprom_access(priv, resp);
402                 break;
403
404         case CMD_RET(CMD_802_11D_DOMAIN_INFO):
405                 ret = lbs_ret_802_11d_domain_info(priv, resp);
406                 break;
407
408         case CMD_RET(CMD_802_11_TPC_CFG):
409                 spin_lock_irqsave(&priv->driver_lock, flags);
410                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
411                         sizeof(struct cmd_ds_802_11_tpc_cfg));
412                 spin_unlock_irqrestore(&priv->driver_lock, flags);
413                 break;
414         case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
415                 spin_lock_irqsave(&priv->driver_lock, flags);
416                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
417                         sizeof(struct cmd_ds_802_11_led_ctrl));
418                 spin_unlock_irqrestore(&priv->driver_lock, flags);
419                 break;
420
421         case CMD_RET(CMD_802_11_PWR_CFG):
422                 spin_lock_irqsave(&priv->driver_lock, flags);
423                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.pwrcfg,
424                         sizeof(struct cmd_ds_802_11_pwr_cfg));
425                 spin_unlock_irqrestore(&priv->driver_lock, flags);
426
427                 break;
428
429         case CMD_RET(CMD_GET_TSF):
430                 spin_lock_irqsave(&priv->driver_lock, flags);
431                 memcpy((void *)priv->cur_cmd->callback_arg,
432                        &resp->params.gettsf.tsfvalue, sizeof(u64));
433                 spin_unlock_irqrestore(&priv->driver_lock, flags);
434                 break;
435         case CMD_RET(CMD_BT_ACCESS):
436                 spin_lock_irqsave(&priv->driver_lock, flags);
437                 if (priv->cur_cmd->callback_arg)
438                         memcpy((void *)priv->cur_cmd->callback_arg,
439                                &resp->params.bt.addr1, 2 * ETH_ALEN);
440                 spin_unlock_irqrestore(&priv->driver_lock, flags);
441                 break;
442         case CMD_RET(CMD_FWT_ACCESS):
443                 spin_lock_irqsave(&priv->driver_lock, flags);
444                 if (priv->cur_cmd->callback_arg)
445                         memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
446                                sizeof(resp->params.fwt));
447                 spin_unlock_irqrestore(&priv->driver_lock, flags);
448                 break;
449         case CMD_RET(CMD_802_11_BEACON_CTRL):
450                 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
451                 break;
452
453         default:
454                 lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
455                              le16_to_cpu(resp->command));
456                 break;
457         }
458         lbs_deb_leave(LBS_DEB_HOST);
459         return ret;
460 }
461
462 int lbs_process_rx_command(struct lbs_private *priv)
463 {
464         uint16_t respcmd, curcmd;
465         struct cmd_header *resp;
466         int ret = 0;
467         unsigned long flags;
468         uint16_t result;
469
470         lbs_deb_enter(LBS_DEB_HOST);
471
472         mutex_lock(&priv->lock);
473         spin_lock_irqsave(&priv->driver_lock, flags);
474
475         if (!priv->cur_cmd) {
476                 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
477                 ret = -1;
478                 spin_unlock_irqrestore(&priv->driver_lock, flags);
479                 goto done;
480         }
481
482         resp = (void *)priv->upld_buf;
483         curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
484         respcmd = le16_to_cpu(resp->command);
485         result = le16_to_cpu(resp->result);
486
487         lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d, jiffies %lu\n",
488                      respcmd, le16_to_cpu(resp->seqnum), priv->upld_len, jiffies);
489         lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, priv->upld_len);
490
491         if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
492                 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
493                             le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
494                 spin_unlock_irqrestore(&priv->driver_lock, flags);
495                 ret = -1;
496                 goto done;
497         }
498         if (respcmd != CMD_RET(curcmd) &&
499             respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
500                 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
501                 spin_unlock_irqrestore(&priv->driver_lock, flags);
502                 ret = -1;
503                 goto done;
504         }
505
506         if (resp->result == cpu_to_le16(0x0004)) {
507                 /* 0x0004 means -EAGAIN. Drop the response, let it time out
508                    and be resubmitted */
509                 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
510                             le16_to_cpu(resp->command));
511                 spin_unlock_irqrestore(&priv->driver_lock, flags);
512                 ret = -1;
513                 goto done;
514         }
515
516         /* Now we got response from FW, cancel the command timer */
517         del_timer(&priv->command_timer);
518         priv->cmd_timed_out = 0;
519         if (priv->nr_retries) {
520                 lbs_pr_info("Received result %x to command %x after %d retries\n",
521                             result, curcmd, priv->nr_retries);
522                 priv->nr_retries = 0;
523         }
524
525         /* Store the response code to cur_cmd_retcode. */
526         priv->cur_cmd_retcode = result;
527
528         if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
529                 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
530                 u16 action = le16_to_cpu(psmode->action);
531
532                 lbs_deb_host(
533                        "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
534                        result, action);
535
536                 if (result) {
537                         lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
538                                     result);
539                         /*
540                          * We should not re-try enter-ps command in
541                          * ad-hoc mode. It takes place in
542                          * lbs_execute_next_command().
543                          */
544                         if (priv->mode == IW_MODE_ADHOC &&
545                             action == CMD_SUBCMD_ENTER_PS)
546                                 priv->psmode = LBS802_11POWERMODECAM;
547                 } else if (action == CMD_SUBCMD_ENTER_PS) {
548                         priv->needtowakeup = 0;
549                         priv->psstate = PS_STATE_AWAKE;
550
551                         lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
552                         if (priv->connect_status != LBS_CONNECTED) {
553                                 /*
554                                  * When Deauth Event received before Enter_PS command
555                                  * response, We need to wake up the firmware.
556                                  */
557                                 lbs_deb_host(
558                                        "disconnected, invoking lbs_ps_wakeup\n");
559
560                                 spin_unlock_irqrestore(&priv->driver_lock, flags);
561                                 mutex_unlock(&priv->lock);
562                                 lbs_ps_wakeup(priv, 0);
563                                 mutex_lock(&priv->lock);
564                                 spin_lock_irqsave(&priv->driver_lock, flags);
565                         }
566                 } else if (action == CMD_SUBCMD_EXIT_PS) {
567                         priv->needtowakeup = 0;
568                         priv->psstate = PS_STATE_FULL_POWER;
569                         lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
570                 } else {
571                         lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
572                 }
573
574                 lbs_complete_command(priv, priv->cur_cmd, result);
575                 spin_unlock_irqrestore(&priv->driver_lock, flags);
576
577                 ret = 0;
578                 goto done;
579         }
580
581         /* If the command is not successful, cleanup and return failure */
582         if ((result != 0 || !(respcmd & 0x8000))) {
583                 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
584                        result, respcmd);
585                 /*
586                  * Handling errors here
587                  */
588                 switch (respcmd) {
589                 case CMD_RET(CMD_GET_HW_SPEC):
590                 case CMD_RET(CMD_802_11_RESET):
591                         lbs_deb_host("CMD_RESP: reset failed\n");
592                         break;
593
594                 }
595                 lbs_complete_command(priv, priv->cur_cmd, result);
596                 spin_unlock_irqrestore(&priv->driver_lock, flags);
597
598                 ret = -1;
599                 goto done;
600         }
601
602         spin_unlock_irqrestore(&priv->driver_lock, flags);
603
604         if (priv->cur_cmd && priv->cur_cmd->callback) {
605                 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
606                                 resp);
607         } else
608                 ret = handle_cmd_response(priv, 0, resp);
609
610         spin_lock_irqsave(&priv->driver_lock, flags);
611
612         if (priv->cur_cmd) {
613                 /* Clean up and Put current command back to cmdfreeq */
614                 lbs_complete_command(priv, priv->cur_cmd, result);
615         }
616         spin_unlock_irqrestore(&priv->driver_lock, flags);
617
618 done:
619         mutex_unlock(&priv->lock);
620         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
621         return ret;
622 }
623
624 static int lbs_send_confirmwake(struct lbs_private *priv)
625 {
626         struct cmd_header *cmd = &priv->lbs_ps_confirm_wake;
627         int ret = 0;
628
629         lbs_deb_enter(LBS_DEB_HOST);
630
631         cmd->command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
632         cmd->size = cpu_to_le16(sizeof(*cmd));
633         cmd->seqnum = cpu_to_le16(++priv->seqnum);
634         cmd->result = 0;
635
636         lbs_deb_host("SEND_WAKEC_CMD: before download\n");
637
638         lbs_deb_hex(LBS_DEB_HOST, "wake confirm command", (void *)cmd, sizeof(*cmd));
639
640         ret = priv->hw_host_to_card(priv, MVMS_CMD, (void *)cmd, sizeof(*cmd));
641         if (ret)
642                 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
643
644         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
645         return ret;
646 }
647
648 int lbs_process_event(struct lbs_private *priv)
649 {
650         int ret = 0;
651         u32 eventcause;
652
653         lbs_deb_enter(LBS_DEB_CMD);
654
655         spin_lock_irq(&priv->driver_lock);
656         eventcause = priv->eventcause >> SBI_EVENT_CAUSE_SHIFT;
657         spin_unlock_irq(&priv->driver_lock);
658
659         switch (eventcause) {
660         case MACREG_INT_CODE_LINK_SENSED:
661                 lbs_deb_cmd("EVENT: link sensed\n");
662                 break;
663
664         case MACREG_INT_CODE_DEAUTHENTICATED:
665                 lbs_deb_cmd("EVENT: deauthenticated\n");
666                 lbs_mac_event_disconnected(priv);
667                 break;
668
669         case MACREG_INT_CODE_DISASSOCIATED:
670                 lbs_deb_cmd("EVENT: disassociated\n");
671                 lbs_mac_event_disconnected(priv);
672                 break;
673
674         case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
675                 lbs_deb_cmd("EVENT: link lost\n");
676                 lbs_mac_event_disconnected(priv);
677                 break;
678
679         case MACREG_INT_CODE_PS_SLEEP:
680                 lbs_deb_cmd("EVENT: ps sleep\n");
681
682                 /* handle unexpected PS SLEEP event */
683                 if (priv->psstate == PS_STATE_FULL_POWER) {
684                         lbs_deb_cmd(
685                                "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
686                         break;
687                 }
688                 priv->psstate = PS_STATE_PRE_SLEEP;
689
690                 lbs_ps_confirm_sleep(priv);
691
692                 break;
693
694         case MACREG_INT_CODE_HOST_AWAKE:
695                 lbs_deb_cmd("EVENT: host awake\n");
696                 lbs_send_confirmwake(priv);
697                 break;
698
699         case MACREG_INT_CODE_PS_AWAKE:
700                 lbs_deb_cmd("EVENT: ps awake\n");
701                 /* handle unexpected PS AWAKE event */
702                 if (priv->psstate == PS_STATE_FULL_POWER) {
703                         lbs_deb_cmd(
704                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
705                         break;
706                 }
707
708                 priv->psstate = PS_STATE_AWAKE;
709
710                 if (priv->needtowakeup) {
711                         /*
712                          * wait for the command processing to finish
713                          * before resuming sending
714                          * priv->needtowakeup will be set to FALSE
715                          * in lbs_ps_wakeup()
716                          */
717                         lbs_deb_cmd("waking up ...\n");
718                         lbs_ps_wakeup(priv, 0);
719                 }
720                 break;
721
722         case MACREG_INT_CODE_MIC_ERR_UNICAST:
723                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
724                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
725                 break;
726
727         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
728                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
729                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
730                 break;
731
732         case MACREG_INT_CODE_MIB_CHANGED:
733                 lbs_deb_cmd("EVENT: MIB CHANGED\n");
734                 break;
735         case MACREG_INT_CODE_INIT_DONE:
736                 lbs_deb_cmd("EVENT: INIT DONE\n");
737                 break;
738         case MACREG_INT_CODE_ADHOC_BCN_LOST:
739                 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
740                 break;
741         case MACREG_INT_CODE_RSSI_LOW:
742                 lbs_pr_alert("EVENT: rssi low\n");
743                 break;
744         case MACREG_INT_CODE_SNR_LOW:
745                 lbs_pr_alert("EVENT: snr low\n");
746                 break;
747         case MACREG_INT_CODE_MAX_FAIL:
748                 lbs_pr_alert("EVENT: max fail\n");
749                 break;
750         case MACREG_INT_CODE_RSSI_HIGH:
751                 lbs_pr_alert("EVENT: rssi high\n");
752                 break;
753         case MACREG_INT_CODE_SNR_HIGH:
754                 lbs_pr_alert("EVENT: snr high\n");
755                 break;
756
757         case MACREG_INT_CODE_MESH_AUTO_STARTED:
758                 /* Ignore spurious autostart events if autostart is disabled */
759                 if (!priv->mesh_autostart_enabled) {
760                         lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
761                         break;
762                 }
763                 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
764                 priv->mesh_connect_status = LBS_CONNECTED;
765                 if (priv->mesh_open) {
766                         netif_carrier_on(priv->mesh_dev);
767                         if (!priv->tx_pending_len)
768                                 netif_wake_queue(priv->mesh_dev);
769                 }
770                 priv->mode = IW_MODE_ADHOC;
771                 schedule_work(&priv->sync_channel);
772                 break;
773
774         default:
775                 lbs_pr_alert("EVENT: unknown event id %d\n", eventcause);
776                 break;
777         }
778
779         spin_lock_irq(&priv->driver_lock);
780         priv->eventcause = 0;
781         spin_unlock_irq(&priv->driver_lock);
782
783         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
784         return ret;
785 }