Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[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_get_log(struct lbs_private *priv,
307                             struct cmd_ds_command *resp)
308 {
309         struct cmd_ds_802_11_get_log *logmessage = &resp->params.glog;
310
311         lbs_deb_enter(LBS_DEB_CMD);
312
313         /* Stored little-endian */
314         memcpy(&priv->logmsg, logmessage, sizeof(struct cmd_ds_802_11_get_log));
315
316         lbs_deb_leave(LBS_DEB_CMD);
317         return 0;
318 }
319
320 static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
321                                         struct cmd_ds_command *resp)
322 {
323         struct cmd_ds_802_11_beacon_control *bcn_ctrl =
324             &resp->params.bcn_ctrl;
325
326         lbs_deb_enter(LBS_DEB_CMD);
327
328         if (bcn_ctrl->action == CMD_ACT_GET) {
329                 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
330                 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
331         }
332
333         lbs_deb_enter(LBS_DEB_CMD);
334         return 0;
335 }
336
337 static inline int handle_cmd_response(struct lbs_private *priv,
338                                       unsigned long dummy,
339                                       struct cmd_header *cmd_response)
340 {
341         struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
342         int ret = 0;
343         unsigned long flags;
344         uint16_t respcmd = le16_to_cpu(resp->command);
345
346         lbs_deb_enter(LBS_DEB_HOST);
347
348         switch (respcmd) {
349         case CMD_RET(CMD_MAC_REG_ACCESS):
350         case CMD_RET(CMD_BBP_REG_ACCESS):
351         case CMD_RET(CMD_RF_REG_ACCESS):
352                 ret = lbs_ret_reg_access(priv, respcmd, resp);
353                 break;
354
355         case CMD_RET(CMD_802_11_GET_LOG):
356                 ret = lbs_ret_get_log(priv, resp);
357                 break;
358
359         case CMD_RET_802_11_ASSOCIATE:
360         case CMD_RET(CMD_802_11_ASSOCIATE):
361         case CMD_RET(CMD_802_11_REASSOCIATE):
362                 ret = lbs_ret_80211_associate(priv, resp);
363                 break;
364
365         case CMD_RET(CMD_802_11_DISASSOCIATE):
366         case CMD_RET(CMD_802_11_DEAUTHENTICATE):
367                 ret = lbs_ret_80211_disassociate(priv, resp);
368                 break;
369
370         case CMD_RET(CMD_802_11_AD_HOC_START):
371         case CMD_RET(CMD_802_11_AD_HOC_JOIN):
372                 ret = lbs_ret_80211_ad_hoc_start(priv, resp);
373                 break;
374
375         case CMD_RET(CMD_802_11_GET_STAT):
376                 ret = lbs_ret_802_11_stat(priv, resp);
377                 break;
378
379         case CMD_RET(CMD_802_11_SNMP_MIB):
380                 ret = lbs_ret_802_11_snmp_mib(priv, resp);
381                 break;
382
383         case CMD_RET(CMD_802_11_RF_TX_POWER):
384                 ret = lbs_ret_802_11_rf_tx_power(priv, resp);
385                 break;
386
387         case CMD_RET(CMD_802_11_SET_AFC):
388         case CMD_RET(CMD_802_11_GET_AFC):
389                 spin_lock_irqsave(&priv->driver_lock, flags);
390                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
391                         sizeof(struct cmd_ds_802_11_afc));
392                 spin_unlock_irqrestore(&priv->driver_lock, flags);
393
394                 break;
395
396         case CMD_RET(CMD_MAC_MULTICAST_ADR):
397         case CMD_RET(CMD_MAC_CONTROL):
398         case CMD_RET(CMD_802_11_RESET):
399         case CMD_RET(CMD_802_11_AUTHENTICATE):
400         case CMD_RET(CMD_802_11_BEACON_STOP):
401                 break;
402
403         case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
404                 ret = lbs_ret_802_11_rate_adapt_rateset(priv, resp);
405                 break;
406
407         case CMD_RET(CMD_802_11_RSSI):
408                 ret = lbs_ret_802_11_rssi(priv, resp);
409                 break;
410
411         case CMD_RET(CMD_802_11_MAC_ADDRESS):
412                 ret = lbs_ret_802_11_mac_address(priv, resp);
413                 break;
414
415         case CMD_RET(CMD_802_11_AD_HOC_STOP):
416                 ret = lbs_ret_80211_ad_hoc_stop(priv, resp);
417                 break;
418
419         case CMD_RET(CMD_802_11_EEPROM_ACCESS):
420                 ret = lbs_ret_802_11_eeprom_access(priv, resp);
421                 break;
422
423         case CMD_RET(CMD_802_11D_DOMAIN_INFO):
424                 ret = lbs_ret_802_11d_domain_info(priv, resp);
425                 break;
426
427         case CMD_RET(CMD_802_11_TPC_CFG):
428                 spin_lock_irqsave(&priv->driver_lock, flags);
429                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
430                         sizeof(struct cmd_ds_802_11_tpc_cfg));
431                 spin_unlock_irqrestore(&priv->driver_lock, flags);
432                 break;
433         case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
434                 spin_lock_irqsave(&priv->driver_lock, flags);
435                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
436                         sizeof(struct cmd_ds_802_11_led_ctrl));
437                 spin_unlock_irqrestore(&priv->driver_lock, flags);
438                 break;
439
440         case CMD_RET(CMD_802_11_PWR_CFG):
441                 spin_lock_irqsave(&priv->driver_lock, flags);
442                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.pwrcfg,
443                         sizeof(struct cmd_ds_802_11_pwr_cfg));
444                 spin_unlock_irqrestore(&priv->driver_lock, flags);
445
446                 break;
447
448         case CMD_RET(CMD_GET_TSF):
449                 spin_lock_irqsave(&priv->driver_lock, flags);
450                 memcpy((void *)priv->cur_cmd->callback_arg,
451                        &resp->params.gettsf.tsfvalue, sizeof(u64));
452                 spin_unlock_irqrestore(&priv->driver_lock, flags);
453                 break;
454         case CMD_RET(CMD_BT_ACCESS):
455                 spin_lock_irqsave(&priv->driver_lock, flags);
456                 if (priv->cur_cmd->callback_arg)
457                         memcpy((void *)priv->cur_cmd->callback_arg,
458                                &resp->params.bt.addr1, 2 * ETH_ALEN);
459                 spin_unlock_irqrestore(&priv->driver_lock, flags);
460                 break;
461         case CMD_RET(CMD_FWT_ACCESS):
462                 spin_lock_irqsave(&priv->driver_lock, flags);
463                 if (priv->cur_cmd->callback_arg)
464                         memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
465                                sizeof(resp->params.fwt));
466                 spin_unlock_irqrestore(&priv->driver_lock, flags);
467                 break;
468         case CMD_RET(CMD_802_11_BEACON_CTRL):
469                 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
470                 break;
471
472         default:
473                 lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
474                              le16_to_cpu(resp->command));
475                 break;
476         }
477         lbs_deb_leave(LBS_DEB_HOST);
478         return ret;
479 }
480
481 int lbs_process_rx_command(struct lbs_private *priv)
482 {
483         uint16_t respcmd, curcmd;
484         struct cmd_header *resp;
485         int ret = 0;
486         unsigned long flags;
487         uint16_t result;
488
489         lbs_deb_enter(LBS_DEB_HOST);
490
491         mutex_lock(&priv->lock);
492         spin_lock_irqsave(&priv->driver_lock, flags);
493
494         if (!priv->cur_cmd) {
495                 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
496                 ret = -1;
497                 spin_unlock_irqrestore(&priv->driver_lock, flags);
498                 goto done;
499         }
500
501         resp = (void *)priv->upld_buf;
502         curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
503         respcmd = le16_to_cpu(resp->command);
504         result = le16_to_cpu(resp->result);
505
506         lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d, jiffies %lu\n",
507                      respcmd, le16_to_cpu(resp->seqnum), priv->upld_len, jiffies);
508         lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, priv->upld_len);
509
510         if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
511                 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
512                             le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
513                 spin_unlock_irqrestore(&priv->driver_lock, flags);
514                 ret = -1;
515                 goto done;
516         }
517         if (respcmd != CMD_RET(curcmd) &&
518             respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
519                 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
520                 spin_unlock_irqrestore(&priv->driver_lock, flags);
521                 ret = -1;
522                 goto done;
523         }
524
525         if (resp->result == cpu_to_le16(0x0004)) {
526                 /* 0x0004 means -EAGAIN. Drop the response, let it time out
527                    and be resubmitted */
528                 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
529                             le16_to_cpu(resp->command));
530                 spin_unlock_irqrestore(&priv->driver_lock, flags);
531                 ret = -1;
532                 goto done;
533         }
534
535         /* Now we got response from FW, cancel the command timer */
536         del_timer(&priv->command_timer);
537         priv->cmd_timed_out = 0;
538         if (priv->nr_retries) {
539                 lbs_pr_info("Received result %x to command %x after %d retries\n",
540                             result, curcmd, priv->nr_retries);
541                 priv->nr_retries = 0;
542         }
543
544         /* Store the response code to cur_cmd_retcode. */
545         priv->cur_cmd_retcode = result;
546
547         if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
548                 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
549                 u16 action = le16_to_cpu(psmode->action);
550
551                 lbs_deb_host(
552                        "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
553                        result, action);
554
555                 if (result) {
556                         lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
557                                     result);
558                         /*
559                          * We should not re-try enter-ps command in
560                          * ad-hoc mode. It takes place in
561                          * lbs_execute_next_command().
562                          */
563                         if (priv->mode == IW_MODE_ADHOC &&
564                             action == CMD_SUBCMD_ENTER_PS)
565                                 priv->psmode = LBS802_11POWERMODECAM;
566                 } else if (action == CMD_SUBCMD_ENTER_PS) {
567                         priv->needtowakeup = 0;
568                         priv->psstate = PS_STATE_AWAKE;
569
570                         lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
571                         if (priv->connect_status != LBS_CONNECTED) {
572                                 /*
573                                  * When Deauth Event received before Enter_PS command
574                                  * response, We need to wake up the firmware.
575                                  */
576                                 lbs_deb_host(
577                                        "disconnected, invoking lbs_ps_wakeup\n");
578
579                                 spin_unlock_irqrestore(&priv->driver_lock, flags);
580                                 mutex_unlock(&priv->lock);
581                                 lbs_ps_wakeup(priv, 0);
582                                 mutex_lock(&priv->lock);
583                                 spin_lock_irqsave(&priv->driver_lock, flags);
584                         }
585                 } else if (action == CMD_SUBCMD_EXIT_PS) {
586                         priv->needtowakeup = 0;
587                         priv->psstate = PS_STATE_FULL_POWER;
588                         lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
589                 } else {
590                         lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
591                 }
592
593                 lbs_complete_command(priv, priv->cur_cmd, result);
594                 spin_unlock_irqrestore(&priv->driver_lock, flags);
595
596                 ret = 0;
597                 goto done;
598         }
599
600         /* If the command is not successful, cleanup and return failure */
601         if ((result != 0 || !(respcmd & 0x8000))) {
602                 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
603                        result, respcmd);
604                 /*
605                  * Handling errors here
606                  */
607                 switch (respcmd) {
608                 case CMD_RET(CMD_GET_HW_SPEC):
609                 case CMD_RET(CMD_802_11_RESET):
610                         lbs_deb_host("CMD_RESP: reset failed\n");
611                         break;
612
613                 }
614                 lbs_complete_command(priv, priv->cur_cmd, result);
615                 spin_unlock_irqrestore(&priv->driver_lock, flags);
616
617                 ret = -1;
618                 goto done;
619         }
620
621         spin_unlock_irqrestore(&priv->driver_lock, flags);
622
623         if (priv->cur_cmd && priv->cur_cmd->callback) {
624                 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
625                                 resp);
626         } else
627                 ret = handle_cmd_response(priv, 0, resp);
628
629         spin_lock_irqsave(&priv->driver_lock, flags);
630
631         if (priv->cur_cmd) {
632                 /* Clean up and Put current command back to cmdfreeq */
633                 lbs_complete_command(priv, priv->cur_cmd, result);
634         }
635         spin_unlock_irqrestore(&priv->driver_lock, flags);
636
637 done:
638         mutex_unlock(&priv->lock);
639         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
640         return ret;
641 }
642
643 static int lbs_send_confirmwake(struct lbs_private *priv)
644 {
645         struct cmd_header *cmd = &priv->lbs_ps_confirm_wake;
646         int ret = 0;
647
648         lbs_deb_enter(LBS_DEB_HOST);
649
650         cmd->command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
651         cmd->size = cpu_to_le16(sizeof(*cmd));
652         cmd->seqnum = cpu_to_le16(++priv->seqnum);
653         cmd->result = 0;
654
655         lbs_deb_host("SEND_WAKEC_CMD: before download\n");
656
657         lbs_deb_hex(LBS_DEB_HOST, "wake confirm command", (void *)cmd, sizeof(*cmd));
658
659         ret = priv->hw_host_to_card(priv, MVMS_CMD, (void *)cmd, sizeof(*cmd));
660         if (ret)
661                 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
662
663         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
664         return ret;
665 }
666
667 int lbs_process_event(struct lbs_private *priv)
668 {
669         int ret = 0;
670         u32 eventcause;
671
672         lbs_deb_enter(LBS_DEB_CMD);
673
674         spin_lock_irq(&priv->driver_lock);
675         eventcause = priv->eventcause >> SBI_EVENT_CAUSE_SHIFT;
676         spin_unlock_irq(&priv->driver_lock);
677
678         lbs_deb_cmd("event cause %d\n", eventcause);
679
680         switch (eventcause) {
681         case MACREG_INT_CODE_LINK_SENSED:
682                 lbs_deb_cmd("EVENT: MACREG_INT_CODE_LINK_SENSED\n");
683                 break;
684
685         case MACREG_INT_CODE_DEAUTHENTICATED:
686                 lbs_deb_cmd("EVENT: deauthenticated\n");
687                 lbs_mac_event_disconnected(priv);
688                 break;
689
690         case MACREG_INT_CODE_DISASSOCIATED:
691                 lbs_deb_cmd("EVENT: disassociated\n");
692                 lbs_mac_event_disconnected(priv);
693                 break;
694
695         case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
696                 lbs_deb_cmd("EVENT: link lost\n");
697                 lbs_mac_event_disconnected(priv);
698                 break;
699
700         case MACREG_INT_CODE_PS_SLEEP:
701                 lbs_deb_cmd("EVENT: sleep\n");
702
703                 /* handle unexpected PS SLEEP event */
704                 if (priv->psstate == PS_STATE_FULL_POWER) {
705                         lbs_deb_cmd(
706                                "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
707                         break;
708                 }
709                 priv->psstate = PS_STATE_PRE_SLEEP;
710
711                 lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
712
713                 break;
714
715         case MACREG_INT_CODE_HOST_AWAKE:
716                 lbs_deb_cmd("EVENT: HOST_AWAKE\n");
717                 lbs_send_confirmwake(priv);
718                 break;
719
720         case MACREG_INT_CODE_PS_AWAKE:
721                 lbs_deb_cmd("EVENT: awake\n");
722                 /* handle unexpected PS AWAKE event */
723                 if (priv->psstate == PS_STATE_FULL_POWER) {
724                         lbs_deb_cmd(
725                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
726                         break;
727                 }
728
729                 priv->psstate = PS_STATE_AWAKE;
730
731                 if (priv->needtowakeup) {
732                         /*
733                          * wait for the command processing to finish
734                          * before resuming sending
735                          * priv->needtowakeup will be set to FALSE
736                          * in lbs_ps_wakeup()
737                          */
738                         lbs_deb_cmd("waking up ...\n");
739                         lbs_ps_wakeup(priv, 0);
740                 }
741                 break;
742
743         case MACREG_INT_CODE_MIC_ERR_UNICAST:
744                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
745                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
746                 break;
747
748         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
749                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
750                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
751                 break;
752         case MACREG_INT_CODE_MIB_CHANGED:
753         case MACREG_INT_CODE_INIT_DONE:
754                 break;
755
756         case MACREG_INT_CODE_ADHOC_BCN_LOST:
757                 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
758                 break;
759
760         case MACREG_INT_CODE_RSSI_LOW:
761                 lbs_pr_alert("EVENT: rssi low\n");
762                 break;
763         case MACREG_INT_CODE_SNR_LOW:
764                 lbs_pr_alert("EVENT: snr low\n");
765                 break;
766         case MACREG_INT_CODE_MAX_FAIL:
767                 lbs_pr_alert("EVENT: max fail\n");
768                 break;
769         case MACREG_INT_CODE_RSSI_HIGH:
770                 lbs_pr_alert("EVENT: rssi high\n");
771                 break;
772         case MACREG_INT_CODE_SNR_HIGH:
773                 lbs_pr_alert("EVENT: snr high\n");
774                 break;
775
776         case MACREG_INT_CODE_MESH_AUTO_STARTED:
777                 /* Ignore spurious autostart events if autostart is disabled */
778                 if (!priv->mesh_autostart_enabled) {
779                         lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
780                         break;
781                 }
782                 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
783                 priv->mesh_connect_status = LBS_CONNECTED;
784                 if (priv->mesh_open) {
785                         netif_carrier_on(priv->mesh_dev);
786                         if (!priv->tx_pending_len)
787                                 netif_wake_queue(priv->mesh_dev);
788                 }
789                 priv->mode = IW_MODE_ADHOC;
790                 schedule_work(&priv->sync_channel);
791                 break;
792
793         default:
794                 lbs_pr_alert("EVENT: unknown event id %d\n", eventcause);
795                 break;
796         }
797
798         spin_lock_irq(&priv->driver_lock);
799         priv->eventcause = 0;
800         spin_unlock_irq(&priv->driver_lock);
801
802         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
803         return ret;
804 }