Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Scanning routines. |
| 3 | * |
| 4 | * These are not exported because they're assigned to the function pointers. |
Johannes Berg | 4855d25 | 2006-01-12 21:12:59 +0100 | [diff] [blame^] | 5 | * |
| 6 | * Copyright (c) 2005 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. |
Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 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 | ret = sm->start_scan(sm->dev); |
| 51 | if (ret) { |
| 52 | spin_lock_irqsave(&sm->lock, flags); |
| 53 | sm->scanning = 0; |
| 54 | spin_unlock_irqrestore(&sm->lock, flags); |
| 55 | } |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | ieee80211softmac_stop_scan(struct ieee80211softmac_device *sm) |
| 61 | { |
| 62 | unsigned long flags; |
| 63 | |
| 64 | spin_lock_irqsave(&sm->lock, flags); |
| 65 | |
| 66 | if (!sm->scanning) { |
| 67 | spin_unlock_irqrestore(&sm->lock, flags); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | spin_unlock_irqrestore(&sm->lock, flags); |
| 72 | sm->stop_scan(sm->dev); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | ieee80211softmac_wait_for_scan(struct ieee80211softmac_device *sm) |
| 77 | { |
| 78 | unsigned long flags; |
| 79 | |
| 80 | spin_lock_irqsave(&sm->lock, flags); |
| 81 | |
| 82 | if (!sm->scanning) { |
| 83 | spin_unlock_irqrestore(&sm->lock, flags); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | spin_unlock_irqrestore(&sm->lock, flags); |
| 88 | sm->wait_for_scan(sm->dev); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /* internal scanning implementation follows */ |
| 93 | void ieee80211softmac_scan(void *d) |
| 94 | { |
| 95 | int invalid_channel; |
| 96 | u8 current_channel_idx; |
| 97 | struct ieee80211softmac_device *sm = (struct ieee80211softmac_device *)d; |
| 98 | struct ieee80211softmac_scaninfo *si = sm->scaninfo; |
| 99 | unsigned long flags; |
| 100 | |
| 101 | while (!(si->stop) && (si->current_channel_idx < si->number_channels)) { |
| 102 | current_channel_idx = si->current_channel_idx; |
| 103 | si->current_channel_idx++; /* go to the next channel */ |
| 104 | |
| 105 | invalid_channel = (si->skip_flags & si->channels[current_channel_idx].flags); |
| 106 | |
| 107 | if (!invalid_channel) { |
| 108 | sm->set_channel(sm->dev, si->channels[current_channel_idx].channel); |
Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 109 | // FIXME make this user configurable (active/passive) |
| 110 | if(ieee80211softmac_send_mgt_frame(sm, NULL, IEEE80211_STYPE_PROBE_REQ, 0)) |
| 111 | printkl(KERN_DEBUG PFX "Sending Probe Request Failed\n"); |
| 112 | |
| 113 | /* also send directed management frame for the network we're looking for */ |
| 114 | // TODO: is this if correct, or should we do this only if scanning from assoc request? |
| 115 | if (sm->associnfo.req_essid.len) |
| 116 | ieee80211softmac_send_mgt_frame(sm, &sm->associnfo.req_essid, IEEE80211_STYPE_PROBE_REQ, 0); |
Johannes Berg | 5c4df6d | 2006-01-06 01:43:45 +0100 | [diff] [blame] | 117 | schedule_delayed_work(&si->softmac_scan, IEEE80211SOFTMAC_PROBE_DELAY); |
Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 118 | return; |
| 119 | } else { |
| 120 | dprintk(PFX "Not probing Channel %d (not allowed here)\n", si->channels[current_channel_idx].channel); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | spin_lock_irqsave(&sm->lock, flags); |
| 125 | cancel_delayed_work(&si->softmac_scan); |
| 126 | si->started = 0; |
| 127 | spin_unlock_irqrestore(&sm->lock, flags); |
| 128 | |
| 129 | dprintk(PFX "Scanning finished\n"); |
| 130 | ieee80211softmac_scan_finished(sm); |
| 131 | complete_all(&sm->scaninfo->finished); |
| 132 | } |
| 133 | |
| 134 | static inline struct ieee80211softmac_scaninfo *allocate_scaninfo(struct ieee80211softmac_device *mac) |
| 135 | { |
| 136 | /* ugh. can we call this without having the spinlock held? */ |
| 137 | struct ieee80211softmac_scaninfo *info = kmalloc(sizeof(struct ieee80211softmac_scaninfo), GFP_ATOMIC); |
| 138 | if (unlikely(!info)) |
| 139 | return NULL; |
| 140 | INIT_WORK(&info->softmac_scan, ieee80211softmac_scan, mac); |
| 141 | init_completion(&info->finished); |
| 142 | return info; |
| 143 | } |
| 144 | |
| 145 | int ieee80211softmac_start_scan_implementation(struct net_device *dev) |
| 146 | { |
| 147 | struct ieee80211softmac_device *sm = ieee80211_priv(dev); |
| 148 | unsigned long flags; |
| 149 | |
| 150 | if (!(dev->flags & IFF_UP)) |
| 151 | return -ENODEV; |
| 152 | |
| 153 | assert(ieee80211softmac_scan_handlers_check_self(sm)); |
| 154 | if (!ieee80211softmac_scan_handlers_check_self(sm)) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | spin_lock_irqsave(&sm->lock, flags); |
| 158 | /* it looks like we need to hold the lock here |
| 159 | * to make sure we don't allocate two of these... */ |
| 160 | if (unlikely(!sm->scaninfo)) |
| 161 | sm->scaninfo = allocate_scaninfo(sm); |
| 162 | if (unlikely(!sm->scaninfo)) { |
| 163 | spin_unlock_irqrestore(&sm->lock, flags); |
| 164 | return -ENOMEM; |
| 165 | } |
| 166 | |
| 167 | sm->scaninfo->skip_flags = IEEE80211_CH_INVALID; |
| 168 | if (0 /* not scanning in IEEE802.11b */)//TODO |
| 169 | sm->scaninfo->skip_flags |= IEEE80211_CH_B_ONLY; |
| 170 | if (0 /* IEEE802.11a */) {//TODO |
| 171 | sm->scaninfo->channels = sm->ieee->geo.a; |
| 172 | sm->scaninfo->number_channels = sm->ieee->geo.a_channels; |
| 173 | } else { |
| 174 | sm->scaninfo->channels = sm->ieee->geo.bg; |
| 175 | sm->scaninfo->number_channels = sm->ieee->geo.bg_channels; |
| 176 | } |
| 177 | dprintk(PFX "Start scanning with channel: %d\n", sm->scaninfo->channels[0].channel); |
| 178 | dprintk(PFX "Scanning %d channels\n", sm->scaninfo->number_channels); |
| 179 | sm->scaninfo->current_channel_idx = 0; |
| 180 | sm->scaninfo->started = 1; |
| 181 | INIT_COMPLETION(sm->scaninfo->finished); |
Johannes Berg | 5c4df6d | 2006-01-06 01:43:45 +0100 | [diff] [blame] | 182 | schedule_work(&sm->scaninfo->softmac_scan); |
Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 183 | spin_unlock_irqrestore(&sm->lock, flags); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | void ieee80211softmac_stop_scan_implementation(struct net_device *dev) |
| 188 | { |
| 189 | struct ieee80211softmac_device *sm = ieee80211_priv(dev); |
| 190 | unsigned long flags; |
| 191 | |
| 192 | assert(ieee80211softmac_scan_handlers_check_self(sm)); |
| 193 | if (!ieee80211softmac_scan_handlers_check_self(sm)) |
| 194 | return; |
| 195 | |
| 196 | spin_lock_irqsave(&sm->lock, flags); |
| 197 | assert(sm->scaninfo != NULL); |
| 198 | if (sm->scaninfo) { |
| 199 | if (sm->scaninfo->started) |
| 200 | sm->scaninfo->stop = 1; |
| 201 | else |
| 202 | complete_all(&sm->scaninfo->finished); |
| 203 | } |
| 204 | spin_unlock_irqrestore(&sm->lock, flags); |
| 205 | } |
| 206 | |
| 207 | void ieee80211softmac_wait_for_scan_implementation(struct net_device *dev) |
| 208 | { |
| 209 | struct ieee80211softmac_device *sm = ieee80211_priv(dev); |
| 210 | unsigned long flags; |
| 211 | |
| 212 | assert(ieee80211softmac_scan_handlers_check_self(sm)); |
| 213 | if (!ieee80211softmac_scan_handlers_check_self(sm)) |
| 214 | return; |
| 215 | |
| 216 | spin_lock_irqsave(&sm->lock, flags); |
| 217 | if (!sm->scaninfo->started) { |
| 218 | spin_unlock_irqrestore(&sm->lock, flags); |
| 219 | return; |
| 220 | } |
| 221 | spin_unlock_irqrestore(&sm->lock, flags); |
| 222 | wait_for_completion(&sm->scaninfo->finished); |
| 223 | } |
| 224 | |
| 225 | /* this is what drivers (that do scanning) call when they're done */ |
| 226 | void ieee80211softmac_scan_finished(struct ieee80211softmac_device *sm) |
| 227 | { |
| 228 | unsigned long flags; |
| 229 | |
| 230 | spin_lock_irqsave(&sm->lock, flags); |
| 231 | sm->scanning = 0; |
| 232 | spin_unlock_irqrestore(&sm->lock, flags); |
| 233 | |
| 234 | ieee80211softmac_call_events(sm, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, NULL); |
| 235 | } |
| 236 | |
| 237 | EXPORT_SYMBOL_GPL(ieee80211softmac_scan_finished); |