Merge branch 'for_paulus' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc
[pandora-kernel.git] / net / ieee80211 / softmac / ieee80211softmac_scan.c
1 /*
2  * Scanning routines.
3  *
4  * These are not exported because they're assigned to the function pointers.
5  *
6  * Copyright (c) 2005, 2006 Johannes Berg <johannes@sipsolutions.net>
7  *                          Joseph Jezak <josejx@gentoo.org>
8  *                          Larry Finger <Larry.Finger@lwfinger.net>
9  *                          Danny van Dyk <kugelfang@gentoo.org>
10  *                          Michael Buesch <mbuesch@freenet.de>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
24  *
25  * The full GNU General Public License is included in this distribution in the
26  * file called COPYING.
27  */
28
29 #include <linux/completion.h>
30 #include "ieee80211softmac_priv.h"
31
32 /* internal, use to trigger scanning if needed.
33  * Returns -EBUSY if already scanning,
34  * result of start_scan otherwise */
35 int
36 ieee80211softmac_start_scan(struct ieee80211softmac_device *sm)
37 {
38         unsigned long flags;
39         int ret;
40
41         spin_lock_irqsave(&sm->lock, flags);
42         if (sm->scanning)
43         {
44                 spin_unlock_irqrestore(&sm->lock, flags);
45                 return -EINPROGRESS;
46         }
47         sm->scanning = 1;
48         spin_unlock_irqrestore(&sm->lock, flags);
49
50         netif_tx_disable(sm->ieee->dev);
51         ret = sm->start_scan(sm->dev);
52         if (ret) {
53                 spin_lock_irqsave(&sm->lock, flags);
54                 sm->scanning = 0;
55                 spin_unlock_irqrestore(&sm->lock, flags);
56         }
57         return ret;
58 }
59
60 void
61 ieee80211softmac_stop_scan(struct ieee80211softmac_device *sm)
62 {
63         unsigned long flags;
64
65         spin_lock_irqsave(&sm->lock, flags);
66         
67         if (!sm->scanning) {
68                 spin_unlock_irqrestore(&sm->lock, flags);
69                 return;
70         }
71         
72         spin_unlock_irqrestore(&sm->lock, flags);
73         sm->stop_scan(sm->dev);
74 }
75
76 void
77 ieee80211softmac_wait_for_scan(struct ieee80211softmac_device *sm)
78 {
79         unsigned long flags;
80
81         spin_lock_irqsave(&sm->lock, flags);
82         
83         if (!sm->scanning) {
84                 spin_unlock_irqrestore(&sm->lock, flags);
85                 return;
86         }
87         
88         spin_unlock_irqrestore(&sm->lock, flags);
89         sm->wait_for_scan(sm->dev);
90 }
91
92
93 /* internal scanning implementation follows */
94 void ieee80211softmac_scan(void *d)
95 {
96         int invalid_channel;
97         u8 current_channel_idx;
98         struct ieee80211softmac_device *sm = (struct ieee80211softmac_device *)d;
99         struct ieee80211softmac_scaninfo *si = sm->scaninfo;
100         unsigned long flags;
101
102         while (!(si->stop) && (si->current_channel_idx < si->number_channels)) {
103                 current_channel_idx = si->current_channel_idx;
104                 si->current_channel_idx++; /* go to the next channel */
105
106                 invalid_channel = (si->skip_flags & si->channels[current_channel_idx].flags);
107
108                 if (!invalid_channel) {
109                         sm->set_channel(sm->dev, si->channels[current_channel_idx].channel);
110                         // FIXME make this user configurable (active/passive)
111                         if(ieee80211softmac_send_mgt_frame(sm, NULL, IEEE80211_STYPE_PROBE_REQ, 0))
112                                 printkl(KERN_DEBUG PFX "Sending Probe Request Failed\n");
113
114                         /* also send directed management frame for the network we're looking for */
115                         // TODO: is this if correct, or should we do this only if scanning from assoc request?
116                         if (sm->associnfo.req_essid.len)
117                                 ieee80211softmac_send_mgt_frame(sm, &sm->associnfo.req_essid, IEEE80211_STYPE_PROBE_REQ, 0);
118                         schedule_delayed_work(&si->softmac_scan, IEEE80211SOFTMAC_PROBE_DELAY);
119                         return;
120                 } else {
121                         dprintk(PFX "Not probing Channel %d (not allowed here)\n", si->channels[current_channel_idx].channel);
122                 }
123         }
124
125         spin_lock_irqsave(&sm->lock, flags);
126         cancel_delayed_work(&si->softmac_scan);
127         si->started = 0;
128         spin_unlock_irqrestore(&sm->lock, flags);
129
130         dprintk(PFX "Scanning finished\n");
131         ieee80211softmac_scan_finished(sm);
132         complete_all(&sm->scaninfo->finished);
133 }
134
135 static inline struct ieee80211softmac_scaninfo *allocate_scaninfo(struct ieee80211softmac_device *mac)
136 {
137         /* ugh. can we call this without having the spinlock held? */
138         struct ieee80211softmac_scaninfo *info = kmalloc(sizeof(struct ieee80211softmac_scaninfo), GFP_ATOMIC);
139         if (unlikely(!info))
140                 return NULL;
141         INIT_WORK(&info->softmac_scan, ieee80211softmac_scan, mac);
142         init_completion(&info->finished);
143         return info;
144 }
145
146 int ieee80211softmac_start_scan_implementation(struct net_device *dev)
147 {
148         struct ieee80211softmac_device *sm = ieee80211_priv(dev);
149         unsigned long flags;
150         
151         if (!(dev->flags & IFF_UP))
152                 return -ENODEV;
153
154         assert(ieee80211softmac_scan_handlers_check_self(sm));
155         if (!ieee80211softmac_scan_handlers_check_self(sm))
156                 return -EINVAL;
157                 
158         spin_lock_irqsave(&sm->lock, flags);
159         /* it looks like we need to hold the lock here
160          * to make sure we don't allocate two of these... */
161         if (unlikely(!sm->scaninfo))
162                 sm->scaninfo = allocate_scaninfo(sm);
163         if (unlikely(!sm->scaninfo)) {
164                 spin_unlock_irqrestore(&sm->lock, flags);
165                 return -ENOMEM;
166         }
167
168         sm->scaninfo->skip_flags = IEEE80211_CH_INVALID;
169         if (0 /* not scanning in IEEE802.11b */)//TODO
170                 sm->scaninfo->skip_flags |= IEEE80211_CH_B_ONLY;
171         if (0 /* IEEE802.11a */) {//TODO
172                 sm->scaninfo->channels = sm->ieee->geo.a;
173                 sm->scaninfo->number_channels = sm->ieee->geo.a_channels;
174         } else {
175                 sm->scaninfo->channels = sm->ieee->geo.bg;
176                 sm->scaninfo->number_channels = sm->ieee->geo.bg_channels;
177         }
178         dprintk(PFX "Start scanning with channel: %d\n", sm->scaninfo->channels[0].channel);
179         dprintk(PFX "Scanning %d channels\n", sm->scaninfo->number_channels);
180         sm->scaninfo->current_channel_idx = 0;
181         sm->scaninfo->started = 1;
182         sm->scaninfo->stop = 0;
183         INIT_COMPLETION(sm->scaninfo->finished);
184         schedule_work(&sm->scaninfo->softmac_scan);
185         spin_unlock_irqrestore(&sm->lock, flags);
186         return 0;
187 }
188
189 void ieee80211softmac_stop_scan_implementation(struct net_device *dev)
190 {
191         struct ieee80211softmac_device *sm = ieee80211_priv(dev);
192         unsigned long flags;
193
194         assert(ieee80211softmac_scan_handlers_check_self(sm));
195         if (!ieee80211softmac_scan_handlers_check_self(sm))
196                 return;
197
198         spin_lock_irqsave(&sm->lock, flags);
199         assert(sm->scaninfo != NULL);
200         if (sm->scaninfo) {
201                 if (sm->scaninfo->started)
202                         sm->scaninfo->stop = 1;
203                 else
204                         complete_all(&sm->scaninfo->finished);
205         }
206         spin_unlock_irqrestore(&sm->lock, flags);
207 }
208
209 void ieee80211softmac_wait_for_scan_implementation(struct net_device *dev)
210 {
211         struct ieee80211softmac_device *sm = ieee80211_priv(dev);
212         unsigned long flags;
213
214         assert(ieee80211softmac_scan_handlers_check_self(sm));
215         if (!ieee80211softmac_scan_handlers_check_self(sm))
216                 return;
217
218         spin_lock_irqsave(&sm->lock, flags);
219         if (!sm->scaninfo->started) {
220                 spin_unlock_irqrestore(&sm->lock, flags);
221                 return;
222         }
223         spin_unlock_irqrestore(&sm->lock, flags);
224         wait_for_completion(&sm->scaninfo->finished);
225 }
226
227 /* this is what drivers (that do scanning) call when they're done */
228 void ieee80211softmac_scan_finished(struct ieee80211softmac_device *sm)
229 {
230         unsigned long flags;
231
232         spin_lock_irqsave(&sm->lock, flags);
233         sm->scanning = 0;
234         spin_unlock_irqrestore(&sm->lock, flags);
235         
236         if (sm->associnfo.bssvalid) {
237                 struct ieee80211softmac_network *net;
238
239                 net = ieee80211softmac_get_network_by_bssid(sm, sm->associnfo.bssid);
240                 if (net)
241                         sm->set_channel(sm->dev, net->channel);
242         }
243         netif_wake_queue(sm->ieee->dev);
244         ieee80211softmac_call_events(sm, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, NULL);
245 }
246 EXPORT_SYMBOL_GPL(ieee80211softmac_scan_finished);