aboutsummaryrefslogtreecommitdiffstats
path: root/package/madwifi/patches/411-autochannel_multi.patch
blob: 827ab5800a320742660ba639a0c5d9185c56f185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
--- a/net80211/ieee80211_scan.c
+++ b/net80211/ieee80211_scan.c
@@ -97,6 +97,123 @@ struct scan_state {
 static void scan_restart_pwrsav(unsigned long);
 static void scan_next(unsigned long);
 
+spinlock_t channel_lock = SPIN_LOCK_UNLOCKED;
+static LIST_HEAD(channels_inuse);
+
+struct channel_inuse {
+	struct list_head list;
+	struct ieee80211com *ic;
+	u16 freq;
+	u8 bw;
+};
+
+static inline u32
+get_signal(u8 bw, u8 distance)
+{
+	u32 v;
+
+	/* signal = 1 - (distance / bw)^2 [scale: 100] */
+	v = 100 * distance / bw;
+	v = (100 - ((v * v) / 100));
+	return v;
+}
+
+static u32
+get_overlap(u16 f1, u16 f2, u8 b1, u8 b2)
+{
+	u32 v;
+	u16 d, c;
+
+	/* add offsets for sidechannel interference */
+	b1 += (b1 / 5);
+	b2 += (b2 / 5);
+
+	/* use only one direction */
+	b1 /= 2;
+	b2 /= 2;
+
+	if (f1 + b1 < f2 - b2)
+		return 0;
+
+	d = f2 - f1;
+	c = d * b1 / (b1 + b2);
+	v = get_signal(b1, c);
+
+	return v * v / 100;
+}
+
+static u8
+get_channel_bw(struct ieee80211_channel *c)
+{
+	switch(c->ic_flags & (
+		IEEE80211_CHAN_HALF |
+		IEEE80211_CHAN_QUARTER |
+		IEEE80211_CHAN_TURBO |
+		IEEE80211_CHAN_STURBO)) {
+	case IEEE80211_CHAN_QUARTER:
+		return 5;
+	case IEEE80211_CHAN_HALF:
+		return 10;
+	case IEEE80211_CHAN_TURBO:
+	case IEEE80211_CHAN_STURBO:
+		return 40;
+	default:
+		return 20;
+	}
+}
+
+/* must be called with channel_lock held */
+u32
+ieee80211_scan_get_bias(struct ieee80211_channel *c)
+{
+	struct channel_inuse *ch;
+	u8 bw = get_channel_bw(c);
+	u32 bias = 0;
+
+	list_for_each_entry(ch, &channels_inuse, list) {
+		if (ch->freq == c->ic_freq) {
+			bias += 50;
+			continue;
+		}
+		if (c->ic_freq < ch->freq)
+			bias += get_overlap(c->ic_freq, ch->freq, bw, ch->bw);
+		else
+			bias += get_overlap(ch->freq, c->ic_freq, ch->bw, bw);
+	}
+	return min(bias, (u32) 100);
+}
+EXPORT_SYMBOL(ieee80211_scan_get_bias);
+
+/* must be called with channel_lock held */
+void
+ieee80211_scan_set_bss_channel(struct ieee80211com *ic, struct ieee80211_channel *c)
+{
+	unsigned long flags;
+	struct channel_inuse *ch;
+
+	list_for_each_entry(ch, &channels_inuse, list) {
+		if (ch->ic == ic)
+			goto found;
+	}
+	ch = NULL;
+found:
+	if (c && (c != IEEE80211_CHAN_ANYC)) {
+		if (!ch) {
+			ch = kmalloc(sizeof(struct channel_inuse), GFP_ATOMIC);
+			ch->ic = ic;
+			INIT_LIST_HEAD(&ch->list);
+			list_add(&ch->list, &channels_inuse);
+		}
+		ch->freq = c->ic_freq;
+		ch->bw = get_channel_bw(c);
+	} else if (ch) {
+		list_del(&ch->list);
+		kfree(ch);
+	}
+}
+EXPORT_SYMBOL(ieee80211_scan_set_bss_channel);
+
+
 void
 ieee80211_scan_attach(struct ieee80211com *ic)
 {
@@ -1168,7 +1285,7 @@ ieee80211_scan_dfs_action(struct ieee802
 				IEEE80211_RADAR_CHANCHANGE_TBTT_COUNT;
 			ic->ic_flags |= IEEE80211_F_CHANSWITCH;
 		} else {
-
+			unsigned long flags;
 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOTH,
 					"%s: directly switching to channel "
 					"%3d (%4d MHz)\n", __func__,
@@ -1179,6 +1296,9 @@ ieee80211_scan_dfs_action(struct ieee802
 			 * change the channel here. */
 			change_channel(ic, new_channel);
 			ic->ic_bsschan = new_channel;
+			spin_lock_irqsave(&channel_lock, flags);
+			ieee80211_scan_set_bss_channel(ic, ic->ic_bsschan);
+			spin_unlock_irqrestore(&channel_lock, flags);
 			if (vap->iv_bss)
 				vap->iv_bss->ni_chan = new_channel;
 		}
--- a/net80211/ieee80211_scan.h
+++ b/net80211/ieee80211_scan.h
@@ -35,6 +35,7 @@
 
 #define	IEEE80211_SCAN_MAX	IEEE80211_CHAN_MAX
 
+extern spinlock_t channel_lock;
 struct ieee80211_scanner;
 struct ieee80211_scan_entry;
 
@@ -116,6 +117,8 @@ void ieee80211_scan_flush(struct ieee802
 struct ieee80211_scan_entry;
 typedef int ieee80211_scan_iter_func(void *, const struct ieee80211_scan_entry *);
 int ieee80211_scan_iterate(struct ieee80211com *, ieee80211_scan_iter_func *, void *);
+u32 ieee80211_scan_get_bias(struct ieee80211_channel *c);
+void ieee80211_scan_set_bss_channel(struct ieee80211com *ic, struct ieee80211_channel *c);
 
 /*
  * Parameters supplied when adding/updating an entry in a
--- a/net80211/ieee80211.c
+++ b/net80211/ieee80211.c
@@ -373,8 +373,16 @@ void
 ieee80211_ifdetach(struct ieee80211com *ic)
 {
 	struct ieee80211vap *vap;
+	unsigned long flags;
 	int count;
 
+	/* mark the channel as no longer in use */
+	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
+	spin_lock_irqsave(&channel_lock, flags);
+	ieee80211_scan_set_bss_channel(ic, ic->ic_bsschan);
+	spin_unlock_irqrestore(&channel_lock, flags);
+
+
 	/* bring down all vaps */
 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 		ieee80211_stop(vap->iv_dev);
--- a/net80211/ieee80211_input.c
+++ b/net80211/ieee80211_input.c
@@ -2772,6 +2772,7 @@ static void
 ieee80211_doth_switch_channel(struct ieee80211vap *vap)
 {
 	struct ieee80211com *ic = vap->iv_ic;
+	unsigned long flags;
 
 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOTH,
 			  "%s: Channel switch to %3d (%4d MHz) NOW!\n",
@@ -2794,6 +2795,9 @@ ieee80211_doth_switch_channel(struct iee
 
 	ic->ic_curchan = ic->ic_bsschan = vap->iv_csa_chan;
 	ic->ic_set_channel(ic);
+	spin_lock_irqsave(&channel_lock, flags);
+	ieee80211_scan_set_bss_channel(ic, ic->ic_bsschan);
+	spin_unlock_irqrestore(&channel_lock, flags);
 }
 
 static void
--- a/net80211/ieee80211_node.c
+++ b/net80211/ieee80211_node.c
@@ -308,6 +308,7 @@ ieee80211_create_ibss(struct ieee80211va
 {
 	struct ieee80211com *ic = vap->iv_ic;
 	struct ieee80211_node *ni;
+	unsigned long flags;
 
 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
 		"%s: creating ibss on channel %u\n", __func__,
@@ -386,6 +387,9 @@ ieee80211_create_ibss(struct ieee80211va
 	ic->ic_bsschan = chan;
 	ieee80211_node_set_chan(ic, ni);
 	ic->ic_curmode = ieee80211_chan2mode(chan);
+	spin_lock_irqsave(&channel_lock, flags);
+	ieee80211_scan_set_bss_channel(ic, ic->ic_bsschan);
+	spin_unlock_irqrestore(&channel_lock, flags);
 
 	/* Update country ie information */
 	ieee80211_build_countryie(ic);
@@ -622,6 +626,7 @@ ieee80211_sta_join1(struct ieee80211_nod
 	struct ieee80211vap *vap = selbs->ni_vap;
 	struct ieee80211com *ic = selbs->ni_ic;
 	struct ieee80211_node *obss;
+	unsigned long flags;
 	int canreassoc;
 
 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
@@ -650,6 +655,9 @@ ieee80211_sta_join1(struct ieee80211_nod
 	ic->ic_curchan = ic->ic_bsschan;
 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
 	ic->ic_set_channel(ic);
+	spin_lock_irqsave(&channel_lock, flags);
+	ieee80211_scan_set_bss_channel(ic, ic->ic_bsschan);
+	spin_unlock_irqrestore(&channel_lock, flags);
 	/*
 	 * Set the erp state (mostly the slot time) to deal with
 	 * the auto-select case; this should be redundant if the
--- a/net80211/ieee80211_proto.c
+++ b/net80211/ieee80211_proto.c
@@ -1225,6 +1225,7 @@ ieee80211_dturbo_switch(struct ieee80211
 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 #endif
 	struct ieee80211_channel *chan;
+	unsigned long flags;
 
 	chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags);
 	if (chan == NULL) {		/* XXX should not happen */
@@ -1243,6 +1244,9 @@ ieee80211_dturbo_switch(struct ieee80211
 	ic->ic_bsschan = chan;
 	ic->ic_curchan = chan;
 	ic->ic_set_channel(ic);
+	spin_lock_irqsave(&channel_lock, flags);
+	ieee80211_scan_set_bss_channel(ic, ic->ic_bsschan);
+	spin_unlock_irqrestore(&channel_lock, flags);
 	/* NB: do not need to reset ERP state because in sta mode */
 }
 EXPORT_SYMBOL(ieee80211_dturbo_switch);
--- a/net80211/ieee80211_wireless.c
+++ b/net80211/ieee80211_wireless.c
@@ -4076,8 +4076,13 @@ ieee80211_ioctl_setchanlist(struct net_d
 	if (nchan == 0)			/* no valid channels, disallow */
 		return -EINVAL;
 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
-	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
+	    isclr(chanlist, ic->ic_bsschan->ic_ieee)) {
+		unsigned long flags;
 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;	/* invalidate */
+		spin_lock_irqsave(&channel_lock, flags);
+		ieee80211_scan_set_bss_channel(ic, ic->ic_bsschan);
+		spin_unlock_irqrestore(&channel_lock, flags);
+	}
 
 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
 	/* update Supported Channels information element */
--- a/net80211/ieee80211_scan_ap.c
+++ b/net80211/ieee80211_scan_ap.c
@@ -208,9 +208,15 @@ ap_start(struct ieee80211_scan_state *ss
 	struct ieee80211com *ic     = NULL;
 	int i;
 	unsigned int mode = 0;
+	unsigned long sflags;
 
 	SCAN_AP_LOCK_IRQ(as);
 	ic = vap->iv_ic;
+
+	spin_lock_irqsave(&channel_lock, sflags);
+	ieee80211_scan_set_bss_channel(ic, NULL);
+	spin_unlock_irqrestore(&channel_lock, sflags);
+
 	/* Determine mode flags to match, or leave zero for auto mode */
 	ss->ss_last = 0;
 	ieee80211_scan_add_channels(ic, ss, vap->iv_des_mode);
@@ -423,8 +429,10 @@ pc_cmp_idletime(struct ieee80211_channel
 	if (!a->ic_idletime || !b->ic_idletime)
 		return 0;
 
-	/* a is better than b (return < 0) when a has more idle time than b */
-	return b->ic_idletime - a->ic_idletime;
+	/* a is better than b (return < 0) when a has more idle and less bias time than b */
+	return
+		((100 - (u32) a->ic_idletime) + ieee80211_scan_get_bias(a)) -
+		((100 - (u32) b->ic_idletime) + ieee80211_scan_get_bias(b));
 }
 
 
@@ -575,6 +583,7 @@ ap_end(struct ieee80211_scan_state *ss, 
 	struct ap_state *as = ss->ss_priv;
 	struct ieee80211_channel *bestchan = NULL;
 	struct ieee80211com *ic = NULL;
+	unsigned long sflags;
 	int res = 1;
 
 	SCAN_AP_LOCK_IRQ(as);
@@ -586,8 +595,11 @@ ap_end(struct ieee80211_scan_state *ss, 
 
 	/* record stats for the channel that was scanned last */
 	ic->ic_set_channel(ic);
+	spin_lock_irqsave(&channel_lock, sflags);
+	ieee80211_scan_set_bss_channel(ic, NULL);
 	bestchan = pick_channel(ss, vap, flags);
 	if (bestchan == NULL) {
+		spin_unlock_irqrestore(&channel_lock, sflags);
 		if (ss->ss_last > 0) {
 			/* no suitable channel, should not happen */
 			printk(KERN_ERR "%s: %s: no suitable channel! "
@@ -606,6 +618,7 @@ ap_end(struct ieee80211_scan_state *ss, 
 					bestchan->ic_freq, bestchan->ic_flags &
 					~IEEE80211_CHAN_TURBO)) == NULL) {
 				/* should never happen ?? */
+				spin_unlock_irqrestore(&channel_lock, sflags);
 				SCAN_AP_UNLOCK_IRQ_EARLY(as);
 				return 0;
 			}
@@ -618,6 +631,9 @@ ap_end(struct ieee80211_scan_state *ss, 
 			as->as_action = action;
 		as->as_selbss = se;
 
+		ieee80211_scan_set_bss_channel(ic, bestchan);
+		spin_unlock_irqrestore(&channel_lock, sflags);
+
 		/* Must defer action to avoid possible recursive call through 
 		 * 80211 state machine, which would result in recursive 
 		 * locking. */