David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 1 | /* 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 Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 8 | #include <linux/ieee80211.h> |
| 9 | #include <net/cfg80211.h> |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 10 | |
| 11 | #include "hermes.h" |
| 12 | #include "orinoco.h" |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 13 | #include "main.h" |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 14 | |
| 15 | #include "scan.h" |
| 16 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 17 | #define ZERO_DBM_OFFSET 0x95 |
| 18 | #define MAX_SIGNAL_LEVEL 0x8A |
| 19 | #define MIN_SIGNAL_LEVEL 0x2F |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 20 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 21 | #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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 25 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 26 | static int symbol_build_supp_rates(u8 *buf, const __le16 *rates) |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 27 | { |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 28 | int i; |
| 29 | u8 rate; |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 30 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 31 | 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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 38 | } |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 39 | buf[1] = i; |
| 40 | |
| 41 | return i + 2; |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 42 | } |
| 43 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 44 | static int prism_build_supp_rates(u8 *buf, const u8 *rates) |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 45 | { |
| 46 | int i; |
| 47 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 48 | 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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 54 | } |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 55 | 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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 71 | } |
| 72 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 73 | static void orinoco_add_hostscan_result(struct orinoco_private *priv, |
| 74 | const union hermes_scan_info *bss) |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 75 | { |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 76 | 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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 87 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 88 | 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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 111 | 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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 117 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 118 | cfg80211_inform_bss(wiphy, channel, bss->a.bssid, timestamp, |
| 119 | capability, beacon_interval, ie_buf, ie_len, |
| 120 | signal, GFP_KERNEL); |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 121 | } |
| 122 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 123 | void orinoco_add_extscan_result(struct orinoco_private *priv, |
| 124 | struct agere_ext_scan_info *bss, |
| 125 | size_t len) |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 126 | { |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 127 | 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 | |
| 154 | void 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 Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 161 | |
| 162 | switch (priv->firmware_type) { |
| 163 | case FIRMWARE_TYPE_AGERE: |
| 164 | atom_len = sizeof(struct agere_scan_apinfo); |
| 165 | offset = 0; |
| 166 | break; |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 167 | |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 168 | 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 Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 185 | |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 186 | 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 Kilroy | 4244f41 | 2009-07-02 20:26:45 +0100 | [diff] [blame] | 193 | "data: %zu\n", priv->ndev->name, |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 194 | atom_len); |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 195 | abort = true; |
| 196 | goto scan_abort; |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 197 | } |
| 198 | } else |
| 199 | atom_len = offsetof(struct prism2_scan_apinfo, atim); |
| 200 | break; |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 201 | |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 202 | default: |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 203 | abort = true; |
| 204 | goto scan_abort; |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* Check that we got an whole number of atoms */ |
| 208 | if ((len - offset) % atom_len) { |
David Kilroy | 4244f41 | 2009-07-02 20:26:45 +0100 | [diff] [blame] | 209 | printk(KERN_ERR "%s: Unexpected scan data length %zu, " |
| 210 | "atom_len %zu, offset %d\n", priv->ndev->name, len, |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 211 | atom_len, offset); |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 212 | abort = true; |
| 213 | goto scan_abort; |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 216 | /* Process the entries one by one */ |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 217 | for (; offset + atom_len <= len; offset += atom_len) { |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 218 | union hermes_scan_info *atom; |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 219 | |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 220 | atom = (union hermes_scan_info *) (buf + offset); |
| 221 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 222 | orinoco_add_hostscan_result(priv, atom); |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 223 | } |
| 224 | |
David Kilroy | c63cdbe | 2009-06-18 23:21:33 +0100 | [diff] [blame] | 225 | scan_abort: |
| 226 | if (priv->scan_request) { |
| 227 | cfg80211_scan_done(priv->scan_request, abort); |
| 228 | priv->scan_request = NULL; |
| 229 | } |
David Kilroy | fb791b1 | 2009-02-04 23:05:50 +0000 | [diff] [blame] | 230 | } |