Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/padovan/blueto...
[pandora-kernel.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
6 #include <linux/slab.h>
7 #include <linux/delay.h>
8 #include <linux/sched.h>
9 #include <asm/unaligned.h>
10 #include <net/cfg80211.h>
11
12 #include "cfg.h"
13 #include "cmd.h"
14
15 /**
16  * lbs_mac_event_disconnected - handles disconnect event. It
17  * reports disconnect to upper layer, clean tx/rx packets,
18  * reset link state etc.
19  *
20  * @priv:       A pointer to struct lbs_private structure
21  *
22  * returns:     n/a
23  */
24 void lbs_mac_event_disconnected(struct lbs_private *priv)
25 {
26         if (priv->connect_status != LBS_CONNECTED)
27                 return;
28
29         lbs_deb_enter(LBS_DEB_ASSOC);
30
31         /*
32          * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
33          * It causes problem in the Supplicant
34          */
35         msleep_interruptible(1000);
36
37         if (priv->wdev->iftype == NL80211_IFTYPE_STATION)
38                 lbs_send_disconnect_notification(priv);
39
40         /* report disconnect to upper layer */
41         netif_stop_queue(priv->dev);
42         netif_carrier_off(priv->dev);
43
44         /* Free Tx and Rx packets */
45         kfree_skb(priv->currenttxskb);
46         priv->currenttxskb = NULL;
47         priv->tx_pending_len = 0;
48
49         priv->connect_status = LBS_DISCONNECTED;
50
51         if (priv->psstate != PS_STATE_FULL_POWER) {
52                 /* make firmware to exit PS mode */
53                 lbs_deb_cmd("disconnected, so exit PS mode\n");
54                 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
55         }
56         lbs_deb_leave(LBS_DEB_ASSOC);
57 }
58
59 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
60 {
61         uint16_t respcmd, curcmd;
62         struct cmd_header *resp;
63         int ret = 0;
64         unsigned long flags;
65         uint16_t result;
66
67         lbs_deb_enter(LBS_DEB_HOST);
68
69         mutex_lock(&priv->lock);
70         spin_lock_irqsave(&priv->driver_lock, flags);
71
72         if (!priv->cur_cmd) {
73                 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
74                 ret = -1;
75                 spin_unlock_irqrestore(&priv->driver_lock, flags);
76                 goto done;
77         }
78
79         resp = (void *)data;
80         curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
81         respcmd = le16_to_cpu(resp->command);
82         result = le16_to_cpu(resp->result);
83
84         lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
85                      respcmd, le16_to_cpu(resp->seqnum), len);
86         lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
87
88         if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
89                 netdev_info(priv->dev,
90                             "Received CMD_RESP with invalid sequence %d (expected %d)\n",
91                             le16_to_cpu(resp->seqnum),
92                             le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
93                 spin_unlock_irqrestore(&priv->driver_lock, flags);
94                 ret = -1;
95                 goto done;
96         }
97         if (respcmd != CMD_RET(curcmd) &&
98             respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
99                 netdev_info(priv->dev, "Invalid CMD_RESP %x to command %x!\n",
100                             respcmd, curcmd);
101                 spin_unlock_irqrestore(&priv->driver_lock, flags);
102                 ret = -1;
103                 goto done;
104         }
105
106         if (resp->result == cpu_to_le16(0x0004)) {
107                 /* 0x0004 means -EAGAIN. Drop the response, let it time out
108                    and be resubmitted */
109                 netdev_info(priv->dev,
110                             "Firmware returns DEFER to command %x. Will let it time out...\n",
111                             le16_to_cpu(resp->command));
112                 spin_unlock_irqrestore(&priv->driver_lock, flags);
113                 ret = -1;
114                 goto done;
115         }
116
117         /* Now we got response from FW, cancel the command timer */
118         del_timer(&priv->command_timer);
119         priv->cmd_timed_out = 0;
120
121         if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
122                 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
123                 u16 action = le16_to_cpu(psmode->action);
124
125                 lbs_deb_host(
126                        "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
127                        result, action);
128
129                 if (result) {
130                         lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
131                                     result);
132                         /*
133                          * We should not re-try enter-ps command in
134                          * ad-hoc mode. It takes place in
135                          * lbs_execute_next_command().
136                          */
137                         if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR &&
138                             action == PS_MODE_ACTION_ENTER_PS)
139                                 priv->psmode = LBS802_11POWERMODECAM;
140                 } else if (action == PS_MODE_ACTION_ENTER_PS) {
141                         priv->needtowakeup = 0;
142                         priv->psstate = PS_STATE_AWAKE;
143
144                         lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
145                         if (priv->connect_status != LBS_CONNECTED) {
146                                 /*
147                                  * When Deauth Event received before Enter_PS command
148                                  * response, We need to wake up the firmware.
149                                  */
150                                 lbs_deb_host(
151                                        "disconnected, invoking lbs_ps_wakeup\n");
152
153                                 spin_unlock_irqrestore(&priv->driver_lock, flags);
154                                 mutex_unlock(&priv->lock);
155                                 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS,
156                                                 false);
157                                 mutex_lock(&priv->lock);
158                                 spin_lock_irqsave(&priv->driver_lock, flags);
159                         }
160                 } else if (action == PS_MODE_ACTION_EXIT_PS) {
161                         priv->needtowakeup = 0;
162                         priv->psstate = PS_STATE_FULL_POWER;
163                         lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
164                 } else {
165                         lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
166                 }
167
168                 lbs_complete_command(priv, priv->cur_cmd, result);
169                 spin_unlock_irqrestore(&priv->driver_lock, flags);
170
171                 ret = 0;
172                 goto done;
173         }
174
175         /* If the command is not successful, cleanup and return failure */
176         if ((result != 0 || !(respcmd & 0x8000))) {
177                 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
178                        result, respcmd);
179                 /*
180                  * Handling errors here
181                  */
182                 switch (respcmd) {
183                 case CMD_RET(CMD_GET_HW_SPEC):
184                 case CMD_RET(CMD_802_11_RESET):
185                         lbs_deb_host("CMD_RESP: reset failed\n");
186                         break;
187
188                 }
189                 lbs_complete_command(priv, priv->cur_cmd, result);
190                 spin_unlock_irqrestore(&priv->driver_lock, flags);
191
192                 ret = -1;
193                 goto done;
194         }
195
196         spin_unlock_irqrestore(&priv->driver_lock, flags);
197
198         if (priv->cur_cmd && priv->cur_cmd->callback) {
199                 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
200                                 resp);
201         }
202
203         spin_lock_irqsave(&priv->driver_lock, flags);
204
205         if (priv->cur_cmd) {
206                 /* Clean up and Put current command back to cmdfreeq */
207                 lbs_complete_command(priv, priv->cur_cmd, result);
208         }
209         spin_unlock_irqrestore(&priv->driver_lock, flags);
210
211 done:
212         mutex_unlock(&priv->lock);
213         lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
214         return ret;
215 }
216
217 int lbs_process_event(struct lbs_private *priv, u32 event)
218 {
219         int ret = 0;
220         struct cmd_header cmd;
221
222         lbs_deb_enter(LBS_DEB_CMD);
223
224         switch (event) {
225         case MACREG_INT_CODE_LINK_SENSED:
226                 lbs_deb_cmd("EVENT: link sensed\n");
227                 break;
228
229         case MACREG_INT_CODE_DEAUTHENTICATED:
230                 lbs_deb_cmd("EVENT: deauthenticated\n");
231                 lbs_mac_event_disconnected(priv);
232                 break;
233
234         case MACREG_INT_CODE_DISASSOCIATED:
235                 lbs_deb_cmd("EVENT: disassociated\n");
236                 lbs_mac_event_disconnected(priv);
237                 break;
238
239         case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
240                 lbs_deb_cmd("EVENT: link lost\n");
241                 lbs_mac_event_disconnected(priv);
242                 break;
243
244         case MACREG_INT_CODE_PS_SLEEP:
245                 lbs_deb_cmd("EVENT: ps sleep\n");
246
247                 /* handle unexpected PS SLEEP event */
248                 if (priv->psstate == PS_STATE_FULL_POWER) {
249                         lbs_deb_cmd(
250                                "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
251                         break;
252                 }
253                 priv->psstate = PS_STATE_PRE_SLEEP;
254
255                 lbs_ps_confirm_sleep(priv);
256
257                 break;
258
259         case MACREG_INT_CODE_HOST_AWAKE:
260                 lbs_deb_cmd("EVENT: host awake\n");
261                 if (priv->reset_deep_sleep_wakeup)
262                         priv->reset_deep_sleep_wakeup(priv);
263                 priv->is_deep_sleep = 0;
264                 lbs_cmd_async(priv, CMD_802_11_WAKEUP_CONFIRM, &cmd,
265                                 sizeof(cmd));
266                 priv->is_host_sleep_activated = 0;
267                 wake_up_interruptible(&priv->host_sleep_q);
268                 break;
269
270         case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
271                 if (priv->reset_deep_sleep_wakeup)
272                         priv->reset_deep_sleep_wakeup(priv);
273                 lbs_deb_cmd("EVENT: ds awake\n");
274                 priv->is_deep_sleep = 0;
275                 priv->wakeup_dev_required = 0;
276                 wake_up_interruptible(&priv->ds_awake_q);
277                 break;
278
279         case MACREG_INT_CODE_PS_AWAKE:
280                 lbs_deb_cmd("EVENT: ps awake\n");
281                 /* handle unexpected PS AWAKE event */
282                 if (priv->psstate == PS_STATE_FULL_POWER) {
283                         lbs_deb_cmd(
284                                "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
285                         break;
286                 }
287
288                 priv->psstate = PS_STATE_AWAKE;
289
290                 if (priv->needtowakeup) {
291                         /*
292                          * wait for the command processing to finish
293                          * before resuming sending
294                          * priv->needtowakeup will be set to FALSE
295                          * in lbs_ps_wakeup()
296                          */
297                         lbs_deb_cmd("waking up ...\n");
298                         lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
299                 }
300                 break;
301
302         case MACREG_INT_CODE_MIC_ERR_UNICAST:
303                 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
304                 lbs_send_mic_failureevent(priv, event);
305                 break;
306
307         case MACREG_INT_CODE_MIC_ERR_MULTICAST:
308                 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
309                 lbs_send_mic_failureevent(priv, event);
310                 break;
311
312         case MACREG_INT_CODE_MIB_CHANGED:
313                 lbs_deb_cmd("EVENT: MIB CHANGED\n");
314                 break;
315         case MACREG_INT_CODE_INIT_DONE:
316                 lbs_deb_cmd("EVENT: INIT DONE\n");
317                 break;
318         case MACREG_INT_CODE_ADHOC_BCN_LOST:
319                 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
320                 break;
321         case MACREG_INT_CODE_RSSI_LOW:
322                 netdev_alert(priv->dev, "EVENT: rssi low\n");
323                 break;
324         case MACREG_INT_CODE_SNR_LOW:
325                 netdev_alert(priv->dev, "EVENT: snr low\n");
326                 break;
327         case MACREG_INT_CODE_MAX_FAIL:
328                 netdev_alert(priv->dev, "EVENT: max fail\n");
329                 break;
330         case MACREG_INT_CODE_RSSI_HIGH:
331                 netdev_alert(priv->dev, "EVENT: rssi high\n");
332                 break;
333         case MACREG_INT_CODE_SNR_HIGH:
334                 netdev_alert(priv->dev, "EVENT: snr high\n");
335                 break;
336
337         case MACREG_INT_CODE_MESH_AUTO_STARTED:
338                 /* Ignore spurious autostart events */
339                 netdev_info(priv->dev, "EVENT: MESH_AUTO_STARTED (ignoring)\n");
340                 break;
341
342         default:
343                 netdev_alert(priv->dev, "EVENT: unknown event id %d\n", event);
344                 break;
345         }
346
347         lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
348         return ret;
349 }