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