xref: /freebsd/sys/dev/rtwn/rtl8192c/r92c_rx.c (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
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 int
56 r92c_classify_intr(struct rtwn_softc *sc, void *buf, int len)
57 {
58 	/* NB: reports are fetched from C2H_MSG register. */
59 	return (RTWN_RX_DATA);
60 }
61 
62 int8_t
63 r92c_get_rssi_cck(struct rtwn_softc *sc, void *physt)
64 {
65 	static const int8_t cckoff[] = { 16, -12, -26, -46 };
66 	struct r92c_rx_cck *cck = (struct r92c_rx_cck *)physt;
67 	uint8_t rpt;
68 	int8_t rssi;
69 
70 	if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR) {
71 		rpt = (cck->agc_rpt >> 5) & 0x03;
72 		rssi = (cck->agc_rpt & 0x1f) << 1;
73 	} else {
74 		rpt = (cck->agc_rpt >> 6) & 0x03;
75 		rssi = cck->agc_rpt & 0x3e;
76 	}
77 	rssi = cckoff[rpt] - rssi;
78 
79 	return (rssi);
80 }
81 
82 int8_t
83 r92c_get_rssi_ofdm(struct rtwn_softc *sc, void *physt)
84 {
85 	struct r92c_rx_phystat *phy = (struct r92c_rx_phystat *)physt;
86 	int rssi;
87 
88 	/* Get average RSSI. */
89 	rssi = ((phy->pwdb_all >> 1) & 0x7f) - 110;
90 
91 	return (rssi);
92 }
93 
94 uint8_t
95 r92c_rx_radiotap_flags(const void *buf)
96 {
97 	const struct r92c_rx_stat *stat = buf;
98 	uint8_t flags, rate;
99 
100 	if (!(stat->rxdw3 & htole32(R92C_RXDW3_SPLCP)))
101 		return (0);
102 
103 	rate = MS(le32toh(stat->rxdw3), R92C_RXDW3_RATE);
104 	if (RTWN_RATE_IS_CCK(rate))
105 		flags = IEEE80211_RADIOTAP_F_SHORTPRE;
106 	else
107 		flags = IEEE80211_RADIOTAP_F_SHORTGI;
108 	return (flags);
109 }
110 
111 void
112 r92c_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs,
113     const void *desc, const void *physt_ptr)
114 {
115 	const struct r92c_rx_stat *stat = desc;
116 	uint32_t rxdw1, rxdw3;
117 	uint8_t rate;
118 
119 	rxdw1 = le32toh(stat->rxdw1);
120 	rxdw3 = le32toh(stat->rxdw3);
121 	rate = MS(rxdw3, R92C_RXDW3_RATE);
122 
123 	if (rxdw1 & R92C_RXDW1_AMPDU)
124 		rxs->c_pktflags |= IEEE80211_RX_F_AMPDU;
125 	else if (rxdw1 & R92C_RXDW1_AMPDU_MORE)
126 		rxs->c_pktflags |= IEEE80211_RX_F_AMPDU_MORE;
127 	if ((rxdw3 & R92C_RXDW3_SPLCP) && rate >= RTWN_RIDX_HT_MCS(0))
128 		rxs->c_pktflags |= IEEE80211_RX_F_SHORTGI;
129 
130 	if (rxdw3 & R92C_RXDW3_HT40)
131 		rxs->c_width = IEEE80211_RX_FW_40MHZ;
132 	else
133 		rxs->c_width = IEEE80211_RX_FW_20MHZ;
134 
135 	if (RTWN_RATE_IS_CCK(rate))
136 		rxs->c_phytype = IEEE80211_RX_FP_11B;
137 	else if (rate < RTWN_RIDX_HT_MCS(0))
138 		rxs->c_phytype = IEEE80211_RX_FP_11G;
139 	else
140 		rxs->c_phytype = IEEE80211_RX_FP_11NG;
141 
142 	/* Map HW rate index to 802.11 rate. */
143 	if (rate < RTWN_RIDX_HT_MCS(0)) {
144 		rxs->c_rate = ridx2rate[rate];
145 		if (RTWN_RATE_IS_CCK(rate))
146 			rxs->c_pktflags |= IEEE80211_RX_F_CCK;
147 		else
148 			rxs->c_pktflags |= IEEE80211_RX_F_OFDM;
149 	} else {	/* MCS0~15. */
150 		rxs->c_rate =
151 		    IEEE80211_RATE_MCS | (rate - RTWN_RIDX_HT_MCS_SHIFT);
152 		rxs->c_pktflags |= IEEE80211_RX_F_HT;
153 	}
154 }
155