2 * This file contains the handling of command
3 * responses as well as events generated by firmware.
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>
16 * lbs_mac_event_disconnected - handles disconnect event. It
17 * reports disconnect to upper layer, clean tx/rx packets,
18 * reset link state etc.
20 * @priv: A pointer to struct lbs_private structure
24 void lbs_mac_event_disconnected(struct lbs_private *priv)
26 if (priv->connect_status != LBS_CONNECTED)
29 lbs_deb_enter(LBS_DEB_ASSOC);
32 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
33 * It causes problem in the Supplicant
35 msleep_interruptible(1000);
37 if (priv->wdev->iftype == NL80211_IFTYPE_STATION)
38 lbs_send_disconnect_notification(priv);
40 /* report disconnect to upper layer */
41 netif_stop_queue(priv->dev);
42 netif_carrier_off(priv->dev);
44 /* Free Tx and Rx packets */
45 kfree_skb(priv->currenttxskb);
46 priv->currenttxskb = NULL;
47 priv->tx_pending_len = 0;
49 priv->connect_status = LBS_DISCONNECTED;
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);
56 lbs_deb_leave(LBS_DEB_ASSOC);
59 int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
61 uint16_t respcmd, curcmd;
62 struct cmd_header *resp;
67 lbs_deb_enter(LBS_DEB_HOST);
69 mutex_lock(&priv->lock);
70 spin_lock_irqsave(&priv->driver_lock, flags);
73 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
75 spin_unlock_irqrestore(&priv->driver_lock, flags);
80 curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
81 respcmd = le16_to_cpu(resp->command);
82 result = le16_to_cpu(resp->result);
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);
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);
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",
101 spin_unlock_irqrestore(&priv->driver_lock, flags);
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);
117 /* Now we got response from FW, cancel the command timer */
118 del_timer(&priv->command_timer);
119 priv->cmd_timed_out = 0;
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);
126 "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
130 lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
133 * We should not re-try enter-ps command in
134 * ad-hoc mode. It takes place in
135 * lbs_execute_next_command().
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;
144 lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
145 if (priv->connect_status != LBS_CONNECTED) {
147 * When Deauth Event received before Enter_PS command
148 * response, We need to wake up the firmware.
151 "disconnected, invoking lbs_ps_wakeup\n");
153 spin_unlock_irqrestore(&priv->driver_lock, flags);
154 mutex_unlock(&priv->lock);
155 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS,
157 mutex_lock(&priv->lock);
158 spin_lock_irqsave(&priv->driver_lock, flags);
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");
165 lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
168 lbs_complete_command(priv, priv->cur_cmd, result);
169 spin_unlock_irqrestore(&priv->driver_lock, flags);
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",
180 * Handling errors here
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");
189 lbs_complete_command(priv, priv->cur_cmd, result);
190 spin_unlock_irqrestore(&priv->driver_lock, flags);
196 spin_unlock_irqrestore(&priv->driver_lock, flags);
198 if (priv->cur_cmd && priv->cur_cmd->callback) {
199 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
203 spin_lock_irqsave(&priv->driver_lock, flags);
206 /* Clean up and Put current command back to cmdfreeq */
207 lbs_complete_command(priv, priv->cur_cmd, result);
209 spin_unlock_irqrestore(&priv->driver_lock, flags);
212 mutex_unlock(&priv->lock);
213 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
217 int lbs_process_event(struct lbs_private *priv, u32 event)
220 struct cmd_header cmd;
222 lbs_deb_enter(LBS_DEB_CMD);
225 case MACREG_INT_CODE_LINK_SENSED:
226 lbs_deb_cmd("EVENT: link sensed\n");
229 case MACREG_INT_CODE_DEAUTHENTICATED:
230 lbs_deb_cmd("EVENT: deauthenticated\n");
231 lbs_mac_event_disconnected(priv);
234 case MACREG_INT_CODE_DISASSOCIATED:
235 lbs_deb_cmd("EVENT: disassociated\n");
236 lbs_mac_event_disconnected(priv);
239 case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
240 lbs_deb_cmd("EVENT: link lost\n");
241 lbs_mac_event_disconnected(priv);
244 case MACREG_INT_CODE_PS_SLEEP:
245 lbs_deb_cmd("EVENT: ps sleep\n");
247 /* handle unexpected PS SLEEP event */
248 if (priv->psstate == PS_STATE_FULL_POWER) {
250 "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
253 priv->psstate = PS_STATE_PRE_SLEEP;
255 lbs_ps_confirm_sleep(priv);
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,
266 priv->is_host_sleep_activated = 0;
267 wake_up_interruptible(&priv->host_sleep_q);
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);
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) {
284 "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
288 priv->psstate = PS_STATE_AWAKE;
290 if (priv->needtowakeup) {
292 * wait for the command processing to finish
293 * before resuming sending
294 * priv->needtowakeup will be set to FALSE
297 lbs_deb_cmd("waking up ...\n");
298 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
302 case MACREG_INT_CODE_MIC_ERR_UNICAST:
303 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
304 lbs_send_mic_failureevent(priv, event);
307 case MACREG_INT_CODE_MIC_ERR_MULTICAST:
308 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
309 lbs_send_mic_failureevent(priv, event);
312 case MACREG_INT_CODE_MIB_CHANGED:
313 lbs_deb_cmd("EVENT: MIB CHANGED\n");
315 case MACREG_INT_CODE_INIT_DONE:
316 lbs_deb_cmd("EVENT: INIT DONE\n");
318 case MACREG_INT_CODE_ADHOC_BCN_LOST:
319 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
321 case MACREG_INT_CODE_RSSI_LOW:
322 netdev_alert(priv->dev, "EVENT: rssi low\n");
324 case MACREG_INT_CODE_SNR_LOW:
325 netdev_alert(priv->dev, "EVENT: snr low\n");
327 case MACREG_INT_CODE_MAX_FAIL:
328 netdev_alert(priv->dev, "EVENT: max fail\n");
330 case MACREG_INT_CODE_RSSI_HIGH:
331 netdev_alert(priv->dev, "EVENT: rssi high\n");
333 case MACREG_INT_CODE_SNR_HIGH:
334 netdev_alert(priv->dev, "EVENT: snr high\n");
337 case MACREG_INT_CODE_MESH_AUTO_STARTED:
338 /* Ignore spurious autostart events */
339 netdev_info(priv->dev, "EVENT: MESH_AUTO_STARTED (ignoring)\n");
343 netdev_alert(priv->dev, "EVENT: unknown event id %d\n", event);
347 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);