libertas: convert SET_WEP to a direct command
[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_CMD);
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_key_material(struct lbs_private *priv,
208                                         struct cmd_ds_command *resp)
209 {
210         struct cmd_ds_802_11_key_material *pkeymaterial =
211             &resp->params.keymaterial;
212         u16 action = le16_to_cpu(pkeymaterial->action);
213
214         lbs_deb_enter(LBS_DEB_CMD);
215
216         /* Copy the returned key to driver private data */
217         if (action == CMD_ACT_GET) {
218                 u8 * buf_ptr = (u8 *) &pkeymaterial->keyParamSet;
219                 u8 * resp_end = (u8 *) (resp + le16_to_cpu(resp->size));
220
221                 while (buf_ptr < resp_end) {
222                         struct MrvlIEtype_keyParamSet * pkeyparamset =
223                             (struct MrvlIEtype_keyParamSet *) buf_ptr;
224                         struct enc_key * pkey;
225                         u16 param_set_len = le16_to_cpu(pkeyparamset->length);
226                         u16 key_len = le16_to_cpu(pkeyparamset->keylen);
227                         u16 key_flags = le16_to_cpu(pkeyparamset->keyinfo);
228                         u16 key_type = le16_to_cpu(pkeyparamset->keytypeid);
229                         u8 * end;
230
231                         end = (u8 *) pkeyparamset + sizeof (pkeyparamset->type)
232                                                   + sizeof (pkeyparamset->length)
233                                                   + param_set_len;
234                         /* Make sure we don't access past the end of the IEs */
235                         if (end > resp_end)
236                                 break;
237
238                         if (key_flags & KEY_INFO_WPA_UNICAST)
239                                 pkey = &priv->wpa_unicast_key;
240                         else if (key_flags & KEY_INFO_WPA_MCAST)
241                                 pkey = &priv->wpa_mcast_key;
242                         else
243                                 break;
244
245                         /* Copy returned key into driver */
246                         memset(pkey, 0, sizeof(struct enc_key));
247                         if (key_len > sizeof(pkey->key))
248                                 break;
249                         pkey->type = key_type;
250                         pkey->flags = key_flags;
251                         pkey->len = key_len;
252                         memcpy(pkey->key, pkeyparamset->key, pkey->len);
253
254                         buf_ptr = end + 1;
255                 }
256         }
257
258         lbs_deb_enter(LBS_DEB_CMD);
259         return 0;
260 }
261
262 static int lbs_ret_802_11_mac_address(struct lbs_private *priv,
263                                        struct cmd_ds_command *resp)
264 {
265         struct cmd_ds_802_11_mac_address *macadd = &resp->params.macadd;
266
267         lbs_deb_enter(LBS_DEB_CMD);
268
269         memcpy(priv->current_addr, macadd->macadd, ETH_ALEN);
270
271         lbs_deb_enter(LBS_DEB_CMD);
272         return 0;
273 }
274
275 static int lbs_ret_802_11_rf_tx_power(struct lbs_private *priv,
276                                        struct cmd_ds_command *resp)
277 {
278         struct cmd_ds_802_11_rf_tx_power *rtp = &resp->params.txp;
279
280         lbs_deb_enter(LBS_DEB_CMD);
281
282         priv->txpowerlevel = le16_to_cpu(rtp->currentlevel);
283
284         lbs_deb_cmd("TX power currently %d\n", priv->txpowerlevel);
285
286         lbs_deb_leave(LBS_DEB_CMD);
287         return 0;
288 }
289
290 static int lbs_ret_802_11_rate_adapt_rateset(struct lbs_private *priv,
291                                               struct cmd_ds_command *resp)
292 {
293         struct cmd_ds_802_11_rate_adapt_rateset *rates = &resp->params.rateset;
294
295         lbs_deb_enter(LBS_DEB_CMD);
296
297         if (rates->action == CMD_ACT_GET) {
298                 priv->enablehwauto = le16_to_cpu(rates->enablehwauto);
299                 priv->ratebitmap = le16_to_cpu(rates->bitmap);
300         }
301
302         lbs_deb_leave(LBS_DEB_CMD);
303         return 0;
304 }
305
306 static int lbs_ret_802_11_rssi(struct lbs_private *priv,
307                                 struct cmd_ds_command *resp)
308 {
309         struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
310
311         lbs_deb_enter(LBS_DEB_CMD);
312
313         /* store the non average value */
314         priv->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
315         priv->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
316
317         priv->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
318         priv->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
319
320         priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
321             CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
322                      priv->NF[TYPE_BEACON][TYPE_NOAVG]);
323
324         priv->RSSI[TYPE_BEACON][TYPE_AVG] =
325             CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
326                      priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
327
328         lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
329                priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
330                priv->RSSI[TYPE_BEACON][TYPE_AVG]);
331
332         lbs_deb_leave(LBS_DEB_CMD);
333         return 0;
334 }
335
336 static int lbs_ret_802_11_eeprom_access(struct lbs_private *priv,
337                                   struct cmd_ds_command *resp)
338 {
339         struct lbs_ioctl_regrdwr *pbuf;
340         pbuf = (struct lbs_ioctl_regrdwr *) priv->prdeeprom;
341
342         lbs_deb_enter_args(LBS_DEB_CMD, "len %d",
343                le16_to_cpu(resp->params.rdeeprom.bytecount));
344         if (pbuf->NOB < le16_to_cpu(resp->params.rdeeprom.bytecount)) {
345                 pbuf->NOB = 0;
346                 lbs_deb_cmd("EEPROM read length too big\n");
347                 return -1;
348         }
349         pbuf->NOB = le16_to_cpu(resp->params.rdeeprom.bytecount);
350         if (pbuf->NOB > 0) {
351
352                 memcpy(&pbuf->value, (u8 *) & resp->params.rdeeprom.value,
353                        le16_to_cpu(resp->params.rdeeprom.bytecount));
354                 lbs_deb_hex(LBS_DEB_CMD, "EEPROM", (char *)&pbuf->value,
355                         le16_to_cpu(resp->params.rdeeprom.bytecount));
356         }
357         lbs_deb_leave(LBS_DEB_CMD);
358         return 0;
359 }
360
361 static int lbs_ret_get_log(struct lbs_private *priv,
362                             struct cmd_ds_command *resp)
363 {
364         struct cmd_ds_802_11_get_log *logmessage = &resp->params.glog;
365
366         lbs_deb_enter(LBS_DEB_CMD);
367
368         /* Stored little-endian */
369         memcpy(&priv->logmsg, logmessage, sizeof(struct cmd_ds_802_11_get_log));
370
371         lbs_deb_leave(LBS_DEB_CMD);
372         return 0;
373 }
374
375 static int lbs_ret_802_11_enable_rsn(struct lbs_private *priv,
376                                           struct cmd_ds_command *resp)
377 {
378         struct cmd_ds_802_11_enable_rsn *enable_rsn = &resp->params.enbrsn;
379         uint32_t * pdata_buf = (uint32_t *)priv->cur_cmd->callback_arg;
380
381         lbs_deb_enter(LBS_DEB_CMD);
382
383         if (enable_rsn->action == cpu_to_le16(CMD_ACT_GET)) {
384                 if (pdata_buf)
385                         *pdata_buf = (uint32_t) le16_to_cpu(enable_rsn->enable);
386         }
387
388         lbs_deb_leave(LBS_DEB_CMD);
389         return 0;
390 }
391
392 static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
393                                         struct cmd_ds_command *resp)
394 {
395         struct cmd_ds_802_11_beacon_control *bcn_ctrl =
396             &resp->params.bcn_ctrl;
397
398         lbs_deb_enter(LBS_DEB_CMD);
399
400         if (bcn_ctrl->action == CMD_ACT_GET) {
401                 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
402                 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
403         }
404
405         lbs_deb_enter(LBS_DEB_CMD);
406         return 0;
407 }
408
409 static int lbs_ret_802_11_subscribe_event(struct lbs_private *priv,
410         struct cmd_ds_command *resp)
411 {
412         struct cmd_ds_802_11_subscribe_event *cmd_event =
413                 &resp->params.subscribe_event;
414         struct cmd_ds_802_11_subscribe_event *dst_event =
415                 (void *)priv->cur_cmd->callback_arg;
416
417         lbs_deb_enter(LBS_DEB_CMD);
418
419         if (dst_event->action == cpu_to_le16(CMD_ACT_GET)) {
420                 dst_event->events = cmd_event->events;
421                 memcpy(dst_event->tlv, cmd_event->tlv, sizeof(dst_event->tlv));
422         }
423
424         lbs_deb_leave(LBS_DEB_CMD);
425         return 0;
426 }
427
428 static inline int handle_cmd_response(struct lbs_private *priv,
429                                       unsigned long dummy,
430                                       struct cmd_header *cmd_response)
431 {
432         struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
433         int ret = 0;
434         unsigned long flags;
435         uint16_t respcmd = le16_to_cpu(resp->command);
436
437         lbs_deb_enter(LBS_DEB_HOST);
438
439         switch (respcmd) {
440         case CMD_RET(CMD_MAC_REG_ACCESS):
441         case CMD_RET(CMD_BBP_REG_ACCESS):
442         case CMD_RET(CMD_RF_REG_ACCESS):
443                 ret = lbs_ret_reg_access(priv, respcmd, resp);
444                 break;
445
446         case CMD_RET(CMD_802_11_SCAN):
447                 ret = lbs_ret_80211_scan(priv, resp);
448                 break;
449
450         case CMD_RET(CMD_802_11_GET_LOG):
451                 ret = lbs_ret_get_log(priv, resp);
452                 break;
453
454         case CMD_RET_802_11_ASSOCIATE:
455         case CMD_RET(CMD_802_11_ASSOCIATE):
456         case CMD_RET(CMD_802_11_REASSOCIATE):
457                 ret = lbs_ret_80211_associate(priv, resp);
458                 break;
459
460         case CMD_RET(CMD_802_11_DISASSOCIATE):
461         case CMD_RET(CMD_802_11_DEAUTHENTICATE):
462                 ret = lbs_ret_80211_disassociate(priv, resp);
463                 break;
464
465         case CMD_RET(CMD_802_11_AD_HOC_START):
466         case CMD_RET(CMD_802_11_AD_HOC_JOIN):
467                 ret = lbs_ret_80211_ad_hoc_start(priv, resp);
468                 break;
469
470         case CMD_RET(CMD_802_11_GET_STAT):
471                 ret = lbs_ret_802_11_stat(priv, resp);
472                 break;
473
474         case CMD_RET(CMD_802_11_SNMP_MIB):
475                 ret = lbs_ret_802_11_snmp_mib(priv, resp);
476                 break;
477
478         case CMD_RET(CMD_802_11_RF_TX_POWER):
479                 ret = lbs_ret_802_11_rf_tx_power(priv, resp);
480                 break;
481
482         case CMD_RET(CMD_802_11_SET_AFC):
483         case CMD_RET(CMD_802_11_GET_AFC):
484                 spin_lock_irqsave(&priv->driver_lock, flags);
485                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
486                         sizeof(struct cmd_ds_802_11_afc));
487                 spin_unlock_irqrestore(&priv->driver_lock, flags);
488
489                 break;
490
491         case CMD_RET(CMD_MAC_MULTICAST_ADR):
492         case CMD_RET(CMD_MAC_CONTROL):
493         case CMD_RET(CMD_802_11_RESET):
494         case CMD_RET(CMD_802_11_AUTHENTICATE):
495         case CMD_RET(CMD_802_11_BEACON_STOP):
496                 break;
497
498         case CMD_RET(CMD_802_11_ENABLE_RSN):
499                 ret = lbs_ret_802_11_enable_rsn(priv, resp);
500                 break;
501
502         case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
503                 ret = lbs_ret_802_11_rate_adapt_rateset(priv, resp);
504                 break;
505
506         case CMD_RET(CMD_802_11_RSSI):
507                 ret = lbs_ret_802_11_rssi(priv, resp);
508                 break;
509
510         case CMD_RET(CMD_802_11_MAC_ADDRESS):
511                 ret = lbs_ret_802_11_mac_address(priv, resp);
512                 break;
513
514         case CMD_RET(CMD_802_11_AD_HOC_STOP):
515                 ret = lbs_ret_80211_ad_hoc_stop(priv, resp);
516                 break;
517
518         case CMD_RET(CMD_802_11_KEY_MATERIAL):
519                 ret = lbs_ret_802_11_key_material(priv, resp);
520                 break;
521
522         case CMD_RET(CMD_802_11_EEPROM_ACCESS):
523                 ret = lbs_ret_802_11_eeprom_access(priv, resp);
524                 break;
525
526         case CMD_RET(CMD_802_11D_DOMAIN_INFO):
527                 ret = lbs_ret_802_11d_domain_info(priv, resp);
528                 break;
529
530         case CMD_RET(CMD_802_11_TPC_CFG):
531                 spin_lock_irqsave(&priv->driver_lock, flags);
532                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
533                         sizeof(struct cmd_ds_802_11_tpc_cfg));
534                 spin_unlock_irqrestore(&priv->driver_lock, flags);
535                 break;
536         case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
537                 spin_lock_irqsave(&priv->driver_lock, flags);
538                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
539                         sizeof(struct cmd_ds_802_11_led_ctrl));
540                 spin_unlock_irqrestore(&priv->driver_lock, flags);
541                 break;
542         case CMD_RET(CMD_802_11_SUBSCRIBE_EVENT):
543                 ret = lbs_ret_802_11_subscribe_event(priv, resp);
544                 break;
545
546         case CMD_RET(CMD_802_11_PWR_CFG):
547                 spin_lock_irqsave(&priv->driver_lock, flags);
548                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.pwrcfg,
549                         sizeof(struct cmd_ds_802_11_pwr_cfg));
550                 spin_unlock_irqrestore(&priv->driver_lock, flags);
551
552                 break;
553
554         case CMD_RET(CMD_GET_TSF):
555                 spin_lock_irqsave(&priv->driver_lock, flags);
556                 memcpy((void *)priv->cur_cmd->callback_arg,
557                        &resp->params.gettsf.tsfvalue, sizeof(u64));
558                 spin_unlock_irqrestore(&priv->driver_lock, flags);
559                 break;
560         case CMD_RET(CMD_BT_ACCESS):
561                 spin_lock_irqsave(&priv->driver_lock, flags);
562                 if (priv->cur_cmd->callback_arg)
563                         memcpy((void *)priv->cur_cmd->callback_arg,
564                                &resp->params.bt.addr1, 2 * ETH_ALEN);
565                 spin_unlock_irqrestore(&priv->driver_lock, flags);
566                 break;
567         case CMD_RET(CMD_FWT_ACCESS):
568                 spin_lock_irqsave(&priv->driver_lock, flags);
569                 if (priv->cur_cmd->callback_arg)
570                         memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
571                                sizeof(resp->params.fwt));
572                 spin_unlock_irqrestore(&priv->driver_lock, flags);
573                 break;
574         case CMD_RET(CMD_802_11_BEACON_CTRL):
575                 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
576                 break;
577
578         default:
579                 lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
580                              le16_to_cpu(resp->command));
581                 break;
582         }
583         lbs_deb_leave(LBS_DEB_HOST);
584         return ret;
585 }
586
587 int lbs_process_rx_command(struct lbs_private *priv)
588 {
589         uint16_t respcmd, curcmd;
590         struct cmd_header *resp;
591         int ret = 0;
592         unsigned long flags;
593         uint16_t result;
594
595         lbs_deb_enter(LBS_DEB_HOST);
596
597         mutex_lock(&priv->lock);
598         spin_lock_irqsave(&priv->driver_lock, flags);
599
600         if (!priv->cur_cmd) {
601                 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
602                 ret = -1;
603                 spin_unlock_irqrestore(&priv->driver_lock, flags);
604                 goto done;
605         }
606
607         resp = (void *)priv->upld_buf;
608
609         curcmd = le16_to_cpu(resp->command);
610
611         respcmd = le16_to_cpu(resp->command);
612         result = le16_to_cpu(resp->result);
613
614         lbs_deb_host("CMD_RESP: response 0x%04x, seq %d, size %d, jiffies %lu\n",
615                      respcmd, le16_to_cpu(resp->seqnum), priv->upld_len, jiffies);
616         lbs_deb_hex(LBS_DEB_HOST, "CMD_RESP", (void *) resp, priv->upld_len);
617
618         if (resp->seqnum != resp->seqnum) {
619                 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
620                             le16_to_cpu(resp->seqnum), le16_to_cpu(resp->seqnum));
621                 spin_unlock_irqrestore(&priv->driver_lock, flags);
622                 ret = -1;
623                 goto done;
624         }
625         if (respcmd != CMD_RET(curcmd) &&
626             respcmd != CMD_802_11_ASSOCIATE && curcmd != CMD_RET_802_11_ASSOCIATE) {
627                 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
628                 spin_unlock_irqrestore(&priv->driver_lock, flags);
629                 ret = -1;
630                 goto done;
631         }
632
633         if (resp->result == cpu_to_le16(0x0004)) {
634                 /* 0x0004 means -EAGAIN. Drop the response, let it time out
635                    and be resubmitted */
636                 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
637                             le16_to_cpu(resp->command));
638                 spin_unlock_irqrestore(&priv->driver_lock, flags);
639                 ret = -1;
640                 goto done;
641         }
642
643         /* Now we got response from FW, cancel the command timer */
644         del_timer(&priv->command_timer);
645         priv->cmd_timed_out = 0;
646         if (priv->nr_retries) {
647                 lbs_pr_info("Received result %x to command %x after %d retries\n",
648                             result, curcmd, priv->nr_retries);
649                 priv->nr_retries = 0;
650         }
651
652         /* Store the response code to cur_cmd_retcode. */
653         priv->cur_cmd_retcode = result;
654
655         if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
656                 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
657                 u16 action = le16_to_cpu(psmode->action);
658
659                 lbs_deb_host(
660                        "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
661                        result, action);
662
663                 if (result) {
664                         lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
665                                     result);
666                         /*
667                          * We should not re-try enter-ps command in
668                          * ad-hoc mode. It takes place in
669                          * lbs_execute_next_command().
670                          */
671                         if (priv->mode == IW_MODE_ADHOC &&
672                             action == CMD_SUBCMD_ENTER_PS)
673                                 priv->psmode = LBS802_11POWERMODECAM;
674                 } else if (action == CMD_SUBCMD_ENTER_PS) {
675                         priv->needtowakeup = 0;
676                         priv->psstate = PS_STATE_AWAKE;
677
678                         lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
679                         if (priv->connect_status != LBS_CONNECTED) {
680                                 /*
681                                  * When Deauth Event received before Enter_PS command
682                                  * response, We need to wake up the firmware.
683                                  */
684                                 lbs_deb_host(
685                                        "disconnected, invoking lbs_ps_wakeup\n");
686
687                                 spin_unlock_irqrestore(&priv->driver_lock, flags);
688                                 mutex_unlock(&priv->lock);
689                                 lbs_ps_wakeup(priv, 0);
690                                 mutex_lock(&priv->lock);
691                                 spin_lock_irqsave(&priv->driver_lock, flags);
692                         }
693                 } else if (action == CMD_SUBCMD_EXIT_PS) {
694                         priv->needtowakeup = 0;
695                         priv->psstate = PS_STATE_FULL_POWER;
696                         lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
697                 } else {
698                         lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
699                 }
700
701                 lbs_complete_command(priv, priv->cur_cmd, result);
702                 spin_unlock_irqrestore(&priv->driver_lock, flags);
703
704                 ret = 0;
705                 goto done;
706         }
707
708         /* If the command is not successful, cleanup and return failure */
709         if ((result != 0 || !(respcmd & 0x8000))) {
710                 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
711                        result, respcmd);
712                 /*
713                  * Handling errors here
714                  */
715                 switch (respcmd) {
716                 case CMD_RET(CMD_GET_HW_SPEC):
717                 case CMD_RET(CMD_802_11_RESET):
718                         lbs_deb_host("CMD_RESP: reset failed\n");
719                         break;
720
721                 }
722                 lbs_complete_command(priv, priv->cur_cmd, result);
723                 spin_unlock_irqrestore(&priv->driver_lock, flags);
724
725                 ret = -1;
726                 goto done;
727         }
728
729         spin_unlock_irqrestore(&priv->driver_lock, flags);
730
731         if (priv->cur_cmd && priv->cur_cmd->callback) {
732                 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
733                                 resp);
734         } else
735                 ret = handle_cmd_response(priv, 0, resp);
736
737         spin_lock_irqsave(&priv->driver_lock, flags);
738
739         if (priv->cur_cmd) {
740                 /* Clean up and Put current command back to cmdfreeq */
741                 lbs_complete_command(priv, priv->cur_cmd, result);
742         }
743         spin_unlock_irqrestore(&priv->driver_lock, flags);
744
745 done:
746         mutex_unlock(&priv->lock);
747         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
748         return ret;
749 }
750
751 static int lbs_send_confirmwake(struct lbs_private *priv)
752 {
753         struct cmd_header *cmd = &priv->lbs_ps_confirm_wake;
754         int ret = 0;
755
756         lbs_deb_enter(LBS_DEB_HOST);
757
758         cmd->command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
759         cmd->size = cpu_to_le16(sizeof(*cmd));
760         cmd->seqnum = cpu_to_le16(++priv->seqnum);
761         cmd->result = 0;
762
763         lbs_deb_host("SEND_WAKEC_CMD: before download\n");
764
765         lbs_deb_hex(LBS_DEB_HOST, "wake confirm command", (void *)cmd, sizeof(*cmd));
766
767         ret = priv->hw_host_to_card(priv, MVMS_CMD, (void *)cmd, sizeof(*cmd));
768         if (ret)
769                 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
770
771         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
772         return ret;
773 }
774
775 int lbs_process_event(struct lbs_private *priv)
776 {
777         int ret = 0;
778         u32 eventcause;
779
780         lbs_deb_enter(LBS_DEB_CMD);
781
782         spin_lock_irq(&priv->driver_lock);
783         eventcause = priv->eventcause >> SBI_EVENT_CAUSE_SHIFT;
784         spin_unlock_irq(&priv->driver_lock);
785
786         lbs_deb_cmd("event cause %d\n", eventcause);
787
788         switch (eventcause) {
789         case MACREG_INT_CODE_LINK_SENSED:
790                 lbs_deb_cmd("EVENT: MACREG_INT_CODE_LINK_SENSED\n");
791                 break;
792
793         case MACREG_INT_CODE_DEAUTHENTICATED:
794                 lbs_deb_cmd("EVENT: deauthenticated\n");
795                 lbs_mac_event_disconnected(priv);
796                 break;
797
798         case MACREG_INT_CODE_DISASSOCIATED:
799                 lbs_deb_cmd("EVENT: disassociated\n");
800                 lbs_mac_event_disconnected(priv);
801                 break;
802
803         case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
804                 lbs_deb_cmd("EVENT: link lost\n");
805                 lbs_mac_event_disconnected(priv);
806                 break;
807
808         case MACREG_INT_CODE_PS_SLEEP:
809                 lbs_deb_cmd("EVENT: sleep\n");
810
811                 /* handle unexpected PS SLEEP event */
812                 if (priv->psstate == PS_STATE_FULL_POWER) {
813                         lbs_deb_cmd(
814                                "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
815                         break;
816                 }
817                 priv->psstate = PS_STATE_PRE_SLEEP;
818
819                 lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
820
821                 break;
822
823         case MACREG_INT_CODE_HOST_AWAKE:
824                 lbs_deb_cmd("EVENT: HOST_AWAKE\n");
825                 lbs_send_confirmwake(priv);
826                 break;
827
828         case MACREG_INT_CODE_PS_AWAKE:
829                 lbs_deb_cmd("EVENT: awake\n");
830                 /* handle unexpected PS AWAKE event */
831                 if (priv->psstate == PS_STATE_FULL_POWER) {
832                         lbs_deb_cmd(
833                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
834                         break;
835                 }
836
837                 priv->psstate = PS_STATE_AWAKE;
838
839                 if (priv->needtowakeup) {
840                         /*
841                          * wait for the command processing to finish
842                          * before resuming sending
843                          * priv->needtowakeup will be set to FALSE
844                          * in lbs_ps_wakeup()
845                          */
846                         lbs_deb_cmd("waking up ...\n");
847                         lbs_ps_wakeup(priv, 0);
848                 }
849                 break;
850
851         case MACREG_INT_CODE_MIC_ERR_UNICAST:
852                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
853                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
854                 break;
855
856         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
857                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
858                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
859                 break;
860         case MACREG_INT_CODE_MIB_CHANGED:
861         case MACREG_INT_CODE_INIT_DONE:
862                 break;
863
864         case MACREG_INT_CODE_ADHOC_BCN_LOST:
865                 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
866                 break;
867
868         case MACREG_INT_CODE_RSSI_LOW:
869                 lbs_pr_alert("EVENT: rssi low\n");
870                 break;
871         case MACREG_INT_CODE_SNR_LOW:
872                 lbs_pr_alert("EVENT: snr low\n");
873                 break;
874         case MACREG_INT_CODE_MAX_FAIL:
875                 lbs_pr_alert("EVENT: max fail\n");
876                 break;
877         case MACREG_INT_CODE_RSSI_HIGH:
878                 lbs_pr_alert("EVENT: rssi high\n");
879                 break;
880         case MACREG_INT_CODE_SNR_HIGH:
881                 lbs_pr_alert("EVENT: snr high\n");
882                 break;
883
884         case MACREG_INT_CODE_MESH_AUTO_STARTED:
885                 /* Ignore spurious autostart events if autostart is disabled */
886                 if (!priv->mesh_autostart_enabled) {
887                         lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
888                         break;
889                 }
890                 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
891                 priv->mesh_connect_status = LBS_CONNECTED;
892                 if (priv->mesh_open) {
893                         netif_carrier_on(priv->mesh_dev);
894                         if (!priv->tx_pending_len)
895                                 netif_wake_queue(priv->mesh_dev);
896                 }
897                 priv->mode = IW_MODE_ADHOC;
898                 schedule_work(&priv->sync_channel);
899                 break;
900
901         default:
902                 lbs_pr_alert("EVENT: unknown event id %d\n", eventcause);
903                 break;
904         }
905
906         spin_lock_irq(&priv->driver_lock);
907         priv->eventcause = 0;
908         spin_unlock_irq(&priv->driver_lock);
909
910         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
911         return ret;
912 }