summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/rtwn/rtl8821a/usb/r21au_dfs.c
blob: bf79fc885c33a45960d83f0334543b886b0da2d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <rtems/bsd/local/opt_wlan.h>

#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/taskqueue.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/linker.h>

#include <net/if.h>
#include <net/ethernet.h>
#include <net/if_media.h>

#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_radiotap.h>

#include <dev/rtwn/if_rtwnvar.h>
#include <dev/rtwn/if_rtwn_debug.h>

#include <dev/rtwn/usb/rtwn_usb_var.h>

#include <dev/rtwn/rtl8812a/r12a_var.h>

#include <dev/rtwn/rtl8821a/usb/r21au.h>
#include <dev/rtwn/rtl8821a/usb/r21au_reg.h>


#define R21AU_RADAR_CHECK_PERIOD	(2 * hz)

static void
r21au_dfs_radar_disable(struct rtwn_softc *sc)
{
	rtwn_bb_setbits(sc, 0x924, 0x00008000, 0);
}

static int
r21au_dfs_radar_is_enabled(struct rtwn_softc *sc)
{
	return !!(rtwn_bb_read(sc, 0x924) & 0x00008000);
}

static int
r21au_dfs_radar_reset(struct rtwn_softc *sc)
{
	int error;

	error = rtwn_bb_setbits(sc, 0x924, 0x00008000, 0);
	if (error != 0)
		return (error);

	return (rtwn_bb_setbits(sc, 0x924, 0, 0x00008000));
}

static int
r21au_dfs_radar_enable(struct rtwn_softc *sc)
{
#define RTWN_CHK(res) do {	\
	if (res != 0)		\
		return (EIO);	\
} while(0)

	RTWN_ASSERT_LOCKED(sc);

	RTWN_CHK(rtwn_bb_setbits(sc, 0x814, 0x3fffffff, 0x04cc4d10));
	RTWN_CHK(rtwn_bb_setbits(sc, R12A_BW_INDICATION, 0xff, 0x06));
	RTWN_CHK(rtwn_bb_write(sc, 0x918, 0x1c16ecdf));
	RTWN_CHK(rtwn_bb_write(sc, 0x924, 0x0152a400));
	RTWN_CHK(rtwn_bb_write(sc, 0x91c, 0x0fa21a20));
	RTWN_CHK(rtwn_bb_write(sc, 0x920, 0xe0f57204));

	return (r21au_dfs_radar_reset(sc));

#undef RTWN_CHK
}

static int
r21au_dfs_radar_is_detected(struct rtwn_softc *sc)
{
	return !!(rtwn_bb_read(sc, 0xf98) & 0x00020000);
}

void
r21au_chan_check(void *arg, int npending __unused)
{
	struct rtwn_softc *sc = arg;
	struct r12a_softc *rs = sc->sc_priv;
	struct ieee80211com *ic = &sc->sc_ic;

	RTWN_LOCK(sc);
#ifdef DIAGNOSTIC
	RTWN_DPRINTF(sc, RTWN_DEBUG_STATE,
	    "%s: periodical radar detection task\n", __func__);
#endif

	if (!r21au_dfs_radar_is_enabled(sc)) {
		if (rs->rs_flags & R12A_RADAR_ENABLED) {
			/* should not happen */
			device_printf(sc->sc_dev,
			    "%s: radar detection was turned off "
			    "unexpectedly, resetting...\n", __func__);

			/* XXX something more appropriate? */
			ieee80211_restart_all(ic);
		}
		RTWN_UNLOCK(sc);
		return;
	}

	if (r21au_dfs_radar_is_detected(sc)) {
		r21au_dfs_radar_reset(sc);

		RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR, "%s: got radar event\n",
		    __func__);

		RTWN_UNLOCK(sc);
		IEEE80211_LOCK(ic);

		ieee80211_dfs_notify_radar(ic, ic->ic_curchan);

		IEEE80211_UNLOCK(ic);
		RTWN_LOCK(sc);
	}

	if (rs->rs_flags & R12A_RADAR_ENABLED) {
		taskqueue_enqueue_timeout(taskqueue_thread,
		    &rs->rs_chan_check, R21AU_RADAR_CHECK_PERIOD);
	}
	RTWN_UNLOCK(sc);
}

int
r21au_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
{
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_softc *sc = ic->ic_softc;
	struct rtwn_vap *rvp = RTWN_VAP(vap);
	struct r12a_softc *rs = sc->sc_priv;
	int error;

	KASSERT(rvp->id == 0 || rvp->id == 1,
	    ("%s: unexpected vap id %d\n", __func__, rvp->id));

	IEEE80211_UNLOCK(ic);
	RTWN_LOCK(sc);

	error = 0;
	if (nstate == IEEE80211_S_CAC &&
	    !(rs->rs_flags & R12A_RADAR_ENABLED)) {
		error = r21au_dfs_radar_enable(sc);
		if (error != 0) {
			device_printf(sc->sc_dev,
			    "%s: cannot enable radar detection\n", __func__);
			goto fail;
		}
		rs->rs_flags |= R12A_RADAR_ENABLED;

		RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR,
		    "%s: radar detection was enabled\n", __func__);

		taskqueue_enqueue_timeout(taskqueue_thread,
		    &rs->rs_chan_check, R21AU_RADAR_CHECK_PERIOD);
	}

	if ((nstate < IEEE80211_S_CAC || nstate == IEEE80211_S_CSA) &&
	    (rs->rs_flags & R12A_RADAR_ENABLED) &&
	    (sc->vaps[!rvp->id] == NULL ||
	    sc->vaps[!rvp->id]->vap.iv_state < IEEE80211_S_CAC ||
	    sc->vaps[!rvp->id]->vap.iv_state == IEEE80211_S_CSA)) {
		taskqueue_cancel_timeout(taskqueue_thread, &rs->rs_chan_check,
		    NULL);

		rs->rs_flags &= ~R12A_RADAR_ENABLED;
		r21au_dfs_radar_disable(sc);

		RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR,
		    "%s: radar detection was disabled\n", __func__);
	}

fail:
	RTWN_UNLOCK(sc);
	IEEE80211_LOCK(ic);

	if (error != 0)
		return (error);

	return (rs->rs_newstate[rvp->id](vap, nstate, arg));
}

void
r21au_scan_start(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;
	struct r12a_softc *rs = sc->sc_priv;

	RTWN_LOCK(sc);
	if (rs->rs_flags & R12A_RADAR_ENABLED) {
		RTWN_UNLOCK(sc);
		while (taskqueue_cancel_timeout(taskqueue_thread,
		    &rs->rs_chan_check, NULL) != 0) {
			taskqueue_drain_timeout(taskqueue_thread,
			    &rs->rs_chan_check);
		}
		RTWN_LOCK(sc);

		r21au_dfs_radar_disable(sc);
		RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR,
		    "%s: radar detection was (temporarily) disabled\n",
		    __func__);
	}
	RTWN_UNLOCK(sc);

	rs->rs_scan_start(ic);
}

void
r21au_scan_end(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;
	struct r12a_softc *rs = sc->sc_priv;
	int error;

	RTWN_LOCK(sc);
	if (rs->rs_flags & R12A_RADAR_ENABLED) {
		error = r21au_dfs_radar_enable(sc);
		if (error != 0) {
			device_printf(sc->sc_dev,
			    "%s: cannot re-enable radar detection\n",
			    __func__);

			/* XXX */
			ieee80211_restart_all(ic);
			RTWN_UNLOCK(sc);
			return;
		}
		RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR,
		    "%s: radar detection was re-enabled\n", __func__);

		taskqueue_enqueue_timeout(taskqueue_thread,
		    &rs->rs_chan_check, R21AU_RADAR_CHECK_PERIOD);
	}
	RTWN_UNLOCK(sc);

	rs->rs_scan_end(ic);
}