]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/rtwn/if_rtwn_beacon.c
Update llvm, clang, lld and lldb to release_39 branch r288513.
[FreeBSD/FreeBSD.git] / sys / dev / rtwn / if_rtwn_beacon.c
1 /*-
2  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
3  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
4  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
21
22 #include <sys/param.h>
23 #include <sys/lock.h>
24 #include <sys/mutex.h>
25 #include <sys/mbuf.h>
26 #include <sys/kernel.h>
27 #include <sys/socket.h>
28 #include <sys/systm.h>
29 #include <sys/malloc.h>
30 #include <sys/queue.h>
31 #include <sys/taskqueue.h>
32 #include <sys/bus.h>
33 #include <sys/endian.h>
34
35 #include <net/if.h>
36 #include <net/ethernet.h>
37 #include <net/if_media.h>
38
39 #include <net80211/ieee80211_var.h>
40 #include <net80211/ieee80211_radiotap.h>
41
42 #include <dev/rtwn/if_rtwnvar.h>
43
44 #include <dev/rtwn/if_rtwn_beacon.h>
45 #include <dev/rtwn/if_rtwn_debug.h>
46 #include <dev/rtwn/if_rtwn_tx.h>
47
48 #include <dev/rtwn/rtl8192c/r92c_reg.h>
49
50
51 static void
52 rtwn_reset_beacon_valid(struct rtwn_softc *sc, int id)
53 {
54
55         KASSERT (id == 0 || id == 1, ("wrong port id %d\n", id));
56
57         /* XXX cannot be cleared on RTL8188CE */
58         rtwn_setbits_1_shift(sc, sc->bcn_status_reg[id],
59             R92C_TDECTRL_BCN_VALID, 0, 2);
60
61         RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
62             "%s: 'beacon valid' bit for vap %d was unset\n",
63             __func__, id);
64 }
65
66 static int
67 rtwn_check_beacon_valid(struct rtwn_softc *sc, int id)
68 {
69         uint16_t reg;
70         int ntries;
71
72         if (id == RTWN_VAP_ID_INVALID)
73                 return (0);
74
75         reg = sc->bcn_status_reg[id];
76         for (ntries = 0; ntries < 10; ntries++) {
77                 if (rtwn_read_4(sc, reg) & R92C_TDECTRL_BCN_VALID) {
78                         RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
79                             "%s: beacon for vap %d was recognized\n",
80                             __func__, id);
81                         break;
82                 }
83                 rtwn_delay(sc, sc->bcn_check_interval);
84         }
85         if (ntries == 10)
86                 return (ETIMEDOUT);
87
88         return (0);
89 }
90
91 void
92 rtwn_switch_bcnq(struct rtwn_softc *sc, int id)
93 {
94
95         if (sc->cur_bcnq_id != id) {
96                 /* Wait until any previous transmit completes. */
97                 (void) rtwn_check_beacon_valid(sc, sc->cur_bcnq_id);
98
99                 /* Change current port. */
100                 rtwn_beacon_select(sc, id);
101                 sc->cur_bcnq_id = id;
102         }
103
104         /* Reset 'beacon valid' bit. */
105         rtwn_reset_beacon_valid(sc, id);
106 }
107
108 int
109 rtwn_setup_beacon(struct rtwn_softc *sc, struct ieee80211_node *ni)
110 {
111         struct ieee80211vap *vap = ni->ni_vap;
112         struct rtwn_vap *uvp = RTWN_VAP(vap);
113         struct mbuf *m;
114
115         RTWN_ASSERT_LOCKED(sc);
116
117         if (ni->ni_chan == IEEE80211_CHAN_ANYC)
118                 return (EINVAL);
119
120         m = ieee80211_beacon_alloc(ni);
121         if (m == NULL) {
122                 device_printf(sc->sc_dev,
123                     "%s: could not allocate beacon frame\n", __func__);
124                 return (ENOMEM);
125         }
126
127         if (uvp->bcn_mbuf != NULL) {
128                 rtwn_beacon_unload(sc, uvp->id);
129                 m_freem(uvp->bcn_mbuf);
130         }
131
132         uvp->bcn_mbuf = m;
133
134         rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0],
135             IEEE80211_IS_CHAN_5GHZ(ni->ni_chan));
136
137         return (rtwn_tx_beacon_check(sc, uvp));
138 }
139
140 /*
141  * Push a beacon frame into the chip. Beacon will
142  * be repeated by the chip every R92C_BCN_INTERVAL.
143  */
144 static int
145 rtwn_tx_beacon(struct rtwn_softc *sc, struct rtwn_vap *uvp)
146 {
147         int error;
148
149         RTWN_ASSERT_LOCKED(sc);
150
151         RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
152             "%s: sending beacon for vap %d\n", __func__, uvp->id);
153
154         error = rtwn_tx_start(sc, NULL, uvp->bcn_mbuf, &uvp->bcn_desc.txd[0],
155             IEEE80211_FC0_TYPE_MGT, uvp->id);
156
157         return (error);
158 }
159
160 void
161 rtwn_update_beacon(struct ieee80211vap *vap, int item)
162 {
163         struct rtwn_softc *sc = vap->iv_ic->ic_softc;
164         struct rtwn_vap *uvp = RTWN_VAP(vap);
165         struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
166         struct ieee80211_node *ni = vap->iv_bss;
167         int mcast = 0;
168
169         RTWN_LOCK(sc);
170         if (uvp->bcn_mbuf == NULL) {
171                 uvp->bcn_mbuf = ieee80211_beacon_alloc(ni);
172                 if (uvp->bcn_mbuf == NULL) {
173                         device_printf(sc->sc_dev,
174                             "%s: could not allocate beacon frame\n", __func__);
175                         RTWN_UNLOCK(sc);
176                         return;
177                 }
178         }
179         rtwn_beacon_update_begin(sc, vap);
180         RTWN_UNLOCK(sc);
181
182         if (item == IEEE80211_BEACON_TIM)
183                 mcast = 1;      /* XXX */
184
185         setbit(bo->bo_flags, item);
186         ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast);
187
188         RTWN_LOCK(sc);
189         rtwn_tx_beacon(sc, uvp);
190         rtwn_beacon_update_end(sc, vap);
191         RTWN_UNLOCK(sc);
192 }
193
194 int
195 rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp)
196 {
197         int ntries, error;
198
199         for (ntries = 0; ntries < 5; ntries++) {
200                 rtwn_reset_beacon_valid(sc, uvp->id);
201
202                 error = rtwn_tx_beacon(sc, uvp);
203                 if (error != 0)
204                         continue;
205
206                 error = rtwn_check_beacon_valid(sc, uvp->id);
207                 if (error == 0)
208                         break;
209         }
210         if (ntries == 5) {
211                 device_printf(sc->sc_dev,
212                     "%s: cannot push beacon into chip, error %d!\n",
213                     __func__, error);
214                 return (error);
215         }
216
217         return (0);
218 }