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