libertas: remove handling for CMD_802_11_LED_GPIO_CTRL
[safe/jmp/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/sched.h>
7 #include <linux/if_arp.h>
8 #include <linux/netdevice.h>
9 #include <asm/unaligned.h>
10 #include <net/iw_handler.h>
11
12 #include "host.h"
13 #include "decl.h"
14 #include "cmd.h"
15 #include "defs.h"
16 #include "dev.h"
17 #include "assoc.h"
18 #include "wext.h"
19
20 /**
21  *  @brief This function handles disconnect event. it
22  *  reports disconnect to upper layer, clean tx/rx packets,
23  *  reset link state etc.
24  *
25  *  @param priv    A pointer to struct lbs_private structure
26  *  @return        n/a
27  */
28 void lbs_mac_event_disconnected(struct lbs_private *priv)
29 {
30         union iwreq_data wrqu;
31
32         if (priv->connect_status != LBS_CONNECTED)
33                 return;
34
35         lbs_deb_enter(LBS_DEB_ASSOC);
36
37         memset(wrqu.ap_addr.sa_data, 0x00, ETH_ALEN);
38         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
39
40         /*
41          * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
42          * It causes problem in the Supplicant
43          */
44
45         msleep_interruptible(1000);
46         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
47
48         /* report disconnect to upper layer */
49         netif_stop_queue(priv->dev);
50         netif_carrier_off(priv->dev);
51
52         /* Free Tx and Rx packets */
53         kfree_skb(priv->currenttxskb);
54         priv->currenttxskb = NULL;
55         priv->tx_pending_len = 0;
56
57         /* reset SNR/NF/RSSI values */
58         memset(priv->SNR, 0x00, sizeof(priv->SNR));
59         memset(priv->NF, 0x00, sizeof(priv->NF));
60         memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
61         memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
62         memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
63         priv->nextSNRNF = 0;
64         priv->numSNRNF = 0;
65         priv->connect_status = LBS_DISCONNECTED;
66
67         /* Clear out associated SSID and BSSID since connection is
68          * no longer valid.
69          */
70         memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
71         memset(&priv->curbssparams.ssid, 0, IEEE80211_MAX_SSID_LEN);
72         priv->curbssparams.ssid_len = 0;
73
74         if (priv->psstate != PS_STATE_FULL_POWER) {
75                 /* make firmware to exit PS mode */
76                 lbs_deb_cmd("disconnected, so exit PS mode\n");
77                 lbs_ps_wakeup(priv, 0);
78         }
79         lbs_deb_leave(LBS_DEB_ASSOC);
80 }
81
82 /**
83  *  @brief This function handles MIC failure event.
84  *
85  *  @param priv    A pointer to struct lbs_private structure
86  *  @para  event   the event id
87  *  @return        n/a
88  */
89 static void handle_mic_failureevent(struct lbs_private *priv, u32 event)
90 {
91         char buf[50];
92
93         lbs_deb_enter(LBS_DEB_CMD);
94         memset(buf, 0, sizeof(buf));
95
96         sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");
97
98         if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
99                 strcat(buf, "unicast ");
100         } else {
101                 strcat(buf, "multicast ");
102         }
103
104         lbs_send_iwevcustom_event(priv, buf);
105         lbs_deb_leave(LBS_DEB_CMD);
106 }
107
108 static int lbs_ret_reg_access(struct lbs_private *priv,
109                                u16 type, struct cmd_ds_command *resp)
110 {
111         int ret = 0;
112
113         lbs_deb_enter(LBS_DEB_CMD);
114
115         switch (type) {
116         case CMD_RET(CMD_MAC_REG_ACCESS):
117                 {
118                         struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
119
120                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
121                         priv->offsetvalue.value = le32_to_cpu(reg->value);
122                         break;
123                 }
124
125         case CMD_RET(CMD_BBP_REG_ACCESS):
126                 {
127                         struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
128
129                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
130                         priv->offsetvalue.value = reg->value;
131                         break;
132                 }
133
134         case CMD_RET(CMD_RF_REG_ACCESS):
135                 {
136                         struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
137
138                         priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
139                         priv->offsetvalue.value = reg->value;
140                         break;
141                 }
142
143         default:
144                 ret = -1;
145         }
146
147         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
148         return ret;
149 }
150
151 static inline int handle_cmd_response(struct lbs_private *priv,
152                                       struct cmd_header *cmd_response)
153 {
154         struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
155         int ret = 0;
156         unsigned long flags;
157         uint16_t respcmd = le16_to_cpu(resp->command);
158
159         lbs_deb_enter(LBS_DEB_HOST);
160
161         switch (respcmd) {
162         case CMD_RET(CMD_MAC_REG_ACCESS):
163         case CMD_RET(CMD_BBP_REG_ACCESS):
164         case CMD_RET(CMD_RF_REG_ACCESS):
165                 ret = lbs_ret_reg_access(priv, respcmd, resp);
166                 break;
167
168         case CMD_RET(CMD_802_11_SET_AFC):
169         case CMD_RET(CMD_802_11_GET_AFC):
170                 spin_lock_irqsave(&priv->driver_lock, flags);
171                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
172                         sizeof(struct cmd_ds_802_11_afc));
173                 spin_unlock_irqrestore(&priv->driver_lock, flags);
174
175                 break;
176
177         case CMD_RET(CMD_802_11_BEACON_STOP):
178                 break;
179
180         case CMD_RET(CMD_802_11_RSSI):
181                 ret = lbs_ret_802_11_rssi(priv, resp);
182                 break;
183
184         case CMD_RET(CMD_802_11_TPC_CFG):
185                 spin_lock_irqsave(&priv->driver_lock, flags);
186                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
187                         sizeof(struct cmd_ds_802_11_tpc_cfg));
188                 spin_unlock_irqrestore(&priv->driver_lock, flags);
189                 break;
190
191         case CMD_RET(CMD_GET_TSF):
192                 spin_lock_irqsave(&priv->driver_lock, flags);
193                 memcpy((void *)priv->cur_cmd->callback_arg,
194                        &resp->params.gettsf.tsfvalue, sizeof(u64));
195                 spin_unlock_irqrestore(&priv->driver_lock, flags);
196                 break;
197         case CMD_RET(CMD_BT_ACCESS):
198                 spin_lock_irqsave(&priv->driver_lock, flags);
199                 if (priv->cur_cmd->callback_arg)
200                         memcpy((void *)priv->cur_cmd->callback_arg,
201                                &resp->params.bt.addr1, 2 * ETH_ALEN);
202                 spin_unlock_irqrestore(&priv->driver_lock, flags);
203                 break;
204         case CMD_RET(CMD_FWT_ACCESS):
205                 spin_lock_irqsave(&priv->driver_lock, flags);
206                 if (priv->cur_cmd->callback_arg)
207                         memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
208                                sizeof(resp->params.fwt));
209                 spin_unlock_irqrestore(&priv->driver_lock, flags);
210                 break;
211         case CMD_RET(CMD_802_11_BEACON_CTRL):
212                 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
213                 break;
214
215         default:
216                 lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
217                            le16_to_cpu(resp->command));
218                 break;
219         }
220         lbs_deb_leave(LBS_DEB_HOST);
221         return ret;
222 }
223
224 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
225 {
226         uint16_t respcmd, curcmd;
227         struct cmd_header *resp;
228         int ret = 0;
229         unsigned long flags;
230         uint16_t result;
231
232         lbs_deb_enter(LBS_DEB_HOST);
233
234         mutex_lock(&priv->lock);
235         spin_lock_irqsave(&priv->driver_lock, flags);
236
237         if (!priv->cur_cmd) {
238                 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
239                 ret = -1;
240                 spin_unlock_irqrestore(&priv->driver_lock, flags);
241                 goto done;
242         }
243
244         resp = (void *)data;
245         curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
246         respcmd = le16_to_cpu(resp->command);
247         result = le16_to_cpu(resp->result);
248
249         lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
250                      respcmd, le16_to_cpu(resp->seqnum), len);
251         lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
252
253         if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
254                 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
255                             le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
256                 spin_unlock_irqrestore(&priv->driver_lock, flags);
257                 ret = -1;
258                 goto done;
259         }
260         if (respcmd != CMD_RET(curcmd) &&
261             respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
262                 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
263                 spin_unlock_irqrestore(&priv->driver_lock, flags);
264                 ret = -1;
265                 goto done;
266         }
267
268         if (resp->result == cpu_to_le16(0x0004)) {
269                 /* 0x0004 means -EAGAIN. Drop the response, let it time out
270                    and be resubmitted */
271                 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
272                             le16_to_cpu(resp->command));
273                 spin_unlock_irqrestore(&priv->driver_lock, flags);
274                 ret = -1;
275                 goto done;
276         }
277
278         /* Now we got response from FW, cancel the command timer */
279         del_timer(&priv->command_timer);
280         priv->cmd_timed_out = 0;
281         if (priv->nr_retries) {
282                 lbs_pr_info("Received result %x to command %x after %d retries\n",
283                             result, curcmd, priv->nr_retries);
284                 priv->nr_retries = 0;
285         }
286
287         /* Store the response code to cur_cmd_retcode. */
288         priv->cur_cmd_retcode = result;
289
290         if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
291                 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
292                 u16 action = le16_to_cpu(psmode->action);
293
294                 lbs_deb_host(
295                        "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
296                        result, action);
297
298                 if (result) {
299                         lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
300                                     result);
301                         /*
302                          * We should not re-try enter-ps command in
303                          * ad-hoc mode. It takes place in
304                          * lbs_execute_next_command().
305                          */
306                         if (priv->mode == IW_MODE_ADHOC &&
307                             action == CMD_SUBCMD_ENTER_PS)
308                                 priv->psmode = LBS802_11POWERMODECAM;
309                 } else if (action == CMD_SUBCMD_ENTER_PS) {
310                         priv->needtowakeup = 0;
311                         priv->psstate = PS_STATE_AWAKE;
312
313                         lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
314                         if (priv->connect_status != LBS_CONNECTED) {
315                                 /*
316                                  * When Deauth Event received before Enter_PS command
317                                  * response, We need to wake up the firmware.
318                                  */
319                                 lbs_deb_host(
320                                        "disconnected, invoking lbs_ps_wakeup\n");
321
322                                 spin_unlock_irqrestore(&priv->driver_lock, flags);
323                                 mutex_unlock(&priv->lock);
324                                 lbs_ps_wakeup(priv, 0);
325                                 mutex_lock(&priv->lock);
326                                 spin_lock_irqsave(&priv->driver_lock, flags);
327                         }
328                 } else if (action == CMD_SUBCMD_EXIT_PS) {
329                         priv->needtowakeup = 0;
330                         priv->psstate = PS_STATE_FULL_POWER;
331                         lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
332                 } else {
333                         lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
334                 }
335
336                 lbs_complete_command(priv, priv->cur_cmd, result);
337                 spin_unlock_irqrestore(&priv->driver_lock, flags);
338
339                 ret = 0;
340                 goto done;
341         }
342
343         /* If the command is not successful, cleanup and return failure */
344         if ((result != 0 || !(respcmd & 0x8000))) {
345                 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
346                        result, respcmd);
347                 /*
348                  * Handling errors here
349                  */
350                 switch (respcmd) {
351                 case CMD_RET(CMD_GET_HW_SPEC):
352                 case CMD_RET(CMD_802_11_RESET):
353                         lbs_deb_host("CMD_RESP: reset failed\n");
354                         break;
355
356                 }
357                 lbs_complete_command(priv, priv->cur_cmd, result);
358                 spin_unlock_irqrestore(&priv->driver_lock, flags);
359
360                 ret = -1;
361                 goto done;
362         }
363
364         spin_unlock_irqrestore(&priv->driver_lock, flags);
365
366         if (priv->cur_cmd && priv->cur_cmd->callback) {
367                 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
368                                 resp);
369         } else
370                 ret = handle_cmd_response(priv, resp);
371
372         spin_lock_irqsave(&priv->driver_lock, flags);
373
374         if (priv->cur_cmd) {
375                 /* Clean up and Put current command back to cmdfreeq */
376                 lbs_complete_command(priv, priv->cur_cmd, result);
377         }
378         spin_unlock_irqrestore(&priv->driver_lock, flags);
379
380 done:
381         mutex_unlock(&priv->lock);
382         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
383         return ret;
384 }
385
386 static int lbs_send_confirmwake(struct lbs_private *priv)
387 {
388         struct cmd_header cmd;
389         int ret = 0;
390
391         lbs_deb_enter(LBS_DEB_HOST);
392
393         cmd.command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
394         cmd.size = cpu_to_le16(sizeof(cmd));
395         cmd.seqnum = cpu_to_le16(++priv->seqnum);
396         cmd.result = 0;
397
398         lbs_deb_hex(LBS_DEB_HOST, "wake confirm", (u8 *) &cmd,
399                 sizeof(cmd));
400
401         ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &cmd, sizeof(cmd));
402         if (ret)
403                 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
404
405         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
406         return ret;
407 }
408
409 int lbs_process_event(struct lbs_private *priv, u32 event)
410 {
411         int ret = 0;
412
413         lbs_deb_enter(LBS_DEB_CMD);
414
415         switch (event) {
416         case MACREG_INT_CODE_LINK_SENSED:
417                 lbs_deb_cmd("EVENT: link sensed\n");
418                 break;
419
420         case MACREG_INT_CODE_DEAUTHENTICATED:
421                 lbs_deb_cmd("EVENT: deauthenticated\n");
422                 lbs_mac_event_disconnected(priv);
423                 break;
424
425         case MACREG_INT_CODE_DISASSOCIATED:
426                 lbs_deb_cmd("EVENT: disassociated\n");
427                 lbs_mac_event_disconnected(priv);
428                 break;
429
430         case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
431                 lbs_deb_cmd("EVENT: link lost\n");
432                 lbs_mac_event_disconnected(priv);
433                 break;
434
435         case MACREG_INT_CODE_PS_SLEEP:
436                 lbs_deb_cmd("EVENT: ps sleep\n");
437
438                 /* handle unexpected PS SLEEP event */
439                 if (priv->psstate == PS_STATE_FULL_POWER) {
440                         lbs_deb_cmd(
441                                "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
442                         break;
443                 }
444                 priv->psstate = PS_STATE_PRE_SLEEP;
445
446                 lbs_ps_confirm_sleep(priv);
447
448                 break;
449
450         case MACREG_INT_CODE_HOST_AWAKE:
451                 lbs_deb_cmd("EVENT: host awake\n");
452                 if (priv->reset_deep_sleep_wakeup)
453                         priv->reset_deep_sleep_wakeup(priv);
454                 priv->is_deep_sleep = 0;
455                 lbs_send_confirmwake(priv);
456                 break;
457
458         case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
459                 if (priv->reset_deep_sleep_wakeup)
460                         priv->reset_deep_sleep_wakeup(priv);
461                 lbs_deb_cmd("EVENT: ds awake\n");
462                 priv->is_deep_sleep = 0;
463                 priv->wakeup_dev_required = 0;
464                 wake_up_interruptible(&priv->ds_awake_q);
465                 break;
466
467         case MACREG_INT_CODE_PS_AWAKE:
468                 lbs_deb_cmd("EVENT: ps awake\n");
469                 /* handle unexpected PS AWAKE event */
470                 if (priv->psstate == PS_STATE_FULL_POWER) {
471                         lbs_deb_cmd(
472                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
473                         break;
474                 }
475
476                 priv->psstate = PS_STATE_AWAKE;
477
478                 if (priv->needtowakeup) {
479                         /*
480                          * wait for the command processing to finish
481                          * before resuming sending
482                          * priv->needtowakeup will be set to FALSE
483                          * in lbs_ps_wakeup()
484                          */
485                         lbs_deb_cmd("waking up ...\n");
486                         lbs_ps_wakeup(priv, 0);
487                 }
488                 break;
489
490         case MACREG_INT_CODE_MIC_ERR_UNICAST:
491                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
492                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
493                 break;
494
495         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
496                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
497                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
498                 break;
499
500         case MACREG_INT_CODE_MIB_CHANGED:
501                 lbs_deb_cmd("EVENT: MIB CHANGED\n");
502                 break;
503         case MACREG_INT_CODE_INIT_DONE:
504                 lbs_deb_cmd("EVENT: INIT DONE\n");
505                 break;
506         case MACREG_INT_CODE_ADHOC_BCN_LOST:
507                 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
508                 break;
509         case MACREG_INT_CODE_RSSI_LOW:
510                 lbs_pr_alert("EVENT: rssi low\n");
511                 break;
512         case MACREG_INT_CODE_SNR_LOW:
513                 lbs_pr_alert("EVENT: snr low\n");
514                 break;
515         case MACREG_INT_CODE_MAX_FAIL:
516                 lbs_pr_alert("EVENT: max fail\n");
517                 break;
518         case MACREG_INT_CODE_RSSI_HIGH:
519                 lbs_pr_alert("EVENT: rssi high\n");
520                 break;
521         case MACREG_INT_CODE_SNR_HIGH:
522                 lbs_pr_alert("EVENT: snr high\n");
523                 break;
524
525         case MACREG_INT_CODE_MESH_AUTO_STARTED:
526                 /* Ignore spurious autostart events if autostart is disabled */
527                 if (!priv->mesh_autostart_enabled) {
528                         lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
529                         break;
530                 }
531                 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
532                 priv->mesh_connect_status = LBS_CONNECTED;
533                 if (priv->mesh_open) {
534                         netif_carrier_on(priv->mesh_dev);
535                         if (!priv->tx_pending_len)
536                                 netif_wake_queue(priv->mesh_dev);
537                 }
538                 priv->mode = IW_MODE_ADHOC;
539                 schedule_work(&priv->sync_channel);
540                 break;
541
542         default:
543                 lbs_pr_alert("EVENT: unknown event id %d\n", event);
544                 break;
545         }
546
547         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
548         return ret;
549 }