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