libertas: move association related commands into assoc.c
[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         case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
191                 spin_lock_irqsave(&priv->driver_lock, flags);
192                 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
193                         sizeof(struct cmd_ds_802_11_led_ctrl));
194                 spin_unlock_irqrestore(&priv->driver_lock, flags);
195                 break;
196
197         case CMD_RET(CMD_GET_TSF):
198                 spin_lock_irqsave(&priv->driver_lock, flags);
199                 memcpy((void *)priv->cur_cmd->callback_arg,
200                        &resp->params.gettsf.tsfvalue, sizeof(u64));
201                 spin_unlock_irqrestore(&priv->driver_lock, flags);
202                 break;
203         case CMD_RET(CMD_BT_ACCESS):
204                 spin_lock_irqsave(&priv->driver_lock, flags);
205                 if (priv->cur_cmd->callback_arg)
206                         memcpy((void *)priv->cur_cmd->callback_arg,
207                                &resp->params.bt.addr1, 2 * ETH_ALEN);
208                 spin_unlock_irqrestore(&priv->driver_lock, flags);
209                 break;
210         case CMD_RET(CMD_FWT_ACCESS):
211                 spin_lock_irqsave(&priv->driver_lock, flags);
212                 if (priv->cur_cmd->callback_arg)
213                         memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
214                                sizeof(resp->params.fwt));
215                 spin_unlock_irqrestore(&priv->driver_lock, flags);
216                 break;
217         case CMD_RET(CMD_802_11_BEACON_CTRL):
218                 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
219                 break;
220
221         default:
222                 lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
223                            le16_to_cpu(resp->command));
224                 break;
225         }
226         lbs_deb_leave(LBS_DEB_HOST);
227         return ret;
228 }
229
230 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
231 {
232         uint16_t respcmd, curcmd;
233         struct cmd_header *resp;
234         int ret = 0;
235         unsigned long flags;
236         uint16_t result;
237
238         lbs_deb_enter(LBS_DEB_HOST);
239
240         mutex_lock(&priv->lock);
241         spin_lock_irqsave(&priv->driver_lock, flags);
242
243         if (!priv->cur_cmd) {
244                 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
245                 ret = -1;
246                 spin_unlock_irqrestore(&priv->driver_lock, flags);
247                 goto done;
248         }
249
250         resp = (void *)data;
251         curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
252         respcmd = le16_to_cpu(resp->command);
253         result = le16_to_cpu(resp->result);
254
255         lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
256                      respcmd, le16_to_cpu(resp->seqnum), len);
257         lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
258
259         if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
260                 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
261                             le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
262                 spin_unlock_irqrestore(&priv->driver_lock, flags);
263                 ret = -1;
264                 goto done;
265         }
266         if (respcmd != CMD_RET(curcmd) &&
267             respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
268                 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
269                 spin_unlock_irqrestore(&priv->driver_lock, flags);
270                 ret = -1;
271                 goto done;
272         }
273
274         if (resp->result == cpu_to_le16(0x0004)) {
275                 /* 0x0004 means -EAGAIN. Drop the response, let it time out
276                    and be resubmitted */
277                 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
278                             le16_to_cpu(resp->command));
279                 spin_unlock_irqrestore(&priv->driver_lock, flags);
280                 ret = -1;
281                 goto done;
282         }
283
284         /* Now we got response from FW, cancel the command timer */
285         del_timer(&priv->command_timer);
286         priv->cmd_timed_out = 0;
287         if (priv->nr_retries) {
288                 lbs_pr_info("Received result %x to command %x after %d retries\n",
289                             result, curcmd, priv->nr_retries);
290                 priv->nr_retries = 0;
291         }
292
293         /* Store the response code to cur_cmd_retcode. */
294         priv->cur_cmd_retcode = result;
295
296         if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
297                 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
298                 u16 action = le16_to_cpu(psmode->action);
299
300                 lbs_deb_host(
301                        "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
302                        result, action);
303
304                 if (result) {
305                         lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
306                                     result);
307                         /*
308                          * We should not re-try enter-ps command in
309                          * ad-hoc mode. It takes place in
310                          * lbs_execute_next_command().
311                          */
312                         if (priv->mode == IW_MODE_ADHOC &&
313                             action == CMD_SUBCMD_ENTER_PS)
314                                 priv->psmode = LBS802_11POWERMODECAM;
315                 } else if (action == CMD_SUBCMD_ENTER_PS) {
316                         priv->needtowakeup = 0;
317                         priv->psstate = PS_STATE_AWAKE;
318
319                         lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
320                         if (priv->connect_status != LBS_CONNECTED) {
321                                 /*
322                                  * When Deauth Event received before Enter_PS command
323                                  * response, We need to wake up the firmware.
324                                  */
325                                 lbs_deb_host(
326                                        "disconnected, invoking lbs_ps_wakeup\n");
327
328                                 spin_unlock_irqrestore(&priv->driver_lock, flags);
329                                 mutex_unlock(&priv->lock);
330                                 lbs_ps_wakeup(priv, 0);
331                                 mutex_lock(&priv->lock);
332                                 spin_lock_irqsave(&priv->driver_lock, flags);
333                         }
334                 } else if (action == CMD_SUBCMD_EXIT_PS) {
335                         priv->needtowakeup = 0;
336                         priv->psstate = PS_STATE_FULL_POWER;
337                         lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
338                 } else {
339                         lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
340                 }
341
342                 lbs_complete_command(priv, priv->cur_cmd, result);
343                 spin_unlock_irqrestore(&priv->driver_lock, flags);
344
345                 ret = 0;
346                 goto done;
347         }
348
349         /* If the command is not successful, cleanup and return failure */
350         if ((result != 0 || !(respcmd & 0x8000))) {
351                 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
352                        result, respcmd);
353                 /*
354                  * Handling errors here
355                  */
356                 switch (respcmd) {
357                 case CMD_RET(CMD_GET_HW_SPEC):
358                 case CMD_RET(CMD_802_11_RESET):
359                         lbs_deb_host("CMD_RESP: reset failed\n");
360                         break;
361
362                 }
363                 lbs_complete_command(priv, priv->cur_cmd, result);
364                 spin_unlock_irqrestore(&priv->driver_lock, flags);
365
366                 ret = -1;
367                 goto done;
368         }
369
370         spin_unlock_irqrestore(&priv->driver_lock, flags);
371
372         if (priv->cur_cmd && priv->cur_cmd->callback) {
373                 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
374                                 resp);
375         } else
376                 ret = handle_cmd_response(priv, resp);
377
378         spin_lock_irqsave(&priv->driver_lock, flags);
379
380         if (priv->cur_cmd) {
381                 /* Clean up and Put current command back to cmdfreeq */
382                 lbs_complete_command(priv, priv->cur_cmd, result);
383         }
384         spin_unlock_irqrestore(&priv->driver_lock, flags);
385
386 done:
387         mutex_unlock(&priv->lock);
388         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
389         return ret;
390 }
391
392 static int lbs_send_confirmwake(struct lbs_private *priv)
393 {
394         struct cmd_header cmd;
395         int ret = 0;
396
397         lbs_deb_enter(LBS_DEB_HOST);
398
399         cmd.command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
400         cmd.size = cpu_to_le16(sizeof(cmd));
401         cmd.seqnum = cpu_to_le16(++priv->seqnum);
402         cmd.result = 0;
403
404         lbs_deb_hex(LBS_DEB_HOST, "wake confirm", (u8 *) &cmd,
405                 sizeof(cmd));
406
407         ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &cmd, sizeof(cmd));
408         if (ret)
409                 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
410
411         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
412         return ret;
413 }
414
415 int lbs_process_event(struct lbs_private *priv, u32 event)
416 {
417         int ret = 0;
418
419         lbs_deb_enter(LBS_DEB_CMD);
420
421         switch (event) {
422         case MACREG_INT_CODE_LINK_SENSED:
423                 lbs_deb_cmd("EVENT: link sensed\n");
424                 break;
425
426         case MACREG_INT_CODE_DEAUTHENTICATED:
427                 lbs_deb_cmd("EVENT: deauthenticated\n");
428                 lbs_mac_event_disconnected(priv);
429                 break;
430
431         case MACREG_INT_CODE_DISASSOCIATED:
432                 lbs_deb_cmd("EVENT: disassociated\n");
433                 lbs_mac_event_disconnected(priv);
434                 break;
435
436         case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
437                 lbs_deb_cmd("EVENT: link lost\n");
438                 lbs_mac_event_disconnected(priv);
439                 break;
440
441         case MACREG_INT_CODE_PS_SLEEP:
442                 lbs_deb_cmd("EVENT: ps sleep\n");
443
444                 /* handle unexpected PS SLEEP event */
445                 if (priv->psstate == PS_STATE_FULL_POWER) {
446                         lbs_deb_cmd(
447                                "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
448                         break;
449                 }
450                 priv->psstate = PS_STATE_PRE_SLEEP;
451
452                 lbs_ps_confirm_sleep(priv);
453
454                 break;
455
456         case MACREG_INT_CODE_HOST_AWAKE:
457                 lbs_deb_cmd("EVENT: host awake\n");
458                 if (priv->reset_deep_sleep_wakeup)
459                         priv->reset_deep_sleep_wakeup(priv);
460                 priv->is_deep_sleep = 0;
461                 lbs_send_confirmwake(priv);
462                 break;
463
464         case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
465                 if (priv->reset_deep_sleep_wakeup)
466                         priv->reset_deep_sleep_wakeup(priv);
467                 lbs_deb_cmd("EVENT: ds awake\n");
468                 priv->is_deep_sleep = 0;
469                 priv->wakeup_dev_required = 0;
470                 wake_up_interruptible(&priv->ds_awake_q);
471                 break;
472
473         case MACREG_INT_CODE_PS_AWAKE:
474                 lbs_deb_cmd("EVENT: ps awake\n");
475                 /* handle unexpected PS AWAKE event */
476                 if (priv->psstate == PS_STATE_FULL_POWER) {
477                         lbs_deb_cmd(
478                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
479                         break;
480                 }
481
482                 priv->psstate = PS_STATE_AWAKE;
483
484                 if (priv->needtowakeup) {
485                         /*
486                          * wait for the command processing to finish
487                          * before resuming sending
488                          * priv->needtowakeup will be set to FALSE
489                          * in lbs_ps_wakeup()
490                          */
491                         lbs_deb_cmd("waking up ...\n");
492                         lbs_ps_wakeup(priv, 0);
493                 }
494                 break;
495
496         case MACREG_INT_CODE_MIC_ERR_UNICAST:
497                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
498                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
499                 break;
500
501         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
502                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
503                 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
504                 break;
505
506         case MACREG_INT_CODE_MIB_CHANGED:
507                 lbs_deb_cmd("EVENT: MIB CHANGED\n");
508                 break;
509         case MACREG_INT_CODE_INIT_DONE:
510                 lbs_deb_cmd("EVENT: INIT DONE\n");
511                 break;
512         case MACREG_INT_CODE_ADHOC_BCN_LOST:
513                 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
514                 break;
515         case MACREG_INT_CODE_RSSI_LOW:
516                 lbs_pr_alert("EVENT: rssi low\n");
517                 break;
518         case MACREG_INT_CODE_SNR_LOW:
519                 lbs_pr_alert("EVENT: snr low\n");
520                 break;
521         case MACREG_INT_CODE_MAX_FAIL:
522                 lbs_pr_alert("EVENT: max fail\n");
523                 break;
524         case MACREG_INT_CODE_RSSI_HIGH:
525                 lbs_pr_alert("EVENT: rssi high\n");
526                 break;
527         case MACREG_INT_CODE_SNR_HIGH:
528                 lbs_pr_alert("EVENT: snr high\n");
529                 break;
530
531         case MACREG_INT_CODE_MESH_AUTO_STARTED:
532                 /* Ignore spurious autostart events if autostart is disabled */
533                 if (!priv->mesh_autostart_enabled) {
534                         lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
535                         break;
536                 }
537                 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
538                 priv->mesh_connect_status = LBS_CONNECTED;
539                 if (priv->mesh_open) {
540                         netif_carrier_on(priv->mesh_dev);
541                         if (!priv->tx_pending_len)
542                                 netif_wake_queue(priv->mesh_dev);
543                 }
544                 priv->mode = IW_MODE_ADHOC;
545                 schedule_work(&priv->sync_channel);
546                 break;
547
548         default:
549                 lbs_pr_alert("EVENT: unknown event id %d\n", event);
550                 break;
551         }
552
553         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
554         return ret;
555 }