1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
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>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.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>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
41 #include <net/ethernet.h>
42 #include <net/if_media.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
47 #include <dev/rtwn/if_rtwnreg.h>
48 #include <dev/rtwn/if_rtwnvar.h>
49 #include <dev/rtwn/if_rtwn_ridx.h>
51 #include <dev/rtwn/rtl8192c/r92c.h>
52 #include <dev/rtwn/rtl8192c/r92c_rx_desc.h>
56 r92c_get_rssi_cck(struct rtwn_softc *sc, void *physt)
58 static const int8_t cckoff[] = { 16, -12, -26, -46 };
59 struct r92c_rx_cck *cck = (struct r92c_rx_cck *)physt;
63 if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR) {
64 rpt = (cck->agc_rpt >> 5) & 0x03;
65 rssi = (cck->agc_rpt & 0x1f) << 1;
67 rpt = (cck->agc_rpt >> 6) & 0x03;
68 rssi = cck->agc_rpt & 0x3e;
70 rssi = cckoff[rpt] - rssi;
76 r92c_get_rssi_ofdm(struct rtwn_softc *sc, void *physt)
78 struct r92c_rx_phystat *phy = (struct r92c_rx_phystat *)physt;
81 /* Get average RSSI. */
82 rssi = ((phy->pwdb_all >> 1) & 0x7f) - 110;
88 r92c_rx_radiotap_flags(const void *buf)
90 const struct r92c_rx_stat *stat = buf;
93 if (!(stat->rxdw3 & htole32(R92C_RXDW3_SPLCP)))
96 rate = MS(le32toh(stat->rxdw3), R92C_RXDW3_RATE);
97 if (RTWN_RATE_IS_CCK(rate))
98 flags = IEEE80211_RADIOTAP_F_SHORTPRE;
100 flags = IEEE80211_RADIOTAP_F_SHORTGI;
105 r92c_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs,
106 const void *desc, const void *physt_ptr)
108 const struct r92c_rx_stat *stat = desc;
109 uint32_t rxdw1, rxdw3;
112 rxdw1 = le32toh(stat->rxdw1);
113 rxdw3 = le32toh(stat->rxdw3);
114 rate = MS(rxdw3, R92C_RXDW3_RATE);
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;
123 if (rxdw3 & R92C_RXDW3_HT40)
124 rxs->c_width = IEEE80211_RX_FW_40MHZ;
126 rxs->c_width = IEEE80211_RX_FW_20MHZ;
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;
133 rxs->c_phytype = IEEE80211_RX_FP_11NG;
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;
141 rxs->c_pktflags |= IEEE80211_RX_F_OFDM;
142 } else { /* MCS0~15. */
143 rxs->c_rate = IEEE80211_RATE_MCS | (rate - 12);
144 rxs->c_pktflags |= IEEE80211_RX_F_HT;