summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/rtwn/if_rtwn_beacon.c
blob: e53801541cdbe805cdb520d096ee4517c6f35f92 (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
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

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

#include <sys/param.h>
#include <rtems/bsd/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 <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_beacon.h>
#include <dev/rtwn/if_rtwn_debug.h>
#include <dev/rtwn/if_rtwn_tx.h>

#include <dev/rtwn/rtl8192c/r92c_reg.h>


static void
rtwn_reset_beacon_valid(struct rtwn_softc *sc, int id)
{

	KASSERT (id == 0 || id == 1, ("wrong port id %d\n", id));

	/* XXX cannot be cleared on RTL8188CE */
	rtwn_setbits_1_shift(sc, sc->bcn_status_reg[id],
	    R92C_TDECTRL_BCN_VALID, 0, 2);

	RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
	    "%s: 'beacon valid' bit for vap %d was unset\n",
	    __func__, id);
}

static int
rtwn_check_beacon_valid(struct rtwn_softc *sc, int id)
{
	uint16_t reg;
	int ntries;

	if (id == RTWN_VAP_ID_INVALID)
		return (0);

	reg = sc->bcn_status_reg[id];
	for (ntries = 0; ntries < 10; ntries++) {
		if (rtwn_read_4(sc, reg) & R92C_TDECTRL_BCN_VALID) {
			RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
			    "%s: beacon for vap %d was recognized\n",
			    __func__, id);
			break;
		}
		rtwn_delay(sc, sc->bcn_check_interval);
	}
	if (ntries == 10)
		return (ETIMEDOUT);

	return (0);
}

void
rtwn_switch_bcnq(struct rtwn_softc *sc, int id)
{

	if (sc->cur_bcnq_id != id) {
		/* Wait until any previous transmit completes. */
		(void) rtwn_check_beacon_valid(sc, sc->cur_bcnq_id);

		/* Change current port. */
		rtwn_beacon_select(sc, id);
		sc->cur_bcnq_id = id;
	}

	/* Reset 'beacon valid' bit. */
	rtwn_reset_beacon_valid(sc, id);
}

int
rtwn_setup_beacon(struct rtwn_softc *sc, struct ieee80211_node *ni)
{
	struct ieee80211vap *vap = ni->ni_vap;
	struct rtwn_vap *uvp = RTWN_VAP(vap);
	struct mbuf *m;

	RTWN_ASSERT_LOCKED(sc);

	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
		return (EINVAL);

	m = ieee80211_beacon_alloc(ni);
	if (m == NULL) {
		device_printf(sc->sc_dev,
		    "%s: could not allocate beacon frame\n", __func__);
		return (ENOMEM);
	}

	if (uvp->bcn_mbuf != NULL) {
		rtwn_beacon_unload(sc, uvp->id);
		m_freem(uvp->bcn_mbuf);
	}

	uvp->bcn_mbuf = m;

	rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0],
	    IEEE80211_IS_CHAN_5GHZ(ni->ni_chan));

	return (rtwn_tx_beacon_check(sc, uvp));
}

/*
 * Push a beacon frame into the chip. Beacon will
 * be repeated by the chip every R92C_BCN_INTERVAL.
 */
static int
rtwn_tx_beacon(struct rtwn_softc *sc, struct rtwn_vap *uvp)
{
	int error;

	RTWN_ASSERT_LOCKED(sc);

	RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
	    "%s: sending beacon for vap %d\n", __func__, uvp->id);

	error = rtwn_tx_start(sc, NULL, uvp->bcn_mbuf, &uvp->bcn_desc.txd[0],
	    IEEE80211_FC0_TYPE_MGT, uvp->id);

	return (error);
}

void
rtwn_update_beacon(struct ieee80211vap *vap, int item)
{
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_softc *sc = ic->ic_softc;
	struct rtwn_vap *uvp = RTWN_VAP(vap);
	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
	struct ieee80211_node *ni = vap->iv_bss;
	int mcast = 0;

	RTWN_LOCK(sc);
	if (uvp->bcn_mbuf == NULL) {
		uvp->bcn_mbuf = ieee80211_beacon_alloc(ni);
		if (uvp->bcn_mbuf == NULL) {
			device_printf(sc->sc_dev,
			    "%s: could not allocate beacon frame\n", __func__);
			RTWN_UNLOCK(sc);
			return;
		}
	}

	RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
	    "%s: vap id %d, iv_csa_count %d, ic_csa_count %d, item %d\n",
	    __func__, uvp->id, vap->iv_csa_count, ic->ic_csa_count, item);

	switch (item) {
	case IEEE80211_BEACON_CSA:
		if (vap->iv_csa_count != ic->ic_csa_count) {
			/*
			 * XXX two APs with different beacon intervals
			 * are not handled properly.
			 */
			/* XXX check TBTT? */
			taskqueue_enqueue_timeout(taskqueue_thread,
			    &uvp->tx_beacon_csa,
			    msecs_to_ticks(ni->ni_intval));
		}
		break;
	case IEEE80211_BEACON_TIM:
		mcast = 1;	/* XXX */
		break;
	default:
		break;
	}

	setbit(bo->bo_flags, item);

	rtwn_beacon_update_begin(sc, vap);
	RTWN_UNLOCK(sc);

	ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast);

	/* XXX clear manually */
	clrbit(bo->bo_flags, IEEE80211_BEACON_CSA);

	RTWN_LOCK(sc);
	rtwn_tx_beacon(sc, uvp);
	rtwn_beacon_update_end(sc, vap);
	RTWN_UNLOCK(sc);
}

void
rtwn_tx_beacon_csa(void *arg, int npending __unused)
{
	struct ieee80211vap *vap = arg;
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_softc *sc = ic->ic_softc;
	struct rtwn_vap *rvp = RTWN_VAP(vap);

	KASSERT (rvp->id == 0 || rvp->id == 1,
	    ("wrong port id %d\n", rvp->id));

	IEEE80211_LOCK(ic);
	if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
		RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
		    "%s: vap id %d, iv_csa_count %d, ic_csa_count %d\n",
		    __func__, rvp->id, vap->iv_csa_count, ic->ic_csa_count);

		rtwn_update_beacon(vap, IEEE80211_BEACON_CSA);
	}
	IEEE80211_UNLOCK(ic);

	(void) rvp;
}

int
rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp)
{
	int ntries, error;

	for (ntries = 0; ntries < 5; ntries++) {
		rtwn_reset_beacon_valid(sc, uvp->id);

		error = rtwn_tx_beacon(sc, uvp);
		if (error != 0)
			continue;

		error = rtwn_check_beacon_valid(sc, uvp->id);
		if (error == 0)
			break;
	}
	if (ntries == 5) {
		device_printf(sc->sc_dev,
		    "%s: cannot push beacon into chip, error %d!\n",
		    __func__, error);
		return (error);
	}

	return (0);
}