Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | Broadcom BCM43xx wireless driver |
| 4 | |
| 5 | Transmission (TX/RX) related functions. |
| 6 | |
| 7 | Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>, |
| 8 | Stefano Brivio <st3@riseup.net> |
| 9 | Michael Buesch <mbuesch@freenet.de> |
| 10 | Danny van Dyk <kugelfang@gentoo.org> |
| 11 | Andreas Jaggi <andreas.jaggi@waterwave.ch> |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation; either version 2 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; see the file COPYING. If not, write to |
| 25 | the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor, |
| 26 | Boston, MA 02110-1301, USA. |
| 27 | |
| 28 | */ |
| 29 | |
| 30 | #include "bcm43xx_xmit.h" |
| 31 | |
| 32 | #include <linux/etherdevice.h> |
| 33 | |
| 34 | |
| 35 | /* Extract the bitrate out of a CCK PLCP header. */ |
| 36 | static u8 bcm43xx_plcp_get_bitrate_cck(struct bcm43xx_plcp_hdr4 *plcp) |
| 37 | { |
| 38 | switch (plcp->raw[0]) { |
| 39 | case 0x0A: |
| 40 | return IEEE80211_CCK_RATE_1MB; |
| 41 | case 0x14: |
| 42 | return IEEE80211_CCK_RATE_2MB; |
| 43 | case 0x37: |
| 44 | return IEEE80211_CCK_RATE_5MB; |
| 45 | case 0x6E: |
| 46 | return IEEE80211_CCK_RATE_11MB; |
| 47 | } |
| 48 | assert(0); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /* Extract the bitrate out of an OFDM PLCP header. */ |
| 53 | static u8 bcm43xx_plcp_get_bitrate_ofdm(struct bcm43xx_plcp_hdr4 *plcp) |
| 54 | { |
| 55 | switch (plcp->raw[0] & 0xF) { |
| 56 | case 0xB: |
| 57 | return IEEE80211_OFDM_RATE_6MB; |
| 58 | case 0xF: |
| 59 | return IEEE80211_OFDM_RATE_9MB; |
| 60 | case 0xA: |
| 61 | return IEEE80211_OFDM_RATE_12MB; |
| 62 | case 0xE: |
| 63 | return IEEE80211_OFDM_RATE_18MB; |
| 64 | case 0x9: |
| 65 | return IEEE80211_OFDM_RATE_24MB; |
| 66 | case 0xD: |
| 67 | return IEEE80211_OFDM_RATE_36MB; |
| 68 | case 0x8: |
| 69 | return IEEE80211_OFDM_RATE_48MB; |
| 70 | case 0xC: |
| 71 | return IEEE80211_OFDM_RATE_54MB; |
| 72 | } |
| 73 | assert(0); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | u8 bcm43xx_plcp_get_ratecode_cck(const u8 bitrate) |
| 78 | { |
| 79 | switch (bitrate) { |
| 80 | case IEEE80211_CCK_RATE_1MB: |
| 81 | return 0x0A; |
| 82 | case IEEE80211_CCK_RATE_2MB: |
| 83 | return 0x14; |
| 84 | case IEEE80211_CCK_RATE_5MB: |
| 85 | return 0x37; |
| 86 | case IEEE80211_CCK_RATE_11MB: |
| 87 | return 0x6E; |
| 88 | } |
| 89 | assert(0); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | u8 bcm43xx_plcp_get_ratecode_ofdm(const u8 bitrate) |
| 94 | { |
| 95 | switch (bitrate) { |
| 96 | case IEEE80211_OFDM_RATE_6MB: |
| 97 | return 0xB; |
| 98 | case IEEE80211_OFDM_RATE_9MB: |
| 99 | return 0xF; |
| 100 | case IEEE80211_OFDM_RATE_12MB: |
| 101 | return 0xA; |
| 102 | case IEEE80211_OFDM_RATE_18MB: |
| 103 | return 0xE; |
| 104 | case IEEE80211_OFDM_RATE_24MB: |
| 105 | return 0x9; |
| 106 | case IEEE80211_OFDM_RATE_36MB: |
| 107 | return 0xD; |
| 108 | case IEEE80211_OFDM_RATE_48MB: |
| 109 | return 0x8; |
| 110 | case IEEE80211_OFDM_RATE_54MB: |
| 111 | return 0xC; |
| 112 | } |
| 113 | assert(0); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void bcm43xx_generate_plcp_hdr(struct bcm43xx_plcp_hdr4 *plcp, |
| 118 | const u16 octets, const u8 bitrate, |
| 119 | const int ofdm_modulation) |
| 120 | { |
| 121 | __le32 *data = &(plcp->data); |
| 122 | __u8 *raw = plcp->raw; |
| 123 | |
| 124 | if (ofdm_modulation) { |
| 125 | *data = bcm43xx_plcp_get_ratecode_ofdm(bitrate); |
| 126 | assert(!(octets & 0xF000)); |
| 127 | *data |= (octets << 5); |
| 128 | *data = cpu_to_le32(*data); |
| 129 | } else { |
| 130 | u32 plen; |
| 131 | |
| 132 | plen = octets * 16 / bitrate; |
| 133 | if ((octets * 16 % bitrate) > 0) { |
| 134 | plen++; |
| 135 | if ((bitrate == IEEE80211_CCK_RATE_11MB) |
| 136 | && ((octets * 8 % 11) < 4)) { |
| 137 | raw[1] = 0x84; |
| 138 | } else |
| 139 | raw[1] = 0x04; |
| 140 | } else |
| 141 | raw[1] = 0x04; |
| 142 | *data |= cpu_to_le32(plen << 16); |
| 143 | raw[0] = bcm43xx_plcp_get_ratecode_cck(bitrate); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static u8 bcm43xx_calc_fallback_rate(u8 bitrate) |
| 148 | { |
| 149 | switch (bitrate) { |
| 150 | case IEEE80211_CCK_RATE_1MB: |
| 151 | return IEEE80211_CCK_RATE_1MB; |
| 152 | case IEEE80211_CCK_RATE_2MB: |
| 153 | return IEEE80211_CCK_RATE_1MB; |
| 154 | case IEEE80211_CCK_RATE_5MB: |
| 155 | return IEEE80211_CCK_RATE_2MB; |
| 156 | case IEEE80211_CCK_RATE_11MB: |
| 157 | return IEEE80211_CCK_RATE_5MB; |
| 158 | case IEEE80211_OFDM_RATE_6MB: |
| 159 | return IEEE80211_CCK_RATE_5MB; |
| 160 | case IEEE80211_OFDM_RATE_9MB: |
| 161 | return IEEE80211_OFDM_RATE_6MB; |
| 162 | case IEEE80211_OFDM_RATE_12MB: |
| 163 | return IEEE80211_OFDM_RATE_9MB; |
| 164 | case IEEE80211_OFDM_RATE_18MB: |
| 165 | return IEEE80211_OFDM_RATE_12MB; |
| 166 | case IEEE80211_OFDM_RATE_24MB: |
| 167 | return IEEE80211_OFDM_RATE_18MB; |
| 168 | case IEEE80211_OFDM_RATE_36MB: |
| 169 | return IEEE80211_OFDM_RATE_24MB; |
| 170 | case IEEE80211_OFDM_RATE_48MB: |
| 171 | return IEEE80211_OFDM_RATE_36MB; |
| 172 | case IEEE80211_OFDM_RATE_54MB: |
| 173 | return IEEE80211_OFDM_RATE_48MB; |
| 174 | } |
| 175 | assert(0); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static |
| 180 | __le16 bcm43xx_calc_duration_id(const struct ieee80211_hdr *wireless_header, |
| 181 | u8 bitrate) |
| 182 | { |
| 183 | const u16 frame_ctl = le16_to_cpu(wireless_header->frame_ctl); |
| 184 | __le16 duration_id = wireless_header->duration_id; |
| 185 | |
| 186 | switch (WLAN_FC_GET_TYPE(frame_ctl)) { |
| 187 | case IEEE80211_FTYPE_DATA: |
| 188 | case IEEE80211_FTYPE_MGMT: |
| 189 | //TODO: Steal the code from ieee80211, once it is completed there. |
| 190 | break; |
| 191 | case IEEE80211_FTYPE_CTL: |
| 192 | /* Use the original duration/id. */ |
| 193 | break; |
| 194 | default: |
| 195 | assert(0); |
| 196 | } |
| 197 | |
| 198 | return duration_id; |
| 199 | } |
| 200 | |
| 201 | static inline |
| 202 | u16 ceiling_div(u16 dividend, u16 divisor) |
| 203 | { |
| 204 | return ((dividend + divisor - 1) / divisor); |
| 205 | } |
| 206 | |
| 207 | static void bcm43xx_generate_rts(const struct bcm43xx_phyinfo *phy, |
| 208 | struct bcm43xx_txhdr *txhdr, |
| 209 | u16 *flags, |
| 210 | u8 bitrate, |
| 211 | const struct ieee80211_hdr_4addr *wlhdr) |
| 212 | { |
| 213 | u16 fctl; |
| 214 | u16 dur; |
| 215 | u8 fallback_bitrate; |
| 216 | int ofdm_modulation; |
| 217 | int fallback_ofdm_modulation; |
Michael Buesch | b5e868e | 2006-03-25 16:27:32 +0100 | [diff] [blame] | 218 | // u8 *sa, *da; |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 219 | u16 flen; |
| 220 | |
| 221 | //FIXME sa = ieee80211_get_SA((struct ieee80211_hdr *)wlhdr); |
| 222 | //FIXME da = ieee80211_get_DA((struct ieee80211_hdr *)wlhdr); |
| 223 | fallback_bitrate = bcm43xx_calc_fallback_rate(bitrate); |
| 224 | ofdm_modulation = !(ieee80211_is_cck_rate(bitrate)); |
| 225 | fallback_ofdm_modulation = !(ieee80211_is_cck_rate(fallback_bitrate)); |
| 226 | |
| 227 | flen = sizeof(u16) + sizeof(u16) + ETH_ALEN + ETH_ALEN + IEEE80211_FCS_LEN, |
| 228 | bcm43xx_generate_plcp_hdr((struct bcm43xx_plcp_hdr4 *)(&txhdr->rts_cts_plcp), |
| 229 | flen, bitrate, |
| 230 | !ieee80211_is_cck_rate(bitrate)); |
| 231 | bcm43xx_generate_plcp_hdr((struct bcm43xx_plcp_hdr4 *)(&txhdr->rts_cts_fallback_plcp), |
| 232 | flen, fallback_bitrate, |
| 233 | !ieee80211_is_cck_rate(fallback_bitrate)); |
| 234 | fctl = IEEE80211_FTYPE_CTL; |
| 235 | fctl |= IEEE80211_STYPE_RTS; |
| 236 | dur = le16_to_cpu(wlhdr->duration_id); |
| 237 | /*FIXME: should we test for dur==0 here and let it unmodified in this case? |
| 238 | * The following assert checks for this case... |
| 239 | */ |
| 240 | assert(dur); |
| 241 | /*FIXME: The duration calculation is not really correct. |
| 242 | * I am not 100% sure which bitrate to use. We use the RTS rate here, |
| 243 | * but this is likely to be wrong. |
| 244 | */ |
| 245 | if (phy->type == BCM43xx_PHYTYPE_A) { |
| 246 | /* Three times SIFS */ |
| 247 | dur += 16 * 3; |
| 248 | /* Add ACK duration. */ |
| 249 | dur += ceiling_div((16 + 8 * (14 /*bytes*/) + 6) * 10, |
| 250 | bitrate * 4); |
| 251 | /* Add CTS duration. */ |
| 252 | dur += ceiling_div((16 + 8 * (14 /*bytes*/) + 6) * 10, |
| 253 | bitrate * 4); |
| 254 | } else { |
| 255 | /* Three times SIFS */ |
| 256 | dur += 10 * 3; |
| 257 | /* Add ACK duration. */ |
| 258 | dur += ceiling_div(8 * (14 /*bytes*/) * 10, |
| 259 | bitrate); |
| 260 | /* Add CTS duration. */ |
| 261 | dur += ceiling_div(8 * (14 /*bytes*/) * 10, |
| 262 | bitrate); |
| 263 | } |
| 264 | |
| 265 | txhdr->rts_cts_frame_control = cpu_to_le16(fctl); |
| 266 | txhdr->rts_cts_dur = cpu_to_le16(dur); |
| 267 | //printk(BCM43xx_MACFMT " " BCM43xx_MACFMT " " BCM43xx_MACFMT "\n", BCM43xx_MACARG(wlhdr->addr1), BCM43xx_MACARG(wlhdr->addr2), BCM43xx_MACARG(wlhdr->addr3)); |
| 268 | //printk(BCM43xx_MACFMT " " BCM43xx_MACFMT "\n", BCM43xx_MACARG(sa), BCM43xx_MACARG(da)); |
| 269 | memcpy(txhdr->rts_cts_mac1, wlhdr->addr1, ETH_ALEN);//FIXME! |
Michael Buesch | b5e868e | 2006-03-25 16:27:32 +0100 | [diff] [blame] | 270 | // memcpy(txhdr->rts_cts_mac2, sa, ETH_ALEN); |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 271 | |
| 272 | *flags |= BCM43xx_TXHDRFLAG_RTSCTS; |
| 273 | *flags |= BCM43xx_TXHDRFLAG_RTS; |
| 274 | if (ofdm_modulation) |
| 275 | *flags |= BCM43xx_TXHDRFLAG_RTSCTS_OFDM; |
| 276 | if (fallback_ofdm_modulation) |
| 277 | *flags |= BCM43xx_TXHDRFLAG_RTSCTSFALLBACK_OFDM; |
| 278 | } |
| 279 | |
| 280 | void bcm43xx_generate_txhdr(struct bcm43xx_private *bcm, |
| 281 | struct bcm43xx_txhdr *txhdr, |
| 282 | const unsigned char *fragment_data, |
| 283 | const unsigned int fragment_len, |
| 284 | const int is_first_fragment, |
| 285 | const u16 cookie) |
| 286 | { |
Michael Buesch | e9357c0 | 2006-03-13 19:27:34 +0100 | [diff] [blame] | 287 | const struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm); |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 288 | const struct ieee80211_hdr_4addr *wireless_header = (const struct ieee80211_hdr_4addr *)fragment_data; |
| 289 | const struct ieee80211_security *secinfo = &bcm->ieee->sec; |
| 290 | u8 bitrate; |
| 291 | u8 fallback_bitrate; |
| 292 | int ofdm_modulation; |
| 293 | int fallback_ofdm_modulation; |
| 294 | u16 plcp_fragment_len = fragment_len; |
| 295 | u16 flags = 0; |
| 296 | u16 control = 0; |
| 297 | u16 wsec_rate = 0; |
| 298 | u16 encrypt_frame; |
| 299 | |
| 300 | /* Now construct the TX header. */ |
| 301 | memset(txhdr, 0, sizeof(*txhdr)); |
| 302 | |
| 303 | bitrate = bcm->softmac->txrates.default_rate; |
| 304 | ofdm_modulation = !(ieee80211_is_cck_rate(bitrate)); |
| 305 | fallback_bitrate = bcm43xx_calc_fallback_rate(bitrate); |
| 306 | fallback_ofdm_modulation = !(ieee80211_is_cck_rate(fallback_bitrate)); |
| 307 | |
| 308 | /* Set Frame Control from 80211 header. */ |
| 309 | txhdr->frame_control = wireless_header->frame_ctl; |
| 310 | /* Copy address1 from 80211 header. */ |
| 311 | memcpy(txhdr->mac1, wireless_header->addr1, 6); |
| 312 | /* Set the fallback duration ID. */ |
| 313 | txhdr->fallback_dur_id = bcm43xx_calc_duration_id((const struct ieee80211_hdr *)wireless_header, |
| 314 | fallback_bitrate); |
| 315 | /* Set the cookie (used as driver internal ID for the frame) */ |
| 316 | txhdr->cookie = cpu_to_le16(cookie); |
| 317 | |
| 318 | /* Hardware appends FCS. */ |
| 319 | plcp_fragment_len += IEEE80211_FCS_LEN; |
| 320 | |
| 321 | /* Hardware encryption. */ |
| 322 | encrypt_frame = le16_to_cpup(&wireless_header->frame_ctl) & IEEE80211_FCTL_PROTECTED; |
| 323 | if (encrypt_frame && !bcm->ieee->host_encrypt) { |
| 324 | const struct ieee80211_hdr_3addr *hdr = (struct ieee80211_hdr_3addr *)wireless_header; |
| 325 | memcpy(txhdr->wep_iv, hdr->payload, 4); |
| 326 | /* Hardware appends ICV. */ |
| 327 | plcp_fragment_len += 4; |
| 328 | |
| 329 | wsec_rate |= (bcm->key[secinfo->active_key].algorithm << BCM43xx_TXHDR_WSEC_ALGO_SHIFT) |
| 330 | & BCM43xx_TXHDR_WSEC_ALGO_MASK; |
| 331 | wsec_rate |= (secinfo->active_key << BCM43xx_TXHDR_WSEC_KEYINDEX_SHIFT) |
| 332 | & BCM43xx_TXHDR_WSEC_KEYINDEX_MASK; |
| 333 | } |
| 334 | |
| 335 | /* Generate the PLCP header and the fallback PLCP header. */ |
| 336 | bcm43xx_generate_plcp_hdr((struct bcm43xx_plcp_hdr4 *)(&txhdr->plcp), |
| 337 | plcp_fragment_len, |
| 338 | bitrate, ofdm_modulation); |
| 339 | bcm43xx_generate_plcp_hdr(&txhdr->fallback_plcp, plcp_fragment_len, |
| 340 | fallback_bitrate, fallback_ofdm_modulation); |
| 341 | |
| 342 | /* Set the CONTROL field */ |
| 343 | if (ofdm_modulation) |
| 344 | control |= BCM43xx_TXHDRCTL_OFDM; |
| 345 | if (bcm->short_preamble) //FIXME: could be the other way around, please test |
| 346 | control |= BCM43xx_TXHDRCTL_SHORT_PREAMBLE; |
| 347 | control |= (phy->antenna_diversity << BCM43xx_TXHDRCTL_ANTENNADIV_SHIFT) |
| 348 | & BCM43xx_TXHDRCTL_ANTENNADIV_MASK; |
| 349 | |
| 350 | /* Set the FLAGS field */ |
| 351 | if (!is_multicast_ether_addr(wireless_header->addr1) && |
| 352 | !is_broadcast_ether_addr(wireless_header->addr1)) |
| 353 | flags |= BCM43xx_TXHDRFLAG_EXPECTACK; |
| 354 | if (1 /* FIXME: PS poll?? */) |
| 355 | flags |= 0x10; // FIXME: unknown meaning. |
| 356 | if (fallback_ofdm_modulation) |
| 357 | flags |= BCM43xx_TXHDRFLAG_FALLBACKOFDM; |
| 358 | if (is_first_fragment) |
| 359 | flags |= BCM43xx_TXHDRFLAG_FIRSTFRAGMENT; |
| 360 | |
| 361 | /* Set WSEC/RATE field */ |
| 362 | wsec_rate |= (txhdr->plcp.raw[0] << BCM43xx_TXHDR_RATE_SHIFT) |
| 363 | & BCM43xx_TXHDR_RATE_MASK; |
| 364 | |
| 365 | /* Generate the RTS/CTS packet, if required. */ |
| 366 | /* FIXME: We should first try with CTS-to-self, |
| 367 | * if we are on 80211g. If we get too many |
| 368 | * failures (hidden nodes), we should switch back to RTS/CTS. |
| 369 | */ |
| 370 | if (0/*FIXME txctl->use_rts_cts*/) { |
| 371 | bcm43xx_generate_rts(phy, txhdr, &flags, |
| 372 | 0/*FIXME txctl->rts_cts_rate*/, |
| 373 | wireless_header); |
| 374 | } |
| 375 | |
| 376 | txhdr->flags = cpu_to_le16(flags); |
| 377 | txhdr->control = cpu_to_le16(control); |
| 378 | txhdr->wsec_rate = cpu_to_le16(wsec_rate); |
| 379 | } |
| 380 | |
| 381 | static s8 bcm43xx_rssi_postprocess(struct bcm43xx_private *bcm, |
| 382 | u8 in_rssi, int ofdm, |
| 383 | int adjust_2053, int adjust_2050) |
| 384 | { |
Michael Buesch | e9357c0 | 2006-03-13 19:27:34 +0100 | [diff] [blame] | 385 | struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm); |
| 386 | struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm); |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 387 | s32 tmp; |
| 388 | |
| 389 | switch (radio->version) { |
| 390 | case 0x2050: |
| 391 | if (ofdm) { |
| 392 | tmp = in_rssi; |
| 393 | if (tmp > 127) |
| 394 | tmp -= 256; |
| 395 | tmp *= 73; |
| 396 | tmp /= 64; |
| 397 | if (adjust_2050) |
| 398 | tmp += 25; |
| 399 | else |
| 400 | tmp -= 3; |
| 401 | } else { |
| 402 | if (bcm->sprom.boardflags & BCM43xx_BFL_RSSI) { |
| 403 | if (in_rssi > 63) |
| 404 | in_rssi = 63; |
| 405 | tmp = radio->nrssi_lt[in_rssi]; |
| 406 | tmp = 31 - tmp; |
| 407 | tmp *= -131; |
| 408 | tmp /= 128; |
| 409 | tmp -= 57; |
| 410 | } else { |
| 411 | tmp = in_rssi; |
| 412 | tmp = 31 - tmp; |
| 413 | tmp *= -149; |
| 414 | tmp /= 128; |
| 415 | tmp -= 68; |
| 416 | } |
| 417 | if (phy->type == BCM43xx_PHYTYPE_G && |
| 418 | adjust_2050) |
| 419 | tmp += 25; |
| 420 | } |
| 421 | break; |
| 422 | case 0x2060: |
| 423 | if (in_rssi > 127) |
| 424 | tmp = in_rssi - 256; |
| 425 | else |
| 426 | tmp = in_rssi; |
| 427 | break; |
| 428 | default: |
| 429 | tmp = in_rssi; |
| 430 | tmp -= 11; |
| 431 | tmp *= 103; |
| 432 | tmp /= 64; |
| 433 | if (adjust_2053) |
| 434 | tmp -= 109; |
| 435 | else |
| 436 | tmp -= 83; |
| 437 | } |
| 438 | |
| 439 | return (s8)tmp; |
| 440 | } |
| 441 | |
Michael Buesch | b5e868e | 2006-03-25 16:27:32 +0100 | [diff] [blame] | 442 | //TODO |
| 443 | #if 0 |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 444 | static s8 bcm43xx_rssinoise_postprocess(struct bcm43xx_private *bcm, |
| 445 | u8 in_rssi) |
| 446 | { |
Michael Buesch | e9357c0 | 2006-03-13 19:27:34 +0100 | [diff] [blame] | 447 | struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm); |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 448 | s8 ret; |
| 449 | |
| 450 | if (phy->type == BCM43xx_PHYTYPE_A) { |
| 451 | //TODO: Incomplete specs. |
| 452 | ret = 0; |
| 453 | } else |
| 454 | ret = bcm43xx_rssi_postprocess(bcm, in_rssi, 0, 1, 1); |
| 455 | |
| 456 | return ret; |
| 457 | } |
Michael Buesch | b5e868e | 2006-03-25 16:27:32 +0100 | [diff] [blame] | 458 | #endif |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 459 | |
| 460 | int bcm43xx_rx(struct bcm43xx_private *bcm, |
| 461 | struct sk_buff *skb, |
| 462 | struct bcm43xx_rxhdr *rxhdr) |
| 463 | { |
Michael Buesch | e9357c0 | 2006-03-13 19:27:34 +0100 | [diff] [blame] | 464 | struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm); |
| 465 | struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm); |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 466 | struct bcm43xx_plcp_hdr4 *plcp; |
| 467 | struct ieee80211_rx_stats stats; |
| 468 | struct ieee80211_hdr_4addr *wlhdr; |
| 469 | u16 frame_ctl; |
| 470 | int is_packet_for_us = 0; |
| 471 | int err = -EINVAL; |
| 472 | const u16 rxflags1 = le16_to_cpu(rxhdr->flags1); |
| 473 | const u16 rxflags2 = le16_to_cpu(rxhdr->flags2); |
| 474 | const u16 rxflags3 = le16_to_cpu(rxhdr->flags3); |
| 475 | const int is_ofdm = !!(rxflags1 & BCM43xx_RXHDR_FLAGS1_OFDM); |
| 476 | |
| 477 | if (rxflags2 & BCM43xx_RXHDR_FLAGS2_TYPE2FRAME) { |
| 478 | plcp = (struct bcm43xx_plcp_hdr4 *)(skb->data + 2); |
| 479 | /* Skip two unknown bytes and the PLCP header. */ |
| 480 | skb_pull(skb, 2 + sizeof(struct bcm43xx_plcp_hdr6)); |
| 481 | } else { |
| 482 | plcp = (struct bcm43xx_plcp_hdr4 *)(skb->data); |
| 483 | /* Skip the PLCP header. */ |
| 484 | skb_pull(skb, sizeof(struct bcm43xx_plcp_hdr6)); |
| 485 | } |
| 486 | /* The SKB contains the PAYLOAD (wireless header + data) |
| 487 | * at this point. The FCS at the end is stripped. |
| 488 | */ |
| 489 | |
| 490 | memset(&stats, 0, sizeof(stats)); |
| 491 | stats.mac_time = le16_to_cpu(rxhdr->mactime); |
| 492 | stats.rssi = bcm43xx_rssi_postprocess(bcm, rxhdr->rssi, is_ofdm, |
| 493 | !!(rxflags1 & BCM43xx_RXHDR_FLAGS1_2053RSSIADJ), |
| 494 | !!(rxflags3 & BCM43xx_RXHDR_FLAGS3_2050RSSIADJ)); |
| 495 | stats.signal = rxhdr->signal_quality; //FIXME |
| 496 | //TODO stats.noise = |
| 497 | if (is_ofdm) |
| 498 | stats.rate = bcm43xx_plcp_get_bitrate_ofdm(plcp); |
| 499 | else |
| 500 | stats.rate = bcm43xx_plcp_get_bitrate_cck(plcp); |
| 501 | //printk("RX ofdm %d, rate == %u\n", is_ofdm, stats.rate); |
Michael Buesch | e9357c0 | 2006-03-13 19:27:34 +0100 | [diff] [blame] | 502 | stats.received_channel = radio->channel; |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 503 | //TODO stats.control = |
| 504 | stats.mask = IEEE80211_STATMASK_SIGNAL | |
| 505 | //TODO IEEE80211_STATMASK_NOISE | |
| 506 | IEEE80211_STATMASK_RATE | |
| 507 | IEEE80211_STATMASK_RSSI; |
Michael Buesch | e9357c0 | 2006-03-13 19:27:34 +0100 | [diff] [blame] | 508 | if (phy->type == BCM43xx_PHYTYPE_A) |
Michael Buesch | f398f02 | 2006-02-23 21:15:39 +0100 | [diff] [blame] | 509 | stats.freq = IEEE80211_52GHZ_BAND; |
| 510 | else |
| 511 | stats.freq = IEEE80211_24GHZ_BAND; |
| 512 | stats.len = skb->len; |
| 513 | |
| 514 | bcm->stats.last_rx = jiffies; |
| 515 | if (bcm->ieee->iw_mode == IW_MODE_MONITOR) { |
| 516 | err = ieee80211_rx(bcm->ieee, skb, &stats); |
| 517 | return (err == 0) ? -EINVAL : 0; |
| 518 | } |
| 519 | |
| 520 | wlhdr = (struct ieee80211_hdr_4addr *)(skb->data); |
| 521 | |
| 522 | switch (bcm->ieee->iw_mode) { |
| 523 | case IW_MODE_ADHOC: |
| 524 | if (memcmp(wlhdr->addr1, bcm->net_dev->dev_addr, ETH_ALEN) == 0 || |
| 525 | memcmp(wlhdr->addr3, bcm->ieee->bssid, ETH_ALEN) == 0 || |
| 526 | is_broadcast_ether_addr(wlhdr->addr1) || |
| 527 | is_multicast_ether_addr(wlhdr->addr1) || |
| 528 | bcm->net_dev->flags & IFF_PROMISC) |
| 529 | is_packet_for_us = 1; |
| 530 | break; |
| 531 | case IW_MODE_INFRA: |
| 532 | default: |
| 533 | /* When receiving multicast or broadcast packets, filter out |
| 534 | the packets we send ourself; we shouldn't see those */ |
| 535 | if (memcmp(wlhdr->addr3, bcm->ieee->bssid, ETH_ALEN) == 0 || |
| 536 | memcmp(wlhdr->addr1, bcm->net_dev->dev_addr, ETH_ALEN) == 0 || |
| 537 | (memcmp(wlhdr->addr3, bcm->net_dev->dev_addr, ETH_ALEN) && |
| 538 | (is_broadcast_ether_addr(wlhdr->addr1) || |
| 539 | is_multicast_ether_addr(wlhdr->addr1) || |
| 540 | bcm->net_dev->flags & IFF_PROMISC))) |
| 541 | is_packet_for_us = 1; |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | frame_ctl = le16_to_cpu(wlhdr->frame_ctl); |
| 546 | if ((frame_ctl & IEEE80211_FCTL_PROTECTED) && !bcm->ieee->host_decrypt) { |
| 547 | frame_ctl &= ~IEEE80211_FCTL_PROTECTED; |
| 548 | wlhdr->frame_ctl = cpu_to_le16(frame_ctl); |
| 549 | /* trim IV and ICV */ |
| 550 | /* FIXME: this must be done only for WEP encrypted packets */ |
| 551 | if (skb->len < 32) { |
| 552 | dprintkl(KERN_ERR PFX "RX packet dropped (PROTECTED flag " |
| 553 | "set and length < 32)\n"); |
| 554 | return -EINVAL; |
| 555 | } else { |
| 556 | memmove(skb->data + 4, skb->data, 24); |
| 557 | skb_pull(skb, 4); |
| 558 | skb_trim(skb, skb->len - 4); |
| 559 | stats.len -= 8; |
| 560 | } |
| 561 | wlhdr = (struct ieee80211_hdr_4addr *)(skb->data); |
| 562 | } |
| 563 | |
| 564 | switch (WLAN_FC_GET_TYPE(frame_ctl)) { |
| 565 | case IEEE80211_FTYPE_MGMT: |
| 566 | ieee80211_rx_mgt(bcm->ieee, wlhdr, &stats); |
| 567 | break; |
| 568 | case IEEE80211_FTYPE_DATA: |
| 569 | if (is_packet_for_us) { |
| 570 | err = ieee80211_rx(bcm->ieee, skb, &stats); |
| 571 | err = (err == 0) ? -EINVAL : 0; |
| 572 | } |
| 573 | break; |
| 574 | case IEEE80211_FTYPE_CTL: |
| 575 | break; |
| 576 | default: |
| 577 | assert(0); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | return err; |
| 582 | } |