blob: d2f10e9c21627a71059b82b5992d6a5ac47bf212 [file] [log] [blame]
David Kilroyfb791b12009-02-04 23:05:50 +00001/* Helpers for managing scan queues
2 *
3 * See copyright notice in main.c
4 */
5
6#include <linux/kernel.h>
7#include <linux/string.h>
David Kilroyc63cdbe2009-06-18 23:21:33 +01008#include <linux/ieee80211.h>
9#include <net/cfg80211.h>
David Kilroyfb791b12009-02-04 23:05:50 +000010
11#include "hermes.h"
12#include "orinoco.h"
David Kilroyc63cdbe2009-06-18 23:21:33 +010013#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +000014
15#include "scan.h"
16
David Kilroyc63cdbe2009-06-18 23:21:33 +010017#define ZERO_DBM_OFFSET 0x95
18#define MAX_SIGNAL_LEVEL 0x8A
19#define MIN_SIGNAL_LEVEL 0x2F
David Kilroyfb791b12009-02-04 23:05:50 +000020
David Kilroyc63cdbe2009-06-18 23:21:33 +010021#define SIGNAL_TO_DBM(x) \
22 (clamp_t(s32, (x), MIN_SIGNAL_LEVEL, MAX_SIGNAL_LEVEL) \
23 - ZERO_DBM_OFFSET)
24#define SIGNAL_TO_MBM(x) (SIGNAL_TO_DBM(x) * 100)
David Kilroyfb791b12009-02-04 23:05:50 +000025
David Kilroyc63cdbe2009-06-18 23:21:33 +010026static int symbol_build_supp_rates(u8 *buf, const __le16 *rates)
David Kilroyfb791b12009-02-04 23:05:50 +000027{
David Kilroyc63cdbe2009-06-18 23:21:33 +010028 int i;
29 u8 rate;
David Kilroyfb791b12009-02-04 23:05:50 +000030
David Kilroyc63cdbe2009-06-18 23:21:33 +010031 buf[0] = WLAN_EID_SUPP_RATES;
32 for (i = 0; i < 5; i++) {
33 rate = le16_to_cpu(rates[i]);
34 /* NULL terminated */
35 if (rate == 0x0)
36 break;
37 buf[i + 2] = rate;
David Kilroyfb791b12009-02-04 23:05:50 +000038 }
David Kilroyc63cdbe2009-06-18 23:21:33 +010039 buf[1] = i;
40
41 return i + 2;
David Kilroyfb791b12009-02-04 23:05:50 +000042}
43
David Kilroyc63cdbe2009-06-18 23:21:33 +010044static int prism_build_supp_rates(u8 *buf, const u8 *rates)
David Kilroyfb791b12009-02-04 23:05:50 +000045{
46 int i;
47
David Kilroyc63cdbe2009-06-18 23:21:33 +010048 buf[0] = WLAN_EID_SUPP_RATES;
49 for (i = 0; i < 8; i++) {
50 /* NULL terminated */
51 if (rates[i] == 0x0)
52 break;
53 buf[i + 2] = rates[i];
David Kilroyfb791b12009-02-04 23:05:50 +000054 }
David Kilroyc63cdbe2009-06-18 23:21:33 +010055 buf[1] = i;
56
57 /* We might still have another 2 rates, which need to go in
58 * extended supported rates */
59 if (i == 8 && rates[i] > 0) {
60 buf[10] = WLAN_EID_EXT_SUPP_RATES;
61 for (; i < 10; i++) {
62 /* NULL terminated */
63 if (rates[i] == 0x0)
64 break;
65 buf[i + 2] = rates[i];
66 }
67 buf[11] = i - 8;
68 }
69
70 return (i < 8) ? i + 2 : i + 4;
David Kilroyfb791b12009-02-04 23:05:50 +000071}
72
David Kilroyc63cdbe2009-06-18 23:21:33 +010073static void orinoco_add_hostscan_result(struct orinoco_private *priv,
74 const union hermes_scan_info *bss)
David Kilroyfb791b12009-02-04 23:05:50 +000075{
David Kilroyc63cdbe2009-06-18 23:21:33 +010076 struct wiphy *wiphy = priv_to_wiphy(priv);
77 struct ieee80211_channel *channel;
78 u8 *ie;
79 u8 ie_buf[46];
80 u64 timestamp;
81 s32 signal;
82 u16 capability;
83 u16 beacon_interval;
84 int ie_len;
85 int freq;
86 int len;
David Kilroyfb791b12009-02-04 23:05:50 +000087
David Kilroyc63cdbe2009-06-18 23:21:33 +010088 len = le16_to_cpu(bss->a.essid_len);
89
90 /* Reconstruct SSID and bitrate IEs to pass up */
91 ie_buf[0] = WLAN_EID_SSID;
92 ie_buf[1] = len;
93 memcpy(&ie_buf[2], bss->a.essid, len);
94
95 ie = ie_buf + len + 2;
96 ie_len = ie_buf[1] + 2;
97 switch (priv->firmware_type) {
98 case FIRMWARE_TYPE_SYMBOL:
99 ie_len += symbol_build_supp_rates(ie, bss->s.rates);
100 break;
101
102 case FIRMWARE_TYPE_INTERSIL:
103 ie_len += prism_build_supp_rates(ie, bss->p.rates);
104 break;
105
106 case FIRMWARE_TYPE_AGERE:
107 default:
David Kilroyfb791b12009-02-04 23:05:50 +0000108 break;
109 }
110
David Kilroyc63cdbe2009-06-18 23:21:33 +0100111 freq = ieee80211_dsss_chan_to_freq(le16_to_cpu(bss->a.channel));
112 channel = ieee80211_get_channel(wiphy, freq);
113 timestamp = 0;
114 capability = le16_to_cpu(bss->a.capabilities);
115 beacon_interval = le16_to_cpu(bss->a.beacon_interv);
116 signal = SIGNAL_TO_MBM(le16_to_cpu(bss->a.level));
David Kilroyfb791b12009-02-04 23:05:50 +0000117
David Kilroyc63cdbe2009-06-18 23:21:33 +0100118 cfg80211_inform_bss(wiphy, channel, bss->a.bssid, timestamp,
119 capability, beacon_interval, ie_buf, ie_len,
120 signal, GFP_KERNEL);
David Kilroyfb791b12009-02-04 23:05:50 +0000121}
122
David Kilroyc63cdbe2009-06-18 23:21:33 +0100123void orinoco_add_extscan_result(struct orinoco_private *priv,
124 struct agere_ext_scan_info *bss,
125 size_t len)
David Kilroyfb791b12009-02-04 23:05:50 +0000126{
David Kilroyc63cdbe2009-06-18 23:21:33 +0100127 struct wiphy *wiphy = priv_to_wiphy(priv);
128 struct ieee80211_channel *channel;
129 u8 *ie;
130 u64 timestamp;
131 s32 signal;
132 u16 capability;
133 u16 beacon_interval;
134 size_t ie_len;
135 int chan, freq;
136
137 ie_len = len - sizeof(*bss);
138 ie = orinoco_get_ie(bss->data, ie_len, WLAN_EID_DS_PARAMS);
139 chan = ie ? ie[2] : 0;
140 freq = ieee80211_dsss_chan_to_freq(chan);
141 channel = ieee80211_get_channel(wiphy, freq);
142
143 timestamp = le64_to_cpu(bss->timestamp);
144 capability = le16_to_cpu(bss->capabilities);
145 beacon_interval = le16_to_cpu(bss->beacon_interval);
146 ie = bss->data;
147 signal = SIGNAL_TO_MBM(bss->level);
148
149 cfg80211_inform_bss(wiphy, channel, bss->bssid, timestamp,
150 capability, beacon_interval, ie, ie_len,
151 signal, GFP_KERNEL);
152}
153
154void orinoco_add_hostscan_results(struct orinoco_private *priv,
155 unsigned char *buf,
156 size_t len)
157{
158 int offset; /* In the scan data */
159 size_t atom_len;
160 bool abort = false;
David Kilroyfb791b12009-02-04 23:05:50 +0000161
162 switch (priv->firmware_type) {
163 case FIRMWARE_TYPE_AGERE:
164 atom_len = sizeof(struct agere_scan_apinfo);
165 offset = 0;
166 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +0100167
David Kilroyfb791b12009-02-04 23:05:50 +0000168 case FIRMWARE_TYPE_SYMBOL:
169 /* Lack of documentation necessitates this hack.
170 * Different firmwares have 68 or 76 byte long atoms.
171 * We try modulo first. If the length divides by both,
172 * we check what would be the channel in the second
173 * frame for a 68-byte atom. 76-byte atoms have 0 there.
174 * Valid channel cannot be 0. */
175 if (len % 76)
176 atom_len = 68;
177 else if (len % 68)
178 atom_len = 76;
179 else if (len >= 1292 && buf[68] == 0)
180 atom_len = 76;
181 else
182 atom_len = 68;
183 offset = 0;
184 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +0100185
David Kilroyfb791b12009-02-04 23:05:50 +0000186 case FIRMWARE_TYPE_INTERSIL:
187 offset = 4;
188 if (priv->has_hostscan) {
189 atom_len = le16_to_cpup((__le16 *)buf);
190 /* Sanity check for atom_len */
191 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
192 printk(KERN_ERR "%s: Invalid atom_len in scan "
David Kilroy4244f412009-07-02 20:26:45 +0100193 "data: %zu\n", priv->ndev->name,
David Kilroyfb791b12009-02-04 23:05:50 +0000194 atom_len);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100195 abort = true;
196 goto scan_abort;
David Kilroyfb791b12009-02-04 23:05:50 +0000197 }
198 } else
199 atom_len = offsetof(struct prism2_scan_apinfo, atim);
200 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +0100201
David Kilroyfb791b12009-02-04 23:05:50 +0000202 default:
David Kilroyc63cdbe2009-06-18 23:21:33 +0100203 abort = true;
204 goto scan_abort;
David Kilroyfb791b12009-02-04 23:05:50 +0000205 }
206
207 /* Check that we got an whole number of atoms */
208 if ((len - offset) % atom_len) {
David Kilroy4244f412009-07-02 20:26:45 +0100209 printk(KERN_ERR "%s: Unexpected scan data length %zu, "
210 "atom_len %zu, offset %d\n", priv->ndev->name, len,
David Kilroyfb791b12009-02-04 23:05:50 +0000211 atom_len, offset);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100212 abort = true;
213 goto scan_abort;
David Kilroyfb791b12009-02-04 23:05:50 +0000214 }
215
David Kilroyc63cdbe2009-06-18 23:21:33 +0100216 /* Process the entries one by one */
David Kilroyfb791b12009-02-04 23:05:50 +0000217 for (; offset + atom_len <= len; offset += atom_len) {
David Kilroyc63cdbe2009-06-18 23:21:33 +0100218 union hermes_scan_info *atom;
David Kilroyfb791b12009-02-04 23:05:50 +0000219
David Kilroyfb791b12009-02-04 23:05:50 +0000220 atom = (union hermes_scan_info *) (buf + offset);
221
David Kilroyc63cdbe2009-06-18 23:21:33 +0100222 orinoco_add_hostscan_result(priv, atom);
David Kilroyfb791b12009-02-04 23:05:50 +0000223 }
224
David Kilroyc63cdbe2009-06-18 23:21:33 +0100225 scan_abort:
226 if (priv->scan_request) {
227 cfg80211_scan_done(priv->scan_request, abort);
228 priv->scan_request = NULL;
229 }
David Kilroyfb791b12009-02-04 23:05:50 +0000230}