xref: /freebsd/sys/dev/rtwn/rtl8192c/r92c_rx.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 #include "opt_wlan.h"
23 
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/linker.h>
37 
38 #include <net/if.h>
39 #include <net/ethernet.h>
40 #include <net/if_media.h>
41 
42 #include <net80211/ieee80211_var.h>
43 #include <net80211/ieee80211_radiotap.h>
44 
45 #include <dev/rtwn/if_rtwnreg.h>
46 #include <dev/rtwn/if_rtwnvar.h>
47 #include <dev/rtwn/if_rtwn_ridx.h>
48 
49 #include <dev/rtwn/rtl8192c/r92c.h>
50 #include <dev/rtwn/rtl8192c/r92c_rx_desc.h>
51 
52 int
53 r92c_classify_intr(struct rtwn_softc *sc, void *buf, int len)
54 {
55 	/* NB: reports are fetched from C2H_MSG register. */
56 	return (RTWN_RX_DATA);
57 }
58 
59 int8_t
60 r92c_get_rssi_cck(struct rtwn_softc *sc, void *physt)
61 {
62 	static const int8_t cckoff[] = { 16, -12, -26, -46 };
63 	struct r92c_rx_cck *cck = (struct r92c_rx_cck *)physt;
64 	uint8_t rpt;
65 	int8_t rssi;
66 
67 	if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR) {
68 		rpt = (cck->agc_rpt >> 5) & 0x03;
69 		rssi = (cck->agc_rpt & 0x1f) << 1;
70 	} else {
71 		rpt = (cck->agc_rpt >> 6) & 0x03;
72 		rssi = cck->agc_rpt & 0x3e;
73 	}
74 	rssi = cckoff[rpt] - rssi;
75 
76 	return (rssi);
77 }
78 
79 int8_t
80 r92c_get_rssi_ofdm(struct rtwn_softc *sc, void *physt)
81 {
82 	struct r92c_rx_phystat *phy = (struct r92c_rx_phystat *)physt;
83 	int rssi;
84 
85 	/* Get average RSSI. */
86 	rssi = ((phy->pwdb_all >> 1) & 0x7f) - 110;
87 
88 	return (rssi);
89 }
90 
91 uint8_t
92 r92c_rx_radiotap_flags(const void *buf)
93 {
94 	const struct r92c_rx_stat *stat = buf;
95 	uint8_t flags, rate;
96 
97 	if (!(stat->rxdw3 & htole32(R92C_RXDW3_SPLCP)))
98 		return (0);
99 
100 	rate = MS(le32toh(stat->rxdw3), R92C_RXDW3_RATE);
101 	if (RTWN_RATE_IS_CCK(rate))
102 		flags = IEEE80211_RADIOTAP_F_SHORTPRE;
103 	else
104 		flags = IEEE80211_RADIOTAP_F_SHORTGI;
105 	return (flags);
106 }
107 
108 void
109 r92c_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs,
110     const void *desc, const void *physt_ptr)
111 {
112 	const struct r92c_rx_stat *stat = desc;
113 	uint32_t rxdw1, rxdw3;
114 	uint8_t rate;
115 
116 	rxdw1 = le32toh(stat->rxdw1);
117 	rxdw3 = le32toh(stat->rxdw3);
118 	rate = MS(rxdw3, R92C_RXDW3_RATE);
119 
120 	if (rxdw1 & R92C_RXDW1_AMPDU)
121 		rxs->c_pktflags |= IEEE80211_RX_F_AMPDU;
122 	else if (rxdw1 & R92C_RXDW1_AMPDU_MORE)
123 		rxs->c_pktflags |= IEEE80211_RX_F_AMPDU_MORE;
124 	if ((rxdw3 & R92C_RXDW3_SPLCP) && rate >= RTWN_RIDX_HT_MCS(0))
125 		rxs->c_pktflags |= IEEE80211_RX_F_SHORTGI;
126 
127 	if (rxdw3 & R92C_RXDW3_HT40)
128 		rxs->c_width = IEEE80211_RX_FW_40MHZ;
129 	else
130 		rxs->c_width = IEEE80211_RX_FW_20MHZ;
131 
132 	if (RTWN_RATE_IS_CCK(rate))
133 		rxs->c_phytype = IEEE80211_RX_FP_11B;
134 	else if (rate < RTWN_RIDX_HT_MCS(0))
135 		rxs->c_phytype = IEEE80211_RX_FP_11G;
136 	else
137 		rxs->c_phytype = IEEE80211_RX_FP_11NG;
138 
139 	/* Map HW rate index to 802.11 rate. */
140 	if (rate < RTWN_RIDX_HT_MCS(0)) {
141 		rxs->c_rate = ridx2rate[rate];
142 		if (RTWN_RATE_IS_CCK(rate))
143 			rxs->c_pktflags |= IEEE80211_RX_F_CCK;
144 		else
145 			rxs->c_pktflags |= IEEE80211_RX_F_OFDM;
146 	} else {	/* MCS0~15. */
147 		rxs->c_rate =
148 		    IEEE80211_RATE_MCS | (rate - RTWN_RIDX_HT_MCS_SHIFT);
149 		rxs->c_pktflags |= IEEE80211_RX_F_HT;
150 	}
151 }
152