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