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