kernel/802_11: Fix panic when the desired ratectl algorithm is unavailable.
[dragonfly.git] / sys / bus / u4b / wlan / if_run.c
CommitLineData
12bd3c8b
SW
1/*-
2 * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr>
3 * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca>
4 * USB Consulting, Hans Petter Selasky <hselasky@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.
10f2cea7
SW
17 *
18 * $FreeBSD: src/sys/dev/usb/wlan/if_run.c,v 1.40 2013/03/19 02:17:34 svnexp Exp $
12bd3c8b
SW
19 */
20
12bd3c8b
SW
21/*-
22 * Ralink Technology RT2700U/RT2800U/RT3000U chipset driver.
23 * http://www.ralinktech.com/
24 */
25
26#include <sys/param.h>
27#include <sys/sockio.h>
28#include <sys/sysctl.h>
29#include <sys/lock.h>
12bd3c8b
SW
30#include <sys/mbuf.h>
31#include <sys/kernel.h>
32#include <sys/socket.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/module.h>
36#include <sys/bus.h>
37#include <sys/endian.h>
38#include <sys/linker.h>
39#include <sys/firmware.h>
12bd3c8b 40
12bd3c8b
SW
41#include <sys/rman.h>
42
43#include <net/bpf.h>
44#include <net/if.h>
45#include <net/if_arp.h>
46#include <net/ethernet.h>
47#include <net/if_dl.h>
48#include <net/if_media.h>
49#include <net/if_types.h>
10f2cea7 50#include <net/ifq_var.h>
12bd3c8b
SW
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/in_var.h>
55#include <netinet/if_ether.h>
56#include <netinet/ip.h>
57
10f2cea7
SW
58#include <netproto/802_11/ieee80211_var.h>
59#include <netproto/802_11/ieee80211_regdomain.h>
60#include <netproto/802_11/ieee80211_radiotap.h>
61#include <netproto/802_11/ieee80211_ratectl.h>
12bd3c8b 62
10f2cea7
SW
63#include <bus/u4b/usb.h>
64#include <bus/u4b/usbdi.h>
ef4aa9ff 65#include "usbdevs.h"
12bd3c8b
SW
66
67#define USB_DEBUG_VAR run_debug
10f2cea7 68#include <bus/u4b/usb_debug.h>
12bd3c8b 69
10f2cea7
SW
70#include <bus/u4b/wlan/if_runreg.h>
71#include <bus/u4b/wlan/if_runvar.h>
12bd3c8b
SW
72
73#ifdef USB_DEBUG
74#define RUN_DEBUG
75#endif
76
77#ifdef RUN_DEBUG
78int run_debug = 0;
79static SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run");
80SYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0,
81 "run debug level");
82#endif
83
84#define IEEE80211_HAS_ADDR4(wh) \
85 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
86
87/*
88 * Because of LOR in run_key_delete(), use atomic instead.
89 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
90 */
91#define RUN_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
92
93static const STRUCT_USB_HOST_ID run_devs[] = {
94#define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
95 RUN_DEV(ABOCOM, RT2770),
96 RUN_DEV(ABOCOM, RT2870),
97 RUN_DEV(ABOCOM, RT3070),
98 RUN_DEV(ABOCOM, RT3071),
99 RUN_DEV(ABOCOM, RT3072),
100 RUN_DEV(ABOCOM2, RT2870_1),
101 RUN_DEV(ACCTON, RT2770),
102 RUN_DEV(ACCTON, RT2870_1),
103 RUN_DEV(ACCTON, RT2870_2),
104 RUN_DEV(ACCTON, RT2870_3),
105 RUN_DEV(ACCTON, RT2870_4),
106 RUN_DEV(ACCTON, RT2870_5),
107 RUN_DEV(ACCTON, RT3070),
108 RUN_DEV(ACCTON, RT3070_1),
109 RUN_DEV(ACCTON, RT3070_2),
110 RUN_DEV(ACCTON, RT3070_3),
111 RUN_DEV(ACCTON, RT3070_4),
112 RUN_DEV(ACCTON, RT3070_5),
113 RUN_DEV(AIRTIES, RT3070),
114 RUN_DEV(ALLWIN, RT2070),
115 RUN_DEV(ALLWIN, RT2770),
116 RUN_DEV(ALLWIN, RT2870),
117 RUN_DEV(ALLWIN, RT3070),
118 RUN_DEV(ALLWIN, RT3071),
119 RUN_DEV(ALLWIN, RT3072),
120 RUN_DEV(ALLWIN, RT3572),
121 RUN_DEV(AMIGO, RT2870_1),
122 RUN_DEV(AMIGO, RT2870_2),
123 RUN_DEV(AMIT, CGWLUSB2GNR),
124 RUN_DEV(AMIT, RT2870_1),
125 RUN_DEV(AMIT2, RT2870),
126 RUN_DEV(ASUS, RT2870_1),
127 RUN_DEV(ASUS, RT2870_2),
128 RUN_DEV(ASUS, RT2870_3),
129 RUN_DEV(ASUS, RT2870_4),
130 RUN_DEV(ASUS, RT2870_5),
131 RUN_DEV(ASUS, USBN13),
132 RUN_DEV(ASUS, RT3070_1),
10f2cea7 133 RUN_DEV(ASUS, USB_N53),
12bd3c8b
SW
134 RUN_DEV(ASUS2, USBN11),
135 RUN_DEV(AZUREWAVE, RT2870_1),
136 RUN_DEV(AZUREWAVE, RT2870_2),
137 RUN_DEV(AZUREWAVE, RT3070_1),
138 RUN_DEV(AZUREWAVE, RT3070_2),
139 RUN_DEV(AZUREWAVE, RT3070_3),
140 RUN_DEV(BELKIN, F5D8053V3),
141 RUN_DEV(BELKIN, F5D8055),
142 RUN_DEV(BELKIN, F5D8055V2),
143 RUN_DEV(BELKIN, F6D4050V1),
144 RUN_DEV(BELKIN, RT2870_1),
145 RUN_DEV(BELKIN, RT2870_2),
146 RUN_DEV(CISCOLINKSYS, AE1000),
147 RUN_DEV(CISCOLINKSYS2, RT3070),
148 RUN_DEV(CISCOLINKSYS3, RT3070),
149 RUN_DEV(CONCEPTRONIC2, RT2870_1),
150 RUN_DEV(CONCEPTRONIC2, RT2870_2),
151 RUN_DEV(CONCEPTRONIC2, RT2870_3),
152 RUN_DEV(CONCEPTRONIC2, RT2870_4),
153 RUN_DEV(CONCEPTRONIC2, RT2870_5),
154 RUN_DEV(CONCEPTRONIC2, RT2870_6),
155 RUN_DEV(CONCEPTRONIC2, RT2870_7),
156 RUN_DEV(CONCEPTRONIC2, RT2870_8),
157 RUN_DEV(CONCEPTRONIC2, RT3070_1),
158 RUN_DEV(CONCEPTRONIC2, RT3070_2),
159 RUN_DEV(CONCEPTRONIC2, VIGORN61),
160 RUN_DEV(COREGA, CGWLUSB300GNM),
161 RUN_DEV(COREGA, RT2870_1),
162 RUN_DEV(COREGA, RT2870_2),
163 RUN_DEV(COREGA, RT2870_3),
164 RUN_DEV(COREGA, RT3070),
165 RUN_DEV(CYBERTAN, RT2870),
166 RUN_DEV(DLINK, RT2870),
167 RUN_DEV(DLINK, RT3072),
168 RUN_DEV(DLINK2, DWA130),
169 RUN_DEV(DLINK2, RT2870_1),
170 RUN_DEV(DLINK2, RT2870_2),
171 RUN_DEV(DLINK2, RT3070_1),
172 RUN_DEV(DLINK2, RT3070_2),
173 RUN_DEV(DLINK2, RT3070_3),
174 RUN_DEV(DLINK2, RT3070_4),
175 RUN_DEV(DLINK2, RT3070_5),
176 RUN_DEV(DLINK2, RT3072),
177 RUN_DEV(DLINK2, RT3072_1),
178 RUN_DEV(EDIMAX, EW7717),
179 RUN_DEV(EDIMAX, EW7718),
180 RUN_DEV(EDIMAX, RT2870_1),
181 RUN_DEV(ENCORE, RT3070_1),
182 RUN_DEV(ENCORE, RT3070_2),
183 RUN_DEV(ENCORE, RT3070_3),
184 RUN_DEV(GIGABYTE, GNWB31N),
185 RUN_DEV(GIGABYTE, GNWB32L),
186 RUN_DEV(GIGABYTE, RT2870_1),
187 RUN_DEV(GIGASET, RT3070_1),
188 RUN_DEV(GIGASET, RT3070_2),
189 RUN_DEV(GUILLEMOT, HWNU300),
190 RUN_DEV(HAWKING, HWUN2),
191 RUN_DEV(HAWKING, RT2870_1),
192 RUN_DEV(HAWKING, RT2870_2),
193 RUN_DEV(HAWKING, RT3070),
194 RUN_DEV(IODATA, RT3072_1),
195 RUN_DEV(IODATA, RT3072_2),
196 RUN_DEV(IODATA, RT3072_3),
197 RUN_DEV(IODATA, RT3072_4),
198 RUN_DEV(LINKSYS4, RT3070),
199 RUN_DEV(LINKSYS4, WUSB100),
200 RUN_DEV(LINKSYS4, WUSB54GCV3),
201 RUN_DEV(LINKSYS4, WUSB600N),
202 RUN_DEV(LINKSYS4, WUSB600NV2),
203 RUN_DEV(LOGITEC, RT2870_1),
204 RUN_DEV(LOGITEC, RT2870_2),
205 RUN_DEV(LOGITEC, RT2870_3),
206 RUN_DEV(LOGITEC, LANW300NU2),
10f2cea7
SW
207 RUN_DEV(LOGITEC, LANW150NU2),
208 RUN_DEV(LOGITEC, LANW300NU2S),
12bd3c8b
SW
209 RUN_DEV(MELCO, RT2870_1),
210 RUN_DEV(MELCO, RT2870_2),
211 RUN_DEV(MELCO, WLIUCAG300N),
212 RUN_DEV(MELCO, WLIUCG300N),
213 RUN_DEV(MELCO, WLIUCG301N),
214 RUN_DEV(MELCO, WLIUCGN),
215 RUN_DEV(MELCO, WLIUCGNM),
10f2cea7 216 RUN_DEV(MELCO, WLIUCGNM2),
12bd3c8b
SW
217 RUN_DEV(MOTOROLA4, RT2770),
218 RUN_DEV(MOTOROLA4, RT3070),
219 RUN_DEV(MSI, RT3070_1),
220 RUN_DEV(MSI, RT3070_2),
221 RUN_DEV(MSI, RT3070_3),
222 RUN_DEV(MSI, RT3070_4),
223 RUN_DEV(MSI, RT3070_5),
224 RUN_DEV(MSI, RT3070_6),
225 RUN_DEV(MSI, RT3070_7),
226 RUN_DEV(MSI, RT3070_8),
227 RUN_DEV(MSI, RT3070_9),
228 RUN_DEV(MSI, RT3070_10),
229 RUN_DEV(MSI, RT3070_11),
230 RUN_DEV(OVISLINK, RT3072),
231 RUN_DEV(PARA, RT3070),
232 RUN_DEV(PEGATRON, RT2870),
233 RUN_DEV(PEGATRON, RT3070),
234 RUN_DEV(PEGATRON, RT3070_2),
235 RUN_DEV(PEGATRON, RT3070_3),
236 RUN_DEV(PHILIPS, RT2870),
237 RUN_DEV(PLANEX2, GWUS300MINIS),
238 RUN_DEV(PLANEX2, GWUSMICRON),
239 RUN_DEV(PLANEX2, RT2870),
240 RUN_DEV(PLANEX2, RT3070),
241 RUN_DEV(QCOM, RT2870),
242 RUN_DEV(QUANTA, RT3070),
243 RUN_DEV(RALINK, RT2070),
244 RUN_DEV(RALINK, RT2770),
245 RUN_DEV(RALINK, RT2870),
246 RUN_DEV(RALINK, RT3070),
247 RUN_DEV(RALINK, RT3071),
248 RUN_DEV(RALINK, RT3072),
249 RUN_DEV(RALINK, RT3370),
250 RUN_DEV(RALINK, RT3572),
251 RUN_DEV(RALINK, RT8070),
252 RUN_DEV(SAMSUNG, WIS09ABGN),
253 RUN_DEV(SAMSUNG2, RT2870_1),
254 RUN_DEV(SENAO, RT2870_1),
255 RUN_DEV(SENAO, RT2870_2),
256 RUN_DEV(SENAO, RT2870_3),
257 RUN_DEV(SENAO, RT2870_4),
258 RUN_DEV(SENAO, RT3070),
259 RUN_DEV(SENAO, RT3071),
260 RUN_DEV(SENAO, RT3072_1),
261 RUN_DEV(SENAO, RT3072_2),
262 RUN_DEV(SENAO, RT3072_3),
263 RUN_DEV(SENAO, RT3072_4),
264 RUN_DEV(SENAO, RT3072_5),
265 RUN_DEV(SITECOMEU, RT2770),
266 RUN_DEV(SITECOMEU, RT2870_1),
267 RUN_DEV(SITECOMEU, RT2870_2),
268 RUN_DEV(SITECOMEU, RT2870_3),
269 RUN_DEV(SITECOMEU, RT2870_4),
270 RUN_DEV(SITECOMEU, RT3070),
271 RUN_DEV(SITECOMEU, RT3070_2),
272 RUN_DEV(SITECOMEU, RT3070_3),
273 RUN_DEV(SITECOMEU, RT3070_4),
274 RUN_DEV(SITECOMEU, RT3071),
275 RUN_DEV(SITECOMEU, RT3072_1),
276 RUN_DEV(SITECOMEU, RT3072_2),
277 RUN_DEV(SITECOMEU, RT3072_3),
278 RUN_DEV(SITECOMEU, RT3072_4),
279 RUN_DEV(SITECOMEU, RT3072_5),
280 RUN_DEV(SITECOMEU, RT3072_6),
281 RUN_DEV(SITECOMEU, WL608),
282 RUN_DEV(SPARKLAN, RT2870_1),
283 RUN_DEV(SPARKLAN, RT3070),
284 RUN_DEV(SWEEX2, LW153),
285 RUN_DEV(SWEEX2, LW303),
286 RUN_DEV(SWEEX2, LW313),
287 RUN_DEV(TOSHIBA, RT3070),
288 RUN_DEV(UMEDIA, RT2870_1),
289 RUN_DEV(ZCOM, RT2870_1),
290 RUN_DEV(ZCOM, RT2870_2),
291 RUN_DEV(ZINWELL, RT2870_1),
292 RUN_DEV(ZINWELL, RT2870_2),
293 RUN_DEV(ZINWELL, RT3070),
294 RUN_DEV(ZINWELL, RT3072_1),
295 RUN_DEV(ZINWELL, RT3072_2),
296 RUN_DEV(ZYXEL, RT2870_1),
297 RUN_DEV(ZYXEL, RT2870_2),
298#undef RUN_DEV
299};
300
301static device_probe_t run_match;
302static device_attach_t run_attach;
303static device_detach_t run_detach;
304
305static usb_callback_t run_bulk_rx_callback;
306static usb_callback_t run_bulk_tx_callback0;
307static usb_callback_t run_bulk_tx_callback1;
308static usb_callback_t run_bulk_tx_callback2;
309static usb_callback_t run_bulk_tx_callback3;
310static usb_callback_t run_bulk_tx_callback4;
311static usb_callback_t run_bulk_tx_callback5;
312
313static void run_bulk_tx_callbackN(struct usb_xfer *xfer,
314 usb_error_t error, unsigned int index);
315static struct ieee80211vap *run_vap_create(struct ieee80211com *,
316 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
317 const uint8_t [IEEE80211_ADDR_LEN],
318 const uint8_t [IEEE80211_ADDR_LEN]);
319static void run_vap_delete(struct ieee80211vap *);
320static void run_cmdq_cb(void *, int);
321static void run_setup_tx_list(struct run_softc *,
322 struct run_endpoint_queue *);
323static void run_unsetup_tx_list(struct run_softc *,
324 struct run_endpoint_queue *);
325static int run_load_microcode(struct run_softc *);
326static int run_reset(struct run_softc *);
327static usb_error_t run_do_request(struct run_softc *,
328 struct usb_device_request *, void *);
329static int run_read(struct run_softc *, uint16_t, uint32_t *);
330static int run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
331static int run_write_2(struct run_softc *, uint16_t, uint16_t);
332static int run_write(struct run_softc *, uint16_t, uint32_t);
333static int run_write_region_1(struct run_softc *, uint16_t,
334 const uint8_t *, int);
335static int run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
336static int run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
337static int run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
338static int run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t);
339static int run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
340static int run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
341static int run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
342static int run_bbp_write(struct run_softc *, uint8_t, uint8_t);
343static int run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
344static const char *run_get_rf(int);
345static int run_read_eeprom(struct run_softc *);
346static struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
347 const uint8_t mac[IEEE80211_ADDR_LEN]);
348static int run_media_change(struct ifnet *);
349static int run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
350static int run_wme_update(struct ieee80211com *);
351static void run_wme_update_cb(void *);
352static void run_key_update_begin(struct ieee80211vap *);
353static void run_key_update_end(struct ieee80211vap *);
354static void run_key_set_cb(void *);
355static int run_key_set(struct ieee80211vap *, struct ieee80211_key *,
356 const uint8_t mac[IEEE80211_ADDR_LEN]);
357static void run_key_delete_cb(void *);
358static int run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
359static void run_ratectl_to(void *);
360static void run_ratectl_cb(void *, int);
361static void run_drain_fifo(void *);
362static void run_iter_func(void *, struct ieee80211_node *);
363static void run_newassoc_cb(void *);
364static void run_newassoc(struct ieee80211_node *, int);
365static void run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
366static void run_tx_free(struct run_endpoint_queue *pq,
367 struct run_tx_data *, int);
368static void run_set_tx_desc(struct run_softc *, struct run_tx_data *);
369static int run_tx(struct run_softc *, struct mbuf *,
370 struct ieee80211_node *);
371static int run_tx_mgt(struct run_softc *, struct mbuf *,
372 struct ieee80211_node *);
373static int run_sendprot(struct run_softc *, const struct mbuf *,
374 struct ieee80211_node *, int, int);
375static int run_tx_param(struct run_softc *, struct mbuf *,
376 struct ieee80211_node *,
377 const struct ieee80211_bpf_params *);
378static int run_raw_xmit(struct ieee80211_node *, struct mbuf *,
379 const struct ieee80211_bpf_params *);
10f2cea7
SW
380static void run_start_locked(struct ifnet *);
381static void run_start(struct ifnet *, struct ifaltq_subque *);
382static int run_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
12bd3c8b
SW
383static void run_set_agc(struct run_softc *, uint8_t);
384static void run_select_chan_group(struct run_softc *, int);
385static void run_set_rx_antenna(struct run_softc *, int);
386static void run_rt2870_set_chan(struct run_softc *, u_int);
387static void run_rt3070_set_chan(struct run_softc *, u_int);
388static void run_rt3572_set_chan(struct run_softc *, u_int);
389static int run_set_chan(struct run_softc *, struct ieee80211_channel *);
390static void run_set_channel(struct ieee80211com *);
391static void run_scan_start(struct ieee80211com *);
392static void run_scan_end(struct ieee80211com *);
393static void run_update_beacon(struct ieee80211vap *, int);
394static void run_update_beacon_cb(void *);
395static void run_updateprot(struct ieee80211com *);
396static void run_updateprot_cb(void *);
397static void run_usb_timeout_cb(void *);
398static void run_reset_livelock(struct run_softc *);
399static void run_enable_tsf_sync(struct run_softc *);
400static void run_enable_mrr(struct run_softc *);
401static void run_set_txpreamble(struct run_softc *);
402static void run_set_basicrates(struct run_softc *);
403static void run_set_leds(struct run_softc *, uint16_t);
404static void run_set_bssid(struct run_softc *, const uint8_t *);
405static void run_set_macaddr(struct run_softc *, const uint8_t *);
406static void run_updateslot(struct ifnet *);
407static void run_updateslot_cb(void *);
408static void run_update_mcast(struct ifnet *);
409static int8_t run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
410static void run_update_promisc_locked(struct ifnet *);
411static void run_update_promisc(struct ifnet *);
412static int run_bbp_init(struct run_softc *);
413static int run_rt3070_rf_init(struct run_softc *);
414static int run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
415 uint8_t *);
416static void run_rt3070_rf_setup(struct run_softc *);
417static int run_txrx_enable(struct run_softc *);
418static void run_init(void *);
419static void run_init_locked(struct run_softc *);
420static void run_stop(void *);
421static void run_delay(struct run_softc *, unsigned int);
422
423static const struct {
424 uint16_t reg;
425 uint32_t val;
426} rt2870_def_mac[] = {
427 RT2870_DEF_MAC
428};
429
430static const struct {
431 uint8_t reg;
432 uint8_t val;
433} rt2860_def_bbp[] = {
434 RT2860_DEF_BBP
435};
436
437static const struct rfprog {
438 uint8_t chan;
439 uint32_t r1, r2, r3, r4;
440} rt2860_rf2850[] = {
441 RT2860_RF2850
442};
443
444struct {
445 uint8_t n, r, k;
446} rt3070_freqs[] = {
447 RT3070_RF3052
448};
449
450static const struct {
451 uint8_t reg;
452 uint8_t val;
453} rt3070_def_rf[] = {
454 RT3070_DEF_RF
455},rt3572_def_rf[] = {
456 RT3572_DEF_RF
457};
458
459static const struct usb_config run_config[RUN_N_XFER] = {
460 [RUN_BULK_TX_BE] = {
461 .type = UE_BULK,
462 .endpoint = UE_ADDR_ANY,
463 .ep_index = 0,
464 .direction = UE_DIR_OUT,
465 .bufsize = RUN_MAX_TXSZ,
466 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
467 .callback = run_bulk_tx_callback0,
468 .timeout = 5000, /* ms */
469 },
470 [RUN_BULK_TX_BK] = {
471 .type = UE_BULK,
472 .endpoint = UE_ADDR_ANY,
473 .direction = UE_DIR_OUT,
474 .ep_index = 1,
475 .bufsize = RUN_MAX_TXSZ,
476 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
477 .callback = run_bulk_tx_callback1,
478 .timeout = 5000, /* ms */
479 },
480 [RUN_BULK_TX_VI] = {
481 .type = UE_BULK,
482 .endpoint = UE_ADDR_ANY,
483 .direction = UE_DIR_OUT,
484 .ep_index = 2,
485 .bufsize = RUN_MAX_TXSZ,
486 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
487 .callback = run_bulk_tx_callback2,
488 .timeout = 5000, /* ms */
489 },
490 [RUN_BULK_TX_VO] = {
491 .type = UE_BULK,
492 .endpoint = UE_ADDR_ANY,
493 .direction = UE_DIR_OUT,
494 .ep_index = 3,
495 .bufsize = RUN_MAX_TXSZ,
496 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
497 .callback = run_bulk_tx_callback3,
498 .timeout = 5000, /* ms */
499 },
500 [RUN_BULK_TX_HCCA] = {
501 .type = UE_BULK,
502 .endpoint = UE_ADDR_ANY,
503 .direction = UE_DIR_OUT,
504 .ep_index = 4,
505 .bufsize = RUN_MAX_TXSZ,
506 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
507 .callback = run_bulk_tx_callback4,
508 .timeout = 5000, /* ms */
509 },
510 [RUN_BULK_TX_PRIO] = {
511 .type = UE_BULK,
512 .endpoint = UE_ADDR_ANY,
513 .direction = UE_DIR_OUT,
514 .ep_index = 5,
515 .bufsize = RUN_MAX_TXSZ,
516 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
517 .callback = run_bulk_tx_callback5,
518 .timeout = 5000, /* ms */
519 },
520 [RUN_BULK_RX] = {
521 .type = UE_BULK,
522 .endpoint = UE_ADDR_ANY,
523 .direction = UE_DIR_IN,
524 .bufsize = RUN_MAX_RXSZ,
525 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
526 .callback = run_bulk_rx_callback,
527 }
528};
529
530static int
531run_match(device_t self)
532{
533 struct usb_attach_arg *uaa = device_get_ivars(self);
534
535 if (uaa->usb_mode != USB_MODE_HOST)
536 return (ENXIO);
537 if (uaa->info.bConfigIndex != 0)
538 return (ENXIO);
539 if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
540 return (ENXIO);
541
542 return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
543}
544
545static int
546run_attach(device_t self)
547{
548 struct run_softc *sc = device_get_softc(self);
549 struct usb_attach_arg *uaa = device_get_ivars(self);
550 struct ieee80211com *ic;
551 struct ifnet *ifp;
552 uint32_t ver;
553 int i, ntries, error;
554 uint8_t iface_index, bands;
10f2cea7
SW
555 char ethstr[ETHER_ADDRSTRLEN + 1];
556
557 wlan_serialize_enter();
12bd3c8b
SW
558
559 device_set_usb_desc(self);
560 sc->sc_udev = uaa->device;
561 sc->sc_dev = self;
562
10f2cea7
SW
563 lockinit(&sc->sc_lock, device_get_nameunit(sc->sc_dev),
564 0, LK_CANRECURSE);
12bd3c8b
SW
565
566 iface_index = RT2860_IFACE_INDEX;
567
568 error = usbd_transfer_setup(uaa->device, &iface_index,
10f2cea7 569 sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_lock);
12bd3c8b
SW
570 if (error) {
571 device_printf(self, "could not allocate USB transfers, "
572 "err=%s\n", usbd_errstr(error));
573 goto detach;
574 }
575
576 RUN_LOCK(sc);
577
578 /* wait for the chip to settle */
579 for (ntries = 0; ntries < 100; ntries++) {
580 if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
581 RUN_UNLOCK(sc);
582 goto detach;
583 }
584 if (ver != 0 && ver != 0xffffffff)
585 break;
586 run_delay(sc, 10);
587 }
588 if (ntries == 100) {
589 device_printf(sc->sc_dev,
590 "timeout waiting for NIC to initialize\n");
591 RUN_UNLOCK(sc);
592 goto detach;
593 }
594 sc->mac_ver = ver >> 16;
595 sc->mac_rev = ver & 0xffff;
596
597 /* retrieve RF rev. no and various other things from EEPROM */
598 run_read_eeprom(sc);
599
600 device_printf(sc->sc_dev,
601 "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
602 sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
10f2cea7 603 sc->ntxchains, sc->nrxchains, kether_ntoa(sc->sc_bssid, ethstr));
12bd3c8b
SW
604
605 RUN_UNLOCK(sc);
606
607 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
608 if (ifp == NULL) {
609 device_printf(sc->sc_dev, "can not if_alloc()\n");
610 goto detach;
611 }
612 ic = ifp->if_l2com;
613
614 ifp->if_softc = sc;
615 if_initname(ifp, "run", device_get_unit(sc->sc_dev));
616 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
617 ifp->if_init = run_init;
618 ifp->if_ioctl = run_ioctl;
619 ifp->if_start = run_start;
10f2cea7
SW
620 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
621#if 0 /* XXX swildner: see c3d4131842e47b168d93a0650d58d425ebeef789 */
622 ifq_set_ready(&ifp->if_snd);
623#endif
12bd3c8b
SW
624
625 ic->ic_ifp = ifp;
626 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
627 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
628
629 /* set device capabilities */
630 ic->ic_caps =
631 IEEE80211_C_STA | /* station mode supported */
632 IEEE80211_C_MONITOR | /* monitor mode supported */
633 IEEE80211_C_IBSS |
634 IEEE80211_C_HOSTAP |
635 IEEE80211_C_WDS | /* 4-address traffic works */
636 IEEE80211_C_MBSS |
637 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
638 IEEE80211_C_SHSLOT | /* short slot time supported */
639 IEEE80211_C_WME | /* WME */
640 IEEE80211_C_WPA; /* WPA1|WPA2(RSN) */
641
642 ic->ic_cryptocaps =
643 IEEE80211_CRYPTO_WEP |
644 IEEE80211_CRYPTO_AES_CCM |
645 IEEE80211_CRYPTO_TKIPMIC |
646 IEEE80211_CRYPTO_TKIP;
647
648 ic->ic_flags |= IEEE80211_F_DATAPAD;
649 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
650
651 bands = 0;
652 setbit(&bands, IEEE80211_MODE_11B);
653 setbit(&bands, IEEE80211_MODE_11G);
654 ieee80211_init_channels(ic, NULL, &bands);
655
656 /*
657 * Do this by own because h/w supports
658 * more channels than ieee80211_init_channels()
659 */
660 if (sc->rf_rev == RT2860_RF_2750 ||
661 sc->rf_rev == RT2860_RF_2850 ||
662 sc->rf_rev == RT3070_RF_3052) {
663 /* set supported .11a rates */
10f2cea7 664 for (i = 14; i < NELEM(rt2860_rf2850); i++) {
12bd3c8b
SW
665 uint8_t chan = rt2860_rf2850[i].chan;
666 ic->ic_channels[ic->ic_nchans].ic_freq =
667 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A);
668 ic->ic_channels[ic->ic_nchans].ic_ieee = chan;
669 ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A;
670 ic->ic_channels[ic->ic_nchans].ic_extieee = 0;
671 ic->ic_nchans++;
672 }
673 }
674
675 ieee80211_ifattach(ic, sc->sc_bssid);
676
677 ic->ic_scan_start = run_scan_start;
678 ic->ic_scan_end = run_scan_end;
679 ic->ic_set_channel = run_set_channel;
680 ic->ic_node_alloc = run_node_alloc;
681 ic->ic_newassoc = run_newassoc;
682 ic->ic_updateslot = run_updateslot;
683 ic->ic_update_mcast = run_update_mcast;
684 ic->ic_wme.wme_update = run_wme_update;
685 ic->ic_raw_xmit = run_raw_xmit;
686 ic->ic_update_promisc = run_update_promisc;
687
688 ic->ic_vap_create = run_vap_create;
689 ic->ic_vap_delete = run_vap_delete;
690
691 ieee80211_radiotap_attach(ic,
692 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
693 RUN_TX_RADIOTAP_PRESENT,
694 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
695 RUN_RX_RADIOTAP_PRESENT);
696
697 TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
698 TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
10f2cea7 699 usb_callout_init_mtx(&sc->ratectl_ch, &sc->sc_lock, 1);
12bd3c8b
SW
700
701 if (bootverbose)
702 ieee80211_announce(ic);
703
10f2cea7 704 wlan_serialize_exit();
12bd3c8b
SW
705 return (0);
706
707detach:
10f2cea7 708 wlan_serialize_exit();
12bd3c8b
SW
709 run_detach(self);
710 return (ENXIO);
711}
712
713static int
714run_detach(device_t self)
715{
716 struct run_softc *sc = device_get_softc(self);
717 struct ifnet *ifp = sc->sc_ifp;
718 struct ieee80211com *ic;
719 int i;
720
10f2cea7
SW
721 wlan_serialize_enter();
722 sc->sc_detached = 1;
723
12bd3c8b
SW
724 /* stop all USB transfers */
725 usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
726
727 RUN_LOCK(sc);
728
729 sc->ratectl_run = RUN_RATECTL_OFF;
730 sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
731
732 /* free TX list, if any */
733 for (i = 0; i != RUN_EP_QUEUES; i++)
734 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
735 RUN_UNLOCK(sc);
736
737 if (ifp) {
738 ic = ifp->if_l2com;
739 /* drain tasks */
740 usb_callout_drain(&sc->ratectl_ch);
741 ieee80211_draintask(ic, &sc->cmdq_task);
742 ieee80211_draintask(ic, &sc->ratectl_task);
743 ieee80211_ifdetach(ic);
744 if_free(ifp);
745 }
746
10f2cea7 747 lockuninit(&sc->sc_lock);
12bd3c8b 748
10f2cea7 749 wlan_serialize_exit();
12bd3c8b
SW
750 return (0);
751}
752
753static struct ieee80211vap *
754run_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
755 enum ieee80211_opmode opmode, int flags,
756 const uint8_t bssid[IEEE80211_ADDR_LEN],
757 const uint8_t mac[IEEE80211_ADDR_LEN])
758{
759 struct ifnet *ifp = ic->ic_ifp;
760 struct run_softc *sc = ifp->if_softc;
761 struct run_vap *rvp;
762 struct ieee80211vap *vap;
763 int i;
764
765 if (sc->rvp_cnt >= RUN_VAP_MAX) {
766 if_printf(ifp, "number of VAPs maxed out\n");
767 return (NULL);
768 }
769
770 switch (opmode) {
771 case IEEE80211_M_STA:
772 /* enable s/w bmiss handling for sta mode */
773 flags |= IEEE80211_CLONE_NOBEACONS;
774 /* fall though */
775 case IEEE80211_M_IBSS:
776 case IEEE80211_M_MONITOR:
777 case IEEE80211_M_HOSTAP:
778 case IEEE80211_M_MBSS:
779 /* other than WDS vaps, only one at a time */
780 if (!TAILQ_EMPTY(&ic->ic_vaps))
781 return (NULL);
782 break;
783 case IEEE80211_M_WDS:
784 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
785 if(vap->iv_opmode != IEEE80211_M_HOSTAP)
786 continue;
787 /* WDS vap's always share the local mac address. */
788 flags &= ~IEEE80211_CLONE_BSSID;
789 break;
790 }
791 if (vap == NULL) {
792 if_printf(ifp, "wds only supported in ap mode\n");
793 return (NULL);
794 }
795 break;
796 default:
797 if_printf(ifp, "unknown opmode %d\n", opmode);
798 return (NULL);
799 }
800
10f2cea7
SW
801 rvp = (struct run_vap *) kmalloc(sizeof(struct run_vap),
802 M_80211_VAP, M_INTWAIT | M_ZERO);
12bd3c8b
SW
803 vap = &rvp->vap;
804 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
805
806 vap->iv_key_update_begin = run_key_update_begin;
807 vap->iv_key_update_end = run_key_update_end;
808 vap->iv_update_beacon = run_update_beacon;
809 vap->iv_max_aid = RT2870_WCID_MAX;
810 /*
811 * To delete the right key from h/w, we need wcid.
812 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
813 * and matching wcid will be written into there. So, cast
814 * some spells to remove 'const' from ieee80211_key{}
815 */
816 vap->iv_key_delete = (void *)run_key_delete;
817 vap->iv_key_set = (void *)run_key_set;
818
819 /* override state transition machine */
820 rvp->newstate = vap->iv_newstate;
821 vap->iv_newstate = run_newstate;
822
823 ieee80211_ratectl_init(vap);
824 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
825
826 /* complete setup */
827 ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
828
829 /* make sure id is always unique */
830 for (i = 0; i < RUN_VAP_MAX; i++) {
831 if((sc->rvp_bmap & 1 << i) == 0){
832 sc->rvp_bmap |= 1 << i;
833 rvp->rvp_id = i;
834 break;
835 }
836 }
837 if (sc->rvp_cnt++ == 0)
838 ic->ic_opmode = opmode;
839
840 if (opmode == IEEE80211_M_HOSTAP)
841 sc->cmdq_run = RUN_CMDQ_GO;
842
843 DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
844 rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
845
846 return (vap);
847}
848
849static void
850run_vap_delete(struct ieee80211vap *vap)
851{
852 struct run_vap *rvp = RUN_VAP(vap);
853 struct ifnet *ifp;
854 struct ieee80211com *ic;
855 struct run_softc *sc;
856 uint8_t rvp_id;
857
858 if (vap == NULL)
859 return;
860
861 ic = vap->iv_ic;
862 ifp = ic->ic_ifp;
863
864 sc = ifp->if_softc;
865
866 RUN_LOCK(sc);
867
868 m_freem(rvp->beacon_mbuf);
869 rvp->beacon_mbuf = NULL;
870
871 rvp_id = rvp->rvp_id;
872 sc->ratectl_run &= ~(1 << rvp_id);
873 sc->rvp_bmap &= ~(1 << rvp_id);
874 run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
875 run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
876 --sc->rvp_cnt;
877
878 DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
879 vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
880
881 RUN_UNLOCK(sc);
882
883 ieee80211_ratectl_deinit(vap);
884 ieee80211_vap_detach(vap);
10f2cea7 885 kfree(rvp, M_80211_VAP);
12bd3c8b
SW
886}
887
888/*
889 * There are numbers of functions need to be called in context thread.
890 * Rather than creating taskqueue event for each of those functions,
891 * here is all-for-one taskqueue callback function. This function
892 * gurantees deferred functions are executed in the same order they
893 * were enqueued.
894 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
895 */
896static void
897run_cmdq_cb(void *arg, int pending)
898{
899 struct run_softc *sc = arg;
900 uint8_t i;
901
902 /* call cmdq[].func locked */
903 RUN_LOCK(sc);
904 for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
905 i = sc->cmdq_exec, pending--) {
906 DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
907 if (sc->cmdq_run == RUN_CMDQ_GO) {
908 /*
909 * If arg0 is NULL, callback func needs more
910 * than one arg. So, pass ptr to cmdq struct.
911 */
912 if (sc->cmdq[i].arg0)
913 sc->cmdq[i].func(sc->cmdq[i].arg0);
914 else
915 sc->cmdq[i].func(&sc->cmdq[i]);
916 }
917 sc->cmdq[i].arg0 = NULL;
918 sc->cmdq[i].func = NULL;
919 sc->cmdq_exec++;
920 sc->cmdq_exec &= RUN_CMDQ_MASQ;
921 }
922 RUN_UNLOCK(sc);
923}
924
925static void
926run_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
927{
928 struct run_tx_data *data;
929
930 memset(pq, 0, sizeof(*pq));
931
932 STAILQ_INIT(&pq->tx_qh);
933 STAILQ_INIT(&pq->tx_fh);
934
935 for (data = &pq->tx_data[0];
936 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
937 data->sc = sc;
938 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
939 }
940 pq->tx_nfree = RUN_TX_RING_COUNT;
941}
942
943static void
944run_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
945{
946 struct run_tx_data *data;
947
948 /* make sure any subsequent use of the queues will fail */
949 pq->tx_nfree = 0;
950 STAILQ_INIT(&pq->tx_fh);
951 STAILQ_INIT(&pq->tx_qh);
952
953 /* free up all node references and mbufs */
954 for (data = &pq->tx_data[0];
955 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
956 if (data->m != NULL) {
957 m_freem(data->m);
958 data->m = NULL;
959 }
960 if (data->ni != NULL) {
961 ieee80211_free_node(data->ni);
962 data->ni = NULL;
963 }
964 }
965}
966
967static int
968run_load_microcode(struct run_softc *sc)
969{
970 usb_device_request_t req;
971 const struct firmware *fw;
972 const u_char *base;
973 uint32_t tmp;
974 int ntries, error;
975 const uint64_t *temp;
976 uint64_t bytes;
977
8e073936
SW
978 wlan_assert_serialized();
979
980 wlan_serialize_exit();
12bd3c8b 981 fw = firmware_get("runfw");
8e073936 982 wlan_serialize_enter();
12bd3c8b
SW
983 if (fw == NULL) {
984 device_printf(sc->sc_dev,
985 "failed loadfirmware of file %s\n", "runfw");
986 return ENOENT;
987 }
988
989 if (fw->datasize != 8192) {
990 device_printf(sc->sc_dev,
991 "invalid firmware size (should be 8KB)\n");
992 error = EINVAL;
993 goto fail;
994 }
995
996 /*
997 * RT3071/RT3072 use a different firmware
998 * run-rt2870 (8KB) contains both,
999 * first half (4KB) is for rt2870,
1000 * last half is for rt3071.
1001 */
1002 base = fw->data;
1003 if ((sc->mac_ver) != 0x2860 &&
1004 (sc->mac_ver) != 0x2872 &&
1005 (sc->mac_ver) != 0x3070) {
1006 base += 4096;
1007 }
1008
1009 /* cheap sanity check */
1010 temp = fw->data;
1011 bytes = *temp;
1012 if (bytes != be64toh(0xffffff0210280210)) {
1013 device_printf(sc->sc_dev, "firmware checksum failed\n");
1014 error = EINVAL;
1015 goto fail;
1016 }
1017
1018 run_read(sc, RT2860_ASIC_VER_ID, &tmp);
1019 /* write microcode image */
1020 run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1021 run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1022 run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1023
1024 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1025 req.bRequest = RT2870_RESET;
1026 USETW(req.wValue, 8);
1027 USETW(req.wIndex, 0);
1028 USETW(req.wLength, 0);
10f2cea7 1029 if ((error = usbd_do_request(sc->sc_udev, &sc->sc_lock, &req, NULL))
12bd3c8b
SW
1030 != 0) {
1031 device_printf(sc->sc_dev, "firmware reset failed\n");
1032 goto fail;
1033 }
1034
1035 run_delay(sc, 10);
1036
1037 run_write(sc, RT2860_H2M_MAILBOX, 0);
1038 if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1039 goto fail;
1040
1041 /* wait until microcontroller is ready */
1042 for (ntries = 0; ntries < 1000; ntries++) {
1043 if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1044 goto fail;
1045 }
1046 if (tmp & RT2860_MCU_READY)
1047 break;
1048 run_delay(sc, 10);
1049 }
1050 if (ntries == 1000) {
1051 device_printf(sc->sc_dev,
1052 "timeout waiting for MCU to initialize\n");
1053 error = ETIMEDOUT;
1054 goto fail;
1055 }
10f2cea7
SW
1056 device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n",
1057 (base == fw->data) ? "RT2870" : "RT3071",
1058 *(base + 4092), *(base + 4093));
12bd3c8b
SW
1059
1060fail:
1061 firmware_put(fw, FIRMWARE_UNLOAD);
1062 return (error);
1063}
1064
1065int
1066run_reset(struct run_softc *sc)
1067{
1068 usb_device_request_t req;
10f2cea7 1069 usb_error_t error;
12bd3c8b
SW
1070
1071 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1072 req.bRequest = RT2870_RESET;
1073 USETW(req.wValue, 1);
1074 USETW(req.wIndex, 0);
1075 USETW(req.wLength, 0);
10f2cea7
SW
1076 error = usbd_do_request(sc->sc_udev, &sc->sc_lock, &req, NULL);
1077 return (error);
12bd3c8b
SW
1078}
1079
1080static usb_error_t
1081run_do_request(struct run_softc *sc,
1082 struct usb_device_request *req, void *data)
1083{
1084 usb_error_t err;
1085 int ntries = 10;
1086
1087 RUN_LOCK_ASSERT(sc, MA_OWNED);
1088
1089 while (ntries--) {
10f2cea7 1090 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
12bd3c8b
SW
1091 req, data, 0, NULL, 250 /* ms */);
1092 if (err == 0)
1093 break;
1094 DPRINTFN(1, "Control request failed, %s (retrying)\n",
1095 usbd_errstr(err));
1096 run_delay(sc, 10);
1097 }
1098 return (err);
1099}
1100
1101static int
1102run_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1103{
1104 uint32_t tmp;
1105 int error;
1106
1107 error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1108 if (error == 0)
1109 *val = le32toh(tmp);
1110 else
1111 *val = 0xffffffff;
1112 return (error);
1113}
1114
1115static int
1116run_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1117{
1118 usb_device_request_t req;
1119
1120 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1121 req.bRequest = RT2870_READ_REGION_1;
1122 USETW(req.wValue, 0);
1123 USETW(req.wIndex, reg);
1124 USETW(req.wLength, len);
1125
1126 return (run_do_request(sc, &req, buf));
1127}
1128
1129static int
1130run_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1131{
1132 usb_device_request_t req;
1133
1134 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1135 req.bRequest = RT2870_WRITE_2;
1136 USETW(req.wValue, val);
1137 USETW(req.wIndex, reg);
1138 USETW(req.wLength, 0);
1139
1140 return (run_do_request(sc, &req, NULL));
1141}
1142
1143static int
1144run_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1145{
1146 int error;
1147
1148 if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1149 error = run_write_2(sc, reg + 2, val >> 16);
1150 return (error);
1151}
1152
1153static int
1154run_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1155 int len)
1156{
1157#if 1
1158 int i, error = 0;
1159 /*
1160 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1161 * We thus issue multiple WRITE_2 commands instead.
1162 */
1163 KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1164 for (i = 0; i < len && error == 0; i += 2)
1165 error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1166 return (error);
1167#else
1168 usb_device_request_t req;
1169
1170 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1171 req.bRequest = RT2870_WRITE_REGION_1;
1172 USETW(req.wValue, 0);
1173 USETW(req.wIndex, reg);
1174 USETW(req.wLength, len);
1175 return (run_do_request(sc, &req, buf));
1176#endif
1177}
1178
1179static int
1180run_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1181{
1182 int i, error = 0;
1183
1184 KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1185 for (i = 0; i < len && error == 0; i += 4)
1186 error = run_write(sc, reg + i, val);
1187 return (error);
1188}
1189
1190/* Read 16-bit from eFUSE ROM (RT3070 only.) */
1191static int
1192run_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1193{
1194 uint32_t tmp;
1195 uint16_t reg;
1196 int error, ntries;
1197
1198 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1199 return (error);
1200
1201 addr *= 2;
1202 /*-
1203 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1204 * DATA0: F E D C
1205 * DATA1: B A 9 8
1206 * DATA2: 7 6 5 4
1207 * DATA3: 3 2 1 0
1208 */
1209 tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1210 tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1211 run_write(sc, RT3070_EFUSE_CTRL, tmp);
1212 for (ntries = 0; ntries < 100; ntries++) {
1213 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1214 return (error);
1215 if (!(tmp & RT3070_EFSROM_KICK))
1216 break;
1217 run_delay(sc, 2);
1218 }
1219 if (ntries == 100)
1220 return (ETIMEDOUT);
1221
1222 if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1223 *val = 0xffff; /* address not found */
1224 return (0);
1225 }
1226 /* determine to which 32-bit register our 16-bit word belongs */
1227 reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1228 if ((error = run_read(sc, reg, &tmp)) != 0)
1229 return (error);
1230
1231 *val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1232 return (0);
1233}
1234
1235static int
1236run_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1237{
1238 usb_device_request_t req;
1239 uint16_t tmp;
1240 int error;
1241
1242 addr *= 2;
1243 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1244 req.bRequest = RT2870_EEPROM_READ;
1245 USETW(req.wValue, 0);
1246 USETW(req.wIndex, addr);
1247 USETW(req.wLength, sizeof tmp);
1248
10f2cea7 1249 error = usbd_do_request(sc->sc_udev, &sc->sc_lock, &req, &tmp);
12bd3c8b
SW
1250 if (error == 0)
1251 *val = le16toh(tmp);
1252 else
1253 *val = 0xffff;
1254 return (error);
1255}
1256
1257static __inline int
1258run_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1259{
1260 /* either eFUSE ROM or EEPROM */
1261 return sc->sc_srom_read(sc, addr, val);
1262}
1263
1264static int
1265run_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val)
1266{
1267 uint32_t tmp;
1268 int error, ntries;
1269
1270 for (ntries = 0; ntries < 10; ntries++) {
1271 if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1272 return (error);
1273 if (!(tmp & RT2860_RF_REG_CTRL))
1274 break;
1275 }
1276 if (ntries == 10)
1277 return (ETIMEDOUT);
1278
1279 /* RF registers are 24-bit on the RT2860 */
1280 tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1281 (val & 0x3fffff) << 2 | (reg & 3);
1282 return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1283}
1284
1285static int
1286run_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1287{
1288 uint32_t tmp;
1289 int error, ntries;
1290
1291 for (ntries = 0; ntries < 100; ntries++) {
1292 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1293 return (error);
1294 if (!(tmp & RT3070_RF_KICK))
1295 break;
1296 }
1297 if (ntries == 100)
1298 return (ETIMEDOUT);
1299
1300 tmp = RT3070_RF_KICK | reg << 8;
1301 if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1302 return (error);
1303
1304 for (ntries = 0; ntries < 100; ntries++) {
1305 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1306 return (error);
1307 if (!(tmp & RT3070_RF_KICK))
1308 break;
1309 }
1310 if (ntries == 100)
1311 return (ETIMEDOUT);
1312
1313 *val = tmp & 0xff;
1314 return (0);
1315}
1316
1317static int
1318run_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1319{
1320 uint32_t tmp;
1321 int error, ntries;
1322
1323 for (ntries = 0; ntries < 10; ntries++) {
1324 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1325 return (error);
1326 if (!(tmp & RT3070_RF_KICK))
1327 break;
1328 }
1329 if (ntries == 10)
1330 return (ETIMEDOUT);
1331
1332 tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1333 return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1334}
1335
1336static int
1337run_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1338{
1339 uint32_t tmp;
1340 int ntries, error;
1341
1342 for (ntries = 0; ntries < 10; ntries++) {
1343 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1344 return (error);
1345 if (!(tmp & RT2860_BBP_CSR_KICK))
1346 break;
1347 }
1348 if (ntries == 10)
1349 return (ETIMEDOUT);
1350
1351 tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1352 if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1353 return (error);
1354
1355 for (ntries = 0; ntries < 10; ntries++) {
1356 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1357 return (error);
1358 if (!(tmp & RT2860_BBP_CSR_KICK))
1359 break;
1360 }
1361 if (ntries == 10)
1362 return (ETIMEDOUT);
1363
1364 *val = tmp & 0xff;
1365 return (0);
1366}
1367
1368static int
1369run_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1370{
1371 uint32_t tmp;
1372 int ntries, error;
1373
1374 for (ntries = 0; ntries < 10; ntries++) {
1375 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1376 return (error);
1377 if (!(tmp & RT2860_BBP_CSR_KICK))
1378 break;
1379 }
1380 if (ntries == 10)
1381 return (ETIMEDOUT);
1382
1383 tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1384 return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1385}
1386
1387/*
1388 * Send a command to the 8051 microcontroller unit.
1389 */
1390static int
1391run_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1392{
1393 uint32_t tmp;
1394 int error, ntries;
1395
1396 for (ntries = 0; ntries < 100; ntries++) {
1397 if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1398 return error;
1399 if (!(tmp & RT2860_H2M_BUSY))
1400 break;
1401 }
1402 if (ntries == 100)
1403 return ETIMEDOUT;
1404
1405 tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1406 if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1407 error = run_write(sc, RT2860_HOST_CMD, cmd);
1408 return (error);
1409}
1410
1411/*
1412 * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1413 * Used to adjust per-rate Tx power registers.
1414 */
1415static __inline uint32_t
1416b4inc(uint32_t b32, int8_t delta)
1417{
1418 int8_t i, b4;
1419
1420 for (i = 0; i < 8; i++) {
1421 b4 = b32 & 0xf;
1422 b4 += delta;
1423 if (b4 < 0)
1424 b4 = 0;
1425 else if (b4 > 0xf)
1426 b4 = 0xf;
1427 b32 = b32 >> 4 | b4 << 28;
1428 }
1429 return (b32);
1430}
1431
1432static const char *
1433run_get_rf(int rev)
1434{
1435 switch (rev) {
1436 case RT2860_RF_2820: return "RT2820";
1437 case RT2860_RF_2850: return "RT2850";
1438 case RT2860_RF_2720: return "RT2720";
1439 case RT2860_RF_2750: return "RT2750";
1440 case RT3070_RF_3020: return "RT3020";
1441 case RT3070_RF_2020: return "RT2020";
1442 case RT3070_RF_3021: return "RT3021";
1443 case RT3070_RF_3022: return "RT3022";
1444 case RT3070_RF_3052: return "RT3052";
1445 }
1446 return ("unknown");
1447}
1448
1449int
1450run_read_eeprom(struct run_softc *sc)
1451{
1452 int8_t delta_2ghz, delta_5ghz;
1453 uint32_t tmp;
1454 uint16_t val;
1455 int ridx, ant, i;
1456
1457 /* check whether the ROM is eFUSE ROM or EEPROM */
1458 sc->sc_srom_read = run_eeprom_read_2;
1459 if (sc->mac_ver >= 0x3070) {
1460 run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1461 DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1462 if (tmp & RT3070_SEL_EFUSE)
1463 sc->sc_srom_read = run_efuse_read_2;
1464 }
1465
1466 /* read ROM version */
1467 run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1468 DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1469
1470 /* read MAC address */
1471 run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1472 sc->sc_bssid[0] = val & 0xff;
1473 sc->sc_bssid[1] = val >> 8;
1474 run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1475 sc->sc_bssid[2] = val & 0xff;
1476 sc->sc_bssid[3] = val >> 8;
1477 run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1478 sc->sc_bssid[4] = val & 0xff;
1479 sc->sc_bssid[5] = val >> 8;
1480
1481 /* read vender BBP settings */
1482 for (i = 0; i < 10; i++) {
1483 run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1484 sc->bbp[i].val = val & 0xff;
1485 sc->bbp[i].reg = val >> 8;
1486 DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val);
1487 }
1488 if (sc->mac_ver >= 0x3071) {
1489 /* read vendor RF settings */
1490 for (i = 0; i < 10; i++) {
1491 run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val);
1492 sc->rf[i].val = val & 0xff;
1493 sc->rf[i].reg = val >> 8;
1494 DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1495 sc->rf[i].val);
1496 }
1497 }
1498
1499 /* read RF frequency offset from EEPROM */
1500 run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1501 sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1502 DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1503
1504 if (val >> 8 != 0xff) {
1505 /* read LEDs operating mode */
1506 sc->leds = val >> 8;
1507 run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1508 run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1509 run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1510 } else {
1511 /* broken EEPROM, use default settings */
1512 sc->leds = 0x01;
1513 sc->led[0] = 0x5555;
1514 sc->led[1] = 0x2221;
1515 sc->led[2] = 0x5627; /* differs from RT2860 */
1516 }
1517 DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1518 sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1519
1520 /* read RF information */
1521 run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1522 if (val == 0xffff) {
1523 DPRINTF("invalid EEPROM antenna info, using default\n");
1524 if (sc->mac_ver == 0x3572) {
1525 /* default to RF3052 2T2R */
1526 sc->rf_rev = RT3070_RF_3052;
1527 sc->ntxchains = 2;
1528 sc->nrxchains = 2;
1529 } else if (sc->mac_ver >= 0x3070) {
1530 /* default to RF3020 1T1R */
1531 sc->rf_rev = RT3070_RF_3020;
1532 sc->ntxchains = 1;
1533 sc->nrxchains = 1;
1534 } else {
1535 /* default to RF2820 1T2R */
1536 sc->rf_rev = RT2860_RF_2820;
1537 sc->ntxchains = 1;
1538 sc->nrxchains = 2;
1539 }
1540 } else {
1541 sc->rf_rev = (val >> 8) & 0xf;
1542 sc->ntxchains = (val >> 4) & 0xf;
1543 sc->nrxchains = val & 0xf;
1544 }
1545 DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n",
1546 sc->rf_rev, sc->ntxchains, sc->nrxchains);
1547
1548 /* check if RF supports automatic Tx access gain control */
1549 run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1550 DPRINTF("EEPROM CFG 0x%04x\n", val);
1551 /* check if driver should patch the DAC issue */
1552 if ((val >> 8) != 0xff)
1553 sc->patch_dac = (val >> 15) & 1;
1554 if ((val & 0xff) != 0xff) {
1555 sc->ext_5ghz_lna = (val >> 3) & 1;
1556 sc->ext_2ghz_lna = (val >> 2) & 1;
1557 /* check if RF supports automatic Tx access gain control */
1558 sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1559 /* check if we have a hardware radio switch */
1560 sc->rfswitch = val & 1;
1561 }
1562
1563 /* read power settings for 2GHz channels */
1564 for (i = 0; i < 14; i += 2) {
1565 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1566 sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1567 sc->txpow1[i + 1] = (int8_t)(val >> 8);
1568
1569 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1570 sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1571 sc->txpow2[i + 1] = (int8_t)(val >> 8);
1572 }
1573 /* fix broken Tx power entries */
1574 for (i = 0; i < 14; i++) {
1575 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1576 sc->txpow1[i] = 5;
1577 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1578 sc->txpow2[i] = 5;
1579 DPRINTF("chan %d: power1=%d, power2=%d\n",
1580 rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1581 }
1582 /* read power settings for 5GHz channels */
1583 for (i = 0; i < 40; i += 2) {
1584 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1585 sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1586 sc->txpow1[i + 15] = (int8_t)(val >> 8);
1587
1588 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1589 sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1590 sc->txpow2[i + 15] = (int8_t)(val >> 8);
1591 }
1592 /* fix broken Tx power entries */
1593 for (i = 0; i < 40; i++) {
1594 if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1595 sc->txpow1[14 + i] = 5;
1596 if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1597 sc->txpow2[14 + i] = 5;
1598 DPRINTF("chan %d: power1=%d, power2=%d\n",
1599 rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1600 sc->txpow2[14 + i]);
1601 }
1602
1603 /* read Tx power compensation for each Tx rate */
1604 run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1605 delta_2ghz = delta_5ghz = 0;
1606 if ((val & 0xff) != 0xff && (val & 0x80)) {
1607 delta_2ghz = val & 0xf;
1608 if (!(val & 0x40)) /* negative number */
1609 delta_2ghz = -delta_2ghz;
1610 }
1611 val >>= 8;
1612 if ((val & 0xff) != 0xff && (val & 0x80)) {
1613 delta_5ghz = val & 0xf;
1614 if (!(val & 0x40)) /* negative number */
1615 delta_5ghz = -delta_5ghz;
1616 }
1617 DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1618 delta_2ghz, delta_5ghz);
1619
1620 for (ridx = 0; ridx < 5; ridx++) {
1621 uint32_t reg;
1622
1623 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1624 reg = val;
1625 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1626 reg |= (uint32_t)val << 16;
1627
1628 sc->txpow20mhz[ridx] = reg;
1629 sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1630 sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1631
1632 DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1633 "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1634 sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1635 }
1636
1637 /* read RSSI offsets and LNA gains from EEPROM */
1638 run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1639 sc->rssi_2ghz[0] = val & 0xff; /* Ant A */
1640 sc->rssi_2ghz[1] = val >> 8; /* Ant B */
1641 run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1642 if (sc->mac_ver >= 0x3070) {
1643 /*
1644 * On RT3070 chips (limited to 2 Rx chains), this ROM
1645 * field contains the Tx mixer gain for the 2GHz band.
1646 */
1647 if ((val & 0xff) != 0xff)
1648 sc->txmixgain_2ghz = val & 0x7;
1649 DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1650 } else
1651 sc->rssi_2ghz[2] = val & 0xff; /* Ant C */
1652 sc->lna[2] = val >> 8; /* channel group 2 */
1653
1654 run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1655 sc->rssi_5ghz[0] = val & 0xff; /* Ant A */
1656 sc->rssi_5ghz[1] = val >> 8; /* Ant B */
1657 run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1658 if (sc->mac_ver == 0x3572) {
1659 /*
1660 * On RT3572 chips (limited to 2 Rx chains), this ROM
1661 * field contains the Tx mixer gain for the 5GHz band.
1662 */
1663 if ((val & 0xff) != 0xff)
1664 sc->txmixgain_5ghz = val & 0x7;
1665 DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1666 } else
1667 sc->rssi_5ghz[2] = val & 0xff; /* Ant C */
1668 sc->lna[3] = val >> 8; /* channel group 3 */
1669
1670 run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1671 sc->lna[0] = val & 0xff; /* channel group 0 */
1672 sc->lna[1] = val >> 8; /* channel group 1 */
1673
1674 /* fix broken 5GHz LNA entries */
1675 if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1676 DPRINTF("invalid LNA for channel group %d\n", 2);
1677 sc->lna[2] = sc->lna[1];
1678 }
1679 if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1680 DPRINTF("invalid LNA for channel group %d\n", 3);
1681 sc->lna[3] = sc->lna[1];
1682 }
1683
1684 /* fix broken RSSI offset entries */
1685 for (ant = 0; ant < 3; ant++) {
1686 if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1687 DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1688 ant + 1, sc->rssi_2ghz[ant]);
1689 sc->rssi_2ghz[ant] = 0;
1690 }
1691 if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1692 DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1693 ant + 1, sc->rssi_5ghz[ant]);
1694 sc->rssi_5ghz[ant] = 0;
1695 }
1696 }
1697 return (0);
1698}
1699
1700static struct ieee80211_node *
1701run_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1702{
10f2cea7 1703 return kmalloc(sizeof (struct run_node), M_DEVBUF, M_WAITOK | M_ZERO);
12bd3c8b
SW
1704}
1705
1706static int
1707run_media_change(struct ifnet *ifp)
1708{
1709 struct ieee80211vap *vap = ifp->if_softc;
1710 struct ieee80211com *ic = vap->iv_ic;
1711 const struct ieee80211_txparam *tp;
1712 struct run_softc *sc = ic->ic_ifp->if_softc;
1713 uint8_t rate, ridx;
1714 int error;
1715
1716 RUN_LOCK(sc);
1717
1718 error = ieee80211_media_change(ifp);
1719 if (error != ENETRESET) {
1720 RUN_UNLOCK(sc);
1721 return (error);
1722 }
1723
1724 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1725 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1726 struct ieee80211_node *ni;
1727 struct run_node *rn;
1728
1729 rate = ic->ic_sup_rates[ic->ic_curmode].
1730 rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1731 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1732 if (rt2860_rates[ridx].rate == rate)
1733 break;
1734 ni = ieee80211_ref_node(vap->iv_bss);
1735 rn = (struct run_node *)ni;
1736 rn->fix_ridx = ridx;
1737 DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1738 ieee80211_free_node(ni);
1739 }
1740
1741#if 0
1742 if ((ifp->if_flags & IFF_UP) &&
10f2cea7 1743 (ifp->if_flags & IFF_RUNNING)){
12bd3c8b
SW
1744 run_init_locked(sc);
1745 }
1746#endif
1747
1748 RUN_UNLOCK(sc);
1749
1750 return (0);
1751}
1752
1753static int
1754run_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1755{
1756 const struct ieee80211_txparam *tp;
1757 struct ieee80211com *ic = vap->iv_ic;
1758 struct run_softc *sc = ic->ic_ifp->if_softc;
1759 struct run_vap *rvp = RUN_VAP(vap);
1760 enum ieee80211_state ostate;
1761 uint32_t sta[3];
1762 uint32_t tmp;
1763 uint8_t ratectl;
1764 uint8_t restart_ratectl = 0;
1765 uint8_t bid = 1 << rvp->rvp_id;
1766
1767 ostate = vap->iv_state;
1768 DPRINTF("%s -> %s\n",
1769 ieee80211_state_name[ostate],
1770 ieee80211_state_name[nstate]);
1771
12bd3c8b
SW
1772 RUN_LOCK(sc);
1773
1774 ratectl = sc->ratectl_run; /* remember current state */
1775 sc->ratectl_run = RUN_RATECTL_OFF;
1776 usb_callout_stop(&sc->ratectl_ch);
1777
1778 if (ostate == IEEE80211_S_RUN) {
1779 /* turn link LED off */
1780 run_set_leds(sc, RT2860_LED_RADIO);
1781 }
1782
1783 switch (nstate) {
1784 case IEEE80211_S_INIT:
1785 restart_ratectl = 1;
1786
1787 if (ostate != IEEE80211_S_RUN)
1788 break;
1789
1790 ratectl &= ~bid;
1791 sc->runbmap &= ~bid;
1792
1793 /* abort TSF synchronization if there is no vap running */
1794 if (--sc->running == 0) {
1795 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1796 run_write(sc, RT2860_BCN_TIME_CFG,
1797 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1798 RT2860_TBTT_TIMER_EN));
1799 }
1800 break;
1801
1802 case IEEE80211_S_RUN:
1803 if (!(sc->runbmap & bid)) {
1804 if(sc->running++)
1805 restart_ratectl = 1;
1806 sc->runbmap |= bid;
1807 }
1808
1809 m_freem(rvp->beacon_mbuf);
1810 rvp->beacon_mbuf = NULL;
1811
1812 switch (vap->iv_opmode) {
1813 case IEEE80211_M_HOSTAP:
1814 case IEEE80211_M_MBSS:
1815 sc->ap_running |= bid;
1816 ic->ic_opmode = vap->iv_opmode;
1817 run_update_beacon_cb(vap);
1818 break;
1819 case IEEE80211_M_IBSS:
1820 sc->adhoc_running |= bid;
1821 if (!sc->ap_running)
1822 ic->ic_opmode = vap->iv_opmode;
1823 run_update_beacon_cb(vap);
1824 break;
1825 case IEEE80211_M_STA:
1826 sc->sta_running |= bid;
1827 if (!sc->ap_running && !sc->adhoc_running)
1828 ic->ic_opmode = vap->iv_opmode;
1829
1830 /* read statistic counters (clear on read) */
1831 run_read_region_1(sc, RT2860_TX_STA_CNT0,
1832 (uint8_t *)sta, sizeof sta);
1833
1834 break;
1835 default:
1836 ic->ic_opmode = vap->iv_opmode;
1837 break;
1838 }
1839
1840 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1841 struct ieee80211_node *ni;
1842
10f2cea7
SW
1843 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
1844 return (-1);
12bd3c8b
SW
1845 run_updateslot(ic->ic_ifp);
1846 run_enable_mrr(sc);
1847 run_set_txpreamble(sc);
1848 run_set_basicrates(sc);
1849 ni = ieee80211_ref_node(vap->iv_bss);
1850 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1851 run_set_bssid(sc, ni->ni_bssid);
1852 ieee80211_free_node(ni);
1853 run_enable_tsf_sync(sc);
1854
1855 /* enable automatic rate adaptation */
1856 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1857 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1858 ratectl |= bid;
1859 }
1860
1861 /* turn link LED on */
1862 run_set_leds(sc, RT2860_LED_RADIO |
1863 (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1864 RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1865
1866 break;
1867 default:
1868 DPRINTFN(6, "undefined case\n");
1869 break;
1870 }
1871
1872 /* restart amrr for running VAPs */
1873 if ((sc->ratectl_run = ratectl) && restart_ratectl)
1874 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1875
1876 RUN_UNLOCK(sc);
12bd3c8b
SW
1877
1878 return(rvp->newstate(vap, nstate, arg));
1879}
1880
1881/* ARGSUSED */
1882static void
1883run_wme_update_cb(void *arg)
1884{
1885 struct ieee80211com *ic = arg;
1886 struct run_softc *sc = ic->ic_ifp->if_softc;
1887 struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1888 int aci, error = 0;
1889
1890 RUN_LOCK_ASSERT(sc, MA_OWNED);
1891
1892 /* update MAC TX configuration registers */
1893 for (aci = 0; aci < WME_NUM_AC; aci++) {
1894 error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1895 wmesp->wme_params[aci].wmep_logcwmax << 16 |
1896 wmesp->wme_params[aci].wmep_logcwmin << 12 |
1897 wmesp->wme_params[aci].wmep_aifsn << 8 |
1898 wmesp->wme_params[aci].wmep_txopLimit);
1899 if (error) goto err;
1900 }
1901
1902 /* update SCH/DMA registers too */
1903 error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1904 wmesp->wme_params[WME_AC_VO].wmep_aifsn << 12 |
1905 wmesp->wme_params[WME_AC_VI].wmep_aifsn << 8 |
1906 wmesp->wme_params[WME_AC_BK].wmep_aifsn << 4 |
1907 wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1908 if (error) goto err;
1909 error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1910 wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1911 wmesp->wme_params[WME_AC_VI].wmep_logcwmin << 8 |
1912 wmesp->wme_params[WME_AC_BK].wmep_logcwmin << 4 |
1913 wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1914 if (error) goto err;
1915 error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1916 wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1917 wmesp->wme_params[WME_AC_VI].wmep_logcwmax << 8 |
1918 wmesp->wme_params[WME_AC_BK].wmep_logcwmax << 4 |
1919 wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1920 if (error) goto err;
1921 error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1922 wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1923 wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1924 if (error) goto err;
1925 error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1926 wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1927 wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1928
1929err:
1930 if (error)
1931 DPRINTF("WME update failed\n");
1932
1933 return;
1934}
1935
1936static int
1937run_wme_update(struct ieee80211com *ic)
1938{
1939 struct run_softc *sc = ic->ic_ifp->if_softc;
1940
10f2cea7
SW
1941#if 0 /* XXX swildner */
1942 /* sometime called without lock */
12bd3c8b
SW
1943 if (mtx_owned(&ic->ic_comlock.mtx)) {
1944 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1945 DPRINTF("cmdq_store=%d\n", i);
1946 sc->cmdq[i].func = run_wme_update_cb;
1947 sc->cmdq[i].arg0 = ic;
1948 ieee80211_runtask(ic, &sc->cmdq_task);
1949 return (0);
1950 }
10f2cea7 1951#endif
12bd3c8b
SW
1952
1953 RUN_LOCK(sc);
1954 run_wme_update_cb(ic);
1955 RUN_UNLOCK(sc);
1956
1957 /* return whatever, upper layer desn't care anyway */
1958 return (0);
1959}
1960
1961static void
1962run_key_update_begin(struct ieee80211vap *vap)
1963{
1964 /*
1965 * To avoid out-of-order events, both run_key_set() and
1966 * _delete() are deferred and handled by run_cmdq_cb().
1967 * So, there is nothing we need to do here.
1968 */
1969}
1970
1971static void
1972run_key_update_end(struct ieee80211vap *vap)
1973{
1974 /* null */
1975}
1976
1977static void
1978run_key_set_cb(void *arg)
1979{
1980 struct run_cmdq *cmdq = arg;
1981 struct ieee80211vap *vap = cmdq->arg1;
1982 struct ieee80211_key *k = cmdq->k;
1983 struct ieee80211com *ic = vap->iv_ic;
1984 struct run_softc *sc = ic->ic_ifp->if_softc;
1985 struct ieee80211_node *ni;
1986 uint32_t attr;
1987 uint16_t base, associd;
1988 uint8_t mode, wcid, iv[8];
1989
1990 RUN_LOCK_ASSERT(sc, MA_OWNED);
1991
1992 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1993 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
1994 else
1995 ni = vap->iv_bss;
1996 associd = (ni != NULL) ? ni->ni_associd : 0;
1997
1998 /* map net80211 cipher to RT2860 security mode */
1999 switch (k->wk_cipher->ic_cipher) {
2000 case IEEE80211_CIPHER_WEP:
2001 if(k->wk_keylen < 8)
2002 mode = RT2860_MODE_WEP40;
2003 else
2004 mode = RT2860_MODE_WEP104;
2005 break;
2006 case IEEE80211_CIPHER_TKIP:
2007 mode = RT2860_MODE_TKIP;
2008 break;
2009 case IEEE80211_CIPHER_AES_CCM:
2010 mode = RT2860_MODE_AES_CCMP;
2011 break;
2012 default:
2013 DPRINTF("undefined case\n");
2014 return;
2015 }
2016
2017 DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2018 associd, k->wk_keyix, mode,
2019 (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2020 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2021 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2022
2023 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2024 wcid = 0; /* NB: update WCID0 for group keys */
2025 base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2026 } else {
10f2cea7
SW
2027 wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2028 1 : RUN_AID2WCID(associd);
12bd3c8b
SW
2029 base = RT2860_PKEY(wcid);
2030 }
2031
2032 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2033 if(run_write_region_1(sc, base, k->wk_key, 16))
2034 return;
2035 if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8)) /* wk_txmic */
2036 return;
2037 if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8)) /* wk_rxmic */
2038 return;
2039 } else {
2040 /* roundup len to 16-bit: XXX fix write_region_1() instead */
2041 if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2042 return;
2043 }
2044
2045 if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2046 (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2047 /* set initial packet number in IV+EIV */
2048 if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2049 memset(iv, 0, sizeof iv);
2050 iv[3] = vap->iv_def_txkey << 6;
2051 } else {
2052 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2053 iv[0] = k->wk_keytsc >> 8;
2054 iv[1] = (iv[0] | 0x20) & 0x7f;
2055 iv[2] = k->wk_keytsc;
2056 } else /* CCMP */ {
2057 iv[0] = k->wk_keytsc;
2058 iv[1] = k->wk_keytsc >> 8;
2059 iv[2] = 0;
2060 }
2061 iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2062 iv[4] = k->wk_keytsc >> 16;
2063 iv[5] = k->wk_keytsc >> 24;
2064 iv[6] = k->wk_keytsc >> 32;
2065 iv[7] = k->wk_keytsc >> 40;
2066 }
2067 if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2068 return;
2069 }
2070
2071 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2072 /* install group key */
2073 if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2074 return;
2075 attr &= ~(0xf << (k->wk_keyix * 4));
2076 attr |= mode << (k->wk_keyix * 4);
2077 if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2078 return;
2079 } else {
2080 /* install pairwise key */
2081 if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2082 return;
2083 attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2084 if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2085 return;
2086 }
2087
2088 /* TODO create a pass-thru key entry? */
2089
2090 /* need wcid to delete the right key later */
2091 k->wk_pad = wcid;
2092}
2093
2094/*
2095 * Don't have to be deferred, but in order to keep order of
2096 * execution, i.e. with run_key_delete(), defer this and let
2097 * run_cmdq_cb() maintain the order.
2098 *
2099 * return 0 on error
2100 */
2101static int
2102run_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2103 const uint8_t mac[IEEE80211_ADDR_LEN])
2104{
2105 struct ieee80211com *ic = vap->iv_ic;
2106 struct run_softc *sc = ic->ic_ifp->if_softc;
2107 uint32_t i;
2108
2109 i = RUN_CMDQ_GET(&sc->cmdq_store);
2110 DPRINTF("cmdq_store=%d\n", i);
2111 sc->cmdq[i].func = run_key_set_cb;
2112 sc->cmdq[i].arg0 = NULL;
2113 sc->cmdq[i].arg1 = vap;
2114 sc->cmdq[i].k = k;
2115 IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2116 ieee80211_runtask(ic, &sc->cmdq_task);
2117
2118 /*
2119 * To make sure key will be set when hostapd
2120 * calls iv_key_set() before if_init().
2121 */
2122 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2123 RUN_LOCK(sc);
2124 sc->cmdq_key_set = RUN_CMDQ_GO;
2125 RUN_UNLOCK(sc);
2126 }
2127
2128 return (1);
2129}
2130
2131/*
2132 * If wlan is destroyed without being brought down i.e. without
2133 * wlan down or wpa_cli terminate, this function is called after
2134 * vap is gone. Don't refer it.
2135 */
2136static void
2137run_key_delete_cb(void *arg)
2138{
2139 struct run_cmdq *cmdq = arg;
2140 struct run_softc *sc = cmdq->arg1;
2141 struct ieee80211_key *k = &cmdq->key;
2142 uint32_t attr;
2143 uint8_t wcid;
2144
2145 RUN_LOCK_ASSERT(sc, MA_OWNED);
2146
2147 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2148 /* remove group key */
2149 DPRINTF("removing group key\n");
2150 run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2151 attr &= ~(0xf << (k->wk_keyix * 4));
2152 run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2153 } else {
2154 /* remove pairwise key */
2155 DPRINTF("removing key for wcid %x\n", k->wk_pad);
2156 /* matching wcid was written to wk_pad in run_key_set() */
2157 wcid = k->wk_pad;
2158 run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2159 attr &= ~0xf;
2160 run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2161 run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2162 }
2163
2164 k->wk_pad = 0;
2165}
2166
2167/*
2168 * return 0 on error
2169 */
2170static int
2171run_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2172{
2173 struct ieee80211com *ic = vap->iv_ic;
2174 struct run_softc *sc = ic->ic_ifp->if_softc;
2175 struct ieee80211_key *k0;
2176 uint32_t i;
2177
2178 /*
2179 * When called back, key might be gone. So, make a copy
2180 * of some values need to delete keys before deferring.
2181 * But, because of LOR with node lock, cannot use lock here.
2182 * So, use atomic instead.
2183 */
2184 i = RUN_CMDQ_GET(&sc->cmdq_store);
2185 DPRINTF("cmdq_store=%d\n", i);
2186 sc->cmdq[i].func = run_key_delete_cb;
2187 sc->cmdq[i].arg0 = NULL;
2188 sc->cmdq[i].arg1 = sc;
2189 k0 = &sc->cmdq[i].key;
2190 k0->wk_flags = k->wk_flags;
2191 k0->wk_keyix = k->wk_keyix;
2192 /* matching wcid was written to wk_pad in run_key_set() */
2193 k0->wk_pad = k->wk_pad;
2194 ieee80211_runtask(ic, &sc->cmdq_task);
2195 return (1); /* return fake success */
2196
2197}
2198
2199static void
2200run_ratectl_to(void *arg)
2201{
2202 struct run_softc *sc = arg;
2203
2204 /* do it in a process context, so it can go sleep */
2205 ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2206 /* next timeout will be rescheduled in the callback task */
2207}
2208
2209/* ARGSUSED */
2210static void
2211run_ratectl_cb(void *arg, int pending)
2212{
2213 struct run_softc *sc = arg;
2214 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2215 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2216
2217 if (vap == NULL)
2218 return;
2219
2220 if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2221 run_iter_func(sc, vap->iv_bss);
2222 else {
2223 /*
2224 * run_reset_livelock() doesn't do anything with AMRR,
2225 * but Ralink wants us to call it every 1 sec. So, we
2226 * piggyback here rather than creating another callout.
2227 * Livelock may occur only in HOSTAP or IBSS mode
2228 * (when h/w is sending beacons).
2229 */
2230 RUN_LOCK(sc);
2231 run_reset_livelock(sc);
2232 /* just in case, there are some stats to drain */
2233 run_drain_fifo(sc);
2234 RUN_UNLOCK(sc);
2235 ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2236 }
2237
2238 if(sc->ratectl_run != RUN_RATECTL_OFF)
2239 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2240}
2241
2242static void
2243run_drain_fifo(void *arg)
2244{
2245 struct run_softc *sc = arg;
2246 struct ifnet *ifp = sc->sc_ifp;
2247 uint32_t stat;
2248 uint16_t (*wstat)[3];
2249 uint8_t wcid, mcs, pid;
2250 int8_t retry;
2251
2252 RUN_LOCK_ASSERT(sc, MA_OWNED);
2253
2254 for (;;) {
2255 /* drain Tx status FIFO (maxsize = 16) */
2256 run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2257 DPRINTFN(4, "tx stat 0x%08x\n", stat);
2258 if (!(stat & RT2860_TXQ_VLD))
2259 break;
2260
2261 wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2262
2263 /* if no ACK was requested, no feedback is available */
2264 if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2265 wcid == 0)
2266 continue;
2267
2268 /*
2269 * Even though each stat is Tx-complete-status like format,
2270 * the device can poll stats. Because there is no guarantee
2271 * that the referring node is still around when read the stats.
2272 * So that, if we use ieee80211_ratectl_tx_update(), we will
2273 * have hard time not to refer already freed node.
2274 *
2275 * To eliminate such page faults, we poll stats in softc.
2276 * Then, update the rates later with ieee80211_ratectl_tx_update().
2277 */
2278 wstat = &(sc->wcid_stats[wcid]);
2279 (*wstat)[RUN_TXCNT]++;
2280 if (stat & RT2860_TXQ_OK)
2281 (*wstat)[RUN_SUCCESS]++;
2282 else
2283 ifp->if_oerrors++;
2284 /*
2285 * Check if there were retries, ie if the Tx success rate is
2286 * different from the requested rate. Note that it works only
2287 * because we do not allow rate fallback from OFDM to CCK.
2288 */
2289 mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2290 pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2291 if ((retry = pid -1 - mcs) > 0) {
2292 (*wstat)[RUN_TXCNT] += retry;
2293 (*wstat)[RUN_RETRY] += retry;
2294 }
2295 }
2296 DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2297
2298 sc->fifo_cnt = 0;
2299}
2300
2301static void
2302run_iter_func(void *arg, struct ieee80211_node *ni)
2303{
2304 struct run_softc *sc = arg;
2305 struct ieee80211vap *vap = ni->ni_vap;
2306 struct ieee80211com *ic = ni->ni_ic;
2307 struct ifnet *ifp = ic->ic_ifp;
2308 struct run_node *rn = (void *)ni;
2309 union run_stats sta[2];
2310 uint16_t (*wstat)[3];
2311 int txcnt, success, retrycnt, error;
2312
2313 RUN_LOCK(sc);
2314
2315 if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2316 vap->iv_opmode == IEEE80211_M_STA)) {
2317 /* read statistic counters (clear on read) and update AMRR state */
2318 error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2319 sizeof sta);
2320 if (error != 0)
2321 goto fail;
2322
2323 /* count failed TX as errors */
2324 ifp->if_oerrors += le16toh(sta[0].error.fail);
2325
2326 retrycnt = le16toh(sta[1].tx.retry);
2327 success = le16toh(sta[1].tx.success);
2328 txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2329
2330 DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2331 retrycnt, success, le16toh(sta[0].error.fail));
2332 } else {
2333 wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2334
2335 if (wstat == &(sc->wcid_stats[0]) ||
2336 wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2337 goto fail;
2338
2339 txcnt = (*wstat)[RUN_TXCNT];
2340 success = (*wstat)[RUN_SUCCESS];
2341 retrycnt = (*wstat)[RUN_RETRY];
2342 DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2343 retrycnt, txcnt, success);
2344
2345 memset(wstat, 0, sizeof(*wstat));
2346 }
2347
2348 ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2349 rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2350
2351fail:
2352 RUN_UNLOCK(sc);
2353
2354 DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2355}
2356
2357static void
2358run_newassoc_cb(void *arg)
2359{
2360 struct run_cmdq *cmdq = arg;
2361 struct ieee80211_node *ni = cmdq->arg1;
2362 struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2363 uint8_t wcid = cmdq->wcid;
2364
2365 RUN_LOCK_ASSERT(sc, MA_OWNED);
2366
2367 run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2368 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2369
2370 memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2371}
2372
2373static void
2374run_newassoc(struct ieee80211_node *ni, int isnew)
2375{
2376 struct run_node *rn = (void *)ni;
2377 struct ieee80211_rateset *rs = &ni->ni_rates;
2378 struct ieee80211vap *vap = ni->ni_vap;
2379 struct ieee80211com *ic = vap->iv_ic;
2380 struct run_softc *sc = ic->ic_ifp->if_softc;
2381 uint8_t rate;
2382 uint8_t ridx;
10f2cea7 2383 uint8_t wcid;
12bd3c8b 2384 int i, j;
1a468664 2385#ifdef RUN_DEBUG
10f2cea7 2386 char ethstr[ETHER_ADDRSTRLEN + 1];
1a468664 2387#endif
10f2cea7
SW
2388
2389 wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2390 1 : RUN_AID2WCID(ni->ni_associd);
12bd3c8b
SW
2391
2392 if (wcid > RT2870_WCID_MAX) {
2393 device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2394 return;
2395 }
2396
2397 /* only interested in true associations */
2398 if (isnew && ni->ni_associd != 0) {
2399
2400 /*
2401 * This function could is called though timeout function.
2402 * Need to defer.
2403 */
2404 uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2405 DPRINTF("cmdq_store=%d\n", cnt);
2406 sc->cmdq[cnt].func = run_newassoc_cb;
2407 sc->cmdq[cnt].arg0 = NULL;
2408 sc->cmdq[cnt].arg1 = ni;
2409 sc->cmdq[cnt].wcid = wcid;
2410 ieee80211_runtask(ic, &sc->cmdq_task);
2411 }
2412
2413 DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
10f2cea7 2414 isnew, ni->ni_associd, kether_ntoa(ni->ni_macaddr, ethstr));
12bd3c8b
SW
2415
2416 for (i = 0; i < rs->rs_nrates; i++) {
2417 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2418 /* convert 802.11 rate to hardware rate index */
2419 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2420 if (rt2860_rates[ridx].rate == rate)
2421 break;
2422 rn->ridx[i] = ridx;
2423 /* determine rate of control response frames */
2424 for (j = i; j >= 0; j--) {
2425 if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2426 rt2860_rates[rn->ridx[i]].phy ==
2427 rt2860_rates[rn->ridx[j]].phy)
2428 break;
2429 }
2430 if (j >= 0) {
2431 rn->ctl_ridx[i] = rn->ridx[j];
2432 } else {
2433 /* no basic rate found, use mandatory one */
2434 rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2435 }
2436 DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2437 rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2438 }
2439 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2440 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2441 if (rt2860_rates[ridx].rate == rate)
2442 break;
2443 rn->mgt_ridx = ridx;
2444 DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2445
2446 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2447}
2448
2449/*
2450 * Return the Rx chain with the highest RSSI for a given frame.
2451 */
2452static __inline uint8_t
2453run_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2454{
2455 uint8_t rxchain = 0;
2456
2457 if (sc->nrxchains > 1) {
2458 if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2459 rxchain = 1;
2460 if (sc->nrxchains > 2)
2461 if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2462 rxchain = 2;
2463 }
2464 return (rxchain);
2465}
2466
2467static void
2468run_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2469{
2470 struct ifnet *ifp = sc->sc_ifp;
2471 struct ieee80211com *ic = ifp->if_l2com;
2472 struct ieee80211_frame *wh;
2473 struct ieee80211_node *ni;
2474 struct rt2870_rxd *rxd;
2475 struct rt2860_rxwi *rxwi;
2476 uint32_t flags;
2477 uint16_t len, phy;
2478 uint8_t ant, rssi;
2479 int8_t nf;
2480
2481 rxwi = mtod(m, struct rt2860_rxwi *);
2482 len = le16toh(rxwi->len) & 0xfff;
2483 if (__predict_false(len > dmalen)) {
2484 m_freem(m);
2485 ifp->if_ierrors++;
2486 DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2487 return;
2488 }
2489 /* Rx descriptor is located at the end */
2490 rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2491 flags = le32toh(rxd->flags);
2492
2493 if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2494 m_freem(m);
2495 ifp->if_ierrors++;
2496 DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2497 return;
2498 }
2499
2500 m->m_data += sizeof(struct rt2860_rxwi);
2501 m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2502
2503 wh = mtod(m, struct ieee80211_frame *);
2504
2505 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2506 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2507 m->m_flags |= M_WEP;
2508 }
2509
2510 if (flags & RT2860_RX_L2PAD) {
2511 DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2512 len += 2;
2513 }
2514
2515 ni = ieee80211_find_rxnode(ic,
2516 mtod(m, struct ieee80211_frame_min *));
2517
2518 if (__predict_false(flags & RT2860_RX_MICERR)) {
2519 /* report MIC failures to net80211 for TKIP */
2520 if (ni != NULL)
2521 ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2522 m_freem(m);
2523 ifp->if_ierrors++;
2524 DPRINTF("MIC error. Someone is lying.\n");
2525 return;
2526 }
2527
2528 ant = run_maxrssi_chain(sc, rxwi);
2529 rssi = rxwi->rssi[ant];
2530 nf = run_rssi2dbm(sc, rssi, ant);
2531
2532 m->m_pkthdr.rcvif = ifp;
2533 m->m_pkthdr.len = m->m_len = len;
2534
2535 if (ni != NULL) {
2536 (void)ieee80211_input(ni, m, rssi, nf);
2537 ieee80211_free_node(ni);
2538 } else {
2539 (void)ieee80211_input_all(ic, m, rssi, nf);
2540 }
2541
2542 if (__predict_false(ieee80211_radiotap_active(ic))) {
2543 struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2544
2545 tap->wr_flags = 0;
10f2cea7
SW
2546 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2547 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
12bd3c8b
SW
2548 tap->wr_antsignal = rssi;
2549 tap->wr_antenna = ant;
2550 tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2551 tap->wr_rate = 2; /* in case it can't be found below */
2552 phy = le16toh(rxwi->phy);
2553 switch (phy & RT2860_PHY_MODE) {
2554 case RT2860_PHY_CCK:
2555 switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2556 case 0: tap->wr_rate = 2; break;
2557 case 1: tap->wr_rate = 4; break;
2558 case 2: tap->wr_rate = 11; break;
2559 case 3: tap->wr_rate = 22; break;
2560 }
2561 if (phy & RT2860_PHY_SHPRE)
2562 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2563 break;
2564 case RT2860_PHY_OFDM:
2565 switch (phy & RT2860_PHY_MCS) {
2566 case 0: tap->wr_rate = 12; break;
2567 case 1: tap->wr_rate = 18; break;
2568 case 2: tap->wr_rate = 24; break;
2569 case 3: tap->wr_rate = 36; break;
2570 case 4: tap->wr_rate = 48; break;
2571 case 5: tap->wr_rate = 72; break;
2572 case 6: tap->wr_rate = 96; break;
2573 case 7: tap->wr_rate = 108; break;
2574 }
2575 break;
2576 }
2577 }
2578}
2579
2580static void
2581run_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2582{
2583 struct run_softc *sc = usbd_xfer_softc(xfer);
2584 struct ifnet *ifp = sc->sc_ifp;
2585 struct mbuf *m = NULL;
2586 struct mbuf *m0;
2587 uint32_t dmalen;
2588 int xferlen;
2589
2590 usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2591
2592 switch (USB_GET_STATE(xfer)) {
2593 case USB_ST_TRANSFERRED:
2594
2595 DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2596
10f2cea7
SW
2597 if (xferlen < (int)(sizeof(uint32_t) +
2598 sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) {
12bd3c8b
SW
2599 DPRINTF("xfer too short %d\n", xferlen);
2600 goto tr_setup;
2601 }
2602
2603 m = sc->rx_m;
2604 sc->rx_m = NULL;
2605
2606 /* FALLTHROUGH */
2607 case USB_ST_SETUP:
2608tr_setup:
2609 if (sc->rx_m == NULL) {
10f2cea7 2610 sc->rx_m = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR,
12bd3c8b
SW
2611 MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2612 }
2613 if (sc->rx_m == NULL) {
2614 DPRINTF("could not allocate mbuf - idle with stall\n");
2615 ifp->if_ierrors++;
2616 usbd_xfer_set_stall(xfer);
2617 usbd_xfer_set_frames(xfer, 0);
2618 } else {
2619 /*
2620 * Directly loading a mbuf cluster into DMA to
2621 * save some data copying. This works because
2622 * there is only one cluster.
2623 */
2624 usbd_xfer_set_frame_data(xfer, 0,
2625 mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2626 usbd_xfer_set_frames(xfer, 1);
2627 }
2628 usbd_transfer_submit(xfer);
2629 break;
2630
2631 default: /* Error */
2632 if (error != USB_ERR_CANCELLED) {
2633 /* try to clear stall first */
2634 usbd_xfer_set_stall(xfer);
2635
2636 if (error == USB_ERR_TIMEOUT)
2637 device_printf(sc->sc_dev, "device timeout\n");
2638
2639 ifp->if_ierrors++;
2640
2641 goto tr_setup;
2642 }
2643 if (sc->rx_m != NULL) {
2644 m_freem(sc->rx_m);
2645 sc->rx_m = NULL;
2646 }
2647 break;
2648 }
2649
2650 if (m == NULL)
2651 return;
2652
2653 /* inputting all the frames must be last */
2654
2655 RUN_UNLOCK(sc);
2656
2657 m->m_pkthdr.len = m->m_len = xferlen;
2658
2659 /* HW can aggregate multiple 802.11 frames in a single USB xfer */
2660 for(;;) {
2661 dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2662
10f2cea7
SW
2663 if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2664 ((dmalen & 3) != 0)) {
12bd3c8b
SW
2665 DPRINTF("bad DMA length %u\n", dmalen);
2666 break;
2667 }
10f2cea7 2668 if ((dmalen + 8) > (uint32_t)xferlen) {
12bd3c8b
SW
2669 DPRINTF("bad DMA length %u > %d\n",
2670 dmalen + 8, xferlen);
2671 break;
2672 }
2673
2674 /* If it is the last one or a single frame, we won't copy. */
2675 if ((xferlen -= dmalen + 8) <= 8) {
2676 /* trim 32-bit DMA-len header */
2677 m->m_data += 4;
2678 m->m_pkthdr.len = m->m_len -= 4;
2679 run_rx_frame(sc, m, dmalen);
2680 break;
2681 }
2682
2683 /* copy aggregated frames to another mbuf */
10f2cea7 2684 m0 = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
12bd3c8b
SW
2685 if (__predict_false(m0 == NULL)) {
2686 DPRINTF("could not allocate mbuf\n");
2687 ifp->if_ierrors++;
2688 break;
2689 }
2690 m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2691 dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2692 m0->m_pkthdr.len = m0->m_len =
2693 dmalen + sizeof(struct rt2870_rxd);
2694 run_rx_frame(sc, m0, dmalen);
2695
2696 /* update data ptr */
2697 m->m_data += dmalen + 8;
2698 m->m_pkthdr.len = m->m_len -= dmalen + 8;
2699 }
2700
2701 RUN_LOCK(sc);
2702}
2703
2704static void
2705run_tx_free(struct run_endpoint_queue *pq,
2706 struct run_tx_data *data, int txerr)
2707{
2708 if (data->m != NULL) {
2709 if (data->m->m_flags & M_TXCB)
2710 ieee80211_process_callback(data->ni, data->m,
2711 txerr ? ETIMEDOUT : 0);
2712 m_freem(data->m);
2713 data->m = NULL;
2714
2715 if (data->ni == NULL) {
2716 DPRINTF("no node\n");
2717 } else {
2718 ieee80211_free_node(data->ni);
2719 data->ni = NULL;
2720 }
2721 }
2722
2723 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2724 pq->tx_nfree++;
2725}
2726
2727static void
2728run_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index)
2729{
2730 struct run_softc *sc = usbd_xfer_softc(xfer);
2731 struct ifnet *ifp = sc->sc_ifp;
2732 struct ieee80211com *ic = ifp->if_l2com;
2733 struct run_tx_data *data;
2734 struct ieee80211vap *vap = NULL;
2735 struct usb_page_cache *pc;
2736 struct run_endpoint_queue *pq = &sc->sc_epq[index];
2737 struct mbuf *m;
2738 usb_frlength_t size;
2739 int actlen;
2740 int sumlen;
2741
2742 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2743
2744 switch (USB_GET_STATE(xfer)) {
2745 case USB_ST_TRANSFERRED:
2746 DPRINTFN(11, "transfer complete: %d "
2747 "bytes @ index %d\n", actlen, index);
2748
2749 data = usbd_xfer_get_priv(xfer);
2750
2751 run_tx_free(pq, data, 0);
10f2cea7 2752 ifq_clr_oactive(&ifp->if_snd);
12bd3c8b
SW
2753
2754 usbd_xfer_set_priv(xfer, NULL);
2755
2756 ifp->if_opackets++;
2757
2758 /* FALLTHROUGH */
2759 case USB_ST_SETUP:
2760tr_setup:
2761 data = STAILQ_FIRST(&pq->tx_qh);
2762 if (data == NULL)
2763 break;
2764
2765 STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2766
2767 m = data->m;
2768 if ((m->m_pkthdr.len +
2769 sizeof(data->desc) + 3 + 8) > RUN_MAX_TXSZ) {
2770 DPRINTF("data overflow, %u bytes\n",
2771 m->m_pkthdr.len);
2772
2773 ifp->if_oerrors++;
2774
2775 run_tx_free(pq, data, 1);
2776
2777 goto tr_setup;
2778 }
2779
2780 pc = usbd_xfer_get_frame(xfer, 0);
2781 size = sizeof(data->desc);
2782 usbd_copy_in(pc, 0, &data->desc, size);
2783 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2784 size += m->m_pkthdr.len;
2785 /*
2786 * Align end on a 4-byte boundary, pad 8 bytes (CRC +
2787 * 4-byte padding), and be sure to zero those trailing
2788 * bytes:
2789 */
2790 usbd_frame_zero(pc, size, ((-size) & 3) + 8);
2791 size += ((-size) & 3) + 8;
2792
2793 vap = data->ni->ni_vap;
2794 if (ieee80211_radiotap_active_vap(vap)) {
2795 struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2796 struct rt2860_txwi *txwi =
2797 (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2798
2799 tap->wt_flags = 0;
2800 tap->wt_rate = rt2860_rates[data->ridx].rate;
10f2cea7
SW
2801 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2802 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
12bd3c8b
SW
2803 tap->wt_hwqueue = index;
2804 if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2805 tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2806
2807 ieee80211_radiotap_tx(vap, m);
2808 }
2809
2810 DPRINTFN(11, "sending frame len=%u/%u @ index %d\n",
2811 m->m_pkthdr.len, size, index);
2812
2813 usbd_xfer_set_frame_len(xfer, 0, size);
2814 usbd_xfer_set_priv(xfer, data);
2815
2816 usbd_transfer_submit(xfer);
2817
10f2cea7 2818 run_start_locked(ifp);
12bd3c8b
SW
2819
2820 break;
2821
2822 default:
2823 DPRINTF("USB transfer error, %s\n",
2824 usbd_errstr(error));
2825
2826 data = usbd_xfer_get_priv(xfer);
2827
2828 ifp->if_oerrors++;
2829
2830 if (data != NULL) {
2831 if(data->ni != NULL)
2832 vap = data->ni->ni_vap;
2833 run_tx_free(pq, data, error);
2834 usbd_xfer_set_priv(xfer, NULL);
2835 }
2836 if (vap == NULL)
2837 vap = TAILQ_FIRST(&ic->ic_vaps);
2838
2839 if (error != USB_ERR_CANCELLED) {
2840 if (error == USB_ERR_TIMEOUT) {
2841 device_printf(sc->sc_dev, "device timeout\n");
2842 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2843 DPRINTF("cmdq_store=%d\n", i);
2844 sc->cmdq[i].func = run_usb_timeout_cb;
2845 sc->cmdq[i].arg0 = vap;
2846 ieee80211_runtask(ic, &sc->cmdq_task);
2847 }
2848
2849 /*
2850 * Try to clear stall first, also if other
2851 * errors occur, hence clearing stall
2852 * introduces a 50 ms delay:
2853 */
2854 usbd_xfer_set_stall(xfer);
2855 goto tr_setup;
2856 }
2857 break;
2858 }
2859}
2860
2861static void
2862run_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2863{
2864 run_bulk_tx_callbackN(xfer, error, 0);
2865}
2866
2867static void
2868run_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2869{
2870 run_bulk_tx_callbackN(xfer, error, 1);
2871}
2872
2873static void
2874run_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2875{
2876 run_bulk_tx_callbackN(xfer, error, 2);
2877}
2878
2879static void
2880run_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2881{
2882 run_bulk_tx_callbackN(xfer, error, 3);
2883}
2884
2885static void
2886run_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2887{
2888 run_bulk_tx_callbackN(xfer, error, 4);
2889}
2890
2891static void
2892run_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2893{
2894 run_bulk_tx_callbackN(xfer, error, 5);
2895}
2896
2897static void
2898run_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2899{
2900 struct mbuf *m = data->m;
2901 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2902 struct ieee80211vap *vap = data->ni->ni_vap;
2903 struct ieee80211_frame *wh;
2904 struct rt2870_txd *txd;
2905 struct rt2860_txwi *txwi;
2906 uint16_t xferlen;
2907 uint16_t mcs;
2908 uint8_t ridx = data->ridx;
2909 uint8_t pad;
2910
2911 /* get MCS code from rate index */
2912 mcs = rt2860_rates[ridx].mcs;
2913
2914 xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2915
2916 /* roundup to 32-bit alignment */
2917 xferlen = (xferlen + 3) & ~3;
2918
2919 txd = (struct rt2870_txd *)&data->desc;
2920 txd->len = htole16(xferlen);
2921
2922 wh = mtod(m, struct ieee80211_frame *);
2923
2924 /*
2925 * Ether both are true or both are false, the header
2926 * are nicely aligned to 32-bit. So, no L2 padding.
2927 */
2928 if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2929 pad = 0;
2930 else
2931 pad = 2;
2932
2933 /* setup TX Wireless Information */
2934 txwi = (struct rt2860_txwi *)(txd + 1);
2935 txwi->len = htole16(m->m_pkthdr.len - pad);
2936 if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2937 txwi->phy = htole16(RT2860_PHY_CCK);
2938 if (ridx != RT2860_RIDX_CCK1 &&
2939 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2940 mcs |= RT2860_PHY_SHPRE;
2941 } else
2942 txwi->phy = htole16(RT2860_PHY_OFDM);
2943 txwi->phy |= htole16(mcs);
2944
2945 /* check if RTS/CTS or CTS-to-self protection is required */
2946 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2947 (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2948 ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2949 rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
2950 txwi->txop |= RT2860_TX_TXOP_HT;
2951 else
2952 txwi->txop |= RT2860_TX_TXOP_BACKOFF;
2953
2954 if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
2955 txwi->xflags |= RT2860_TX_NSEQ;
2956}
2957
2958/* This function must be called locked */
2959static int
2960run_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2961{
2962 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2963 struct ieee80211vap *vap = ni->ni_vap;
2964 struct ieee80211_frame *wh;
2965 struct ieee80211_channel *chan;
2966 const struct ieee80211_txparam *tp;
2967 struct run_node *rn = (void *)ni;
2968 struct run_tx_data *data;
2969 struct rt2870_txd *txd;
2970 struct rt2860_txwi *txwi;
2971 uint16_t qos;
2972 uint16_t dur;
2973 uint16_t qid;
2974 uint8_t type;
2975 uint8_t tid;
2976 uint8_t ridx;
2977 uint8_t ctl_ridx;
2978 uint8_t qflags;
2979 uint8_t xflags = 0;
2980 int hasqos;
2981
10f2cea7 2982#if 0 /* XXX swildner: lock needed? */
12bd3c8b 2983 RUN_LOCK_ASSERT(sc, MA_OWNED);
10f2cea7 2984#endif
12bd3c8b
SW
2985
2986 wh = mtod(m, struct ieee80211_frame *);
2987
2988 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2989
2990 /*
2991 * There are 7 bulk endpoints: 1 for RX
2992 * and 6 for TX (4 EDCAs + HCCA + Prio).
2993 * Update 03-14-2009: some devices like the Planex GW-US300MiniS
2994 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
2995 */
2996 if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
2997 uint8_t *frm;
2998
2999 if(IEEE80211_HAS_ADDR4(wh))
3000 frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
3001 else
3002 frm =((struct ieee80211_qosframe *)wh)->i_qos;
3003
3004 qos = le16toh(*(const uint16_t *)frm);
3005 tid = qos & IEEE80211_QOS_TID;
3006 qid = TID_TO_WME_AC(tid);
3007 } else {
3008 qos = 0;
3009 tid = 0;
3010 qid = WME_AC_BE;
3011 }
3012 qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
3013
3014 DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
3015 qos, qid, tid, qflags);
3016
3017 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
3018 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
3019
3020 /* pickup a rate index */
3021 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
3022 type != IEEE80211_FC0_TYPE_DATA) {
3023 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3024 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3025 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3026 } else {
3027 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3028 ridx = rn->fix_ridx;
3029 else
3030 ridx = rn->amrr_ridx;
3031 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3032 }
3033
3034 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3035 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3036 IEEE80211_QOS_ACKPOLICY_NOACK)) {
3037 xflags |= RT2860_TX_ACK;
3038 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3039 dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3040 else
3041 dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3042 *(uint16_t *)wh->i_dur = htole16(dur);
3043 }
3044
3045 /* reserve slots for mgmt packets, just in case */
3046 if (sc->sc_epq[qid].tx_nfree < 3) {
3047 DPRINTFN(10, "tx ring %d is full\n", qid);
3048 return (-1);
3049 }
3050
3051 data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3052 STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3053 sc->sc_epq[qid].tx_nfree--;
3054
3055 txd = (struct rt2870_txd *)&data->desc;
3056 txd->flags = qflags;
3057 txwi = (struct rt2860_txwi *)(txd + 1);
3058 txwi->xflags = xflags;
10f2cea7
SW
3059 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3060 txwi->wcid = 0;
3061 } else {
3062 txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
3063 1 : RUN_AID2WCID(ni->ni_associd);
3064 }
12bd3c8b
SW
3065 /* clear leftover garbage bits */
3066 txwi->flags = 0;
3067 txwi->txop = 0;
3068
3069 data->m = m;
3070 data->ni = ni;
3071 data->ridx = ridx;
3072
3073 run_set_tx_desc(sc, data);
3074
3075 /*
3076 * The chip keeps track of 2 kind of Tx stats,
3077 * * TX_STAT_FIFO, for per WCID stats, and
3078 * * TX_STA_CNT0 for all-TX-in-one stats.
3079 *
3080 * To use FIFO stats, we need to store MCS into the driver-private
3081 * PacketID field. So that, we can tell whose stats when we read them.
3082 * We add 1 to the MCS because setting the PacketID field to 0 means
3083 * that we don't want feedback in TX_STAT_FIFO.
3084 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3085 *
3086 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3087 */
3088 if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3089 vap->iv_opmode == IEEE80211_M_MBSS) {
3090 uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3091 txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3092
3093 /*
3094 * Unlike PCI based devices, we don't get any interrupt from
3095 * USB devices, so we simulate FIFO-is-full interrupt here.
3096 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3097 * quickly get fulled. To prevent overflow, increment a counter on
3098 * every FIFO stat request, so we know how many slots are left.
3099 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3100 * are used only in those modes.
3101 * We just drain stats. AMRR gets updated every 1 sec by
3102 * run_ratectl_cb() via callout.
3103 * Call it early. Otherwise overflow.
3104 */
3105 if (sc->fifo_cnt++ == 10) {
3106 /*
3107 * With multiple vaps or if_bridge, if_start() is called
3108 * with a non-sleepable lock, tcpinp. So, need to defer.
3109 */
3110 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3111 DPRINTFN(6, "cmdq_store=%d\n", i);
3112 sc->cmdq[i].func = run_drain_fifo;
3113 sc->cmdq[i].arg0 = sc;
3114 ieee80211_runtask(ic, &sc->cmdq_task);
3115 }
3116 }
3117
3118 STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3119
10f2cea7 3120 RUN_LOCK(sc);
12bd3c8b 3121 usbd_transfer_start(sc->sc_xfer[qid]);
10f2cea7 3122 RUN_UNLOCK(sc);
12bd3c8b
SW
3123
3124 DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3125 (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3126 rt2860_rates[ridx].rate, qid);
3127
3128 return (0);
3129}
3130
3131static int
3132run_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3133{
3134 struct ifnet *ifp = sc->sc_ifp;
3135 struct ieee80211com *ic = ifp->if_l2com;
3136 struct run_node *rn = (void *)ni;
3137 struct run_tx_data *data;
3138 struct ieee80211_frame *wh;
3139 struct rt2870_txd *txd;
3140 struct rt2860_txwi *txwi;
3141 uint16_t dur;
3142 uint8_t ridx = rn->mgt_ridx;
3143 uint8_t type;
3144 uint8_t xflags = 0;
3145 uint8_t wflags = 0;
3146
3147 RUN_LOCK_ASSERT(sc, MA_OWNED);
3148
3149 wh = mtod(m, struct ieee80211_frame *);
3150
3151 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3152
3153 /* tell hardware to add timestamp for probe responses */
3154 if ((wh->i_fc[0] &
3155 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3156 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3157 wflags |= RT2860_TX_TS;
3158 else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3159 xflags |= RT2860_TX_ACK;
3160
3161 dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3162 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3163 *(uint16_t *)wh->i_dur = htole16(dur);
3164 }
3165
3166 if (sc->sc_epq[0].tx_nfree == 0) {
3167 /* let caller free mbuf */
10f2cea7 3168 ifq_set_oactive(&ifp->if_snd);
12bd3c8b
SW
3169 return (EIO);
3170 }
3171 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3172 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3173 sc->sc_epq[0].tx_nfree--;
3174
3175 txd = (struct rt2870_txd *)&data->desc;
3176 txd->flags = RT2860_TX_QSEL_EDCA;
3177 txwi = (struct rt2860_txwi *)(txd + 1);
3178 txwi->wcid = 0xff;
3179 txwi->flags = wflags;
3180 txwi->xflags = xflags;
3181 txwi->txop = 0; /* clear leftover garbage bits */
3182
3183 data->m = m;
3184 data->ni = ni;
3185 data->ridx = ridx;
3186
3187 run_set_tx_desc(sc, data);
3188
3189 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3190 (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3191 rt2860_rates[ridx].rate);
3192
3193 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3194
3195 usbd_transfer_start(sc->sc_xfer[0]);
3196
3197 return (0);
3198}
3199
3200static int
3201run_sendprot(struct run_softc *sc,
3202 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3203{
3204 struct ieee80211com *ic = ni->ni_ic;
3205 struct ieee80211_frame *wh;
3206 struct run_tx_data *data;
3207 struct rt2870_txd *txd;
3208 struct rt2860_txwi *txwi;
3209 struct mbuf *mprot;
3210 int ridx;
3211 int protrate;
3212 int ackrate;
3213 int pktlen;
3214 int isshort;
3215 uint16_t dur;
3216 uint8_t type;
3217 uint8_t wflags = 0;
3218 uint8_t xflags = 0;
3219
3220 RUN_LOCK_ASSERT(sc, MA_OWNED);
3221
3222 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3223 ("protection %d", prot));
3224
3225 wh = mtod(m, struct ieee80211_frame *);
3226 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3227 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3228
3229 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3230 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3231
3232 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3233 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3234 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3235 wflags = RT2860_TX_FRAG;
3236
3237 /* check that there are free slots before allocating the mbuf */
3238 if (sc->sc_epq[0].tx_nfree == 0) {
3239 /* let caller free mbuf */
10f2cea7 3240 ifq_set_oactive(&sc->sc_ifp->if_snd);
12bd3c8b
SW
3241 return (ENOBUFS);
3242 }
3243
3244 if (prot == IEEE80211_PROT_RTSCTS) {
3245 /* NB: CTS is the same size as an ACK */
3246 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3247 xflags |= RT2860_TX_ACK;
3248 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3249 } else {
3250 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3251 }
3252 if (mprot == NULL) {
3253 sc->sc_ifp->if_oerrors++;
3254 DPRINTF("could not allocate mbuf\n");
3255 return (ENOBUFS);
3256 }
3257
3258 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3259 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3260 sc->sc_epq[0].tx_nfree--;
3261
3262 txd = (struct rt2870_txd *)&data->desc;
3263 txd->flags = RT2860_TX_QSEL_EDCA;
3264 txwi = (struct rt2860_txwi *)(txd + 1);
3265 txwi->wcid = 0xff;
3266 txwi->flags = wflags;
3267 txwi->xflags = xflags;
3268 txwi->txop = 0; /* clear leftover garbage bits */
3269
3270 data->m = mprot;
3271 data->ni = ieee80211_ref_node(ni);
3272
3273 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3274 if (rt2860_rates[ridx].rate == protrate)
3275 break;
3276 data->ridx = ridx;
3277
3278 run_set_tx_desc(sc, data);
3279
3280 DPRINTFN(1, "sending prot len=%u rate=%u\n",
3281 m->m_pkthdr.len, rate);
3282
3283 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3284
3285 usbd_transfer_start(sc->sc_xfer[0]);
3286
3287 return (0);
3288}
3289
3290static int
3291run_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3292 const struct ieee80211_bpf_params *params)
3293{
3294 struct ieee80211com *ic = ni->ni_ic;
3295 struct ieee80211_frame *wh;
3296 struct run_tx_data *data;
3297 struct rt2870_txd *txd;
3298 struct rt2860_txwi *txwi;
3299 uint8_t type;
3300 uint8_t ridx;
3301 uint8_t rate;
3302 uint8_t opflags = 0;
3303 uint8_t xflags = 0;
3304 int error;
3305
3306 RUN_LOCK_ASSERT(sc, MA_OWNED);
3307
3308 KASSERT(params != NULL, ("no raw xmit params"));
3309
3310 wh = mtod(m, struct ieee80211_frame *);
3311 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3312
3313 rate = params->ibp_rate0;
3314 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3315 /* let caller free mbuf */
3316 return (EINVAL);
3317 }
3318
3319 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3320 xflags |= RT2860_TX_ACK;
3321 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3322 error = run_sendprot(sc, m, ni,
3323 params->ibp_flags & IEEE80211_BPF_RTS ?
3324 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3325 rate);
3326 if (error) {
3327 /* let caller free mbuf */
3328 return error;
3329 }
3330 opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3331 }
3332
3333 if (sc->sc_epq[0].tx_nfree == 0) {
3334 /* let caller free mbuf */
10f2cea7 3335 ifq_set_oactive(&sc->sc_ifp->if_snd);
12bd3c8b
SW
3336 DPRINTF("sending raw frame, but tx ring is full\n");
3337 return (EIO);
3338 }
3339 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3340 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3341 sc->sc_epq[0].tx_nfree--;
3342
3343 txd = (struct rt2870_txd *)&data->desc;
3344 txd->flags = RT2860_TX_QSEL_EDCA;
3345 txwi = (struct rt2860_txwi *)(txd + 1);
3346 txwi->wcid = 0xff;
3347 txwi->xflags = xflags;
3348 txwi->txop = opflags;
3349 txwi->flags = 0; /* clear leftover garbage bits */
3350
3351 data->m = m;
3352 data->ni = ni;
3353 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3354 if (rt2860_rates[ridx].rate == rate)
3355 break;
3356 data->ridx = ridx;
3357
3358 run_set_tx_desc(sc, data);
3359
3360 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3361 m->m_pkthdr.len, rate);
3362
3363 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3364
3365 usbd_transfer_start(sc->sc_xfer[0]);
3366
3367 return (0);
3368}
3369
3370static int
3371run_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3372 const struct ieee80211_bpf_params *params)
3373{
3374 struct ifnet *ifp = ni->ni_ic->ic_ifp;
3375 struct run_softc *sc = ifp->if_softc;
3376 int error = 0;
3377
3378 RUN_LOCK(sc);
3379
3380 /* prevent management frames from being sent if we're not ready */
10f2cea7 3381 if (!(ifp->if_flags & IFF_RUNNING)) {
12bd3c8b
SW
3382 error = ENETDOWN;
3383 goto done;
3384 }
3385
3386 if (params == NULL) {
3387 /* tx mgt packet */
3388 if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3389 ifp->if_oerrors++;
3390 DPRINTF("mgt tx failed\n");
3391 goto done;
3392 }
3393 } else {
3394 /* tx raw packet with param */
3395 if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3396 ifp->if_oerrors++;
3397 DPRINTF("tx with param failed\n");
3398 goto done;
3399 }
3400 }
3401
3402 ifp->if_opackets++;
3403
3404done:
3405 RUN_UNLOCK(sc);
3406
3407 if (error != 0) {
3408 if(m != NULL)
3409 m_freem(m);
3410 ieee80211_free_node(ni);
3411 }
3412
3413 return (error);
3414}
3415
3416static void
10f2cea7 3417run_start_locked(struct ifnet *ifp)
12bd3c8b
SW
3418{
3419 struct run_softc *sc = ifp->if_softc;
3420 struct ieee80211_node *ni;
10f2cea7 3421 struct mbuf *m = NULL;
12bd3c8b 3422
ce73fac0 3423 if ((ifp->if_flags & IFF_RUNNING) == 0)
12bd3c8b 3424 return;
12bd3c8b
SW
3425
3426 for (;;) {
3427 /* send data frames */
10f2cea7 3428 m = ifq_dequeue(&ifp->if_snd);
12bd3c8b
SW
3429 if (m == NULL)
3430 break;
3431
3432 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3433 if (run_tx(sc, m, ni) != 0) {
10f2cea7
SW
3434 ifq_prepend(&ifp->if_snd, m);
3435 ifq_set_oactive(&ifp->if_snd);
12bd3c8b
SW
3436 break;
3437 }
3438 }
10f2cea7 3439}
12bd3c8b 3440
10f2cea7
SW
3441static void
3442run_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
3443{
3444 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
3445 run_start_locked(ifp);
12bd3c8b
SW
3446}
3447
3448static int
10f2cea7 3449run_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
12bd3c8b
SW
3450{
3451 struct run_softc *sc = ifp->if_softc;
3452 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3453 struct ifreq *ifr = (struct ifreq *) data;
3454 int startall = 0;
10f2cea7
SW
3455 int error;
3456
3457 RUN_LOCK(sc);
3458 error = sc->sc_detached ? ENXIO : 0;
3459 RUN_UNLOCK(sc);
3460 if (error)
3461 return (error);
12bd3c8b
SW
3462
3463 switch (cmd) {
3464 case SIOCSIFFLAGS:
3465 RUN_LOCK(sc);
3466 if (ifp->if_flags & IFF_UP) {
10f2cea7 3467 if (!(ifp->if_flags & IFF_RUNNING)){
12bd3c8b
SW
3468 startall = 1;
3469 run_init_locked(sc);
3470 } else
3471 run_update_promisc_locked(ifp);
3472 } else {
10f2cea7 3473 if (ifp->if_flags & IFF_RUNNING &&
12bd3c8b
SW
3474 (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3475 run_stop(sc);
3476 }
3477 }
3478 RUN_UNLOCK(sc);
3479 if (startall)
3480 ieee80211_start_all(ic);
3481 break;
3482 case SIOCGIFMEDIA:
3483 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3484 break;
3485 case SIOCGIFADDR:
3486 error = ether_ioctl(ifp, cmd, data);
3487 break;
3488 default:
3489 error = EINVAL;
3490 break;
3491 }
3492
3493 return (error);
3494}
3495
3496static void
3497run_set_agc(struct run_softc *sc, uint8_t agc)
3498{
3499 uint8_t bbp;
3500
3501 if (sc->mac_ver == 0x3572) {
3502 run_bbp_read(sc, 27, &bbp);
3503 bbp &= ~(0x3 << 5);
3504 run_bbp_write(sc, 27, bbp | 0 << 5); /* select Rx0 */
3505 run_bbp_write(sc, 66, agc);
3506 run_bbp_write(sc, 27, bbp | 1 << 5); /* select Rx1 */
3507 run_bbp_write(sc, 66, agc);
3508 } else
3509 run_bbp_write(sc, 66, agc);
3510}
3511
3512static void
3513run_select_chan_group(struct run_softc *sc, int group)
3514{
3515 uint32_t tmp;
3516 uint8_t agc;
3517
3518 run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3519 run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3520 run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3521 run_bbp_write(sc, 86, 0x00);
3522
3523 if (group == 0) {
3524 if (sc->ext_2ghz_lna) {
3525 run_bbp_write(sc, 82, 0x62);
3526 run_bbp_write(sc, 75, 0x46);
3527 } else {
3528 run_bbp_write(sc, 82, 0x84);
3529 run_bbp_write(sc, 75, 0x50);
3530 }
3531 } else {
3532 if (sc->mac_ver == 0x3572)
3533 run_bbp_write(sc, 82, 0x94);
3534 else
3535 run_bbp_write(sc, 82, 0xf2);
3536 if (sc->ext_5ghz_lna)
3537 run_bbp_write(sc, 75, 0x46);
3538 else
3539 run_bbp_write(sc, 75, 0x50);
3540 }
3541
3542 run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3543 tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3544 tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3545 run_write(sc, RT2860_TX_BAND_CFG, tmp);
3546
3547 /* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3548 tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3549 if (sc->nrxchains > 1)
3550 tmp |= RT2860_LNA_PE1_EN;
3551 if (group == 0) { /* 2GHz */
3552 tmp |= RT2860_PA_PE_G0_EN;
3553 if (sc->ntxchains > 1)
3554 tmp |= RT2860_PA_PE_G1_EN;
3555 } else { /* 5GHz */
3556 tmp |= RT2860_PA_PE_A0_EN;
3557 if (sc->ntxchains > 1)
3558 tmp |= RT2860_PA_PE_A1_EN;
3559 }
3560 if (sc->mac_ver == 0x3572) {
3561 run_rt3070_rf_write(sc, 8, 0x00);
3562 run_write(sc, RT2860_TX_PIN_CFG, tmp);
3563 run_rt3070_rf_write(sc, 8, 0x80);
3564 } else
3565 run_write(sc, RT2860_TX_PIN_CFG, tmp);
3566
3567 /* set initial AGC value */
3568 if (group == 0) { /* 2GHz band */
3569 if (sc->mac_ver >= 0x3070)
3570 agc = 0x1c + sc->lna[0] * 2;
3571 else
3572 agc = 0x2e + sc->lna[0];
3573 } else { /* 5GHz band */
3574 if (sc->mac_ver == 0x3572)
3575 agc = 0x22 + (sc->lna[group] * 5) / 3;
3576 else
3577 agc = 0x32 + (sc->lna[group] * 5) / 3;
3578 }
3579 run_set_agc(sc, agc);
3580}
3581
3582static void
3583run_rt2870_set_chan(struct run_softc *sc, uint32_t chan)
3584{
3585 const struct rfprog *rfprog = rt2860_rf2850;
3586 uint32_t r2, r3, r4;
3587 int8_t txpow1, txpow2;
3588 int i;
3589
3590 /* find the settings for this channel (we know it exists) */
3591 for (i = 0; rfprog[i].chan != chan; i++);
3592
3593 r2 = rfprog[i].r2;
3594 if (sc->ntxchains == 1)
3595 r2 |= 1 << 12; /* 1T: disable Tx chain 2 */
3596 if (sc->nrxchains == 1)
3597 r2 |= 1 << 15 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
3598 else if (sc->nrxchains == 2)
3599 r2 |= 1 << 4; /* 2R: disable Rx chain 3 */
3600
3601 /* use Tx power values from EEPROM */
3602 txpow1 = sc->txpow1[i];
3603 txpow2 = sc->txpow2[i];
3604 if (chan > 14) {
3605 if (txpow1 >= 0)
3606 txpow1 = txpow1 << 1 | 1;
3607 else
3608 txpow1 = (7 + txpow1) << 1;
3609 if (txpow2 >= 0)
3610 txpow2 = txpow2 << 1 | 1;
3611 else
3612 txpow2 = (7 + txpow2) << 1;
3613 }
3614 r3 = rfprog[i].r3 | txpow1 << 7;
3615 r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3616
3617 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3618 run_rt2870_rf_write(sc, RT2860_RF2, r2);
3619 run_rt2870_rf_write(sc, RT2860_RF3, r3);
3620 run_rt2870_rf_write(sc, RT2860_RF4, r4);
3621
3622 run_delay(sc, 10);
3623
3624 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3625 run_rt2870_rf_write(sc, RT2860_RF2, r2);
3626 run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3627 run_rt2870_rf_write(sc, RT2860_RF4, r4);
3628
3629 run_delay(sc, 10);
3630
3631 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3632 run_rt2870_rf_write(sc, RT2860_RF2, r2);
3633 run_rt2870_rf_write(sc, RT2860_RF3, r3);
3634 run_rt2870_rf_write(sc, RT2860_RF4, r4);
3635}
3636
3637static void
3638run_rt3070_set_chan(struct run_softc *sc, uint32_t chan)
3639{
3640 int8_t txpow1, txpow2;
3641 uint8_t rf;
3642 int i;
3643
3644 /* RT3070 is 2GHz only */
3645 KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3646
3647 /* find the settings for this channel (we know it exists) */
3648 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3649
3650 /* use Tx power values from EEPROM */
3651 txpow1 = sc->txpow1[i];
3652 txpow2 = sc->txpow2[i];
3653
3654 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3655 run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3656 run_rt3070_rf_read(sc, 6, &rf);
3657 rf = (rf & ~0x03) | rt3070_freqs[i].r;
3658 run_rt3070_rf_write(sc, 6, rf);
3659
3660 /* set Tx0 power */
3661 run_rt3070_rf_read(sc, 12, &rf);
3662 rf = (rf & ~0x1f) | txpow1;
3663 run_rt3070_rf_write(sc, 12, rf);
3664
3665 /* set Tx1 power */
3666 run_rt3070_rf_read(sc, 13, &rf);
3667 rf = (rf & ~0x1f) | txpow2;
3668 run_rt3070_rf_write(sc, 13, rf);
3669
3670 run_rt3070_rf_read(sc, 1, &rf);
3671 rf &= ~0xfc;
3672 if (sc->ntxchains == 1)
3673 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
3674 else if (sc->ntxchains == 2)
3675 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
3676 if (sc->nrxchains == 1)
3677 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
3678 else if (sc->nrxchains == 2)
3679 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
3680 run_rt3070_rf_write(sc, 1, rf);
3681
3682 /* set RF offset */
3683 run_rt3070_rf_read(sc, 23, &rf);
3684 rf = (rf & ~0x7f) | sc->freq;
3685 run_rt3070_rf_write(sc, 23, rf);
3686
3687 /* program RF filter */
3688 run_rt3070_rf_read(sc, 24, &rf); /* Tx */
3689 rf = (rf & ~0x3f) | sc->rf24_20mhz;
3690 run_rt3070_rf_write(sc, 24, rf);
3691 run_rt3070_rf_read(sc, 31, &rf); /* Rx */
3692 rf = (rf & ~0x3f) | sc->rf24_20mhz;
3693 run_rt3070_rf_write(sc, 31, rf);
3694
3695 /* enable RF tuning */
3696 run_rt3070_rf_read(sc, 7, &rf);
3697 run_rt3070_rf_write(sc, 7, rf | 0x01);
3698}
3699
3700static void
3701run_rt3572_set_chan(struct run_softc *sc, u_int chan)
3702{
3703 int8_t txpow1, txpow2;
3704 uint32_t tmp;
3705 uint8_t rf;
3706 int i;
3707
3708 /* find the settings for this channel (we know it exists) */
3709 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3710
3711 /* use Tx power values from EEPROM */
3712 txpow1 = sc->txpow1[i];
3713 txpow2 = sc->txpow2[i];
3714
3715 if (chan <= 14) {
3716 run_bbp_write(sc, 25, sc->bbp25);
3717 run_bbp_write(sc, 26, sc->bbp26);
3718 } else {
3719 /* enable IQ phase correction */
3720 run_bbp_write(sc, 25, 0x09);
3721 run_bbp_write(sc, 26, 0xff);
3722 }
3723
3724 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3725 run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3726 run_rt3070_rf_read(sc, 6, &rf);
3727 rf = (rf & ~0x0f) | rt3070_freqs[i].r;
3728 rf |= (chan <= 14) ? 0x08 : 0x04;
3729 run_rt3070_rf_write(sc, 6, rf);
3730
3731 /* set PLL mode */
3732 run_rt3070_rf_read(sc, 5, &rf);
3733 rf &= ~(0x08 | 0x04);
3734 rf |= (chan <= 14) ? 0x04 : 0x08;
3735 run_rt3070_rf_write(sc, 5, rf);
3736
3737 /* set Tx power for chain 0 */
3738 if (chan <= 14)
3739 rf = 0x60 | txpow1;
3740 else
3741 rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3742 run_rt3070_rf_write(sc, 12, rf);
3743
3744 /* set Tx power for chain 1 */
3745 if (chan <= 14)
3746 rf = 0x60 | txpow2;
3747 else
3748 rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3749 run_rt3070_rf_write(sc, 13, rf);
3750
3751 /* set Tx/Rx streams */
3752 run_rt3070_rf_read(sc, 1, &rf);
3753 rf &= ~0xfc;
3754 if (sc->ntxchains == 1)
3755 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
3756 else if (sc->ntxchains == 2)
3757 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
3758 if (sc->nrxchains == 1)
3759 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
3760 else if (sc->nrxchains == 2)
3761 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
3762 run_rt3070_rf_write(sc, 1, rf);
3763
3764 /* set RF offset */
3765 run_rt3070_rf_read(sc, 23, &rf);
3766 rf = (rf & ~0x7f) | sc->freq;
3767 run_rt3070_rf_write(sc, 23, rf);
3768
3769 /* program RF filter */
3770 rf = sc->rf24_20mhz;
3771 run_rt3070_rf_write(sc, 24, rf); /* Tx */
3772 run_rt3070_rf_write(sc, 31, rf); /* Rx */
3773
3774 /* enable RF tuning */
3775 run_rt3070_rf_read(sc, 7, &rf);
3776 rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3777 run_rt3070_rf_write(sc, 7, rf);
3778
3779 /* TSSI */
3780 rf = (chan <= 14) ? 0xc3 : 0xc0;
3781 run_rt3070_rf_write(sc, 9, rf);
3782
3783 /* set loop filter 1 */
3784 run_rt3070_rf_write(sc, 10, 0xf1);
3785 /* set loop filter 2 */
3786 run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3787
3788 /* set tx_mx2_ic */
3789 run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3790 /* set tx_mx1_ic */
3791 if (chan <= 14)
3792 rf = 0x48 | sc->txmixgain_2ghz;
3793 else
3794 rf = 0x78 | sc->txmixgain_5ghz;
3795 run_rt3070_rf_write(sc, 16, rf);
3796
3797 /* set tx_lo1 */
3798 run_rt3070_rf_write(sc, 17, 0x23);
3799 /* set tx_lo2 */
3800 if (chan <= 14)
3801 rf = 0x93;
3802 else if (chan <= 64)
3803 rf = 0xb7;
3804 else if (chan <= 128)
3805 rf = 0x74;
3806 else
3807 rf = 0x72;
3808 run_rt3070_rf_write(sc, 19, rf);
3809
3810 /* set rx_lo1 */
3811 if (chan <= 14)
3812 rf = 0xb3;
3813 else if (chan <= 64)
3814 rf = 0xf6;
3815 else if (chan <= 128)
3816 rf = 0xf4;
3817 else
3818 rf = 0xf3;
3819 run_rt3070_rf_write(sc, 20, rf);
3820
3821 /* set pfd_delay */
3822 if (chan <= 14)
3823 rf = 0x15;
3824 else if (chan <= 64)
3825 rf = 0x3d;
3826 else
3827 rf = 0x01;
3828 run_rt3070_rf_write(sc, 25, rf);
3829
3830 /* set rx_lo2 */
3831 run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3832 /* set ldo_rf_vc */
3833 run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3834 /* set drv_cc */
3835 run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3836
3837 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3838 tmp &= ~0x8080;
3839 if (chan <= 14)
3840 tmp |= 0x80;
3841 run_write(sc, RT2860_GPIO_CTRL, tmp);
3842
3843 /* enable RF tuning */
3844 run_rt3070_rf_read(sc, 7, &rf);
3845 run_rt3070_rf_write(sc, 7, rf | 0x01);
3846
3847 run_delay(sc, 2);
3848}
3849
3850static void
3851run_set_rx_antenna(struct run_softc *sc, int aux)
3852{
3853 uint32_t tmp;
3854
3855 if (aux) {
3856 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
3857 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3858 run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
3859 } else {
3860 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
3861 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3862 run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
3863 }
3864}
3865
3866static int
3867run_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
3868{
3869 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3870 uint32_t chan, group;
3871
3872 chan = ieee80211_chan2ieee(ic, c);
3873 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
3874 return (EINVAL);
3875
3876 if (sc->mac_ver == 0x3572)
3877 run_rt3572_set_chan(sc, chan);
3878 else if (sc->mac_ver >= 0x3070)
3879 run_rt3070_set_chan(sc, chan);
3880 else
3881 run_rt2870_set_chan(sc, chan);
3882
3883 /* determine channel group */
3884 if (chan <= 14)
3885 group = 0;
3886 else if (chan <= 64)
3887 group = 1;
3888 else if (chan <= 128)
3889 group = 2;
3890 else
3891 group = 3;
3892
3893 /* XXX necessary only when group has changed! */
3894 run_select_chan_group(sc, group);
3895
3896 run_delay(sc, 10);
3897
3898 return (0);
3899}
3900
3901static void
3902run_set_channel(struct ieee80211com *ic)
3903{
3904 struct run_softc *sc = ic->ic_ifp->if_softc;
3905
3906 RUN_LOCK(sc);
3907 run_set_chan(sc, ic->ic_curchan);
3908 RUN_UNLOCK(sc);
3909
3910 return;
3911}
3912
3913static void
3914run_scan_start(struct ieee80211com *ic)
3915{
3916 struct run_softc *sc = ic->ic_ifp->if_softc;
3917 uint32_t tmp;
3918
3919 RUN_LOCK(sc);
3920
3921 /* abort TSF synchronization */
3922 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
3923 run_write(sc, RT2860_BCN_TIME_CFG,
3924 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
3925 RT2860_TBTT_TIMER_EN));
3926 run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
3927
3928 RUN_UNLOCK(sc);
3929
3930 return;
3931}
3932
3933static void
3934run_scan_end(struct ieee80211com *ic)
3935{
3936 struct run_softc *sc = ic->ic_ifp->if_softc;
3937
3938 RUN_LOCK(sc);
3939
3940 run_enable_tsf_sync(sc);
3941 /* XXX keep local copy */
3942 run_set_bssid(sc, sc->sc_bssid);
3943
3944 RUN_UNLOCK(sc);
3945
3946 return;
3947}
3948
3949/*
3950 * Could be called from ieee80211_node_timeout()
3951 * (non-sleepable thread)
3952 */
3953static void
3954run_update_beacon(struct ieee80211vap *vap, int item)
3955{
3956 struct ieee80211com *ic = vap->iv_ic;
3957 struct run_softc *sc = ic->ic_ifp->if_softc;
3958 struct run_vap *rvp = RUN_VAP(vap);
3959 int mcast = 0;
3960 uint32_t i;
3961
3962 KASSERT(vap != NULL, ("no beacon"));
3963
3964 switch (item) {
3965 case IEEE80211_BEACON_ERP:
3966 run_updateslot(ic->ic_ifp);
3967 break;
3968 case IEEE80211_BEACON_HTINFO:
3969 run_updateprot(ic);
3970 break;
3971 case IEEE80211_BEACON_TIM:
3972 mcast = 1; /*TODO*/
3973 break;
3974 default:
3975 break;
3976 }
3977
3978 setbit(rvp->bo.bo_flags, item);
3979 ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
3980
3981 i = RUN_CMDQ_GET(&sc->cmdq_store);
3982 DPRINTF("cmdq_store=%d\n", i);
3983 sc->cmdq[i].func = run_update_beacon_cb;
3984 sc->cmdq[i].arg0 = vap;
3985 ieee80211_runtask(ic, &sc->cmdq_task);
3986
3987 return;
3988}
3989
3990static void
3991run_update_beacon_cb(void *arg)
3992{
3993 struct ieee80211vap *vap = arg;
3994 struct run_vap *rvp = RUN_VAP(vap);
3995 struct ieee80211com *ic = vap->iv_ic;
3996 struct run_softc *sc = ic->ic_ifp->if_softc;
3997 struct rt2860_txwi txwi;
3998 struct mbuf *m;
3999 uint8_t ridx;
4000
4001 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
4002 return;
10f2cea7
SW
4003 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
4004 return;
12bd3c8b
SW
4005
4006 /*
4007 * No need to call ieee80211_beacon_update(), run_update_beacon()
4008 * is taking care of apropriate calls.
4009 */
4010 if (rvp->beacon_mbuf == NULL) {
4011 rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
4012 &rvp->bo);
4013 if (rvp->beacon_mbuf == NULL)
4014 return;
4015 }
4016 m = rvp->beacon_mbuf;
4017
4018 memset(&txwi, 0, sizeof txwi);
4019 txwi.wcid = 0xff;
4020 txwi.len = htole16(m->m_pkthdr.len);
4021 /* send beacons at the lowest available rate */
4022 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4023 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
4024 txwi.phy = htole16(rt2860_rates[ridx].mcs);
4025 if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
4026 txwi.phy |= htole16(RT2860_PHY_OFDM);
4027 txwi.txop = RT2860_TX_TXOP_HT;
4028 txwi.flags = RT2860_TX_TS;
4029 txwi.xflags = RT2860_TX_NSEQ;
4030
4031 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
4032 (uint8_t *)&txwi, sizeof txwi);
4033 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
4034 mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1); /* roundup len */
4035
4036 return;
4037}
4038
4039static void
4040run_updateprot(struct ieee80211com *ic)
4041{
4042 struct run_softc *sc = ic->ic_ifp->if_softc;
4043 uint32_t i;
4044
4045 i = RUN_CMDQ_GET(&sc->cmdq_store);
4046 DPRINTF("cmdq_store=%d\n", i);
4047 sc->cmdq[i].func = run_updateprot_cb;
4048 sc->cmdq[i].arg0 = ic;
4049 ieee80211_runtask(ic, &sc->cmdq_task);
4050}
4051
4052static void
4053run_updateprot_cb(void *arg)
4054{
4055 struct ieee80211com *ic = arg;
4056 struct run_softc *sc = ic->ic_ifp->if_softc;
4057 uint32_t tmp;
4058
4059 tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4060 /* setup protection frame rate (MCS code) */
4061 tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4062 rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4063 rt2860_rates[RT2860_RIDX_CCK11].mcs;
4064
4065 /* CCK frames don't require protection */
4066 run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4067 if (ic->ic_flags & IEEE80211_F_USEPROT) {
4068 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4069 tmp |= RT2860_PROT_CTRL_RTS_CTS;
4070 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4071 tmp |= RT2860_PROT_CTRL_CTS;
4072 }
4073 run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4074}
4075
4076static void
4077run_usb_timeout_cb(void *arg)
4078{
4079 struct ieee80211vap *vap = arg;
4080 struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4081
4082 RUN_LOCK_ASSERT(sc, MA_OWNED);
4083
4084 if(vap->iv_state == IEEE80211_S_RUN &&
4085 vap->iv_opmode != IEEE80211_M_STA)
4086 run_reset_livelock(sc);
4087 else if (vap->iv_state == IEEE80211_S_SCAN) {