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