1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org> 6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 23 24 #include "opt_wlan.h" 25 26 #include <sys/param.h> 27 #include <sys/lock.h> 28 #include <sys/mutex.h> 29 #include <sys/mbuf.h> 30 #include <sys/kernel.h> 31 #include <sys/socket.h> 32 #include <sys/systm.h> 33 #include <sys/malloc.h> 34 #include <sys/queue.h> 35 #include <sys/taskqueue.h> 36 #include <sys/bus.h> 37 #include <sys/endian.h> 38 #include <sys/linker.h> 39 40 #include <net/if.h> 41 #include <net/ethernet.h> 42 #include <net/if_media.h> 43 44 #include <net80211/ieee80211_var.h> 45 #include <net80211/ieee80211_radiotap.h> 46 47 #include <dev/rtwn/if_rtwnreg.h> 48 #include <dev/rtwn/if_rtwnvar.h> 49 #include <dev/rtwn/if_rtwn_ridx.h> 50 51 #include <dev/rtwn/rtl8192c/r92c.h> 52 #include <dev/rtwn/rtl8192c/r92c_rx_desc.h> 53 54 55 int8_t 56 r92c_get_rssi_cck(struct rtwn_softc *sc, void *physt) 57 { 58 static const int8_t cckoff[] = { 16, -12, -26, -46 }; 59 struct r92c_rx_cck *cck = (struct r92c_rx_cck *)physt; 60 uint8_t rpt; 61 int8_t rssi; 62 63 if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR) { 64 rpt = (cck->agc_rpt >> 5) & 0x03; 65 rssi = (cck->agc_rpt & 0x1f) << 1; 66 } else { 67 rpt = (cck->agc_rpt >> 6) & 0x03; 68 rssi = cck->agc_rpt & 0x3e; 69 } 70 rssi = cckoff[rpt] - rssi; 71 72 return (rssi); 73 } 74 75 int8_t 76 r92c_get_rssi_ofdm(struct rtwn_softc *sc, void *physt) 77 { 78 struct r92c_rx_phystat *phy = (struct r92c_rx_phystat *)physt; 79 int rssi; 80 81 /* Get average RSSI. */ 82 rssi = ((phy->pwdb_all >> 1) & 0x7f) - 110; 83 84 return (rssi); 85 } 86 87 uint8_t 88 r92c_rx_radiotap_flags(const void *buf) 89 { 90 const struct r92c_rx_stat *stat = buf; 91 uint8_t flags, rate; 92 93 if (!(stat->rxdw3 & htole32(R92C_RXDW3_SPLCP))) 94 return (0); 95 96 rate = MS(le32toh(stat->rxdw3), R92C_RXDW3_RATE); 97 if (RTWN_RATE_IS_CCK(rate)) 98 flags = IEEE80211_RADIOTAP_F_SHORTPRE; 99 else 100 flags = IEEE80211_RADIOTAP_F_SHORTGI; 101 return (flags); 102 } 103 104 void 105 r92c_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs, 106 const void *desc, const void *physt_ptr) 107 { 108 const struct r92c_rx_stat *stat = desc; 109 uint32_t rxdw1, rxdw3; 110 uint8_t rate; 111 112 rxdw1 = le32toh(stat->rxdw1); 113 rxdw3 = le32toh(stat->rxdw3); 114 rate = MS(rxdw3, R92C_RXDW3_RATE); 115 116 if (rxdw1 & R92C_RXDW1_AMPDU) 117 rxs->c_pktflags |= IEEE80211_RX_F_AMPDU; 118 else if (rxdw1 & R92C_RXDW1_AMPDU_MORE) 119 rxs->c_pktflags |= IEEE80211_RX_F_AMPDU_MORE; 120 if ((rxdw3 & R92C_RXDW3_SPLCP) && rate >= RTWN_RIDX_HT_MCS(0)) 121 rxs->c_pktflags |= IEEE80211_RX_F_SHORTGI; 122 123 if (rxdw3 & R92C_RXDW3_HT40) 124 rxs->c_width = IEEE80211_RX_FW_40MHZ; 125 else 126 rxs->c_width = IEEE80211_RX_FW_20MHZ; 127 128 if (RTWN_RATE_IS_CCK(rate)) 129 rxs->c_phytype = IEEE80211_RX_FP_11B; 130 else if (rate < RTWN_RIDX_HT_MCS(0)) 131 rxs->c_phytype = IEEE80211_RX_FP_11G; 132 else 133 rxs->c_phytype = IEEE80211_RX_FP_11NG; 134 135 /* Map HW rate index to 802.11 rate. */ 136 if (rate < RTWN_RIDX_HT_MCS(0)) { 137 rxs->c_rate = ridx2rate[rate]; 138 if (RTWN_RATE_IS_CCK(rate)) 139 rxs->c_pktflags |= IEEE80211_RX_F_CCK; 140 else 141 rxs->c_pktflags |= IEEE80211_RX_F_OFDM; 142 } else { /* MCS0~15. */ 143 rxs->c_rate = 144 IEEE80211_RATE_MCS | (rate - RTWN_RIDX_HT_MCS_SHIFT); 145 rxs->c_pktflags |= IEEE80211_RX_F_HT; 146 } 147 } 148