Fixed wrong module name for usbatm
[openwrt.git] / target / linux / package / ieee80211-dscape / src / ieee80211_scan.c
1 /*
2 * Copyright 2002-2004, Instant802 Networks, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9 #include <linux/config.h>
10 #include <linux/version.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16
17 #include <net/ieee80211.h>
18 #include "ieee80211_i.h"
19 #include "rate_control.h"
20
21
22 /* Maximum number of seconds to wait for the traffic load to get below
23 * threshold before forcing a passive scan. */
24 #define MAX_SCAN_WAIT 60
25 /* Threshold (pkts/sec TX or RX) for delaying passive scan */
26 #define SCAN_TXRX_THRESHOLD 75
27
28 static void get_channel_params(struct ieee80211_local *local, int channel,
29 struct ieee80211_hw_modes **mode,
30 struct ieee80211_channel **chan)
31 {
32 int m;
33
34 for (m = 0; m < local->hw->num_modes; m++) {
35 *mode = &local->hw->modes[m];
36 if ((*mode)->mode == local->conf.phymode)
37 break;
38 }
39 local->scan.mode_idx = m;
40 local->scan.chan_idx = 0;
41 do {
42 *chan = &(*mode)->channels[local->scan.chan_idx];
43 if ((*chan)->chan == channel) {
44 return;
45 }
46 local->scan.chan_idx++;
47 } while (local->scan.chan_idx < (*mode)->num_channels);
48 *chan = NULL;
49 }
50
51
52 static void next_chan_same_mode(struct ieee80211_local *local,
53 struct ieee80211_hw_modes **mode,
54 struct ieee80211_channel **chan)
55 {
56 int m, prev;
57
58 for (m = 0; m < local->hw->num_modes; m++) {
59 *mode = &local->hw->modes[m];
60 if ((*mode)->mode == local->conf.phymode)
61 break;
62 }
63 local->scan.mode_idx = m;
64
65 /* Select next channel - scan only channels marked with W_SCAN flag */
66 prev = local->scan.chan_idx;
67 do {
68 local->scan.chan_idx++;
69 if (local->scan.chan_idx >= (*mode)->num_channels)
70 local->scan.chan_idx = 0;
71 *chan = &(*mode)->channels[local->scan.chan_idx];
72 if ((*chan)->flag & IEEE80211_CHAN_W_SCAN)
73 break;
74 } while (local->scan.chan_idx != prev);
75 }
76
77
78 static void next_chan_all_modes(struct ieee80211_local *local,
79 struct ieee80211_hw_modes **mode,
80 struct ieee80211_channel **chan)
81 {
82 int prev, prev_m;
83
84 if (local->scan.mode_idx >= local->hw->num_modes) {
85 local->scan.mode_idx = 0;
86 local->scan.chan_idx = 0;
87 }
88
89 /* Select next channel - scan only channels marked with W_SCAN flag */
90 prev = local->scan.chan_idx;
91 prev_m = local->scan.mode_idx;
92 do {
93 *mode = &local->hw->modes[local->scan.mode_idx];
94 local->scan.chan_idx++;
95 if (local->scan.chan_idx >= (*mode)->num_channels) {
96 local->scan.chan_idx = 0;
97 local->scan.mode_idx++;
98 if (local->scan.mode_idx >= local->hw->num_modes)
99 local->scan.mode_idx = 0;
100 *mode = &local->hw->modes[local->scan.mode_idx];
101 }
102 *chan = &(*mode)->channels[local->scan.chan_idx];
103 if ((*chan)->flag & IEEE80211_CHAN_W_SCAN)
104 break;
105 } while (local->scan.chan_idx != prev ||
106 local->scan.mode_idx != prev_m);
107 }
108
109
110 static void ieee80211_scan_start(struct net_device *dev,
111 struct ieee80211_scan_conf *conf)
112 {
113 struct ieee80211_local *local = dev->priv;
114 int old_mode_idx = local->scan.mode_idx;
115 int old_chan_idx = local->scan.chan_idx;
116 struct ieee80211_hw_modes *mode = NULL;
117 struct ieee80211_channel *chan = NULL;
118 int ret;
119
120 if (local->hw->passive_scan == 0) {
121 printk(KERN_DEBUG "%s: Scan handler called, yet the hardware "
122 "does not support passive scanning. Disabled.\n",
123 dev->name);
124 return;
125 }
126
127 if ((local->scan.tries < MAX_SCAN_WAIT &&
128 local->scan.txrx_count > SCAN_TXRX_THRESHOLD)) {
129 local->scan.tries++;
130 /* Count TX/RX packets during one second interval and allow
131 * scan to start only if the number of packets is below the
132 * threshold. */
133 local->scan.txrx_count = 0;
134 local->scan.timer.expires = jiffies + HZ;
135 add_timer(&local->scan.timer);
136 return;
137 }
138
139 if (local->scan.skb == NULL) {
140 printk(KERN_DEBUG "%s: Scan start called even though scan.skb "
141 "is not set\n", dev->name);
142 }
143
144 if (local->scan.our_mode_only) {
145 if (local->scan.channel > 0) {
146 get_channel_params(local, local->scan.channel, &mode,
147 &chan);
148 } else
149 next_chan_same_mode(local, &mode, &chan);
150 }
151 else
152 next_chan_all_modes(local, &mode, &chan);
153
154 conf->scan_channel = chan->chan;
155 conf->scan_freq = chan->freq;
156 conf->scan_channel_val = chan->val;
157 conf->scan_phymode = mode->mode;
158 conf->scan_power_level = chan->power_level;
159 conf->scan_antenna_max = chan->antenna_max;
160 conf->scan_time = 2 * local->hw->channel_change_time +
161 local->scan.time; /* 10ms scan time+hardware changes */
162 conf->skb = local->scan.skb ?
163 skb_clone(local->scan.skb, GFP_ATOMIC) : NULL;
164 conf->tx_control = &local->scan.tx_control;
165 #if 0
166 printk(KERN_DEBUG "%s: Doing scan on mode: %d freq: %d chan: %d "
167 "for %d ms\n",
168 dev->name, conf->scan_phymode, conf->scan_freq,
169 conf->scan_channel, conf->scan_time);
170 #endif
171 local->scan.rx_packets = 0;
172 local->scan.rx_beacon = 0;
173 local->scan.freq = chan->freq;
174 local->scan.in_scan = 1;
175
176 ieee80211_netif_oper(dev, NETIF_STOP);
177
178 ret = local->hw->passive_scan(dev, IEEE80211_SCAN_START, conf);
179
180 if (ret == 0) {
181 long usec = local->hw->channel_change_time +
182 local->scan.time;
183 usec += 1000000L / HZ - 1;
184 usec /= 1000000L / HZ;
185 local->scan.timer.expires = jiffies + usec;
186 } else {
187 local->scan.in_scan = 0;
188 if (conf->skb)
189 dev_kfree_skb(conf->skb);
190 ieee80211_netif_oper(dev, NETIF_WAKE);
191 if (ret == -EAGAIN) {
192 local->scan.timer.expires = jiffies +
193 (local->scan.interval * HZ / 100);
194 local->scan.mode_idx = old_mode_idx;
195 local->scan.chan_idx = old_chan_idx;
196 } else {
197 printk(KERN_DEBUG "%s: Got unknown error from "
198 "passive_scan %d\n", dev->name, ret);
199 local->scan.timer.expires = jiffies +
200 (local->scan.interval * HZ);
201 }
202 local->scan.in_scan = 0;
203 }
204
205 add_timer(&local->scan.timer);
206 }
207
208
209 static void ieee80211_scan_stop(struct net_device *dev,
210 struct ieee80211_scan_conf *conf)
211 {
212 struct ieee80211_local *local = dev->priv;
213 struct ieee80211_hw_modes *mode;
214 struct ieee80211_channel *chan;
215 int wait;
216
217 if (local->hw->passive_scan == NULL)
218 return;
219
220 if (local->scan.mode_idx >= local->hw->num_modes) {
221 local->scan.mode_idx = 0;
222 local->scan.chan_idx = 0;
223 }
224
225 mode = &local->hw->modes[local->scan.mode_idx];
226
227 if (local->scan.chan_idx >= mode->num_channels) {
228 local->scan.chan_idx = 0;
229 }
230
231 chan = &mode->channels[local->scan.chan_idx];
232
233 local->hw->passive_scan(dev, IEEE80211_SCAN_END, conf);
234
235 #ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
236 printk(KERN_DEBUG "%s: Did scan on mode: %d freq: %d chan: %d "
237 "GOT: %d Beacon: %d (%d)\n",
238 dev->name,
239 mode->mode, chan->freq, chan->chan,
240 local->scan.rx_packets, local->scan.rx_beacon,
241 local->scan.tries);
242 #endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
243 local->scan.num_scans++;
244
245 local->scan.in_scan = 0;
246 ieee80211_netif_oper(dev, NETIF_WAKE);
247
248 local->scan.tries = 0;
249 /* Use random interval of scan.interval .. 2 * scan.interval */
250 wait = (local->scan.interval * HZ * ((net_random() & 127) + 128)) /
251 128;
252 local->scan.timer.expires = jiffies + wait;
253
254 add_timer(&local->scan.timer);
255 }
256
257
258 static void ieee80211_scan_handler(unsigned long uldev)
259 {
260 struct net_device *dev = (struct net_device *) uldev;
261 struct ieee80211_local *local = dev->priv;
262 struct ieee80211_scan_conf conf;
263
264 if (local->scan.interval == 0 && !local->scan.in_scan) {
265 /* Passive scanning is disabled - keep the timer always
266 * running to make code cleaner. */
267 local->scan.timer.expires = jiffies + 10 * HZ;
268 add_timer(&local->scan.timer);
269 return;
270 }
271
272 memset(&conf, 0, sizeof(struct ieee80211_scan_conf));
273 conf.running_freq = local->conf.freq;
274 conf.running_channel = local->conf.channel;
275 conf.running_phymode = local->conf.phymode;
276 conf.running_channel_val = local->conf.channel_val;
277 conf.running_power_level = local->conf.power_level;
278 conf.running_antenna_max = local->conf.antenna_max;
279
280 if (local->scan.in_scan == 0)
281 ieee80211_scan_start(dev, &conf);
282 else
283 ieee80211_scan_stop(dev, &conf);
284 }
285
286
287 void ieee80211_init_scan(struct net_device *dev)
288 {
289 struct ieee80211_local *local = dev->priv;
290 struct ieee80211_hdr hdr;
291 u16 fc;
292 int len = 10;
293 struct rate_control_extra extra;
294
295 /* Only initialize passive scanning if the hardware supports it */
296 if (!local->hw->passive_scan) {
297 local->scan.skb = NULL;
298 memset(&local->scan.tx_control, 0,
299 sizeof(local->scan.tx_control));
300 printk(KERN_DEBUG "%s: Does not support passive scan, "
301 "disabled\n", dev->name);
302 return;
303 }
304
305 local->scan.interval = 0;
306 local->scan.our_mode_only = 1;
307 local->scan.time = 10000;
308 local->scan.timer.function = ieee80211_scan_handler;
309 local->scan.timer.data = (unsigned long) dev;
310 local->scan.timer.expires = jiffies + local->scan.interval * HZ;
311 add_timer(&local->scan.timer);
312
313 /* Create a CTS from for broadcasting before
314 * the low level changes channels */
315 local->scan.skb = alloc_skb(len, GFP_KERNEL);
316 if (local->scan.skb == NULL) {
317 printk(KERN_WARNING "%s: Failed to allocate CTS packet for "
318 "passive scan\n", dev->name);
319 return;
320 }
321
322 fc = (WLAN_FC_TYPE_CTRL << 2) | (WLAN_FC_STYPE_CTS << 4);
323 hdr.frame_control = cpu_to_le16(fc);
324 hdr.duration_id =
325 cpu_to_le16(2 * local->hw->channel_change_time +
326 local->scan.time);
327 memcpy(hdr.addr1, dev->dev_addr, ETH_ALEN); /* DA */
328 hdr.seq_ctrl = 0;
329
330 memcpy(skb_put(local->scan.skb, len), &hdr, len);
331
332 memset(&local->scan.tx_control, 0, sizeof(local->scan.tx_control));
333 local->scan.tx_control.key_idx = HW_KEY_IDX_INVALID;
334 local->scan.tx_control.do_not_encrypt = 1;
335 memset(&extra, 0, sizeof(extra));
336 extra.endidx = local->num_curr_rates;
337 local->scan.tx_control.tx_rate =
338 rate_control_get_rate(dev, local->scan.skb, &extra)->val;
339 local->scan.tx_control.no_ack = 1;
340 }
341
342
343 void ieee80211_stop_scan(struct net_device *dev)
344 {
345 struct ieee80211_local *local = dev->priv;
346
347 if (local->hw->passive_scan != 0) {
348 del_timer_sync(&local->scan.timer);
349 dev_kfree_skb(local->scan.skb);
350 local->scan.skb = NULL;
351 }
352 }
This page took 0.058313 seconds and 5 git commands to generate.