Merge master.kernel.org:/home/rmk/linux-2.6-arm
[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 int libertas_ret_802_11_enable_rsn(wlan_private * priv,
541                                           struct cmd_ds_command *resp)
542 {
543         struct cmd_ds_802_11_enable_rsn *enable_rsn = &resp->params.enbrsn;
544         wlan_adapter *adapter = priv->adapter;
545         u32 * pdata_buf = adapter->cur_cmd->pdata_buf;
546
547         lbs_deb_enter(LBS_DEB_CMD);
548
549         if (enable_rsn->action == cpu_to_le16(cmd_act_get)) {
550                 if (pdata_buf)
551                         *pdata_buf = (u32) le16_to_cpu(enable_rsn->enable);
552         }
553
554         lbs_deb_enter(LBS_DEB_CMD);
555         return 0;
556 }
557
558 static inline int handle_cmd_response(u16 respcmd,
559                                       struct cmd_ds_command *resp,
560                                       wlan_private *priv)
561 {
562         int ret = 0;
563         unsigned long flags;
564         wlan_adapter *adapter = priv->adapter;
565
566         switch (respcmd) {
567         case cmd_ret_mac_reg_access:
568         case cmd_ret_bbp_reg_access:
569         case cmd_ret_rf_reg_access:
570                 ret = wlan_ret_reg_access(priv, respcmd, resp);
571                 break;
572
573         case cmd_ret_hw_spec_info:
574                 ret = wlan_ret_get_hw_spec(priv, resp);
575                 break;
576
577         case cmd_ret_802_11_scan:
578                 ret = libertas_ret_80211_scan(priv, resp);
579                 break;
580
581         case cmd_ret_802_11_get_log:
582                 ret = wlan_ret_get_log(priv, resp);
583                 break;
584
585         case cmd_ret_802_11_associate:
586         case cmd_ret_802_11_reassociate:
587                 ret = libertas_ret_80211_associate(priv, resp);
588                 break;
589
590         case cmd_ret_802_11_disassociate:
591         case cmd_ret_802_11_deauthenticate:
592                 ret = libertas_ret_80211_disassociate(priv, resp);
593                 break;
594
595         case cmd_ret_802_11_ad_hoc_start:
596         case cmd_ret_802_11_ad_hoc_join:
597                 ret = libertas_ret_80211_ad_hoc_start(priv, resp);
598                 break;
599
600         case cmd_ret_802_11_stat:
601                 ret = wlan_ret_802_11_stat(priv, resp);
602                 break;
603
604         case cmd_ret_802_11_snmp_mib:
605                 ret = wlan_ret_802_11_snmp_mib(priv, resp);
606                 break;
607
608         case cmd_ret_802_11_rf_tx_power:
609                 ret = wlan_ret_802_11_rf_tx_power(priv, resp);
610                 break;
611
612         case cmd_ret_802_11_set_afc:
613         case cmd_ret_802_11_get_afc:
614                 spin_lock_irqsave(&adapter->driver_lock, flags);
615                 memmove(adapter->cur_cmd->pdata_buf, &resp->params.afc,
616                         sizeof(struct cmd_ds_802_11_afc));
617                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
618
619                 break;
620         case cmd_ret_802_11_rf_antenna:
621                 ret = wlan_ret_802_11_rf_antenna(priv, resp);
622                 break;
623
624         case cmd_ret_mac_multicast_adr:
625         case cmd_ret_mac_control:
626         case cmd_ret_802_11_set_wep:
627         case cmd_ret_802_11_reset:
628         case cmd_ret_802_11_authenticate:
629         case cmd_ret_802_11_radio_control:
630         case cmd_ret_802_11_beacon_stop:
631                 break;
632
633         case cmd_ret_802_11_enable_rsn:
634                 ret = libertas_ret_802_11_enable_rsn(priv, resp);
635                 break;
636
637         case cmd_ret_802_11_data_rate:
638                 ret = wlan_ret_802_11_data_rate(priv, resp);
639                 break;
640         case cmd_ret_802_11_rate_adapt_rateset:
641                 ret = wlan_ret_802_11_rate_adapt_rateset(priv, resp);
642                 break;
643         case cmd_ret_802_11_rf_channel:
644                 ret = wlan_ret_802_11_rf_channel(priv, resp);
645                 break;
646
647         case cmd_ret_802_11_rssi:
648                 ret = wlan_ret_802_11_rssi(priv, resp);
649                 break;
650
651         case cmd_ret_802_11_mac_address:
652                 ret = wlan_ret_802_11_mac_address(priv, resp);
653                 break;
654
655         case cmd_ret_802_11_ad_hoc_stop:
656                 ret = libertas_ret_80211_ad_hoc_stop(priv, resp);
657                 break;
658
659         case cmd_ret_802_11_key_material:
660                 lbs_deb_cmd("CMD_RESP: KEY_MATERIAL command response\n");
661                 ret = wlan_ret_802_11_key_material(priv, resp);
662                 break;
663
664         case cmd_ret_802_11_eeprom_access:
665                 ret = wlan_ret_802_11_eeprom_access(priv, resp);
666                 break;
667
668         case cmd_ret_802_11d_domain_info:
669                 ret = libertas_ret_802_11d_domain_info(priv, resp);
670                 break;
671
672         case cmd_ret_802_11_sleep_params:
673                 ret = wlan_ret_802_11_sleep_params(priv, resp);
674                 break;
675         case cmd_ret_802_11_inactivity_timeout:
676                 spin_lock_irqsave(&adapter->driver_lock, flags);
677                 *((u16 *) adapter->cur_cmd->pdata_buf) =
678                     le16_to_cpu(resp->params.inactivity_timeout.timeout);
679                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
680                 break;
681
682         case cmd_ret_802_11_tpc_cfg:
683                 spin_lock_irqsave(&adapter->driver_lock, flags);
684                 memmove(adapter->cur_cmd->pdata_buf, &resp->params.tpccfg,
685                         sizeof(struct cmd_ds_802_11_tpc_cfg));
686                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
687                 break;
688         case cmd_ret_802_11_led_gpio_ctrl:
689                 spin_lock_irqsave(&adapter->driver_lock, flags);
690                 memmove(adapter->cur_cmd->pdata_buf, &resp->params.ledgpio,
691                         sizeof(struct cmd_ds_802_11_led_ctrl));
692                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
693                 break;
694         case cmd_ret_802_11_pwr_cfg:
695                 spin_lock_irqsave(&adapter->driver_lock, flags);
696                 memmove(adapter->cur_cmd->pdata_buf, &resp->params.pwrcfg,
697                         sizeof(struct cmd_ds_802_11_pwr_cfg));
698                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
699
700                 break;
701
702         case cmd_ret_get_tsf:
703                 spin_lock_irqsave(&adapter->driver_lock, flags);
704                 memcpy(priv->adapter->cur_cmd->pdata_buf,
705                        &resp->params.gettsf.tsfvalue, sizeof(u64));
706                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
707                 break;
708         case cmd_ret_bt_access:
709                 spin_lock_irqsave(&adapter->driver_lock, flags);
710                 if (adapter->cur_cmd->pdata_buf)
711                         memcpy(adapter->cur_cmd->pdata_buf,
712                                &resp->params.bt.addr1, 2 * ETH_ALEN);
713                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
714                 break;
715         case cmd_ret_fwt_access:
716                 spin_lock_irqsave(&adapter->driver_lock, flags);
717                 if (adapter->cur_cmd->pdata_buf)
718                         memcpy(adapter->cur_cmd->pdata_buf, &resp->params.fwt,
719                                sizeof(resp->params.fwt));
720                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
721                 break;
722         case cmd_ret_mesh_access:
723                 if (adapter->cur_cmd->pdata_buf)
724                         memcpy(adapter->cur_cmd->pdata_buf, &resp->params.mesh,
725                                sizeof(resp->params.mesh));
726                 break;
727         case cmd_rte_802_11_tx_rate_query:
728                 priv->adapter->txrate = resp->params.txrate.txrate;
729                 break;
730         default:
731                 lbs_deb_cmd("CMD_RESP: Unknown command response %#x\n",
732                             resp->command);
733                 break;
734         }
735         return ret;
736 }
737
738 int libertas_process_rx_command(wlan_private * priv)
739 {
740         u16 respcmd;
741         struct cmd_ds_command *resp;
742         wlan_adapter *adapter = priv->adapter;
743         int ret = 0;
744         ulong flags;
745         u16 result;
746
747         lbs_deb_enter(LBS_DEB_CMD);
748
749         lbs_deb_cmd("CMD_RESP: @ %lu\n", jiffies);
750
751         /* Now we got response from FW, cancel the command timer */
752         del_timer(&adapter->command_timer);
753
754         mutex_lock(&adapter->lock);
755         spin_lock_irqsave(&adapter->driver_lock, flags);
756
757         if (!adapter->cur_cmd) {
758                 lbs_deb_cmd("CMD_RESP: NULL cur_cmd=%p\n", adapter->cur_cmd);
759                 ret = -1;
760                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
761                 goto done;
762         }
763         resp = (struct cmd_ds_command *)(adapter->cur_cmd->bufvirtualaddr);
764
765         lbs_dbg_hex("CMD_RESP:", adapter->cur_cmd->bufvirtualaddr,
766                     priv->upld_len);
767
768         respcmd = le16_to_cpu(resp->command);
769
770         result = le16_to_cpu(resp->result);
771
772         lbs_deb_cmd("CMD_RESP: %x result: %d length: %d\n", respcmd,
773                     result, priv->upld_len);
774
775         if (!(respcmd & 0x8000)) {
776                 lbs_deb_cmd("Invalid response to command!");
777                 adapter->cur_cmd_retcode = -1;
778                 __libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
779                 adapter->nr_cmd_pending--;
780                 adapter->cur_cmd = NULL;
781                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
782                 ret = -1;
783                 goto done;
784         }
785
786         /* Store the response code to cur_cmd_retcode. */
787         adapter->cur_cmd_retcode = result;;
788
789         if (respcmd == cmd_ret_802_11_ps_mode) {
790                 struct cmd_ds_802_11_ps_mode *psmode = &resp->params.psmode;
791                 u16 action = le16_to_cpu(psmode->action);
792
793                 lbs_deb_cmd(
794                        "CMD_RESP: PS_MODE cmd reply result=%#x action=0x%X\n",
795                        result, action);
796
797                 if (result) {
798                         lbs_deb_cmd("CMD_RESP: PS command failed- %#x \n",
799                                     result);
800                         /*
801                          * We should not re-try enter-ps command in
802                          * ad-hoc mode. It takes place in
803                          * libertas_execute_next_command().
804                          */
805                         if (adapter->mode == IW_MODE_ADHOC &&
806                             action == cmd_subcmd_enter_ps)
807                                 adapter->psmode = wlan802_11powermodecam;
808                 } else if (action == cmd_subcmd_enter_ps) {
809                         adapter->needtowakeup = 0;
810                         adapter->psstate = PS_STATE_AWAKE;
811
812                         lbs_deb_cmd("CMD_RESP: Enter_PS command response\n");
813                         if (adapter->connect_status != libertas_connected) {
814                                 /*
815                                  * When Deauth Event received before Enter_PS command
816                                  * response, We need to wake up the firmware.
817                                  */
818                                 lbs_deb_cmd(
819                                        "Disconnected, Going to invoke libertas_ps_wakeup\n");
820
821                                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
822                                 mutex_unlock(&adapter->lock);
823                                 libertas_ps_wakeup(priv, 0);
824                                 mutex_lock(&adapter->lock);
825                                 spin_lock_irqsave(&adapter->driver_lock, flags);
826                         }
827                 } else if (action == cmd_subcmd_exit_ps) {
828                         adapter->needtowakeup = 0;
829                         adapter->psstate = PS_STATE_FULL_POWER;
830                         lbs_deb_cmd("CMD_RESP: Exit_PS command response\n");
831                 } else {
832                         lbs_deb_cmd("CMD_RESP: PS- action=0x%X\n", action);
833                 }
834
835                 __libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
836                 adapter->nr_cmd_pending--;
837                 adapter->cur_cmd = NULL;
838                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
839
840                 ret = 0;
841                 goto done;
842         }
843
844         if (adapter->cur_cmd->cmdflags & CMD_F_HOSTCMD) {
845                 /* Copy the response back to response buffer */
846                 memcpy(adapter->cur_cmd->pdata_buf, resp, resp->size);
847
848                 adapter->cur_cmd->cmdflags &= ~CMD_F_HOSTCMD;
849         }
850
851         /* If the command is not successful, cleanup and return failure */
852         if ((result != 0 || !(respcmd & 0x8000))) {
853                 lbs_deb_cmd("CMD_RESP: command reply %#x result=%#x\n",
854                        respcmd, result);
855                 /*
856                  * Handling errors here
857                  */
858                 switch (respcmd) {
859                 case cmd_ret_hw_spec_info:
860                 case cmd_ret_802_11_reset:
861                         lbs_deb_cmd("CMD_RESP: Reset command failed\n");
862                         break;
863
864                 }
865
866                 __libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
867                 adapter->nr_cmd_pending--;
868                 adapter->cur_cmd = NULL;
869                 spin_unlock_irqrestore(&adapter->driver_lock, flags);
870
871                 ret = -1;
872                 goto done;
873         }
874
875         spin_unlock_irqrestore(&adapter->driver_lock, flags);
876
877         ret = handle_cmd_response(respcmd, resp, priv);
878
879         spin_lock_irqsave(&adapter->driver_lock, flags);
880         if (adapter->cur_cmd) {
881                 /* Clean up and Put current command back to cmdfreeq */
882                 __libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
883                 adapter->nr_cmd_pending--;
884                 WARN_ON(adapter->nr_cmd_pending > 128);
885                 adapter->cur_cmd = NULL;
886         }
887         spin_unlock_irqrestore(&adapter->driver_lock, flags);
888
889 done:
890         mutex_unlock(&adapter->lock);
891         lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
892         return ret;
893 }
894
895 int libertas_process_event(wlan_private * priv)
896 {
897         int ret = 0;
898         wlan_adapter *adapter = priv->adapter;
899         u32 eventcause;
900
901         spin_lock_irq(&adapter->driver_lock);
902         eventcause = adapter->eventcause;
903         spin_unlock_irq(&adapter->driver_lock);
904
905         lbs_deb_enter(LBS_DEB_CMD);
906
907         lbs_deb_cmd("EVENT Cause %x\n", eventcause);
908
909         switch (eventcause >> SBI_EVENT_CAUSE_SHIFT) {
910         case MACREG_INT_CODE_LINK_SENSED:
911                 lbs_deb_cmd("EVENT: MACREG_INT_CODE_LINK_SENSED\n");
912                 break;
913
914         case MACREG_INT_CODE_DEAUTHENTICATED:
915                 lbs_deb_cmd("EVENT: Deauthenticated\n");
916                 libertas_mac_event_disconnected(priv);
917                 break;
918
919         case MACREG_INT_CODE_DISASSOCIATED:
920                 lbs_deb_cmd("EVENT: Disassociated\n");
921                 libertas_mac_event_disconnected(priv);
922                 break;
923
924         case MACREG_INT_CODE_LINK_LOSE_NO_SCAN:
925                 lbs_deb_cmd("EVENT: Link lost\n");
926                 libertas_mac_event_disconnected(priv);
927                 break;
928
929         case MACREG_INT_CODE_PS_SLEEP:
930                 lbs_deb_cmd("EVENT: SLEEP\n");
931                 lbs_deb_cmd("_");
932
933                 /* handle unexpected PS SLEEP event */
934                 if (adapter->psstate == PS_STATE_FULL_POWER) {
935                         lbs_deb_cmd(
936                                "EVENT: In FULL POWER mode - ignore PS SLEEP\n");
937                         break;
938                 }
939                 adapter->psstate = PS_STATE_PRE_SLEEP;
940
941                 libertas_ps_confirm_sleep(priv, (u16) adapter->psmode);
942
943                 break;
944
945         case MACREG_INT_CODE_PS_AWAKE:
946                 lbs_deb_cmd("EVENT: AWAKE \n");
947                 lbs_deb_cmd("|");
948
949                 /* handle unexpected PS AWAKE event */
950                 if (adapter->psstate == PS_STATE_FULL_POWER) {
951                         lbs_deb_cmd(
952                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
953                         break;
954                 }
955
956                 adapter->psstate = PS_STATE_AWAKE;
957
958                 if (adapter->needtowakeup) {
959                         /*
960                          * wait for the command processing to finish
961                          * before resuming sending
962                          * adapter->needtowakeup will be set to FALSE
963                          * in libertas_ps_wakeup()
964                          */
965                         lbs_deb_cmd("Waking up...\n");
966                         libertas_ps_wakeup(priv, 0);
967                 }
968                 break;
969
970         case MACREG_INT_CODE_MIC_ERR_UNICAST:
971                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
972                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
973                 break;
974
975         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
976                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
977                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
978                 break;
979         case MACREG_INT_CODE_MIB_CHANGED:
980         case MACREG_INT_CODE_INIT_DONE:
981                 break;
982
983         case MACREG_INT_CODE_ADHOC_BCN_LOST:
984                 lbs_deb_cmd("EVENT: HWAC - ADHOC BCN LOST\n");
985                 break;
986
987         case MACREG_INT_CODE_RSSI_LOW:
988                 lbs_pr_alert( "EVENT: RSSI_LOW\n");
989                 break;
990         case MACREG_INT_CODE_SNR_LOW:
991                 lbs_pr_alert( "EVENT: SNR_LOW\n");
992                 break;
993         case MACREG_INT_CODE_MAX_FAIL:
994                 lbs_pr_alert( "EVENT: MAX_FAIL\n");
995                 break;
996         case MACREG_INT_CODE_RSSI_HIGH:
997                 lbs_pr_alert( "EVENT: RSSI_HIGH\n");
998                 break;
999         case MACREG_INT_CODE_SNR_HIGH:
1000                 lbs_pr_alert( "EVENT: SNR_HIGH\n");
1001                 break;
1002
1003         case MACREG_INT_CODE_MESH_AUTO_STARTED:
1004                 lbs_pr_alert( "EVENT: MESH_AUTO_STARTED\n");
1005                 adapter->connect_status = libertas_connected ;
1006                 if (priv->mesh_open == 1) {
1007                         netif_wake_queue(priv->mesh_dev) ;
1008                         netif_carrier_on(priv->mesh_dev) ;
1009                 }
1010                 adapter->mode = IW_MODE_ADHOC ;
1011                 schedule_work(&priv->sync_channel);
1012                 break;
1013
1014         default:
1015                 lbs_pr_alert( "EVENT: unknown event id: %#x\n",
1016                        eventcause >> SBI_EVENT_CAUSE_SHIFT);
1017                 break;
1018         }
1019
1020         spin_lock_irq(&adapter->driver_lock);
1021         adapter->eventcause = 0;
1022         spin_unlock_irq(&adapter->driver_lock);
1023
1024         lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
1025         return ret;
1026 }