Bluetooth: Fix handling of L2CAP Command Reject over LE
[cascardo/linux.git] / drivers / net / wireless / cw1200 / scan.c
1 /*
2  * Scan implementation for ST-Ericsson CW1200 mac80211 drivers
3  *
4  * Copyright (c) 2010, ST-Ericsson
5  * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/sched.h>
13 #include "cw1200.h"
14 #include "scan.h"
15 #include "sta.h"
16 #include "pm.h"
17
18 static void cw1200_scan_restart_delayed(struct cw1200_common *priv);
19
20 static int cw1200_scan_start(struct cw1200_common *priv, struct wsm_scan *scan)
21 {
22         int ret, i;
23         int tmo = 2000;
24
25         switch (priv->join_status) {
26         case CW1200_JOIN_STATUS_PRE_STA:
27         case CW1200_JOIN_STATUS_JOINING:
28                 return -EBUSY;
29         default:
30                 break;
31         }
32
33         wiphy_dbg(priv->hw->wiphy, "[SCAN] hw req, type %d, %d channels, flags: 0x%x.\n",
34                   scan->type, scan->num_channels, scan->flags);
35
36         for (i = 0; i < scan->num_channels; ++i)
37                 tmo += scan->ch[i].max_chan_time + 10;
38
39         cancel_delayed_work_sync(&priv->clear_recent_scan_work);
40         atomic_set(&priv->scan.in_progress, 1);
41         atomic_set(&priv->recent_scan, 1);
42         cw1200_pm_stay_awake(&priv->pm_state, tmo * HZ / 1000);
43         queue_delayed_work(priv->workqueue, &priv->scan.timeout,
44                            tmo * HZ / 1000);
45         ret = wsm_scan(priv, scan);
46         if (ret) {
47                 atomic_set(&priv->scan.in_progress, 0);
48                 cancel_delayed_work_sync(&priv->scan.timeout);
49                 cw1200_scan_restart_delayed(priv);
50         }
51         return ret;
52 }
53
54 int cw1200_hw_scan(struct ieee80211_hw *hw,
55                    struct ieee80211_vif *vif,
56                    struct cfg80211_scan_request *req)
57 {
58         struct cw1200_common *priv = hw->priv;
59         struct wsm_template_frame frame = {
60                 .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
61         };
62         int i, ret;
63
64         if (!priv->vif)
65                 return -EINVAL;
66
67         /* Scan when P2P_GO corrupt firmware MiniAP mode */
68         if (priv->join_status == CW1200_JOIN_STATUS_AP)
69                 return -EOPNOTSUPP;
70
71         if (req->n_ssids == 1 && !req->ssids[0].ssid_len)
72                 req->n_ssids = 0;
73
74         wiphy_dbg(hw->wiphy, "[SCAN] Scan request for %d SSIDs.\n",
75                   req->n_ssids);
76
77         if (req->n_ssids > WSM_SCAN_MAX_NUM_OF_SSIDS)
78                 return -EINVAL;
79
80         frame.skb = ieee80211_probereq_get(hw, priv->vif, NULL, 0,
81                 req->ie_len);
82         if (!frame.skb)
83                 return -ENOMEM;
84
85         if (req->ie_len)
86                 memcpy(skb_put(frame.skb, req->ie_len), req->ie, req->ie_len);
87
88         /* will be unlocked in cw1200_scan_work() */
89         down(&priv->scan.lock);
90         mutex_lock(&priv->conf_mutex);
91
92         ret = wsm_set_template_frame(priv, &frame);
93         if (!ret) {
94                 /* Host want to be the probe responder. */
95                 ret = wsm_set_probe_responder(priv, true);
96         }
97         if (ret) {
98                 mutex_unlock(&priv->conf_mutex);
99                 up(&priv->scan.lock);
100                 dev_kfree_skb(frame.skb);
101                 return ret;
102         }
103
104         wsm_lock_tx(priv);
105
106         BUG_ON(priv->scan.req);
107         priv->scan.req = req;
108         priv->scan.n_ssids = 0;
109         priv->scan.status = 0;
110         priv->scan.begin = &req->channels[0];
111         priv->scan.curr = priv->scan.begin;
112         priv->scan.end = &req->channels[req->n_channels];
113         priv->scan.output_power = priv->output_power;
114
115         for (i = 0; i < req->n_ssids; ++i) {
116                 struct wsm_ssid *dst = &priv->scan.ssids[priv->scan.n_ssids];
117                 memcpy(&dst->ssid[0], req->ssids[i].ssid, sizeof(dst->ssid));
118                 dst->length = req->ssids[i].ssid_len;
119                 ++priv->scan.n_ssids;
120         }
121
122         mutex_unlock(&priv->conf_mutex);
123
124         if (frame.skb)
125                 dev_kfree_skb(frame.skb);
126         queue_work(priv->workqueue, &priv->scan.work);
127         return 0;
128 }
129
130 void cw1200_scan_work(struct work_struct *work)
131 {
132         struct cw1200_common *priv = container_of(work, struct cw1200_common,
133                                                         scan.work);
134         struct ieee80211_channel **it;
135         struct wsm_scan scan = {
136                 .type = WSM_SCAN_TYPE_FOREGROUND,
137                 .flags = WSM_SCAN_FLAG_SPLIT_METHOD,
138         };
139         bool first_run = (priv->scan.begin == priv->scan.curr &&
140                           priv->scan.begin != priv->scan.end);
141         int i;
142
143         if (first_run) {
144                 /* Firmware gets crazy if scan request is sent
145                  * when STA is joined but not yet associated.
146                  * Force unjoin in this case.
147                  */
148                 if (cancel_delayed_work_sync(&priv->join_timeout) > 0)
149                         cw1200_join_timeout(&priv->join_timeout.work);
150         }
151
152         mutex_lock(&priv->conf_mutex);
153
154         if (first_run) {
155                 if (priv->join_status == CW1200_JOIN_STATUS_STA &&
156                     !(priv->powersave_mode.mode & WSM_PSM_PS)) {
157                         struct wsm_set_pm pm = priv->powersave_mode;
158                         pm.mode = WSM_PSM_PS;
159                         cw1200_set_pm(priv, &pm);
160                 } else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
161                         /* FW bug: driver has to restart p2p-dev mode
162                          * after scan
163                          */
164                         cw1200_disable_listening(priv);
165                 }
166         }
167
168         if (!priv->scan.req || (priv->scan.curr == priv->scan.end)) {
169                 if (priv->scan.output_power != priv->output_power)
170                         wsm_set_output_power(priv, priv->output_power * 10);
171                 if (priv->join_status == CW1200_JOIN_STATUS_STA &&
172                     !(priv->powersave_mode.mode & WSM_PSM_PS))
173                         cw1200_set_pm(priv, &priv->powersave_mode);
174
175                 if (priv->scan.status < 0)
176                         wiphy_dbg(priv->hw->wiphy, "[SCAN] Scan failed (%d).\n",
177                                   priv->scan.status);
178                 else if (priv->scan.req)
179                         wiphy_dbg(priv->hw->wiphy,
180                                   "[SCAN] Scan completed.\n");
181                 else
182                         wiphy_dbg(priv->hw->wiphy,
183                                   "[SCAN] Scan canceled.\n");
184
185                 priv->scan.req = NULL;
186                 cw1200_scan_restart_delayed(priv);
187                 wsm_unlock_tx(priv);
188                 mutex_unlock(&priv->conf_mutex);
189                 ieee80211_scan_completed(priv->hw, priv->scan.status ? 1 : 0);
190                 up(&priv->scan.lock);
191                 return;
192         } else {
193                 struct ieee80211_channel *first = *priv->scan.curr;
194                 for (it = priv->scan.curr + 1, i = 1;
195                      it != priv->scan.end && i < WSM_SCAN_MAX_NUM_OF_CHANNELS;
196                      ++it, ++i) {
197                         if ((*it)->band != first->band)
198                                 break;
199                         if (((*it)->flags ^ first->flags) &
200                                         IEEE80211_CHAN_NO_IR)
201                                 break;
202                         if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
203                             (*it)->max_power != first->max_power)
204                                 break;
205                 }
206                 scan.band = first->band;
207
208                 if (priv->scan.req->no_cck)
209                         scan.max_tx_rate = WSM_TRANSMIT_RATE_6;
210                 else
211                         scan.max_tx_rate = WSM_TRANSMIT_RATE_1;
212                 scan.num_probes =
213                         (first->flags & IEEE80211_CHAN_NO_IR) ? 0 : 2;
214                 scan.num_ssids = priv->scan.n_ssids;
215                 scan.ssids = &priv->scan.ssids[0];
216                 scan.num_channels = it - priv->scan.curr;
217                 /* TODO: Is it optimal? */
218                 scan.probe_delay = 100;
219                 /* It is not stated in WSM specification, however
220                  * FW team says that driver may not use FG scan
221                  * when joined.
222                  */
223                 if (priv->join_status == CW1200_JOIN_STATUS_STA) {
224                         scan.type = WSM_SCAN_TYPE_BACKGROUND;
225                         scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
226                 }
227                 scan.ch = kzalloc(
228                         sizeof(struct wsm_scan_ch) * (it - priv->scan.curr),
229                         GFP_KERNEL);
230                 if (!scan.ch) {
231                         priv->scan.status = -ENOMEM;
232                         goto fail;
233                 }
234                 for (i = 0; i < scan.num_channels; ++i) {
235                         scan.ch[i].number = priv->scan.curr[i]->hw_value;
236                         if (priv->scan.curr[i]->flags & IEEE80211_CHAN_NO_IR) {
237                                 scan.ch[i].min_chan_time = 50;
238                                 scan.ch[i].max_chan_time = 100;
239                         } else {
240                                 scan.ch[i].min_chan_time = 10;
241                                 scan.ch[i].max_chan_time = 25;
242                         }
243                 }
244                 if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
245                     priv->scan.output_power != first->max_power) {
246                         priv->scan.output_power = first->max_power;
247                         wsm_set_output_power(priv,
248                                              priv->scan.output_power * 10);
249                 }
250                 priv->scan.status = cw1200_scan_start(priv, &scan);
251                 kfree(scan.ch);
252                 if (priv->scan.status)
253                         goto fail;
254                 priv->scan.curr = it;
255         }
256         mutex_unlock(&priv->conf_mutex);
257         return;
258
259 fail:
260         priv->scan.curr = priv->scan.end;
261         mutex_unlock(&priv->conf_mutex);
262         queue_work(priv->workqueue, &priv->scan.work);
263         return;
264 }
265
266 static void cw1200_scan_restart_delayed(struct cw1200_common *priv)
267 {
268         /* FW bug: driver has to restart p2p-dev mode after scan. */
269         if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
270                 cw1200_enable_listening(priv);
271                 cw1200_update_filtering(priv);
272         }
273
274         if (priv->delayed_unjoin) {
275                 priv->delayed_unjoin = false;
276                 if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
277                         wsm_unlock_tx(priv);
278         } else if (priv->delayed_link_loss) {
279                         wiphy_dbg(priv->hw->wiphy, "[CQM] Requeue BSS loss.\n");
280                         priv->delayed_link_loss = 0;
281                         cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
282         }
283 }
284
285 static void cw1200_scan_complete(struct cw1200_common *priv)
286 {
287         queue_delayed_work(priv->workqueue, &priv->clear_recent_scan_work, HZ);
288         if (priv->scan.direct_probe) {
289                 wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe complete.\n");
290                 cw1200_scan_restart_delayed(priv);
291                 priv->scan.direct_probe = 0;
292                 up(&priv->scan.lock);
293                 wsm_unlock_tx(priv);
294         } else {
295                 cw1200_scan_work(&priv->scan.work);
296         }
297 }
298
299 void cw1200_scan_failed_cb(struct cw1200_common *priv)
300 {
301         if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
302                 /* STA is stopped. */
303                 return;
304
305         if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
306                 priv->scan.status = -EIO;
307                 queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
308         }
309 }
310
311
312 void cw1200_scan_complete_cb(struct cw1200_common *priv,
313                                 struct wsm_scan_complete *arg)
314 {
315         if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
316                 /* STA is stopped. */
317                 return;
318
319         if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
320                 priv->scan.status = 1;
321                 queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
322         }
323 }
324
325 void cw1200_clear_recent_scan_work(struct work_struct *work)
326 {
327         struct cw1200_common *priv =
328                 container_of(work, struct cw1200_common,
329                              clear_recent_scan_work.work);
330         atomic_xchg(&priv->recent_scan, 0);
331 }
332
333 void cw1200_scan_timeout(struct work_struct *work)
334 {
335         struct cw1200_common *priv =
336                 container_of(work, struct cw1200_common, scan.timeout.work);
337         if (atomic_xchg(&priv->scan.in_progress, 0)) {
338                 if (priv->scan.status > 0) {
339                         priv->scan.status = 0;
340                 } else if (!priv->scan.status) {
341                         wiphy_warn(priv->hw->wiphy,
342                                    "Timeout waiting for scan complete notification.\n");
343                         priv->scan.status = -ETIMEDOUT;
344                         priv->scan.curr = priv->scan.end;
345                         wsm_stop_scan(priv);
346                 }
347                 cw1200_scan_complete(priv);
348         }
349 }
350
351 void cw1200_probe_work(struct work_struct *work)
352 {
353         struct cw1200_common *priv =
354                 container_of(work, struct cw1200_common, scan.probe_work.work);
355         u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
356         struct cw1200_queue *queue = &priv->tx_queue[queue_id];
357         const struct cw1200_txpriv *txpriv;
358         struct wsm_tx *wsm;
359         struct wsm_template_frame frame = {
360                 .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
361         };
362         struct wsm_ssid ssids[1] = {{
363                 .length = 0,
364         } };
365         struct wsm_scan_ch ch[1] = {{
366                 .min_chan_time = 0,
367                 .max_chan_time = 10,
368         } };
369         struct wsm_scan scan = {
370                 .type = WSM_SCAN_TYPE_FOREGROUND,
371                 .num_probes = 1,
372                 .probe_delay = 0,
373                 .num_channels = 1,
374                 .ssids = ssids,
375                 .ch = ch,
376         };
377         u8 *ies;
378         size_t ies_len;
379         int ret;
380
381         wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe work.\n");
382
383         mutex_lock(&priv->conf_mutex);
384         if (down_trylock(&priv->scan.lock)) {
385                 /* Scan is already in progress. Requeue self. */
386                 schedule();
387                 queue_delayed_work(priv->workqueue,
388                                    &priv->scan.probe_work, HZ / 10);
389                 mutex_unlock(&priv->conf_mutex);
390                 return;
391         }
392
393         /* Make sure we still have a pending probe req */
394         if (cw1200_queue_get_skb(queue, priv->pending_frame_id,
395                                  &frame.skb, &txpriv)) {
396                 up(&priv->scan.lock);
397                 mutex_unlock(&priv->conf_mutex);
398                 wsm_unlock_tx(priv);
399                 return;
400         }
401         wsm = (struct wsm_tx *)frame.skb->data;
402         scan.max_tx_rate = wsm->max_tx_rate;
403         scan.band = (priv->channel->band == IEEE80211_BAND_5GHZ) ?
404                 WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
405         if (priv->join_status == CW1200_JOIN_STATUS_STA ||
406             priv->join_status == CW1200_JOIN_STATUS_IBSS) {
407                 scan.type = WSM_SCAN_TYPE_BACKGROUND;
408                 scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
409         }
410         ch[0].number = priv->channel->hw_value;
411
412         skb_pull(frame.skb, txpriv->offset);
413
414         ies = &frame.skb->data[sizeof(struct ieee80211_hdr_3addr)];
415         ies_len = frame.skb->len - sizeof(struct ieee80211_hdr_3addr);
416
417         if (ies_len) {
418                 u8 *ssidie =
419                         (u8 *)cfg80211_find_ie(WLAN_EID_SSID, ies, ies_len);
420                 if (ssidie && ssidie[1] && ssidie[1] <= sizeof(ssids[0].ssid)) {
421                         u8 *nextie = &ssidie[2 + ssidie[1]];
422                         /* Remove SSID from the IE list. It has to be provided
423                          * as a separate argument in cw1200_scan_start call
424                          */
425
426                         /* Store SSID localy */
427                         ssids[0].length = ssidie[1];
428                         memcpy(ssids[0].ssid, &ssidie[2], ssids[0].length);
429                         scan.num_ssids = 1;
430
431                         /* Remove SSID from IE list */
432                         ssidie[1] = 0;
433                         memmove(&ssidie[2], nextie, &ies[ies_len] - nextie);
434                         skb_trim(frame.skb, frame.skb->len - ssids[0].length);
435                 }
436         }
437
438         /* FW bug: driver has to restart p2p-dev mode after scan */
439         if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
440                 cw1200_disable_listening(priv);
441         ret = wsm_set_template_frame(priv, &frame);
442         priv->scan.direct_probe = 1;
443         if (!ret) {
444                 wsm_flush_tx(priv);
445                 ret = cw1200_scan_start(priv, &scan);
446         }
447         mutex_unlock(&priv->conf_mutex);
448
449         skb_push(frame.skb, txpriv->offset);
450         if (!ret)
451                 IEEE80211_SKB_CB(frame.skb)->flags |= IEEE80211_TX_STAT_ACK;
452         BUG_ON(cw1200_queue_remove(queue, priv->pending_frame_id));
453
454         if (ret) {
455                 priv->scan.direct_probe = 0;
456                 up(&priv->scan.lock);
457                 wsm_unlock_tx(priv);
458         }
459
460         return;
461 }