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