if_run.c revision 259031
1203134Sthompsa/*-
2205042Sthompsa * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr>
3205042Sthompsa * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca>
4205042Sthompsa * USB Consulting, Hans Petter Selasky <hselasky@freebsd.org>
5203134Sthompsa *
6203134Sthompsa * Permission to use, copy, modify, and distribute this software for any
7203134Sthompsa * purpose with or without fee is hereby granted, provided that the above
8203134Sthompsa * copyright notice and this permission notice appear in all copies.
9203134Sthompsa *
10203134Sthompsa * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11203134Sthompsa * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12203134Sthompsa * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13203134Sthompsa * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14203134Sthompsa * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15203134Sthompsa * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16203134Sthompsa * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17203134Sthompsa */
18203134Sthompsa
19203134Sthompsa#include <sys/cdefs.h>
20203134Sthompsa__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_run.c 259031 2013-12-06 15:17:28Z kevlo $");
21203134Sthompsa
22203134Sthompsa/*-
23257955Skevlo * Ralink Technology RT2700U/RT2800U/RT3000U/RT3900E chipset driver.
24203134Sthompsa * http://www.ralinktech.com/
25203134Sthompsa */
26203134Sthompsa
27203134Sthompsa#include <sys/param.h>
28203134Sthompsa#include <sys/sockio.h>
29203134Sthompsa#include <sys/sysctl.h>
30203134Sthompsa#include <sys/lock.h>
31203134Sthompsa#include <sys/mutex.h>
32203134Sthompsa#include <sys/mbuf.h>
33203134Sthompsa#include <sys/kernel.h>
34203134Sthompsa#include <sys/socket.h>
35203134Sthompsa#include <sys/systm.h>
36203134Sthompsa#include <sys/malloc.h>
37203134Sthompsa#include <sys/module.h>
38203134Sthompsa#include <sys/bus.h>
39203134Sthompsa#include <sys/endian.h>
40203134Sthompsa#include <sys/linker.h>
41203134Sthompsa#include <sys/firmware.h>
42203134Sthompsa#include <sys/kdb.h>
43203134Sthompsa
44203134Sthompsa#include <machine/bus.h>
45203134Sthompsa#include <machine/resource.h>
46203134Sthompsa#include <sys/rman.h>
47203134Sthompsa
48203134Sthompsa#include <net/bpf.h>
49203134Sthompsa#include <net/if.h>
50257176Sglebius#include <net/if_var.h>
51203134Sthompsa#include <net/if_arp.h>
52203134Sthompsa#include <net/ethernet.h>
53203134Sthompsa#include <net/if_dl.h>
54203134Sthompsa#include <net/if_media.h>
55203134Sthompsa#include <net/if_types.h>
56203134Sthompsa
57203134Sthompsa#include <netinet/in.h>
58203134Sthompsa#include <netinet/in_systm.h>
59203134Sthompsa#include <netinet/in_var.h>
60203134Sthompsa#include <netinet/if_ether.h>
61203134Sthompsa#include <netinet/ip.h>
62203134Sthompsa
63203134Sthompsa#include <net80211/ieee80211_var.h>
64203134Sthompsa#include <net80211/ieee80211_regdomain.h>
65203134Sthompsa#include <net80211/ieee80211_radiotap.h>
66206358Srpaulo#include <net80211/ieee80211_ratectl.h>
67203134Sthompsa
68203134Sthompsa#include <dev/usb/usb.h>
69203134Sthompsa#include <dev/usb/usbdi.h>
70203134Sthompsa#include "usbdevs.h"
71203134Sthompsa
72203134Sthompsa#define USB_DEBUG_VAR run_debug
73203134Sthompsa#include <dev/usb/usb_debug.h>
74203134Sthompsa
75220235Skevlo#include <dev/usb/wlan/if_runreg.h>
76220235Skevlo#include <dev/usb/wlan/if_runvar.h>
77203134Sthompsa
78207077Sthompsa#ifdef	USB_DEBUG
79203134Sthompsa#define RUN_DEBUG
80203134Sthompsa#endif
81203134Sthompsa
82203134Sthompsa#ifdef	RUN_DEBUG
83203134Sthompsaint run_debug = 0;
84227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run");
85203134SthompsaSYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0,
86203134Sthompsa    "run debug level");
87203134Sthompsa#endif
88203134Sthompsa
89203134Sthompsa#define IEEE80211_HAS_ADDR4(wh) \
90203134Sthompsa	(((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
91203134Sthompsa
92208019Sthompsa/*
93208019Sthompsa * Because of LOR in run_key_delete(), use atomic instead.
94208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[].
95208019Sthompsa */
96208019Sthompsa#define RUN_CMDQ_GET(c)	(atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
97208019Sthompsa
98223486Shselaskystatic const STRUCT_USB_HOST_ID run_devs[] = {
99209918Sthompsa#define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
100209918Sthompsa    RUN_DEV(ABOCOM,		RT2770),
101209918Sthompsa    RUN_DEV(ABOCOM,		RT2870),
102209918Sthompsa    RUN_DEV(ABOCOM,		RT3070),
103209918Sthompsa    RUN_DEV(ABOCOM,		RT3071),
104209918Sthompsa    RUN_DEV(ABOCOM,		RT3072),
105209918Sthompsa    RUN_DEV(ABOCOM2,		RT2870_1),
106209918Sthompsa    RUN_DEV(ACCTON,		RT2770),
107209918Sthompsa    RUN_DEV(ACCTON,		RT2870_1),
108209918Sthompsa    RUN_DEV(ACCTON,		RT2870_2),
109209918Sthompsa    RUN_DEV(ACCTON,		RT2870_3),
110209918Sthompsa    RUN_DEV(ACCTON,		RT2870_4),
111209918Sthompsa    RUN_DEV(ACCTON,		RT2870_5),
112209918Sthompsa    RUN_DEV(ACCTON,		RT3070),
113209918Sthompsa    RUN_DEV(ACCTON,		RT3070_1),
114209918Sthompsa    RUN_DEV(ACCTON,		RT3070_2),
115209918Sthompsa    RUN_DEV(ACCTON,		RT3070_3),
116209918Sthompsa    RUN_DEV(ACCTON,		RT3070_4),
117209918Sthompsa    RUN_DEV(ACCTON,		RT3070_5),
118209918Sthompsa    RUN_DEV(AIRTIES,		RT3070),
119209918Sthompsa    RUN_DEV(ALLWIN,		RT2070),
120209918Sthompsa    RUN_DEV(ALLWIN,		RT2770),
121209918Sthompsa    RUN_DEV(ALLWIN,		RT2870),
122209918Sthompsa    RUN_DEV(ALLWIN,		RT3070),
123209918Sthompsa    RUN_DEV(ALLWIN,		RT3071),
124209918Sthompsa    RUN_DEV(ALLWIN,		RT3072),
125209918Sthompsa    RUN_DEV(ALLWIN,		RT3572),
126209918Sthompsa    RUN_DEV(AMIGO,		RT2870_1),
127209918Sthompsa    RUN_DEV(AMIGO,		RT2870_2),
128209918Sthompsa    RUN_DEV(AMIT,		CGWLUSB2GNR),
129209918Sthompsa    RUN_DEV(AMIT,		RT2870_1),
130209918Sthompsa    RUN_DEV(AMIT2,		RT2870),
131209918Sthompsa    RUN_DEV(ASUS,		RT2870_1),
132209918Sthompsa    RUN_DEV(ASUS,		RT2870_2),
133209918Sthompsa    RUN_DEV(ASUS,		RT2870_3),
134209918Sthompsa    RUN_DEV(ASUS,		RT2870_4),
135209918Sthompsa    RUN_DEV(ASUS,		RT2870_5),
136209918Sthompsa    RUN_DEV(ASUS,		USBN13),
137209918Sthompsa    RUN_DEV(ASUS,		RT3070_1),
138239358Shselasky    RUN_DEV(ASUS,		USB_N53),
139209918Sthompsa    RUN_DEV(ASUS2,		USBN11),
140209918Sthompsa    RUN_DEV(AZUREWAVE,		RT2870_1),
141209918Sthompsa    RUN_DEV(AZUREWAVE,		RT2870_2),
142209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_1),
143209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_2),
144209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_3),
145209918Sthompsa    RUN_DEV(BELKIN,		F5D8053V3),
146209918Sthompsa    RUN_DEV(BELKIN,		F5D8055),
147226534Shselasky    RUN_DEV(BELKIN,		F5D8055V2),
148209918Sthompsa    RUN_DEV(BELKIN,		F6D4050V1),
149256500Shselasky    RUN_DEV(BELKIN,		F6D4050V2),
150209918Sthompsa    RUN_DEV(BELKIN,		RT2870_1),
151209918Sthompsa    RUN_DEV(BELKIN,		RT2870_2),
152226534Shselasky    RUN_DEV(CISCOLINKSYS,	AE1000),
153209918Sthompsa    RUN_DEV(CISCOLINKSYS2,	RT3070),
154209918Sthompsa    RUN_DEV(CISCOLINKSYS3,	RT3070),
155209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_1),
156209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_2),
157209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_3),
158209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_4),
159209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_5),
160209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_6),
161209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_7),
162209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_8),
163209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT3070_1),
164209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT3070_2),
165209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	VIGORN61),
166209918Sthompsa    RUN_DEV(COREGA,		CGWLUSB300GNM),
167209918Sthompsa    RUN_DEV(COREGA,		RT2870_1),
168209918Sthompsa    RUN_DEV(COREGA,		RT2870_2),
169209918Sthompsa    RUN_DEV(COREGA,		RT2870_3),
170209918Sthompsa    RUN_DEV(COREGA,		RT3070),
171209918Sthompsa    RUN_DEV(CYBERTAN,		RT2870),
172209918Sthompsa    RUN_DEV(DLINK,		RT2870),
173209918Sthompsa    RUN_DEV(DLINK,		RT3072),
174255238Sbr    RUN_DEV(DLINK,		DWA127),
175257955Skevlo    RUN_DEV(DLINK,		DWA140B3),
176209918Sthompsa    RUN_DEV(DLINK2,		DWA130),
177209918Sthompsa    RUN_DEV(DLINK2,		RT2870_1),
178209918Sthompsa    RUN_DEV(DLINK2,		RT2870_2),
179209918Sthompsa    RUN_DEV(DLINK2,		RT3070_1),
180209918Sthompsa    RUN_DEV(DLINK2,		RT3070_2),
181209918Sthompsa    RUN_DEV(DLINK2,		RT3070_3),
182209918Sthompsa    RUN_DEV(DLINK2,		RT3070_4),
183209918Sthompsa    RUN_DEV(DLINK2,		RT3070_5),
184209918Sthompsa    RUN_DEV(DLINK2,		RT3072),
185209918Sthompsa    RUN_DEV(DLINK2,		RT3072_1),
186209918Sthompsa    RUN_DEV(EDIMAX,		EW7717),
187209918Sthompsa    RUN_DEV(EDIMAX,		EW7718),
188209918Sthompsa    RUN_DEV(EDIMAX,		RT2870_1),
189209918Sthompsa    RUN_DEV(ENCORE,		RT3070_1),
190209918Sthompsa    RUN_DEV(ENCORE,		RT3070_2),
191209918Sthompsa    RUN_DEV(ENCORE,		RT3070_3),
192209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB31N),
193209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB32L),
194209918Sthompsa    RUN_DEV(GIGABYTE,		RT2870_1),
195209918Sthompsa    RUN_DEV(GIGASET,		RT3070_1),
196209918Sthompsa    RUN_DEV(GIGASET,		RT3070_2),
197209918Sthompsa    RUN_DEV(GUILLEMOT,		HWNU300),
198209918Sthompsa    RUN_DEV(HAWKING,		HWUN2),
199209918Sthompsa    RUN_DEV(HAWKING,		RT2870_1),
200209918Sthompsa    RUN_DEV(HAWKING,		RT2870_2),
201209918Sthompsa    RUN_DEV(HAWKING,		RT3070),
202209918Sthompsa    RUN_DEV(IODATA,		RT3072_1),
203209918Sthompsa    RUN_DEV(IODATA,		RT3072_2),
204209918Sthompsa    RUN_DEV(IODATA,		RT3072_3),
205209918Sthompsa    RUN_DEV(IODATA,		RT3072_4),
206209918Sthompsa    RUN_DEV(LINKSYS4,		RT3070),
207209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB100),
208209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB54GCV3),
209209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600N),
210209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600NV2),
211209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_1),
212209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_2),
213209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_3),
214230333Shselasky    RUN_DEV(LOGITEC,		LANW300NU2),
215238274Shrs    RUN_DEV(LOGITEC,		LANW150NU2),
216248458Shselasky    RUN_DEV(LOGITEC,		LANW300NU2S),
217209918Sthompsa    RUN_DEV(MELCO,		RT2870_1),
218209918Sthompsa    RUN_DEV(MELCO,		RT2870_2),
219209918Sthompsa    RUN_DEV(MELCO,		WLIUCAG300N),
220209918Sthompsa    RUN_DEV(MELCO,		WLIUCG300N),
221219257Sdaichi    RUN_DEV(MELCO,		WLIUCG301N),
222209918Sthompsa    RUN_DEV(MELCO,		WLIUCGN),
223227781Shselasky    RUN_DEV(MELCO,		WLIUCGNM),
224238274Shrs    RUN_DEV(MELCO,		WLIUCGNM2),
225209918Sthompsa    RUN_DEV(MOTOROLA4,		RT2770),
226209918Sthompsa    RUN_DEV(MOTOROLA4,		RT3070),
227209918Sthompsa    RUN_DEV(MSI,		RT3070_1),
228209918Sthompsa    RUN_DEV(MSI,		RT3070_2),
229209918Sthompsa    RUN_DEV(MSI,		RT3070_3),
230209918Sthompsa    RUN_DEV(MSI,		RT3070_4),
231209918Sthompsa    RUN_DEV(MSI,		RT3070_5),
232209918Sthompsa    RUN_DEV(MSI,		RT3070_6),
233209918Sthompsa    RUN_DEV(MSI,		RT3070_7),
234209918Sthompsa    RUN_DEV(MSI,		RT3070_8),
235209918Sthompsa    RUN_DEV(MSI,		RT3070_9),
236209918Sthompsa    RUN_DEV(MSI,		RT3070_10),
237209918Sthompsa    RUN_DEV(MSI,		RT3070_11),
238209918Sthompsa    RUN_DEV(OVISLINK,		RT3072),
239209918Sthompsa    RUN_DEV(PARA,		RT3070),
240209918Sthompsa    RUN_DEV(PEGATRON,		RT2870),
241209918Sthompsa    RUN_DEV(PEGATRON,		RT3070),
242209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_2),
243209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_3),
244209918Sthompsa    RUN_DEV(PHILIPS,		RT2870),
245209918Sthompsa    RUN_DEV(PLANEX2,		GWUS300MINIS),
246209918Sthompsa    RUN_DEV(PLANEX2,		GWUSMICRON),
247209918Sthompsa    RUN_DEV(PLANEX2,		RT2870),
248209918Sthompsa    RUN_DEV(PLANEX2,		RT3070),
249209918Sthompsa    RUN_DEV(QCOM,		RT2870),
250209918Sthompsa    RUN_DEV(QUANTA,		RT3070),
251209918Sthompsa    RUN_DEV(RALINK,		RT2070),
252209918Sthompsa    RUN_DEV(RALINK,		RT2770),
253209918Sthompsa    RUN_DEV(RALINK,		RT2870),
254209918Sthompsa    RUN_DEV(RALINK,		RT3070),
255209918Sthompsa    RUN_DEV(RALINK,		RT3071),
256209918Sthompsa    RUN_DEV(RALINK,		RT3072),
257209918Sthompsa    RUN_DEV(RALINK,		RT3370),
258209918Sthompsa    RUN_DEV(RALINK,		RT3572),
259257955Skevlo    RUN_DEV(RALINK,		RT5370),
260209918Sthompsa    RUN_DEV(RALINK,		RT8070),
261226534Shselasky    RUN_DEV(SAMSUNG,		WIS09ABGN),
262209918Sthompsa    RUN_DEV(SAMSUNG2,		RT2870_1),
263209918Sthompsa    RUN_DEV(SENAO,		RT2870_1),
264209918Sthompsa    RUN_DEV(SENAO,		RT2870_2),
265209918Sthompsa    RUN_DEV(SENAO,		RT2870_3),
266209918Sthompsa    RUN_DEV(SENAO,		RT2870_4),
267209918Sthompsa    RUN_DEV(SENAO,		RT3070),
268209918Sthompsa    RUN_DEV(SENAO,		RT3071),
269209918Sthompsa    RUN_DEV(SENAO,		RT3072_1),
270209918Sthompsa    RUN_DEV(SENAO,		RT3072_2),
271209918Sthompsa    RUN_DEV(SENAO,		RT3072_3),
272209918Sthompsa    RUN_DEV(SENAO,		RT3072_4),
273209918Sthompsa    RUN_DEV(SENAO,		RT3072_5),
274209918Sthompsa    RUN_DEV(SITECOMEU,		RT2770),
275209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_1),
276209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_2),
277209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_3),
278209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_4),
279209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070),
280209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_2),
281209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_3),
282209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_4),
283209918Sthompsa    RUN_DEV(SITECOMEU,		RT3071),
284209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_1),
285209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_2),
286209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_3),
287209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_4),
288209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_5),
289209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_6),
290209918Sthompsa    RUN_DEV(SITECOMEU,		WL608),
291209918Sthompsa    RUN_DEV(SPARKLAN,		RT2870_1),
292209918Sthompsa    RUN_DEV(SPARKLAN,		RT3070),
293209918Sthompsa    RUN_DEV(SWEEX2,		LW153),
294209918Sthompsa    RUN_DEV(SWEEX2,		LW303),
295209918Sthompsa    RUN_DEV(SWEEX2,		LW313),
296209918Sthompsa    RUN_DEV(TOSHIBA,		RT3070),
297209918Sthompsa    RUN_DEV(UMEDIA,		RT2870_1),
298209918Sthompsa    RUN_DEV(ZCOM,		RT2870_1),
299209918Sthompsa    RUN_DEV(ZCOM,		RT2870_2),
300209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_1),
301209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_2),
302209918Sthompsa    RUN_DEV(ZINWELL,		RT3070),
303209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_1),
304209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_2),
305209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_1),
306209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_2),
307209918Sthompsa#undef RUN_DEV
308203134Sthompsa};
309203134Sthompsa
310203134Sthompsastatic device_probe_t	run_match;
311203134Sthompsastatic device_attach_t	run_attach;
312203134Sthompsastatic device_detach_t	run_detach;
313203134Sthompsa
314203134Sthompsastatic usb_callback_t	run_bulk_rx_callback;
315203134Sthompsastatic usb_callback_t	run_bulk_tx_callback0;
316203134Sthompsastatic usb_callback_t	run_bulk_tx_callback1;
317203134Sthompsastatic usb_callback_t	run_bulk_tx_callback2;
318203134Sthompsastatic usb_callback_t	run_bulk_tx_callback3;
319203134Sthompsastatic usb_callback_t	run_bulk_tx_callback4;
320203134Sthompsastatic usb_callback_t	run_bulk_tx_callback5;
321203134Sthompsa
322203134Sthompsastatic void	run_bulk_tx_callbackN(struct usb_xfer *xfer,
323257429Shselasky		    usb_error_t error, u_int index);
324203134Sthompsastatic struct ieee80211vap *run_vap_create(struct ieee80211com *,
325228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
326228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
327228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
328203134Sthompsastatic void	run_vap_delete(struct ieee80211vap *);
329208019Sthompsastatic void	run_cmdq_cb(void *, int);
330203134Sthompsastatic void	run_setup_tx_list(struct run_softc *,
331203134Sthompsa		    struct run_endpoint_queue *);
332203134Sthompsastatic void	run_unsetup_tx_list(struct run_softc *,
333203134Sthompsa		    struct run_endpoint_queue *);
334203134Sthompsastatic int	run_load_microcode(struct run_softc *);
335203134Sthompsastatic int	run_reset(struct run_softc *);
336203134Sthompsastatic usb_error_t run_do_request(struct run_softc *,
337203134Sthompsa		    struct usb_device_request *, void *);
338203134Sthompsastatic int	run_read(struct run_softc *, uint16_t, uint32_t *);
339203134Sthompsastatic int	run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
340203134Sthompsastatic int	run_write_2(struct run_softc *, uint16_t, uint16_t);
341203134Sthompsastatic int	run_write(struct run_softc *, uint16_t, uint32_t);
342203134Sthompsastatic int	run_write_region_1(struct run_softc *, uint16_t,
343203134Sthompsa		    const uint8_t *, int);
344203134Sthompsastatic int	run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
345203134Sthompsastatic int	run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
346203134Sthompsastatic int	run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
347258733Skevlostatic int	run_rt2870_rf_write(struct run_softc *, uint32_t);
348203134Sthompsastatic int	run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
349203134Sthompsastatic int	run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
350203134Sthompsastatic int	run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
351203134Sthompsastatic int	run_bbp_write(struct run_softc *, uint8_t, uint8_t);
352203134Sthompsastatic int	run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
353257955Skevlostatic const char *run_get_rf(uint16_t);
354203134Sthompsastatic int	run_read_eeprom(struct run_softc *);
355203134Sthompsastatic struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
356203134Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
357203134Sthompsastatic int	run_media_change(struct ifnet *);
358203134Sthompsastatic int	run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
359203134Sthompsastatic int	run_wme_update(struct ieee80211com *);
360208019Sthompsastatic void	run_wme_update_cb(void *);
361203134Sthompsastatic void	run_key_update_begin(struct ieee80211vap *);
362203134Sthompsastatic void	run_key_update_end(struct ieee80211vap *);
363208019Sthompsastatic void	run_key_set_cb(void *);
364208019Sthompsastatic int	run_key_set(struct ieee80211vap *, struct ieee80211_key *,
365257955Skevlo		    const uint8_t mac[IEEE80211_ADDR_LEN]);
366208019Sthompsastatic void	run_key_delete_cb(void *);
367208019Sthompsastatic int	run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
368206358Srpaulostatic void	run_ratectl_to(void *);
369206358Srpaulostatic void	run_ratectl_cb(void *, int);
370208019Sthompsastatic void	run_drain_fifo(void *);
371203134Sthompsastatic void	run_iter_func(void *, struct ieee80211_node *);
372208019Sthompsastatic void	run_newassoc_cb(void *);
373203134Sthompsastatic void	run_newassoc(struct ieee80211_node *, int);
374203134Sthompsastatic void	run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
375203134Sthompsastatic void	run_tx_free(struct run_endpoint_queue *pq,
376203134Sthompsa		    struct run_tx_data *, int);
377208019Sthompsastatic void	run_set_tx_desc(struct run_softc *, struct run_tx_data *);
378203134Sthompsastatic int	run_tx(struct run_softc *, struct mbuf *,
379203134Sthompsa		    struct ieee80211_node *);
380203134Sthompsastatic int	run_tx_mgt(struct run_softc *, struct mbuf *,
381203134Sthompsa		    struct ieee80211_node *);
382203134Sthompsastatic int	run_sendprot(struct run_softc *, const struct mbuf *,
383203134Sthompsa		    struct ieee80211_node *, int, int);
384203134Sthompsastatic int	run_tx_param(struct run_softc *, struct mbuf *,
385203134Sthompsa		    struct ieee80211_node *,
386203134Sthompsa		    const struct ieee80211_bpf_params *);
387203134Sthompsastatic int	run_raw_xmit(struct ieee80211_node *, struct mbuf *,
388203134Sthompsa		    const struct ieee80211_bpf_params *);
389203134Sthompsastatic void	run_start(struct ifnet *);
390203134Sthompsastatic int	run_ioctl(struct ifnet *, u_long, caddr_t);
391205042Sthompsastatic void	run_set_agc(struct run_softc *, uint8_t);
392203134Sthompsastatic void	run_select_chan_group(struct run_softc *, int);
393203134Sthompsastatic void	run_set_rx_antenna(struct run_softc *, int);
394203134Sthompsastatic void	run_rt2870_set_chan(struct run_softc *, u_int);
395203134Sthompsastatic void	run_rt3070_set_chan(struct run_softc *, u_int);
396205042Sthompsastatic void	run_rt3572_set_chan(struct run_softc *, u_int);
397257955Skevlostatic void	run_rt5390_set_chan(struct run_softc *, u_int);
398203134Sthompsastatic int	run_set_chan(struct run_softc *, struct ieee80211_channel *);
399203134Sthompsastatic void	run_set_channel(struct ieee80211com *);
400203134Sthompsastatic void	run_scan_start(struct ieee80211com *);
401203134Sthompsastatic void	run_scan_end(struct ieee80211com *);
402203134Sthompsastatic void	run_update_beacon(struct ieee80211vap *, int);
403208019Sthompsastatic void	run_update_beacon_cb(void *);
404203134Sthompsastatic void	run_updateprot(struct ieee80211com *);
405218492Sbschmidtstatic void	run_updateprot_cb(void *);
406208019Sthompsastatic void	run_usb_timeout_cb(void *);
407203134Sthompsastatic void	run_reset_livelock(struct run_softc *);
408203134Sthompsastatic void	run_enable_tsf_sync(struct run_softc *);
409203134Sthompsastatic void	run_enable_mrr(struct run_softc *);
410203134Sthompsastatic void	run_set_txpreamble(struct run_softc *);
411203134Sthompsastatic void	run_set_basicrates(struct run_softc *);
412203134Sthompsastatic void	run_set_leds(struct run_softc *, uint16_t);
413203134Sthompsastatic void	run_set_bssid(struct run_softc *, const uint8_t *);
414203134Sthompsastatic void	run_set_macaddr(struct run_softc *, const uint8_t *);
415203134Sthompsastatic void	run_updateslot(struct ifnet *);
416218492Sbschmidtstatic void	run_updateslot_cb(void *);
417208019Sthompsastatic void	run_update_mcast(struct ifnet *);
418203134Sthompsastatic int8_t	run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
419203134Sthompsastatic void	run_update_promisc_locked(struct ifnet *);
420203134Sthompsastatic void	run_update_promisc(struct ifnet *);
421257955Skevlostatic void	run_rt5390_bbp_init(struct run_softc *);
422203134Sthompsastatic int	run_bbp_init(struct run_softc *);
423203134Sthompsastatic int	run_rt3070_rf_init(struct run_softc *);
424257955Skevlostatic void	run_rt5390_rf_init(struct run_softc *);
425203134Sthompsastatic int	run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
426203134Sthompsa		    uint8_t *);
427205042Sthompsastatic void	run_rt3070_rf_setup(struct run_softc *);
428203134Sthompsastatic int	run_txrx_enable(struct run_softc *);
429257955Skevlostatic void	run_adjust_freq_offset(struct run_softc *);
430203134Sthompsastatic void	run_init(void *);
431203134Sthompsastatic void	run_init_locked(struct run_softc *);
432203134Sthompsastatic void	run_stop(void *);
433257429Shselaskystatic void	run_delay(struct run_softc *, u_int);
434203134Sthompsa
435203134Sthompsastatic const struct {
436208019Sthompsa	uint16_t	reg;
437203134Sthompsa	uint32_t	val;
438203134Sthompsa} rt2870_def_mac[] = {
439203134Sthompsa	RT2870_DEF_MAC
440203134Sthompsa};
441203134Sthompsa
442203134Sthompsastatic const struct {
443203134Sthompsa	uint8_t	reg;
444203134Sthompsa	uint8_t	val;
445203134Sthompsa} rt2860_def_bbp[] = {
446203134Sthompsa	RT2860_DEF_BBP
447257955Skevlo},rt5390_def_bbp[] = {
448257955Skevlo	RT5390_DEF_BBP
449203134Sthompsa};
450203134Sthompsa
451203134Sthompsastatic const struct rfprog {
452203134Sthompsa	uint8_t		chan;
453203134Sthompsa	uint32_t	r1, r2, r3, r4;
454203134Sthompsa} rt2860_rf2850[] = {
455203134Sthompsa	RT2860_RF2850
456203134Sthompsa};
457203134Sthompsa
458203134Sthompsastruct {
459203134Sthompsa	uint8_t	n, r, k;
460205042Sthompsa} rt3070_freqs[] = {
461205042Sthompsa	RT3070_RF3052
462203134Sthompsa};
463203134Sthompsa
464203134Sthompsastatic const struct {
465203134Sthompsa	uint8_t	reg;
466203134Sthompsa	uint8_t	val;
467203134Sthompsa} rt3070_def_rf[] = {
468203134Sthompsa	RT3070_DEF_RF
469205042Sthompsa},rt3572_def_rf[] = {
470205042Sthompsa	RT3572_DEF_RF
471257955Skevlo},rt5390_def_rf[] = {
472257955Skevlo	RT5390_DEF_RF
473257955Skevlo},rt5392_def_rf[] = {
474257955Skevlo	RT5392_DEF_RF
475203134Sthompsa};
476203134Sthompsa
477203134Sthompsastatic const struct usb_config run_config[RUN_N_XFER] = {
478203134Sthompsa    [RUN_BULK_TX_BE] = {
479203134Sthompsa	.type = UE_BULK,
480203134Sthompsa	.endpoint = UE_ADDR_ANY,
481203134Sthompsa	.ep_index = 0,
482203134Sthompsa	.direction = UE_DIR_OUT,
483203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
484203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
485203134Sthompsa	.callback = run_bulk_tx_callback0,
486203134Sthompsa	.timeout = 5000,	/* ms */
487203134Sthompsa    },
488203134Sthompsa    [RUN_BULK_TX_BK] = {
489203134Sthompsa	.type = UE_BULK,
490203134Sthompsa	.endpoint = UE_ADDR_ANY,
491203134Sthompsa	.direction = UE_DIR_OUT,
492203134Sthompsa	.ep_index = 1,
493203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
494203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
495203134Sthompsa	.callback = run_bulk_tx_callback1,
496203134Sthompsa	.timeout = 5000,	/* ms */
497203134Sthompsa    },
498203134Sthompsa    [RUN_BULK_TX_VI] = {
499203134Sthompsa	.type = UE_BULK,
500203134Sthompsa	.endpoint = UE_ADDR_ANY,
501203134Sthompsa	.direction = UE_DIR_OUT,
502203134Sthompsa	.ep_index = 2,
503203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
504203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
505203134Sthompsa	.callback = run_bulk_tx_callback2,
506203134Sthompsa	.timeout = 5000,	/* ms */
507203134Sthompsa    },
508203134Sthompsa    [RUN_BULK_TX_VO] = {
509203134Sthompsa	.type = UE_BULK,
510203134Sthompsa	.endpoint = UE_ADDR_ANY,
511203134Sthompsa	.direction = UE_DIR_OUT,
512203134Sthompsa	.ep_index = 3,
513203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
514203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
515203134Sthompsa	.callback = run_bulk_tx_callback3,
516203134Sthompsa	.timeout = 5000,	/* ms */
517203134Sthompsa    },
518203134Sthompsa    [RUN_BULK_TX_HCCA] = {
519203134Sthompsa	.type = UE_BULK,
520203134Sthompsa	.endpoint = UE_ADDR_ANY,
521203134Sthompsa	.direction = UE_DIR_OUT,
522203134Sthompsa	.ep_index = 4,
523203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
524203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
525203134Sthompsa	.callback = run_bulk_tx_callback4,
526203134Sthompsa	.timeout = 5000,	/* ms */
527203134Sthompsa    },
528203134Sthompsa    [RUN_BULK_TX_PRIO] = {
529203134Sthompsa	.type = UE_BULK,
530203134Sthompsa	.endpoint = UE_ADDR_ANY,
531203134Sthompsa	.direction = UE_DIR_OUT,
532203134Sthompsa	.ep_index = 5,
533203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
534203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
535203134Sthompsa	.callback = run_bulk_tx_callback5,
536203134Sthompsa	.timeout = 5000,	/* ms */
537203134Sthompsa    },
538203134Sthompsa    [RUN_BULK_RX] = {
539203134Sthompsa	.type = UE_BULK,
540203134Sthompsa	.endpoint = UE_ADDR_ANY,
541203134Sthompsa	.direction = UE_DIR_IN,
542203134Sthompsa	.bufsize = RUN_MAX_RXSZ,
543203134Sthompsa	.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
544203134Sthompsa	.callback = run_bulk_rx_callback,
545203134Sthompsa    }
546203134Sthompsa};
547203134Sthompsa
548220235Skevlostatic int
549203134Sthompsarun_match(device_t self)
550203134Sthompsa{
551203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
552203134Sthompsa
553203134Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
554203134Sthompsa		return (ENXIO);
555203134Sthompsa	if (uaa->info.bConfigIndex != 0)
556203134Sthompsa		return (ENXIO);
557203134Sthompsa	if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
558203134Sthompsa		return (ENXIO);
559203134Sthompsa
560203134Sthompsa	return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
561203134Sthompsa}
562203134Sthompsa
563203134Sthompsastatic int
564203134Sthompsarun_attach(device_t self)
565203134Sthompsa{
566203134Sthompsa	struct run_softc *sc = device_get_softc(self);
567203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
568203134Sthompsa	struct ieee80211com *ic;
569203134Sthompsa	struct ifnet *ifp;
570205042Sthompsa	uint32_t ver;
571258082Skevlo	int ntries, error;
572203134Sthompsa	uint8_t iface_index, bands;
573203134Sthompsa
574203134Sthompsa	device_set_usb_desc(self);
575203134Sthompsa	sc->sc_udev = uaa->device;
576203134Sthompsa	sc->sc_dev = self;
577203134Sthompsa
578203134Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
579203134Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
580203134Sthompsa
581203134Sthompsa	iface_index = RT2860_IFACE_INDEX;
582208019Sthompsa
583203134Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
584203134Sthompsa	    sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
585203134Sthompsa	if (error) {
586205042Sthompsa		device_printf(self, "could not allocate USB transfers, "
587203134Sthompsa		    "err=%s\n", usbd_errstr(error));
588203134Sthompsa		goto detach;
589203134Sthompsa	}
590203134Sthompsa
591203134Sthompsa	RUN_LOCK(sc);
592203134Sthompsa
593203134Sthompsa	/* wait for the chip to settle */
594203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
595209917Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
596203134Sthompsa			RUN_UNLOCK(sc);
597203134Sthompsa			goto detach;
598203134Sthompsa		}
599205042Sthompsa		if (ver != 0 && ver != 0xffffffff)
600203134Sthompsa			break;
601203134Sthompsa		run_delay(sc, 10);
602203134Sthompsa	}
603203134Sthompsa	if (ntries == 100) {
604203138Sthompsa		device_printf(sc->sc_dev,
605203138Sthompsa		    "timeout waiting for NIC to initialize\n");
606203134Sthompsa		RUN_UNLOCK(sc);
607203134Sthompsa		goto detach;
608203134Sthompsa	}
609205042Sthompsa	sc->mac_ver = ver >> 16;
610205042Sthompsa	sc->mac_rev = ver & 0xffff;
611203134Sthompsa
612203134Sthompsa	/* retrieve RF rev. no and various other things from EEPROM */
613203134Sthompsa	run_read_eeprom(sc);
614203134Sthompsa
615203138Sthompsa	device_printf(sc->sc_dev,
616203138Sthompsa	    "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
617205042Sthompsa	    sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
618203138Sthompsa	    sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
619203134Sthompsa
620203134Sthompsa	RUN_UNLOCK(sc);
621203134Sthompsa
622203134Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
623220235Skevlo	if (ifp == NULL) {
624203138Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
625203134Sthompsa		goto detach;
626203134Sthompsa	}
627203134Sthompsa	ic = ifp->if_l2com;
628203134Sthompsa
629203134Sthompsa	ifp->if_softc = sc;
630203134Sthompsa	if_initname(ifp, "run", device_get_unit(sc->sc_dev));
631203134Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
632203134Sthompsa	ifp->if_init = run_init;
633203134Sthompsa	ifp->if_ioctl = run_ioctl;
634203134Sthompsa	ifp->if_start = run_start;
635207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
636207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
637203134Sthompsa	IFQ_SET_READY(&ifp->if_snd);
638203134Sthompsa
639203134Sthompsa	ic->ic_ifp = ifp;
640203134Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
641203134Sthompsa	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
642208019Sthompsa
643203134Sthompsa	/* set device capabilities */
644203134Sthompsa	ic->ic_caps =
645203134Sthompsa	    IEEE80211_C_STA |		/* station mode supported */
646203134Sthompsa	    IEEE80211_C_MONITOR |	/* monitor mode supported */
647203134Sthompsa	    IEEE80211_C_IBSS |
648203134Sthompsa	    IEEE80211_C_HOSTAP |
649208019Sthompsa	    IEEE80211_C_WDS |		/* 4-address traffic works */
650208019Sthompsa	    IEEE80211_C_MBSS |
651203134Sthompsa	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
652203134Sthompsa	    IEEE80211_C_SHSLOT |	/* short slot time supported */
653203134Sthompsa	    IEEE80211_C_WME |		/* WME */
654214894Sbschmidt	    IEEE80211_C_WPA;		/* WPA1|WPA2(RSN) */
655203134Sthompsa
656203134Sthompsa	ic->ic_cryptocaps =
657203134Sthompsa	    IEEE80211_CRYPTO_WEP |
658203134Sthompsa	    IEEE80211_CRYPTO_AES_CCM |
659203134Sthompsa	    IEEE80211_CRYPTO_TKIPMIC |
660203134Sthompsa	    IEEE80211_CRYPTO_TKIP;
661203134Sthompsa
662203134Sthompsa	ic->ic_flags |= IEEE80211_F_DATAPAD;
663203134Sthompsa	ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
664203134Sthompsa
665203134Sthompsa	bands = 0;
666203134Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
667203134Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
668258082Skevlo	if (sc->rf_rev == RT2860_RF_2750 || sc->rf_rev == RT2860_RF_2850 ||
669258082Skevlo	    sc->rf_rev == RT3070_RF_3052)
670258082Skevlo		setbit(&bands, IEEE80211_MODE_11A);
671203134Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
672203134Sthompsa
673203134Sthompsa	ieee80211_ifattach(ic, sc->sc_bssid);
674203134Sthompsa
675203134Sthompsa	ic->ic_scan_start = run_scan_start;
676203134Sthompsa	ic->ic_scan_end = run_scan_end;
677203134Sthompsa	ic->ic_set_channel = run_set_channel;
678203134Sthompsa	ic->ic_node_alloc = run_node_alloc;
679203134Sthompsa	ic->ic_newassoc = run_newassoc;
680218492Sbschmidt	ic->ic_updateslot = run_updateslot;
681208019Sthompsa	ic->ic_update_mcast = run_update_mcast;
682203134Sthompsa	ic->ic_wme.wme_update = run_wme_update;
683203134Sthompsa	ic->ic_raw_xmit = run_raw_xmit;
684203134Sthompsa	ic->ic_update_promisc = run_update_promisc;
685203134Sthompsa
686203134Sthompsa	ic->ic_vap_create = run_vap_create;
687203134Sthompsa	ic->ic_vap_delete = run_vap_delete;
688203134Sthompsa
689203134Sthompsa	ieee80211_radiotap_attach(ic,
690203134Sthompsa	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
691203134Sthompsa		RUN_TX_RADIOTAP_PRESENT,
692203134Sthompsa	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
693203134Sthompsa		RUN_RX_RADIOTAP_PRESENT);
694203134Sthompsa
695208019Sthompsa	TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
696208019Sthompsa	TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
697257712Shselasky	usb_callout_init_mtx(&sc->ratectl_ch, &sc->sc_mtx, 0);
698208019Sthompsa
699203134Sthompsa	if (bootverbose)
700203134Sthompsa		ieee80211_announce(ic);
701203134Sthompsa
702209917Sthompsa	return (0);
703203134Sthompsa
704203134Sthompsadetach:
705203134Sthompsa	run_detach(self);
706209917Sthompsa	return (ENXIO);
707203134Sthompsa}
708203134Sthompsa
709203134Sthompsastatic int
710203134Sthompsarun_detach(device_t self)
711203134Sthompsa{
712203134Sthompsa	struct run_softc *sc = device_get_softc(self);
713203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
714203134Sthompsa	struct ieee80211com *ic;
715203134Sthompsa	int i;
716203134Sthompsa
717246614Shselasky	RUN_LOCK(sc);
718246614Shselasky	sc->sc_detached = 1;
719246614Shselasky	RUN_UNLOCK(sc);
720246614Shselasky
721203134Sthompsa	/* stop all USB transfers */
722203134Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
723203134Sthompsa
724203134Sthompsa	RUN_LOCK(sc);
725209144Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
726209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
727209144Sthompsa
728203134Sthompsa	/* free TX list, if any */
729203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
730203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
731203134Sthompsa	RUN_UNLOCK(sc);
732203134Sthompsa
733203134Sthompsa	if (ifp) {
734203134Sthompsa		ic = ifp->if_l2com;
735208019Sthompsa		/* drain tasks */
736208019Sthompsa		usb_callout_drain(&sc->ratectl_ch);
737208019Sthompsa		ieee80211_draintask(ic, &sc->cmdq_task);
738208019Sthompsa		ieee80211_draintask(ic, &sc->ratectl_task);
739203134Sthompsa		ieee80211_ifdetach(ic);
740203134Sthompsa		if_free(ifp);
741203134Sthompsa	}
742203134Sthompsa
743203134Sthompsa	mtx_destroy(&sc->sc_mtx);
744203134Sthompsa
745203134Sthompsa	return (0);
746203134Sthompsa}
747203134Sthompsa
748203134Sthompsastatic struct ieee80211vap *
749228621Sbschmidtrun_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
750228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
751203134Sthompsa    const uint8_t bssid[IEEE80211_ADDR_LEN],
752203134Sthompsa    const uint8_t mac[IEEE80211_ADDR_LEN])
753203134Sthompsa{
754208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
755208019Sthompsa	struct run_softc *sc = ifp->if_softc;
756203134Sthompsa	struct run_vap *rvp;
757203134Sthompsa	struct ieee80211vap *vap;
758208019Sthompsa	int i;
759203134Sthompsa
760209917Sthompsa	if (sc->rvp_cnt >= RUN_VAP_MAX) {
761208019Sthompsa		if_printf(ifp, "number of VAPs maxed out\n");
762209917Sthompsa		return (NULL);
763208019Sthompsa	}
764208019Sthompsa
765208019Sthompsa	switch (opmode) {
766208019Sthompsa	case IEEE80211_M_STA:
767208019Sthompsa		/* enable s/w bmiss handling for sta mode */
768208019Sthompsa		flags |= IEEE80211_CLONE_NOBEACONS;
769208019Sthompsa		/* fall though */
770208019Sthompsa	case IEEE80211_M_IBSS:
771208019Sthompsa	case IEEE80211_M_MONITOR:
772208019Sthompsa	case IEEE80211_M_HOSTAP:
773208019Sthompsa	case IEEE80211_M_MBSS:
774208019Sthompsa		/* other than WDS vaps, only one at a time */
775208019Sthompsa		if (!TAILQ_EMPTY(&ic->ic_vaps))
776209917Sthompsa			return (NULL);
777208019Sthompsa		break;
778208019Sthompsa	case IEEE80211_M_WDS:
779208019Sthompsa		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
780208019Sthompsa			if(vap->iv_opmode != IEEE80211_M_HOSTAP)
781208019Sthompsa				continue;
782208019Sthompsa			/* WDS vap's always share the local mac address. */
783208019Sthompsa			flags &= ~IEEE80211_CLONE_BSSID;
784208019Sthompsa			break;
785208019Sthompsa		}
786209917Sthompsa		if (vap == NULL) {
787208019Sthompsa			if_printf(ifp, "wds only supported in ap mode\n");
788209917Sthompsa			return (NULL);
789208019Sthompsa		}
790208019Sthompsa		break;
791208019Sthompsa	default:
792208019Sthompsa		if_printf(ifp, "unknown opmode %d\n", opmode);
793209917Sthompsa		return (NULL);
794208019Sthompsa	}
795208019Sthompsa
796208019Sthompsa	rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
797203134Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
798203134Sthompsa	if (rvp == NULL)
799209917Sthompsa		return (NULL);
800203134Sthompsa	vap = &rvp->vap;
801203134Sthompsa
802257743Shselasky	if (ieee80211_vap_setup(ic, vap, name, unit,
803257743Shselasky	    opmode, flags, bssid, mac) != 0) {
804257743Shselasky		/* out of memory */
805257743Shselasky		free(rvp, M_80211_VAP);
806257743Shselasky		return (NULL);
807257743Shselasky	}
808257743Shselasky
809203134Sthompsa	vap->iv_key_update_begin = run_key_update_begin;
810203134Sthompsa	vap->iv_key_update_end = run_key_update_end;
811203134Sthompsa	vap->iv_update_beacon = run_update_beacon;
812208019Sthompsa	vap->iv_max_aid = RT2870_WCID_MAX;
813208019Sthompsa	/*
814208019Sthompsa	 * To delete the right key from h/w, we need wcid.
815208019Sthompsa	 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
816208019Sthompsa	 * and matching wcid will be written into there. So, cast
817208019Sthompsa	 * some spells to remove 'const' from ieee80211_key{}
818208019Sthompsa	 */
819208019Sthompsa	vap->iv_key_delete = (void *)run_key_delete;
820208019Sthompsa	vap->iv_key_set = (void *)run_key_set;
821203134Sthompsa
822203134Sthompsa	/* override state transition machine */
823203134Sthompsa	rvp->newstate = vap->iv_newstate;
824203134Sthompsa	vap->iv_newstate = run_newstate;
825203134Sthompsa
826206358Srpaulo	ieee80211_ratectl_init(vap);
827206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
828203134Sthompsa
829203134Sthompsa	/* complete setup */
830203134Sthompsa	ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
831208019Sthompsa
832208019Sthompsa	/* make sure id is always unique */
833209917Sthompsa	for (i = 0; i < RUN_VAP_MAX; i++) {
834208019Sthompsa		if((sc->rvp_bmap & 1 << i) == 0){
835208019Sthompsa			sc->rvp_bmap |= 1 << i;
836208019Sthompsa			rvp->rvp_id = i;
837208019Sthompsa			break;
838208019Sthompsa		}
839208019Sthompsa	}
840209917Sthompsa	if (sc->rvp_cnt++ == 0)
841208019Sthompsa		ic->ic_opmode = opmode;
842208019Sthompsa
843209917Sthompsa	if (opmode == IEEE80211_M_HOSTAP)
844209144Sthompsa		sc->cmdq_run = RUN_CMDQ_GO;
845209144Sthompsa
846208019Sthompsa	DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
847208019Sthompsa	    rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
848208019Sthompsa
849209917Sthompsa	return (vap);
850203134Sthompsa}
851203134Sthompsa
852203134Sthompsastatic void
853203134Sthompsarun_vap_delete(struct ieee80211vap *vap)
854203134Sthompsa{
855203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
856203134Sthompsa	struct ifnet *ifp;
857203134Sthompsa	struct ieee80211com *ic;
858203134Sthompsa	struct run_softc *sc;
859208019Sthompsa	uint8_t rvp_id;
860203134Sthompsa
861209917Sthompsa	if (vap == NULL)
862203134Sthompsa		return;
863203134Sthompsa
864203134Sthompsa	ic = vap->iv_ic;
865203134Sthompsa	ifp = ic->ic_ifp;
866203134Sthompsa
867203134Sthompsa	sc = ifp->if_softc;
868203134Sthompsa
869205042Sthompsa	RUN_LOCK(sc);
870208019Sthompsa
871218492Sbschmidt	m_freem(rvp->beacon_mbuf);
872218492Sbschmidt	rvp->beacon_mbuf = NULL;
873218492Sbschmidt
874208019Sthompsa	rvp_id = rvp->rvp_id;
875208019Sthompsa	sc->ratectl_run &= ~(1 << rvp_id);
876208019Sthompsa	sc->rvp_bmap &= ~(1 << rvp_id);
877208019Sthompsa	run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
878208019Sthompsa	run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
879208019Sthompsa	--sc->rvp_cnt;
880208019Sthompsa
881208019Sthompsa	DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
882208019Sthompsa	    vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
883208019Sthompsa
884205042Sthompsa	RUN_UNLOCK(sc);
885203134Sthompsa
886206358Srpaulo	ieee80211_ratectl_deinit(vap);
887203134Sthompsa	ieee80211_vap_detach(vap);
888203134Sthompsa	free(rvp, M_80211_VAP);
889203134Sthompsa}
890203134Sthompsa
891208019Sthompsa/*
892208019Sthompsa * There are numbers of functions need to be called in context thread.
893208019Sthompsa * Rather than creating taskqueue event for each of those functions,
894208019Sthompsa * here is all-for-one taskqueue callback function. This function
895208019Sthompsa * gurantees deferred functions are executed in the same order they
896208019Sthompsa * were enqueued.
897208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[].
898208019Sthompsa */
899203134Sthompsastatic void
900208019Sthompsarun_cmdq_cb(void *arg, int pending)
901208019Sthompsa{
902208019Sthompsa	struct run_softc *sc = arg;
903208019Sthompsa	uint8_t i;
904208019Sthompsa
905208019Sthompsa	/* call cmdq[].func locked */
906208019Sthompsa	RUN_LOCK(sc);
907209917Sthompsa	for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
908209917Sthompsa	    i = sc->cmdq_exec, pending--) {
909208019Sthompsa		DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
910209917Sthompsa		if (sc->cmdq_run == RUN_CMDQ_GO) {
911208019Sthompsa			/*
912208019Sthompsa			 * If arg0 is NULL, callback func needs more
913208019Sthompsa			 * than one arg. So, pass ptr to cmdq struct.
914208019Sthompsa			 */
915209917Sthompsa			if (sc->cmdq[i].arg0)
916208019Sthompsa				sc->cmdq[i].func(sc->cmdq[i].arg0);
917208019Sthompsa			else
918208019Sthompsa				sc->cmdq[i].func(&sc->cmdq[i]);
919208019Sthompsa		}
920208019Sthompsa		sc->cmdq[i].arg0 = NULL;
921208019Sthompsa		sc->cmdq[i].func = NULL;
922208019Sthompsa		sc->cmdq_exec++;
923208019Sthompsa		sc->cmdq_exec &= RUN_CMDQ_MASQ;
924208019Sthompsa	}
925208019Sthompsa	RUN_UNLOCK(sc);
926208019Sthompsa}
927208019Sthompsa
928208019Sthompsastatic void
929203134Sthompsarun_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
930203134Sthompsa{
931203134Sthompsa	struct run_tx_data *data;
932203134Sthompsa
933203134Sthompsa	memset(pq, 0, sizeof(*pq));
934203134Sthompsa
935203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
936203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
937203134Sthompsa
938203134Sthompsa	for (data = &pq->tx_data[0];
939203134Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
940203134Sthompsa		data->sc = sc;
941203134Sthompsa		STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
942203134Sthompsa	}
943203134Sthompsa	pq->tx_nfree = RUN_TX_RING_COUNT;
944203134Sthompsa}
945203134Sthompsa
946203134Sthompsastatic void
947203134Sthompsarun_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
948203134Sthompsa{
949203134Sthompsa	struct run_tx_data *data;
950203134Sthompsa
951203134Sthompsa	/* make sure any subsequent use of the queues will fail */
952203134Sthompsa	pq->tx_nfree = 0;
953203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
954203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
955203134Sthompsa
956203134Sthompsa	/* free up all node references and mbufs */
957203134Sthompsa	for (data = &pq->tx_data[0];
958209917Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
959203134Sthompsa		if (data->m != NULL) {
960203134Sthompsa			m_freem(data->m);
961203134Sthompsa			data->m = NULL;
962203134Sthompsa		}
963203134Sthompsa		if (data->ni != NULL) {
964203134Sthompsa			ieee80211_free_node(data->ni);
965203134Sthompsa			data->ni = NULL;
966203134Sthompsa		}
967203134Sthompsa	}
968203134Sthompsa}
969203134Sthompsa
970220235Skevlostatic int
971203134Sthompsarun_load_microcode(struct run_softc *sc)
972203134Sthompsa{
973203134Sthompsa	usb_device_request_t req;
974203137Sthompsa	const struct firmware *fw;
975203134Sthompsa	const u_char *base;
976203134Sthompsa	uint32_t tmp;
977203134Sthompsa	int ntries, error;
978203134Sthompsa	const uint64_t *temp;
979203134Sthompsa	uint64_t bytes;
980203134Sthompsa
981205042Sthompsa	RUN_UNLOCK(sc);
982203137Sthompsa	fw = firmware_get("runfw");
983205042Sthompsa	RUN_LOCK(sc);
984209917Sthompsa	if (fw == NULL) {
985203138Sthompsa		device_printf(sc->sc_dev,
986203138Sthompsa		    "failed loadfirmware of file %s\n", "runfw");
987203134Sthompsa		return ENOENT;
988203134Sthompsa	}
989203134Sthompsa
990203137Sthompsa	if (fw->datasize != 8192) {
991203138Sthompsa		device_printf(sc->sc_dev,
992203138Sthompsa		    "invalid firmware size (should be 8KB)\n");
993203137Sthompsa		error = EINVAL;
994203137Sthompsa		goto fail;
995203134Sthompsa	}
996203134Sthompsa
997203134Sthompsa	/*
998203134Sthompsa	 * RT3071/RT3072 use a different firmware
999203134Sthompsa	 * run-rt2870 (8KB) contains both,
1000203134Sthompsa	 * first half (4KB) is for rt2870,
1001203134Sthompsa	 * last half is for rt3071.
1002203134Sthompsa	 */
1003203137Sthompsa	base = fw->data;
1004205042Sthompsa	if ((sc->mac_ver) != 0x2860 &&
1005205042Sthompsa	    (sc->mac_ver) != 0x2872 &&
1006209917Sthompsa	    (sc->mac_ver) != 0x3070) {
1007203134Sthompsa		base += 4096;
1008205042Sthompsa	}
1009203134Sthompsa
1010203134Sthompsa	/* cheap sanity check */
1011203137Sthompsa	temp = fw->data;
1012203134Sthompsa	bytes = *temp;
1013257712Shselasky	if (bytes != be64toh(0xffffff0210280210ULL)) {
1014203138Sthompsa		device_printf(sc->sc_dev, "firmware checksum failed\n");
1015203137Sthompsa		error = EINVAL;
1016203137Sthompsa		goto fail;
1017203137Sthompsa	}
1018203134Sthompsa
1019203134Sthompsa	/* write microcode image */
1020203134Sthompsa	run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1021203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1022203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1023203134Sthompsa
1024203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1025203134Sthompsa	req.bRequest = RT2870_RESET;
1026203134Sthompsa	USETW(req.wValue, 8);
1027203134Sthompsa	USETW(req.wIndex, 0);
1028203134Sthompsa	USETW(req.wLength, 0);
1029220235Skevlo	if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL))
1030220235Skevlo	    != 0) {
1031203138Sthompsa		device_printf(sc->sc_dev, "firmware reset failed\n");
1032203137Sthompsa		goto fail;
1033203137Sthompsa	}
1034203134Sthompsa
1035203134Sthompsa	run_delay(sc, 10);
1036203134Sthompsa
1037203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
1038205042Sthompsa	if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1039203137Sthompsa		goto fail;
1040203134Sthompsa
1041203134Sthompsa	/* wait until microcontroller is ready */
1042203134Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
1043203137Sthompsa		if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1044203137Sthompsa			goto fail;
1045203137Sthompsa		}
1046203134Sthompsa		if (tmp & RT2860_MCU_READY)
1047203134Sthompsa			break;
1048203134Sthompsa		run_delay(sc, 10);
1049203134Sthompsa	}
1050203134Sthompsa	if (ntries == 1000) {
1051203138Sthompsa		device_printf(sc->sc_dev,
1052203138Sthompsa		    "timeout waiting for MCU to initialize\n");
1053203137Sthompsa		error = ETIMEDOUT;
1054203137Sthompsa		goto fail;
1055203134Sthompsa	}
1056233283Sbschmidt	device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n",
1057233283Sbschmidt	    (base == fw->data) ? "RT2870" : "RT3071",
1058233283Sbschmidt	    *(base + 4092), *(base + 4093));
1059203134Sthompsa
1060203137Sthompsafail:
1061203137Sthompsa	firmware_put(fw, FIRMWARE_UNLOAD);
1062203137Sthompsa	return (error);
1063203134Sthompsa}
1064203134Sthompsa
1065258641Shselaskystatic int
1066203134Sthompsarun_reset(struct run_softc *sc)
1067203134Sthompsa{
1068203134Sthompsa	usb_device_request_t req;
1069203134Sthompsa
1070203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1071203134Sthompsa	req.bRequest = RT2870_RESET;
1072203134Sthompsa	USETW(req.wValue, 1);
1073203134Sthompsa	USETW(req.wIndex, 0);
1074203134Sthompsa	USETW(req.wLength, 0);
1075209917Sthompsa	return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1076203134Sthompsa}
1077203134Sthompsa
1078203134Sthompsastatic usb_error_t
1079203134Sthompsarun_do_request(struct run_softc *sc,
1080203134Sthompsa    struct usb_device_request *req, void *data)
1081203134Sthompsa{
1082203134Sthompsa	usb_error_t err;
1083203134Sthompsa	int ntries = 10;
1084203134Sthompsa
1085203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1086203134Sthompsa
1087203134Sthompsa	while (ntries--) {
1088203134Sthompsa		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1089203134Sthompsa		    req, data, 0, NULL, 250 /* ms */);
1090203134Sthompsa		if (err == 0)
1091203134Sthompsa			break;
1092203134Sthompsa		DPRINTFN(1, "Control request failed, %s (retrying)\n",
1093203134Sthompsa		    usbd_errstr(err));
1094203134Sthompsa		run_delay(sc, 10);
1095203134Sthompsa	}
1096203134Sthompsa	return (err);
1097203134Sthompsa}
1098203134Sthompsa
1099203134Sthompsastatic int
1100203134Sthompsarun_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1101203134Sthompsa{
1102203134Sthompsa	uint32_t tmp;
1103203134Sthompsa	int error;
1104203134Sthompsa
1105203134Sthompsa	error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1106203134Sthompsa	if (error == 0)
1107203134Sthompsa		*val = le32toh(tmp);
1108203134Sthompsa	else
1109203134Sthompsa		*val = 0xffffffff;
1110209917Sthompsa	return (error);
1111203134Sthompsa}
1112203134Sthompsa
1113203134Sthompsastatic int
1114203134Sthompsarun_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1115203134Sthompsa{
1116203134Sthompsa	usb_device_request_t req;
1117203134Sthompsa
1118203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1119203134Sthompsa	req.bRequest = RT2870_READ_REGION_1;
1120203134Sthompsa	USETW(req.wValue, 0);
1121203134Sthompsa	USETW(req.wIndex, reg);
1122203134Sthompsa	USETW(req.wLength, len);
1123203134Sthompsa
1124209917Sthompsa	return (run_do_request(sc, &req, buf));
1125203134Sthompsa}
1126203134Sthompsa
1127203134Sthompsastatic int
1128203134Sthompsarun_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1129203134Sthompsa{
1130203134Sthompsa	usb_device_request_t req;
1131203134Sthompsa
1132203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1133203134Sthompsa	req.bRequest = RT2870_WRITE_2;
1134203134Sthompsa	USETW(req.wValue, val);
1135203134Sthompsa	USETW(req.wIndex, reg);
1136203134Sthompsa	USETW(req.wLength, 0);
1137203134Sthompsa
1138209917Sthompsa	return (run_do_request(sc, &req, NULL));
1139203134Sthompsa}
1140203134Sthompsa
1141203134Sthompsastatic int
1142203134Sthompsarun_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1143203134Sthompsa{
1144203134Sthompsa	int error;
1145203134Sthompsa
1146203134Sthompsa	if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1147203134Sthompsa		error = run_write_2(sc, reg + 2, val >> 16);
1148209917Sthompsa	return (error);
1149203134Sthompsa}
1150203134Sthompsa
1151203134Sthompsastatic int
1152203134Sthompsarun_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1153203134Sthompsa    int len)
1154203134Sthompsa{
1155203134Sthompsa#if 1
1156203134Sthompsa	int i, error = 0;
1157203134Sthompsa	/*
1158203134Sthompsa	 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1159203134Sthompsa	 * We thus issue multiple WRITE_2 commands instead.
1160203134Sthompsa	 */
1161203134Sthompsa	KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1162203134Sthompsa	for (i = 0; i < len && error == 0; i += 2)
1163203134Sthompsa		error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1164209917Sthompsa	return (error);
1165203134Sthompsa#else
1166203134Sthompsa	usb_device_request_t req;
1167257958Skevlo	int error = 0;
1168203134Sthompsa
1169257958Skevlo	/*
1170257958Skevlo	 * NOTE: It appears the WRITE_REGION_1 command cannot be
1171257958Skevlo	 * passed a huge amount of data, which will crash the
1172257958Skevlo	 * firmware. Limit amount of data passed to 64-bytes at a
1173257958Skevlo	 * time.
1174257958Skevlo	 */
1175257958Skevlo	while (len > 0) {
1176257958Skevlo		int delta = 64;
1177257958Skevlo		if (delta > len)
1178257958Skevlo			delta = len;
1179257958Skevlo
1180257958Skevlo		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1181257958Skevlo		req.bRequest = RT2870_WRITE_REGION_1;
1182257958Skevlo		USETW(req.wValue, 0);
1183257958Skevlo		USETW(req.wIndex, reg);
1184257958Skevlo		USETW(req.wLength, delta);
1185257958Skevlo		error = run_do_request(sc, &req, __DECONST(uint8_t *, buf));
1186257958Skevlo		if (error != 0)
1187257958Skevlo			break;
1188257958Skevlo		reg += delta;
1189257958Skevlo		buf += delta;
1190257958Skevlo		len -= delta;
1191257958Skevlo	}
1192257958Skevlo	return (error);
1193203134Sthompsa#endif
1194203134Sthompsa}
1195203134Sthompsa
1196203134Sthompsastatic int
1197203134Sthompsarun_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1198203134Sthompsa{
1199203134Sthompsa	int i, error = 0;
1200203134Sthompsa
1201203134Sthompsa	KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1202203134Sthompsa	for (i = 0; i < len && error == 0; i += 4)
1203203134Sthompsa		error = run_write(sc, reg + i, val);
1204209917Sthompsa	return (error);
1205203134Sthompsa}
1206203134Sthompsa
1207203134Sthompsa/* Read 16-bit from eFUSE ROM (RT3070 only.) */
1208203134Sthompsastatic int
1209203134Sthompsarun_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1210203134Sthompsa{
1211203134Sthompsa	uint32_t tmp;
1212203134Sthompsa	uint16_t reg;
1213203134Sthompsa	int error, ntries;
1214203134Sthompsa
1215203134Sthompsa	if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1216209917Sthompsa		return (error);
1217203134Sthompsa
1218203134Sthompsa	addr *= 2;
1219203134Sthompsa	/*-
1220203134Sthompsa	 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1221203134Sthompsa	 * DATA0: F E D C
1222203134Sthompsa	 * DATA1: B A 9 8
1223203134Sthompsa	 * DATA2: 7 6 5 4
1224203134Sthompsa	 * DATA3: 3 2 1 0
1225203134Sthompsa	 */
1226203134Sthompsa	tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1227203134Sthompsa	tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1228203134Sthompsa	run_write(sc, RT3070_EFUSE_CTRL, tmp);
1229203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1230203134Sthompsa		if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1231209917Sthompsa			return (error);
1232203134Sthompsa		if (!(tmp & RT3070_EFSROM_KICK))
1233203134Sthompsa			break;
1234203134Sthompsa		run_delay(sc, 2);
1235203134Sthompsa	}
1236203134Sthompsa	if (ntries == 100)
1237209917Sthompsa		return (ETIMEDOUT);
1238203134Sthompsa
1239203134Sthompsa	if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1240203134Sthompsa		*val = 0xffff;	/* address not found */
1241209917Sthompsa		return (0);
1242203134Sthompsa	}
1243203134Sthompsa	/* determine to which 32-bit register our 16-bit word belongs */
1244203134Sthompsa	reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1245203134Sthompsa	if ((error = run_read(sc, reg, &tmp)) != 0)
1246209917Sthompsa		return (error);
1247203134Sthompsa
1248203134Sthompsa	*val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1249209917Sthompsa	return (0);
1250203134Sthompsa}
1251203134Sthompsa
1252203134Sthompsastatic int
1253203134Sthompsarun_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1254203134Sthompsa{
1255203134Sthompsa	usb_device_request_t req;
1256203134Sthompsa	uint16_t tmp;
1257203134Sthompsa	int error;
1258203134Sthompsa
1259203134Sthompsa	addr *= 2;
1260203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1261203134Sthompsa	req.bRequest = RT2870_EEPROM_READ;
1262203134Sthompsa	USETW(req.wValue, 0);
1263203134Sthompsa	USETW(req.wIndex, addr);
1264203134Sthompsa	USETW(req.wLength, sizeof tmp);
1265203134Sthompsa
1266203134Sthompsa	error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1267203134Sthompsa	if (error == 0)
1268203134Sthompsa		*val = le16toh(tmp);
1269203134Sthompsa	else
1270203134Sthompsa		*val = 0xffff;
1271209917Sthompsa	return (error);
1272203134Sthompsa}
1273203134Sthompsa
1274203134Sthompsastatic __inline int
1275203134Sthompsarun_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1276203134Sthompsa{
1277203134Sthompsa	/* either eFUSE ROM or EEPROM */
1278203134Sthompsa	return sc->sc_srom_read(sc, addr, val);
1279203134Sthompsa}
1280203134Sthompsa
1281203134Sthompsastatic int
1282258733Skevlorun_rt2870_rf_write(struct run_softc *sc, uint32_t val)
1283203134Sthompsa{
1284203134Sthompsa	uint32_t tmp;
1285203134Sthompsa	int error, ntries;
1286203134Sthompsa
1287203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1288203134Sthompsa		if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1289209917Sthompsa			return (error);
1290203134Sthompsa		if (!(tmp & RT2860_RF_REG_CTRL))
1291203134Sthompsa			break;
1292203134Sthompsa	}
1293203134Sthompsa	if (ntries == 10)
1294209917Sthompsa		return (ETIMEDOUT);
1295203134Sthompsa
1296258732Skevlo	return (run_write(sc, RT2860_RF_CSR_CFG0, val));
1297203134Sthompsa}
1298203134Sthompsa
1299203134Sthompsastatic int
1300203134Sthompsarun_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1301203134Sthompsa{
1302203134Sthompsa	uint32_t tmp;
1303203134Sthompsa	int error, ntries;
1304203134Sthompsa
1305203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1306203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1307209917Sthompsa			return (error);
1308203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1309203134Sthompsa			break;
1310203134Sthompsa	}
1311203134Sthompsa	if (ntries == 100)
1312209917Sthompsa		return (ETIMEDOUT);
1313203134Sthompsa
1314203134Sthompsa	tmp = RT3070_RF_KICK | reg << 8;
1315203134Sthompsa	if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1316209917Sthompsa		return (error);
1317203134Sthompsa
1318203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1319203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1320209917Sthompsa			return (error);
1321203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1322203134Sthompsa			break;
1323203134Sthompsa	}
1324203134Sthompsa	if (ntries == 100)
1325209917Sthompsa		return (ETIMEDOUT);
1326203134Sthompsa
1327203134Sthompsa	*val = tmp & 0xff;
1328209917Sthompsa	return (0);
1329203134Sthompsa}
1330203134Sthompsa
1331203134Sthompsastatic int
1332203134Sthompsarun_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1333203134Sthompsa{
1334203134Sthompsa	uint32_t tmp;
1335203134Sthompsa	int error, ntries;
1336203134Sthompsa
1337203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1338203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1339209917Sthompsa			return (error);
1340203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1341203134Sthompsa			break;
1342203134Sthompsa	}
1343203134Sthompsa	if (ntries == 10)
1344209917Sthompsa		return (ETIMEDOUT);
1345203134Sthompsa
1346203134Sthompsa	tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1347209917Sthompsa	return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1348203134Sthompsa}
1349203134Sthompsa
1350203134Sthompsastatic int
1351203134Sthompsarun_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1352203134Sthompsa{
1353203134Sthompsa	uint32_t tmp;
1354203134Sthompsa	int ntries, error;
1355203134Sthompsa
1356203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1357203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1358209917Sthompsa			return (error);
1359203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1360203134Sthompsa			break;
1361203134Sthompsa	}
1362203134Sthompsa	if (ntries == 10)
1363209917Sthompsa		return (ETIMEDOUT);
1364203134Sthompsa
1365203134Sthompsa	tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1366203134Sthompsa	if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1367209917Sthompsa		return (error);
1368203134Sthompsa
1369203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1370203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1371209917Sthompsa			return (error);
1372203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1373203134Sthompsa			break;
1374203134Sthompsa	}
1375203134Sthompsa	if (ntries == 10)
1376209917Sthompsa		return (ETIMEDOUT);
1377203134Sthompsa
1378203134Sthompsa	*val = tmp & 0xff;
1379209917Sthompsa	return (0);
1380203134Sthompsa}
1381203134Sthompsa
1382203134Sthompsastatic int
1383203134Sthompsarun_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1384203134Sthompsa{
1385203134Sthompsa	uint32_t tmp;
1386203134Sthompsa	int ntries, error;
1387203134Sthompsa
1388203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1389203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1390209917Sthompsa			return (error);
1391203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1392203134Sthompsa			break;
1393203134Sthompsa	}
1394203134Sthompsa	if (ntries == 10)
1395209917Sthompsa		return (ETIMEDOUT);
1396203134Sthompsa
1397203134Sthompsa	tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1398209917Sthompsa	return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1399203134Sthompsa}
1400203134Sthompsa
1401203134Sthompsa/*
1402203134Sthompsa * Send a command to the 8051 microcontroller unit.
1403203134Sthompsa */
1404203134Sthompsastatic int
1405203134Sthompsarun_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1406203134Sthompsa{
1407203134Sthompsa	uint32_t tmp;
1408203134Sthompsa	int error, ntries;
1409203134Sthompsa
1410203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1411203134Sthompsa		if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1412203134Sthompsa			return error;
1413203134Sthompsa		if (!(tmp & RT2860_H2M_BUSY))
1414203134Sthompsa			break;
1415203134Sthompsa	}
1416203134Sthompsa	if (ntries == 100)
1417203134Sthompsa		return ETIMEDOUT;
1418203134Sthompsa
1419203134Sthompsa	tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1420203134Sthompsa	if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1421203134Sthompsa		error = run_write(sc, RT2860_HOST_CMD, cmd);
1422209917Sthompsa	return (error);
1423203134Sthompsa}
1424203134Sthompsa
1425203134Sthompsa/*
1426203134Sthompsa * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1427203134Sthompsa * Used to adjust per-rate Tx power registers.
1428203134Sthompsa */
1429203134Sthompsastatic __inline uint32_t
1430203134Sthompsab4inc(uint32_t b32, int8_t delta)
1431203134Sthompsa{
1432203134Sthompsa	int8_t i, b4;
1433203134Sthompsa
1434203134Sthompsa	for (i = 0; i < 8; i++) {
1435203134Sthompsa		b4 = b32 & 0xf;
1436203134Sthompsa		b4 += delta;
1437203134Sthompsa		if (b4 < 0)
1438203134Sthompsa			b4 = 0;
1439203134Sthompsa		else if (b4 > 0xf)
1440203134Sthompsa			b4 = 0xf;
1441203134Sthompsa		b32 = b32 >> 4 | b4 << 28;
1442203134Sthompsa	}
1443209917Sthompsa	return (b32);
1444203134Sthompsa}
1445203134Sthompsa
1446203134Sthompsastatic const char *
1447257955Skevlorun_get_rf(uint16_t rev)
1448203134Sthompsa{
1449203134Sthompsa	switch (rev) {
1450203134Sthompsa	case RT2860_RF_2820:	return "RT2820";
1451203134Sthompsa	case RT2860_RF_2850:	return "RT2850";
1452203134Sthompsa	case RT2860_RF_2720:	return "RT2720";
1453203134Sthompsa	case RT2860_RF_2750:	return "RT2750";
1454203134Sthompsa	case RT3070_RF_3020:	return "RT3020";
1455203134Sthompsa	case RT3070_RF_2020:	return "RT2020";
1456203134Sthompsa	case RT3070_RF_3021:	return "RT3021";
1457203134Sthompsa	case RT3070_RF_3022:	return "RT3022";
1458203134Sthompsa	case RT3070_RF_3052:	return "RT3052";
1459257955Skevlo	case RT5390_RF_5370:	return "RT5370";
1460257955Skevlo	case RT5390_RF_5372:	return "RT5372";
1461203134Sthompsa	}
1462209917Sthompsa	return ("unknown");
1463203134Sthompsa}
1464203134Sthompsa
1465258641Shselaskystatic int
1466203134Sthompsarun_read_eeprom(struct run_softc *sc)
1467203134Sthompsa{
1468203134Sthompsa	int8_t delta_2ghz, delta_5ghz;
1469203134Sthompsa	uint32_t tmp;
1470203134Sthompsa	uint16_t val;
1471203134Sthompsa	int ridx, ant, i;
1472203134Sthompsa
1473203134Sthompsa	/* check whether the ROM is eFUSE ROM or EEPROM */
1474203134Sthompsa	sc->sc_srom_read = run_eeprom_read_2;
1475205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1476203134Sthompsa		run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1477203134Sthompsa		DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1478203134Sthompsa		if (tmp & RT3070_SEL_EFUSE)
1479203134Sthompsa			sc->sc_srom_read = run_efuse_read_2;
1480203134Sthompsa	}
1481203134Sthompsa
1482203134Sthompsa	/* read ROM version */
1483203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1484203134Sthompsa	DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1485203134Sthompsa
1486203134Sthompsa	/* read MAC address */
1487203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1488203134Sthompsa	sc->sc_bssid[0] = val & 0xff;
1489203134Sthompsa	sc->sc_bssid[1] = val >> 8;
1490203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1491203134Sthompsa	sc->sc_bssid[2] = val & 0xff;
1492203134Sthompsa	sc->sc_bssid[3] = val >> 8;
1493203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1494203134Sthompsa	sc->sc_bssid[4] = val & 0xff;
1495203134Sthompsa	sc->sc_bssid[5] = val >> 8;
1496203134Sthompsa
1497257955Skevlo	if (sc->mac_ver < 0x5390) {
1498257955Skevlo		/* read vender BBP settings */
1499205042Sthompsa		for (i = 0; i < 10; i++) {
1500257955Skevlo			run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1501257955Skevlo			sc->bbp[i].val = val & 0xff;
1502257955Skevlo			sc->bbp[i].reg = val >> 8;
1503257955Skevlo			DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg,
1504257955Skevlo			    sc->bbp[i].val);
1505205042Sthompsa		}
1506257955Skevlo		if (sc->mac_ver >= 0x3071) {
1507257955Skevlo			/* read vendor RF settings */
1508257955Skevlo			for (i = 0; i < 10; i++) {
1509257955Skevlo				run_srom_read(sc, RT3071_EEPROM_RF_BASE + i,
1510257955Skevlo				   &val);
1511257955Skevlo				sc->rf[i].val = val & 0xff;
1512257955Skevlo				sc->rf[i].reg = val >> 8;
1513257955Skevlo				DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1514257955Skevlo				    sc->rf[i].val);
1515257955Skevlo			}
1516257955Skevlo		}
1517205042Sthompsa	}
1518203134Sthompsa
1519203134Sthompsa	/* read RF frequency offset from EEPROM */
1520203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1521203134Sthompsa	sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1522203134Sthompsa	DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1523203134Sthompsa
1524205042Sthompsa	if (val >> 8 != 0xff) {
1525203134Sthompsa		/* read LEDs operating mode */
1526205042Sthompsa		sc->leds = val >> 8;
1527203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1528203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1529203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1530203134Sthompsa	} else {
1531203134Sthompsa		/* broken EEPROM, use default settings */
1532203134Sthompsa		sc->leds = 0x01;
1533203134Sthompsa		sc->led[0] = 0x5555;
1534203134Sthompsa		sc->led[1] = 0x2221;
1535203134Sthompsa		sc->led[2] = 0x5627;	/* differs from RT2860 */
1536203134Sthompsa	}
1537203134Sthompsa	DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1538203134Sthompsa	    sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1539203134Sthompsa
1540203134Sthompsa	/* read RF information */
1541257955Skevlo	if (sc->mac_ver >= 0x5390)
1542257955Skevlo		run_srom_read(sc, 0x00, &val);
1543257955Skevlo	else
1544257955Skevlo		run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1545257955Skevlo
1546203134Sthompsa	if (val == 0xffff) {
1547203134Sthompsa		DPRINTF("invalid EEPROM antenna info, using default\n");
1548205042Sthompsa		if (sc->mac_ver == 0x3572) {
1549205042Sthompsa			/* default to RF3052 2T2R */
1550205042Sthompsa			sc->rf_rev = RT3070_RF_3052;
1551205042Sthompsa			sc->ntxchains = 2;
1552205042Sthompsa			sc->nrxchains = 2;
1553205042Sthompsa		} else if (sc->mac_ver >= 0x3070) {
1554203134Sthompsa			/* default to RF3020 1T1R */
1555203134Sthompsa			sc->rf_rev = RT3070_RF_3020;
1556203134Sthompsa			sc->ntxchains = 1;
1557203134Sthompsa			sc->nrxchains = 1;
1558203134Sthompsa		} else {
1559203134Sthompsa			/* default to RF2820 1T2R */
1560203134Sthompsa			sc->rf_rev = RT2860_RF_2820;
1561203134Sthompsa			sc->ntxchains = 1;
1562203134Sthompsa			sc->nrxchains = 2;
1563203134Sthompsa		}
1564203134Sthompsa	} else {
1565257955Skevlo		if (sc->mac_ver >= 0x5390) {
1566257955Skevlo			sc->rf_rev = val;
1567257955Skevlo			run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1568257955Skevlo		} else
1569257955Skevlo			sc->rf_rev = (val >> 8) & 0xf;
1570203134Sthompsa		sc->ntxchains = (val >> 4) & 0xf;
1571203134Sthompsa		sc->nrxchains = val & 0xf;
1572203134Sthompsa	}
1573257955Skevlo	DPRINTF("EEPROM RF rev=0x%04x chains=%dT%dR\n",
1574203134Sthompsa	    sc->rf_rev, sc->ntxchains, sc->nrxchains);
1575203134Sthompsa
1576208019Sthompsa	/* check if RF supports automatic Tx access gain control */
1577203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1578203134Sthompsa	DPRINTF("EEPROM CFG 0x%04x\n", val);
1579205042Sthompsa	/* check if driver should patch the DAC issue */
1580205042Sthompsa	if ((val >> 8) != 0xff)
1581205042Sthompsa		sc->patch_dac = (val >> 15) & 1;
1582203134Sthompsa	if ((val & 0xff) != 0xff) {
1583203134Sthompsa		sc->ext_5ghz_lna = (val >> 3) & 1;
1584203134Sthompsa		sc->ext_2ghz_lna = (val >> 2) & 1;
1585205042Sthompsa		/* check if RF supports automatic Tx access gain control */
1586203134Sthompsa		sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1587205042Sthompsa		/* check if we have a hardware radio switch */
1588205042Sthompsa		sc->rfswitch = val & 1;
1589203134Sthompsa	}
1590203134Sthompsa
1591203134Sthompsa	/* read power settings for 2GHz channels */
1592203134Sthompsa	for (i = 0; i < 14; i += 2) {
1593203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1594203134Sthompsa		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1595203134Sthompsa		sc->txpow1[i + 1] = (int8_t)(val >> 8);
1596203134Sthompsa
1597257955Skevlo		if (sc->mac_ver != 0x5390) {
1598257955Skevlo			run_srom_read(sc,
1599257955Skevlo			    RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1600257955Skevlo			sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1601257955Skevlo			sc->txpow2[i + 1] = (int8_t)(val >> 8);
1602257955Skevlo		}
1603203134Sthompsa	}
1604203134Sthompsa	/* fix broken Tx power entries */
1605203134Sthompsa	for (i = 0; i < 14; i++) {
1606257955Skevlo		if (sc->mac_ver >= 0x5390) {
1607257955Skevlo			if (sc->txpow1[i] < 0 || sc->txpow1[i] > 27)
1608257955Skevlo				sc->txpow1[i] = 5;
1609257955Skevlo		} else {
1610257955Skevlo			if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1611257955Skevlo				sc->txpow1[i] = 5;
1612257955Skevlo		}
1613257955Skevlo		if (sc->mac_ver > 0x5390) {
1614257955Skevlo			if (sc->txpow2[i] < 0 || sc->txpow2[i] > 27)
1615257955Skevlo				sc->txpow2[i] = 5;
1616257955Skevlo		} else if (sc->mac_ver < 0x5390) {
1617257955Skevlo			if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1618257955Skevlo				sc->txpow2[i] = 5;
1619257955Skevlo		}
1620203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1621203134Sthompsa		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1622203134Sthompsa	}
1623203134Sthompsa	/* read power settings for 5GHz channels */
1624205042Sthompsa	for (i = 0; i < 40; i += 2) {
1625203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1626203134Sthompsa		sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1627203134Sthompsa		sc->txpow1[i + 15] = (int8_t)(val >> 8);
1628203134Sthompsa
1629203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1630203134Sthompsa		sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1631203134Sthompsa		sc->txpow2[i + 15] = (int8_t)(val >> 8);
1632203134Sthompsa	}
1633203134Sthompsa	/* fix broken Tx power entries */
1634205042Sthompsa	for (i = 0; i < 40; i++) {
1635203134Sthompsa		if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1636203134Sthompsa			sc->txpow1[14 + i] = 5;
1637203134Sthompsa		if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1638203134Sthompsa			sc->txpow2[14 + i] = 5;
1639203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1640203134Sthompsa		    rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1641203134Sthompsa		    sc->txpow2[14 + i]);
1642203134Sthompsa	}
1643203134Sthompsa
1644203134Sthompsa	/* read Tx power compensation for each Tx rate */
1645203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1646203134Sthompsa	delta_2ghz = delta_5ghz = 0;
1647203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1648203134Sthompsa		delta_2ghz = val & 0xf;
1649203134Sthompsa		if (!(val & 0x40))	/* negative number */
1650203134Sthompsa			delta_2ghz = -delta_2ghz;
1651203134Sthompsa	}
1652203134Sthompsa	val >>= 8;
1653203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1654203134Sthompsa		delta_5ghz = val & 0xf;
1655203134Sthompsa		if (!(val & 0x40))	/* negative number */
1656203134Sthompsa			delta_5ghz = -delta_5ghz;
1657203134Sthompsa	}
1658203134Sthompsa	DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1659203134Sthompsa	    delta_2ghz, delta_5ghz);
1660203134Sthompsa
1661203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
1662203134Sthompsa		uint32_t reg;
1663203134Sthompsa
1664208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1665208019Sthompsa		reg = val;
1666208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1667208019Sthompsa		reg |= (uint32_t)val << 16;
1668203134Sthompsa
1669203134Sthompsa		sc->txpow20mhz[ridx] = reg;
1670203134Sthompsa		sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1671203134Sthompsa		sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1672203134Sthompsa
1673203134Sthompsa		DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1674203134Sthompsa		    "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1675203134Sthompsa		    sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1676203134Sthompsa	}
1677203134Sthompsa
1678203134Sthompsa	/* read RSSI offsets and LNA gains from EEPROM */
1679203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1680203134Sthompsa	sc->rssi_2ghz[0] = val & 0xff;	/* Ant A */
1681203134Sthompsa	sc->rssi_2ghz[1] = val >> 8;	/* Ant B */
1682203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1683205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1684205042Sthompsa		/*
1685205042Sthompsa		 * On RT3070 chips (limited to 2 Rx chains), this ROM
1686205042Sthompsa		 * field contains the Tx mixer gain for the 2GHz band.
1687205042Sthompsa		 */
1688205042Sthompsa		if ((val & 0xff) != 0xff)
1689205042Sthompsa			sc->txmixgain_2ghz = val & 0x7;
1690205042Sthompsa		DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1691205042Sthompsa	} else
1692205042Sthompsa		sc->rssi_2ghz[2] = val & 0xff;	/* Ant C */
1693203134Sthompsa	sc->lna[2] = val >> 8;		/* channel group 2 */
1694203134Sthompsa
1695203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1696203134Sthompsa	sc->rssi_5ghz[0] = val & 0xff;	/* Ant A */
1697203134Sthompsa	sc->rssi_5ghz[1] = val >> 8;	/* Ant B */
1698203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1699205042Sthompsa	if (sc->mac_ver == 0x3572) {
1700205042Sthompsa		/*
1701205042Sthompsa		 * On RT3572 chips (limited to 2 Rx chains), this ROM
1702205042Sthompsa		 * field contains the Tx mixer gain for the 5GHz band.
1703205042Sthompsa		 */
1704205042Sthompsa		if ((val & 0xff) != 0xff)
1705205042Sthompsa			sc->txmixgain_5ghz = val & 0x7;
1706205042Sthompsa		DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1707205042Sthompsa	} else
1708205042Sthompsa		sc->rssi_5ghz[2] = val & 0xff;	/* Ant C */
1709203134Sthompsa	sc->lna[3] = val >> 8;		/* channel group 3 */
1710203134Sthompsa
1711203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1712203134Sthompsa	sc->lna[0] = val & 0xff;	/* channel group 0 */
1713203134Sthompsa	sc->lna[1] = val >> 8;		/* channel group 1 */
1714203134Sthompsa
1715203134Sthompsa	/* fix broken 5GHz LNA entries */
1716203134Sthompsa	if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1717203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 2);
1718203134Sthompsa		sc->lna[2] = sc->lna[1];
1719203134Sthompsa	}
1720203134Sthompsa	if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1721203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 3);
1722203134Sthompsa		sc->lna[3] = sc->lna[1];
1723203134Sthompsa	}
1724203134Sthompsa
1725203134Sthompsa	/* fix broken RSSI offset entries */
1726203134Sthompsa	for (ant = 0; ant < 3; ant++) {
1727203134Sthompsa		if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1728203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1729203134Sthompsa			    ant + 1, sc->rssi_2ghz[ant]);
1730203134Sthompsa			sc->rssi_2ghz[ant] = 0;
1731203134Sthompsa		}
1732203134Sthompsa		if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1733203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1734203134Sthompsa			    ant + 1, sc->rssi_5ghz[ant]);
1735203134Sthompsa			sc->rssi_5ghz[ant] = 0;
1736203134Sthompsa		}
1737203134Sthompsa	}
1738209917Sthompsa	return (0);
1739203134Sthompsa}
1740203134Sthompsa
1741218676Shselaskystatic struct ieee80211_node *
1742203134Sthompsarun_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1743203134Sthompsa{
1744203134Sthompsa	return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1745203134Sthompsa}
1746203134Sthompsa
1747203134Sthompsastatic int
1748203134Sthompsarun_media_change(struct ifnet *ifp)
1749203134Sthompsa{
1750208019Sthompsa	struct ieee80211vap *vap = ifp->if_softc;
1751208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1752203134Sthompsa	const struct ieee80211_txparam *tp;
1753208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1754203134Sthompsa	uint8_t rate, ridx;
1755203134Sthompsa	int error;
1756203134Sthompsa
1757203134Sthompsa	RUN_LOCK(sc);
1758203134Sthompsa
1759203134Sthompsa	error = ieee80211_media_change(ifp);
1760209917Sthompsa	if (error != ENETRESET) {
1761203134Sthompsa		RUN_UNLOCK(sc);
1762209917Sthompsa		return (error);
1763208019Sthompsa	}
1764203134Sthompsa
1765203134Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1766203134Sthompsa	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1767212127Sthompsa		struct ieee80211_node *ni;
1768212127Sthompsa		struct run_node	*rn;
1769212127Sthompsa
1770203134Sthompsa		rate = ic->ic_sup_rates[ic->ic_curmode].
1771203134Sthompsa		    rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1772203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1773203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
1774203134Sthompsa				break;
1775212127Sthompsa		ni = ieee80211_ref_node(vap->iv_bss);
1776212127Sthompsa		rn = (struct run_node *)ni;
1777208019Sthompsa		rn->fix_ridx = ridx;
1778208019Sthompsa		DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1779212127Sthompsa		ieee80211_free_node(ni);
1780203134Sthompsa	}
1781203134Sthompsa
1782208019Sthompsa#if 0
1783203134Sthompsa	if ((ifp->if_flags & IFF_UP) &&
1784203134Sthompsa	    (ifp->if_drv_flags &  IFF_DRV_RUNNING)){
1785203134Sthompsa		run_init_locked(sc);
1786203134Sthompsa	}
1787208019Sthompsa#endif
1788203134Sthompsa
1789203134Sthompsa	RUN_UNLOCK(sc);
1790203134Sthompsa
1791209917Sthompsa	return (0);
1792203134Sthompsa}
1793203134Sthompsa
1794203134Sthompsastatic int
1795203134Sthompsarun_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1796203134Sthompsa{
1797203134Sthompsa	const struct ieee80211_txparam *tp;
1798203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1799203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1800203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
1801203134Sthompsa	enum ieee80211_state ostate;
1802208019Sthompsa	uint32_t sta[3];
1803203134Sthompsa	uint32_t tmp;
1804208019Sthompsa	uint8_t ratectl;
1805208019Sthompsa	uint8_t restart_ratectl = 0;
1806208019Sthompsa	uint8_t bid = 1 << rvp->rvp_id;
1807203134Sthompsa
1808203134Sthompsa	ostate = vap->iv_state;
1809203134Sthompsa	DPRINTF("%s -> %s\n",
1810203134Sthompsa		ieee80211_state_name[ostate],
1811203134Sthompsa		ieee80211_state_name[nstate]);
1812203134Sthompsa
1813203134Sthompsa	IEEE80211_UNLOCK(ic);
1814203134Sthompsa	RUN_LOCK(sc);
1815203134Sthompsa
1816208019Sthompsa	ratectl = sc->ratectl_run; /* remember current state */
1817208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
1818208019Sthompsa	usb_callout_stop(&sc->ratectl_ch);
1819203134Sthompsa
1820203134Sthompsa	if (ostate == IEEE80211_S_RUN) {
1821203134Sthompsa		/* turn link LED off */
1822203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO);
1823203134Sthompsa	}
1824203134Sthompsa
1825203134Sthompsa	switch (nstate) {
1826203134Sthompsa	case IEEE80211_S_INIT:
1827208019Sthompsa		restart_ratectl = 1;
1828208019Sthompsa
1829208019Sthompsa		if (ostate != IEEE80211_S_RUN)
1830208019Sthompsa			break;
1831208019Sthompsa
1832208019Sthompsa		ratectl &= ~bid;
1833208019Sthompsa		sc->runbmap &= ~bid;
1834208019Sthompsa
1835208019Sthompsa		/* abort TSF synchronization if there is no vap running */
1836209917Sthompsa		if (--sc->running == 0) {
1837203134Sthompsa			run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1838203134Sthompsa			run_write(sc, RT2860_BCN_TIME_CFG,
1839203134Sthompsa			    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1840203134Sthompsa			    RT2860_TBTT_TIMER_EN));
1841203134Sthompsa		}
1842203134Sthompsa		break;
1843203134Sthompsa
1844203134Sthompsa	case IEEE80211_S_RUN:
1845209917Sthompsa		if (!(sc->runbmap & bid)) {
1846208019Sthompsa			if(sc->running++)
1847208019Sthompsa				restart_ratectl = 1;
1848208019Sthompsa			sc->runbmap |= bid;
1849208019Sthompsa		}
1850203134Sthompsa
1851218492Sbschmidt		m_freem(rvp->beacon_mbuf);
1852218492Sbschmidt		rvp->beacon_mbuf = NULL;
1853218492Sbschmidt
1854209917Sthompsa		switch (vap->iv_opmode) {
1855208019Sthompsa		case IEEE80211_M_HOSTAP:
1856208019Sthompsa		case IEEE80211_M_MBSS:
1857208019Sthompsa			sc->ap_running |= bid;
1858208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1859208019Sthompsa			run_update_beacon_cb(vap);
1860208019Sthompsa			break;
1861208019Sthompsa		case IEEE80211_M_IBSS:
1862208019Sthompsa			sc->adhoc_running |= bid;
1863209917Sthompsa			if (!sc->ap_running)
1864208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1865208019Sthompsa			run_update_beacon_cb(vap);
1866208019Sthompsa			break;
1867208019Sthompsa		case IEEE80211_M_STA:
1868208019Sthompsa			sc->sta_running |= bid;
1869209917Sthompsa			if (!sc->ap_running && !sc->adhoc_running)
1870208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1871208019Sthompsa
1872208019Sthompsa			/* read statistic counters (clear on read) */
1873208019Sthompsa			run_read_region_1(sc, RT2860_TX_STA_CNT0,
1874208019Sthompsa			    (uint8_t *)sta, sizeof sta);
1875208019Sthompsa
1876208019Sthompsa			break;
1877208019Sthompsa		default:
1878208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1879208019Sthompsa			break;
1880208019Sthompsa		}
1881208019Sthompsa
1882203134Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1883212127Sthompsa			struct ieee80211_node *ni;
1884212127Sthompsa
1885236439Shselasky			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
1886236439Shselasky				RUN_UNLOCK(sc);
1887236439Shselasky				IEEE80211_LOCK(ic);
1888236439Shselasky				return (-1);
1889236439Shselasky			}
1890203134Sthompsa			run_updateslot(ic->ic_ifp);
1891203134Sthompsa			run_enable_mrr(sc);
1892203134Sthompsa			run_set_txpreamble(sc);
1893203134Sthompsa			run_set_basicrates(sc);
1894212127Sthompsa			ni = ieee80211_ref_node(vap->iv_bss);
1895203134Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1896203134Sthompsa			run_set_bssid(sc, ni->ni_bssid);
1897212127Sthompsa			ieee80211_free_node(ni);
1898208019Sthompsa			run_enable_tsf_sync(sc);
1899203134Sthompsa
1900208019Sthompsa			/* enable automatic rate adaptation */
1901208019Sthompsa			tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1902208019Sthompsa			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1903208019Sthompsa				ratectl |= bid;
1904203134Sthompsa		}
1905203134Sthompsa
1906203134Sthompsa		/* turn link LED on */
1907203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO |
1908208019Sthompsa		    (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1909203134Sthompsa		     RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1910203134Sthompsa
1911203134Sthompsa		break;
1912203134Sthompsa	default:
1913203134Sthompsa		DPRINTFN(6, "undefined case\n");
1914203134Sthompsa		break;
1915203134Sthompsa	}
1916203134Sthompsa
1917208019Sthompsa	/* restart amrr for running VAPs */
1918209917Sthompsa	if ((sc->ratectl_run = ratectl) && restart_ratectl)
1919208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1920208019Sthompsa
1921203134Sthompsa	RUN_UNLOCK(sc);
1922203134Sthompsa	IEEE80211_LOCK(ic);
1923203134Sthompsa
1924203134Sthompsa	return(rvp->newstate(vap, nstate, arg));
1925203134Sthompsa}
1926203134Sthompsa
1927203134Sthompsa/* ARGSUSED */
1928203134Sthompsastatic void
1929208019Sthompsarun_wme_update_cb(void *arg)
1930203134Sthompsa{
1931203134Sthompsa	struct ieee80211com *ic = arg;
1932203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1933203134Sthompsa	struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1934203134Sthompsa	int aci, error = 0;
1935203134Sthompsa
1936208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1937203134Sthompsa
1938203134Sthompsa	/* update MAC TX configuration registers */
1939203134Sthompsa	for (aci = 0; aci < WME_NUM_AC; aci++) {
1940203134Sthompsa		error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1941203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmax << 16 |
1942203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmin << 12 |
1943203134Sthompsa		    wmesp->wme_params[aci].wmep_aifsn  <<  8 |
1944203134Sthompsa		    wmesp->wme_params[aci].wmep_txopLimit);
1945209917Sthompsa		if (error) goto err;
1946203134Sthompsa	}
1947203134Sthompsa
1948203134Sthompsa	/* update SCH/DMA registers too */
1949203134Sthompsa	error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1950203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_aifsn  << 12 |
1951203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_aifsn  <<  8 |
1952203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_aifsn  <<  4 |
1953203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1954209917Sthompsa	if (error) goto err;
1955203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1956203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1957203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmin <<  8 |
1958203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmin <<  4 |
1959203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1960209917Sthompsa	if (error) goto err;
1961203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1962203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1963203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmax <<  8 |
1964203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmax <<  4 |
1965203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1966209917Sthompsa	if (error) goto err;
1967203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1968203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1969203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1970209917Sthompsa	if (error) goto err;
1971203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1972203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1973203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1974203134Sthompsa
1975203134Sthompsaerr:
1976209917Sthompsa	if (error)
1977203134Sthompsa		DPRINTF("WME update failed\n");
1978203134Sthompsa
1979203134Sthompsa	return;
1980203134Sthompsa}
1981203134Sthompsa
1982208019Sthompsastatic int
1983208019Sthompsarun_wme_update(struct ieee80211com *ic)
1984208019Sthompsa{
1985208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1986208019Sthompsa
1987208019Sthompsa	/* sometime called wothout lock */
1988209917Sthompsa	if (mtx_owned(&ic->ic_comlock.mtx)) {
1989208019Sthompsa		uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1990208019Sthompsa		DPRINTF("cmdq_store=%d\n", i);
1991208019Sthompsa		sc->cmdq[i].func = run_wme_update_cb;
1992208019Sthompsa		sc->cmdq[i].arg0 = ic;
1993208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
1994209918Sthompsa		return (0);
1995208019Sthompsa	}
1996208019Sthompsa
1997208019Sthompsa	RUN_LOCK(sc);
1998208019Sthompsa	run_wme_update_cb(ic);
1999208019Sthompsa	RUN_UNLOCK(sc);
2000208019Sthompsa
2001208019Sthompsa	/* return whatever, upper layer desn't care anyway */
2002208019Sthompsa	return (0);
2003208019Sthompsa}
2004208019Sthompsa
2005203134Sthompsastatic void
2006203134Sthompsarun_key_update_begin(struct ieee80211vap *vap)
2007203134Sthompsa{
2008203134Sthompsa	/*
2009208019Sthompsa	 * To avoid out-of-order events, both run_key_set() and
2010208019Sthompsa	 * _delete() are deferred and handled by run_cmdq_cb().
2011208019Sthompsa	 * So, there is nothing we need to do here.
2012203134Sthompsa	 */
2013203134Sthompsa}
2014203134Sthompsa
2015203134Sthompsastatic void
2016203134Sthompsarun_key_update_end(struct ieee80211vap *vap)
2017203134Sthompsa{
2018203134Sthompsa	/* null */
2019203134Sthompsa}
2020203134Sthompsa
2021208019Sthompsastatic void
2022208019Sthompsarun_key_set_cb(void *arg)
2023203134Sthompsa{
2024208019Sthompsa	struct run_cmdq *cmdq = arg;
2025208019Sthompsa	struct ieee80211vap *vap = cmdq->arg1;
2026208019Sthompsa	struct ieee80211_key *k = cmdq->k;
2027203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2028208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2029203134Sthompsa	struct ieee80211_node *ni;
2030203134Sthompsa	uint32_t attr;
2031203134Sthompsa	uint16_t base, associd;
2032209144Sthompsa	uint8_t mode, wcid, iv[8];
2033203134Sthompsa
2034208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2035203134Sthompsa
2036209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2037208019Sthompsa		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
2038209144Sthompsa	else
2039203134Sthompsa		ni = vap->iv_bss;
2040208019Sthompsa	associd = (ni != NULL) ? ni->ni_associd : 0;
2041203134Sthompsa
2042203134Sthompsa	/* map net80211 cipher to RT2860 security mode */
2043203134Sthompsa	switch (k->wk_cipher->ic_cipher) {
2044203134Sthompsa	case IEEE80211_CIPHER_WEP:
2045203134Sthompsa		if(k->wk_keylen < 8)
2046203134Sthompsa			mode = RT2860_MODE_WEP40;
2047203134Sthompsa		else
2048203134Sthompsa			mode = RT2860_MODE_WEP104;
2049203134Sthompsa		break;
2050203134Sthompsa	case IEEE80211_CIPHER_TKIP:
2051203134Sthompsa		mode = RT2860_MODE_TKIP;
2052203134Sthompsa		break;
2053203134Sthompsa	case IEEE80211_CIPHER_AES_CCM:
2054203134Sthompsa		mode = RT2860_MODE_AES_CCMP;
2055203134Sthompsa		break;
2056203134Sthompsa	default:
2057203134Sthompsa		DPRINTF("undefined case\n");
2058208019Sthompsa		return;
2059203134Sthompsa	}
2060203134Sthompsa
2061208019Sthompsa	DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2062203134Sthompsa	    associd, k->wk_keyix, mode,
2063208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2064208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2065208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2066203134Sthompsa
2067203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2068203134Sthompsa		wcid = 0;	/* NB: update WCID0 for group keys */
2069208019Sthompsa		base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2070203134Sthompsa	} else {
2071245047Shselasky		wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2072245047Shselasky		    1 : RUN_AID2WCID(associd);
2073203134Sthompsa		base = RT2860_PKEY(wcid);
2074203134Sthompsa	}
2075203134Sthompsa
2076203134Sthompsa	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2077203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, 16))
2078208019Sthompsa			return;
2079209144Sthompsa		if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8))	/* wk_txmic */
2080208019Sthompsa			return;
2081209144Sthompsa		if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8))	/* wk_rxmic */
2082208019Sthompsa			return;
2083203134Sthompsa	} else {
2084203134Sthompsa		/* roundup len to 16-bit: XXX fix write_region_1() instead */
2085203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2086208019Sthompsa			return;
2087203134Sthompsa	}
2088203134Sthompsa
2089203134Sthompsa	if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2090203134Sthompsa	    (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2091203134Sthompsa		/* set initial packet number in IV+EIV */
2092209917Sthompsa		if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2093203134Sthompsa			memset(iv, 0, sizeof iv);
2094208019Sthompsa			iv[3] = vap->iv_def_txkey << 6;
2095203134Sthompsa		} else {
2096203134Sthompsa			if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2097203134Sthompsa				iv[0] = k->wk_keytsc >> 8;
2098203134Sthompsa				iv[1] = (iv[0] | 0x20) & 0x7f;
2099203134Sthompsa				iv[2] = k->wk_keytsc;
2100203134Sthompsa			} else /* CCMP */ {
2101203134Sthompsa				iv[0] = k->wk_keytsc;
2102203134Sthompsa				iv[1] = k->wk_keytsc >> 8;
2103203134Sthompsa				iv[2] = 0;
2104203134Sthompsa			}
2105203134Sthompsa			iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2106203134Sthompsa			iv[4] = k->wk_keytsc >> 16;
2107203134Sthompsa			iv[5] = k->wk_keytsc >> 24;
2108203134Sthompsa			iv[6] = k->wk_keytsc >> 32;
2109203134Sthompsa			iv[7] = k->wk_keytsc >> 40;
2110203134Sthompsa		}
2111209917Sthompsa		if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2112208019Sthompsa			return;
2113203134Sthompsa	}
2114203134Sthompsa
2115203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2116203134Sthompsa		/* install group key */
2117209917Sthompsa		if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2118208019Sthompsa			return;
2119203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2120203134Sthompsa		attr |= mode << (k->wk_keyix * 4);
2121209917Sthompsa		if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2122208019Sthompsa			return;
2123203134Sthompsa	} else {
2124203134Sthompsa		/* install pairwise key */
2125209917Sthompsa		if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2126208019Sthompsa			return;
2127203134Sthompsa		attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2128209917Sthompsa		if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2129208019Sthompsa			return;
2130203134Sthompsa	}
2131203134Sthompsa
2132203134Sthompsa	/* TODO create a pass-thru key entry? */
2133203134Sthompsa
2134208019Sthompsa	/* need wcid to delete the right key later */
2135208019Sthompsa	k->wk_pad = wcid;
2136203134Sthompsa}
2137203134Sthompsa
2138203134Sthompsa/*
2139208019Sthompsa * Don't have to be deferred, but in order to keep order of
2140208019Sthompsa * execution, i.e. with run_key_delete(), defer this and let
2141208019Sthompsa * run_cmdq_cb() maintain the order.
2142208019Sthompsa *
2143203134Sthompsa * return 0 on error
2144203134Sthompsa */
2145203134Sthompsastatic int
2146208019Sthompsarun_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2147208019Sthompsa		const uint8_t mac[IEEE80211_ADDR_LEN])
2148203134Sthompsa{
2149203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2150203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2151208019Sthompsa	uint32_t i;
2152208019Sthompsa
2153208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2154208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2155208019Sthompsa	sc->cmdq[i].func = run_key_set_cb;
2156208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2157208019Sthompsa	sc->cmdq[i].arg1 = vap;
2158208019Sthompsa	sc->cmdq[i].k = k;
2159208019Sthompsa	IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2160208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2161208019Sthompsa
2162209144Sthompsa	/*
2163209144Sthompsa	 * To make sure key will be set when hostapd
2164209144Sthompsa	 * calls iv_key_set() before if_init().
2165209144Sthompsa	 */
2166209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2167209144Sthompsa		RUN_LOCK(sc);
2168209144Sthompsa		sc->cmdq_key_set = RUN_CMDQ_GO;
2169209144Sthompsa		RUN_UNLOCK(sc);
2170209144Sthompsa	}
2171209144Sthompsa
2172209917Sthompsa	return (1);
2173208019Sthompsa}
2174208019Sthompsa
2175208019Sthompsa/*
2176208019Sthompsa * If wlan is destroyed without being brought down i.e. without
2177208019Sthompsa * wlan down or wpa_cli terminate, this function is called after
2178208019Sthompsa * vap is gone. Don't refer it.
2179208019Sthompsa */
2180208019Sthompsastatic void
2181208019Sthompsarun_key_delete_cb(void *arg)
2182208019Sthompsa{
2183208019Sthompsa	struct run_cmdq *cmdq = arg;
2184208019Sthompsa	struct run_softc *sc = cmdq->arg1;
2185208019Sthompsa	struct ieee80211_key *k = &cmdq->key;
2186203134Sthompsa	uint32_t attr;
2187203134Sthompsa	uint8_t wcid;
2188203134Sthompsa
2189208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2190203134Sthompsa
2191203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2192203134Sthompsa		/* remove group key */
2193208019Sthompsa		DPRINTF("removing group key\n");
2194208019Sthompsa		run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2195203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2196208019Sthompsa		run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2197203134Sthompsa	} else {
2198203134Sthompsa		/* remove pairwise key */
2199208019Sthompsa		DPRINTF("removing key for wcid %x\n", k->wk_pad);
2200208019Sthompsa		/* matching wcid was written to wk_pad in run_key_set() */
2201208019Sthompsa		wcid = k->wk_pad;
2202208019Sthompsa		run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2203203134Sthompsa		attr &= ~0xf;
2204208019Sthompsa		run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2205208019Sthompsa		run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2206203134Sthompsa	}
2207203134Sthompsa
2208208019Sthompsa	k->wk_pad = 0;
2209203134Sthompsa}
2210203134Sthompsa
2211208019Sthompsa/*
2212208019Sthompsa * return 0 on error
2213208019Sthompsa */
2214208019Sthompsastatic int
2215208019Sthompsarun_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2216203134Sthompsa{
2217208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2218208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2219208019Sthompsa	struct ieee80211_key *k0;
2220208019Sthompsa	uint32_t i;
2221203134Sthompsa
2222208019Sthompsa	/*
2223208019Sthompsa	 * When called back, key might be gone. So, make a copy
2224208019Sthompsa	 * of some values need to delete keys before deferring.
2225208019Sthompsa	 * But, because of LOR with node lock, cannot use lock here.
2226208019Sthompsa	 * So, use atomic instead.
2227208019Sthompsa	 */
2228208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2229208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2230208019Sthompsa	sc->cmdq[i].func = run_key_delete_cb;
2231208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2232208019Sthompsa	sc->cmdq[i].arg1 = sc;
2233208019Sthompsa	k0 = &sc->cmdq[i].key;
2234208019Sthompsa	k0->wk_flags = k->wk_flags;
2235208019Sthompsa	k0->wk_keyix = k->wk_keyix;
2236208019Sthompsa	/* matching wcid was written to wk_pad in run_key_set() */
2237208019Sthompsa	k0->wk_pad = k->wk_pad;
2238208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2239208019Sthompsa	return (1);	/* return fake success */
2240203134Sthompsa
2241203134Sthompsa}
2242203134Sthompsa
2243203134Sthompsastatic void
2244206358Srpaulorun_ratectl_to(void *arg)
2245203134Sthompsa{
2246208019Sthompsa	struct run_softc *sc = arg;
2247203134Sthompsa
2248203134Sthompsa	/* do it in a process context, so it can go sleep */
2249208019Sthompsa	ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2250203134Sthompsa	/* next timeout will be rescheduled in the callback task */
2251203134Sthompsa}
2252203134Sthompsa
2253203134Sthompsa/* ARGSUSED */
2254203134Sthompsastatic void
2255206358Srpaulorun_ratectl_cb(void *arg, int pending)
2256203134Sthompsa{
2257208019Sthompsa	struct run_softc *sc = arg;
2258208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2259208019Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2260203134Sthompsa
2261209917Sthompsa	if (vap == NULL)
2262208019Sthompsa		return;
2263208019Sthompsa
2264209917Sthompsa	if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2265208019Sthompsa		run_iter_func(sc, vap->iv_bss);
2266203134Sthompsa	else {
2267203134Sthompsa		/*
2268203134Sthompsa		 * run_reset_livelock() doesn't do anything with AMRR,
2269203134Sthompsa		 * but Ralink wants us to call it every 1 sec. So, we
2270203134Sthompsa		 * piggyback here rather than creating another callout.
2271203134Sthompsa		 * Livelock may occur only in HOSTAP or IBSS mode
2272203134Sthompsa		 * (when h/w is sending beacons).
2273203134Sthompsa		 */
2274203134Sthompsa		RUN_LOCK(sc);
2275203134Sthompsa		run_reset_livelock(sc);
2276208019Sthompsa		/* just in case, there are some stats to drain */
2277208019Sthompsa		run_drain_fifo(sc);
2278203134Sthompsa		RUN_UNLOCK(sc);
2279208019Sthompsa		ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2280203134Sthompsa	}
2281203134Sthompsa
2282257712Shselasky	RUN_LOCK(sc);
2283208019Sthompsa	if(sc->ratectl_run != RUN_RATECTL_OFF)
2284208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2285257712Shselasky	RUN_UNLOCK(sc);
2286203134Sthompsa}
2287203134Sthompsa
2288203134Sthompsastatic void
2289208019Sthompsarun_drain_fifo(void *arg)
2290203134Sthompsa{
2291208019Sthompsa	struct run_softc *sc = arg;
2292208019Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2293208019Sthompsa	uint32_t stat;
2294218676Shselasky	uint16_t (*wstat)[3];
2295203134Sthompsa	uint8_t wcid, mcs, pid;
2296218676Shselasky	int8_t retry;
2297203134Sthompsa
2298208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2299203134Sthompsa
2300208019Sthompsa	for (;;) {
2301203134Sthompsa		/* drain Tx status FIFO (maxsize = 16) */
2302203134Sthompsa		run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2303208019Sthompsa		DPRINTFN(4, "tx stat 0x%08x\n", stat);
2304209917Sthompsa		if (!(stat & RT2860_TXQ_VLD))
2305208019Sthompsa			break;
2306203134Sthompsa
2307208019Sthompsa		wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2308203134Sthompsa
2309208019Sthompsa		/* if no ACK was requested, no feedback is available */
2310208019Sthompsa		if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2311208019Sthompsa		    wcid == 0)
2312208019Sthompsa			continue;
2313203134Sthompsa
2314218676Shselasky		/*
2315218676Shselasky		 * Even though each stat is Tx-complete-status like format,
2316218676Shselasky		 * the device can poll stats. Because there is no guarantee
2317218676Shselasky		 * that the referring node is still around when read the stats.
2318218676Shselasky		 * So that, if we use ieee80211_ratectl_tx_update(), we will
2319218676Shselasky		 * have hard time not to refer already freed node.
2320218676Shselasky		 *
2321218676Shselasky		 * To eliminate such page faults, we poll stats in softc.
2322218676Shselasky		 * Then, update the rates later with ieee80211_ratectl_tx_update().
2323218676Shselasky		 */
2324218676Shselasky		wstat = &(sc->wcid_stats[wcid]);
2325218676Shselasky		(*wstat)[RUN_TXCNT]++;
2326218676Shselasky		if (stat & RT2860_TXQ_OK)
2327218676Shselasky			(*wstat)[RUN_SUCCESS]++;
2328218676Shselasky		else
2329208019Sthompsa			ifp->if_oerrors++;
2330218676Shselasky		/*
2331218676Shselasky		 * Check if there were retries, ie if the Tx success rate is
2332218676Shselasky		 * different from the requested rate. Note that it works only
2333218676Shselasky		 * because we do not allow rate fallback from OFDM to CCK.
2334218676Shselasky		 */
2335218676Shselasky		mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2336218676Shselasky		pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2337218676Shselasky		if ((retry = pid -1 - mcs) > 0) {
2338218676Shselasky			(*wstat)[RUN_TXCNT] += retry;
2339218676Shselasky			(*wstat)[RUN_RETRY] += retry;
2340203134Sthompsa		}
2341208019Sthompsa	}
2342208019Sthompsa	DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2343208019Sthompsa
2344208019Sthompsa	sc->fifo_cnt = 0;
2345208019Sthompsa}
2346208019Sthompsa
2347208019Sthompsastatic void
2348208019Sthompsarun_iter_func(void *arg, struct ieee80211_node *ni)
2349208019Sthompsa{
2350208019Sthompsa	struct run_softc *sc = arg;
2351208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2352208019Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2353208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2354208019Sthompsa	struct run_node *rn = (void *)ni;
2355218676Shselasky	union run_stats sta[2];
2356218676Shselasky	uint16_t (*wstat)[3];
2357218676Shselasky	int txcnt, success, retrycnt, error;
2358208019Sthompsa
2359218676Shselasky	RUN_LOCK(sc);
2360218676Shselasky
2361209917Sthompsa	if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2362209917Sthompsa	    vap->iv_opmode == IEEE80211_M_STA)) {
2363203134Sthompsa		/* read statistic counters (clear on read) and update AMRR state */
2364203134Sthompsa		error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2365203134Sthompsa		    sizeof sta);
2366203134Sthompsa		if (error != 0)
2367218676Shselasky			goto fail;
2368203134Sthompsa
2369203134Sthompsa		/* count failed TX as errors */
2370218676Shselasky		ifp->if_oerrors += le16toh(sta[0].error.fail);
2371203134Sthompsa
2372218676Shselasky		retrycnt = le16toh(sta[1].tx.retry);
2373218676Shselasky		success = le16toh(sta[1].tx.success);
2374218676Shselasky		txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2375203134Sthompsa
2376218676Shselasky		DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2377218676Shselasky			retrycnt, success, le16toh(sta[0].error.fail));
2378218676Shselasky	} else {
2379218676Shselasky		wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2380203134Sthompsa
2381218676Shselasky		if (wstat == &(sc->wcid_stats[0]) ||
2382218676Shselasky		    wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2383218676Shselasky			goto fail;
2384208019Sthompsa
2385218676Shselasky		txcnt = (*wstat)[RUN_TXCNT];
2386218676Shselasky		success = (*wstat)[RUN_SUCCESS];
2387218676Shselasky		retrycnt = (*wstat)[RUN_RETRY];
2388218676Shselasky		DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2389218676Shselasky		    retrycnt, txcnt, success);
2390208019Sthompsa
2391218676Shselasky		memset(wstat, 0, sizeof(*wstat));
2392203134Sthompsa	}
2393203134Sthompsa
2394218676Shselasky	ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2395208019Sthompsa	rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2396218676Shselasky
2397218676Shselaskyfail:
2398218676Shselasky	RUN_UNLOCK(sc);
2399218676Shselasky
2400208019Sthompsa	DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2401208019Sthompsa}
2402203134Sthompsa
2403208019Sthompsastatic void
2404208019Sthompsarun_newassoc_cb(void *arg)
2405208019Sthompsa{
2406208019Sthompsa	struct run_cmdq *cmdq = arg;
2407208019Sthompsa	struct ieee80211_node *ni = cmdq->arg1;
2408208019Sthompsa	struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2409208019Sthompsa	uint8_t wcid = cmdq->wcid;
2410203134Sthompsa
2411208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2412208019Sthompsa
2413208019Sthompsa	run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2414208019Sthompsa	    ni->ni_macaddr, IEEE80211_ADDR_LEN);
2415218676Shselasky
2416218676Shselasky	memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2417203134Sthompsa}
2418203134Sthompsa
2419203134Sthompsastatic void
2420203134Sthompsarun_newassoc(struct ieee80211_node *ni, int isnew)
2421203134Sthompsa{
2422203134Sthompsa	struct run_node *rn = (void *)ni;
2423203134Sthompsa	struct ieee80211_rateset *rs = &ni->ni_rates;
2424208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2425208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2426208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2427203134Sthompsa	uint8_t rate;
2428208019Sthompsa	uint8_t ridx;
2429245047Shselasky	uint8_t wcid;
2430208019Sthompsa	int i, j;
2431203134Sthompsa
2432245047Shselasky	wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2433245047Shselasky	    1 : RUN_AID2WCID(ni->ni_associd);
2434245047Shselasky
2435209917Sthompsa	if (wcid > RT2870_WCID_MAX) {
2436208019Sthompsa		device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2437208019Sthompsa		return;
2438208019Sthompsa	}
2439203134Sthompsa
2440208019Sthompsa	/* only interested in true associations */
2441209917Sthompsa	if (isnew && ni->ni_associd != 0) {
2442208019Sthompsa
2443208019Sthompsa		/*
2444208019Sthompsa		 * This function could is called though timeout function.
2445208019Sthompsa		 * Need to defer.
2446208019Sthompsa		 */
2447208019Sthompsa		uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2448208019Sthompsa		DPRINTF("cmdq_store=%d\n", cnt);
2449208019Sthompsa		sc->cmdq[cnt].func = run_newassoc_cb;
2450208019Sthompsa		sc->cmdq[cnt].arg0 = NULL;
2451208019Sthompsa		sc->cmdq[cnt].arg1 = ni;
2452208019Sthompsa		sc->cmdq[cnt].wcid = wcid;
2453208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
2454208019Sthompsa	}
2455208019Sthompsa
2456208019Sthompsa	DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2457208019Sthompsa	    isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2458208019Sthompsa
2459203134Sthompsa	for (i = 0; i < rs->rs_nrates; i++) {
2460203134Sthompsa		rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2461203134Sthompsa		/* convert 802.11 rate to hardware rate index */
2462203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2463203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
2464203134Sthompsa				break;
2465203134Sthompsa		rn->ridx[i] = ridx;
2466203134Sthompsa		/* determine rate of control response frames */
2467203134Sthompsa		for (j = i; j >= 0; j--) {
2468203134Sthompsa			if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2469203134Sthompsa			    rt2860_rates[rn->ridx[i]].phy ==
2470203134Sthompsa			    rt2860_rates[rn->ridx[j]].phy)
2471203134Sthompsa				break;
2472203134Sthompsa		}
2473203134Sthompsa		if (j >= 0) {
2474203134Sthompsa			rn->ctl_ridx[i] = rn->ridx[j];
2475203134Sthompsa		} else {
2476203134Sthompsa			/* no basic rate found, use mandatory one */
2477203134Sthompsa			rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2478203134Sthompsa		}
2479203134Sthompsa		DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2480203134Sthompsa		    rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2481203134Sthompsa	}
2482208019Sthompsa	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2483208019Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2484208019Sthompsa		if (rt2860_rates[ridx].rate == rate)
2485208019Sthompsa			break;
2486208019Sthompsa	rn->mgt_ridx = ridx;
2487208019Sthompsa	DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2488208019Sthompsa
2489208019Sthompsa	usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2490203134Sthompsa}
2491203134Sthompsa
2492203134Sthompsa/*
2493203134Sthompsa * Return the Rx chain with the highest RSSI for a given frame.
2494203134Sthompsa */
2495203134Sthompsastatic __inline uint8_t
2496203134Sthompsarun_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2497203134Sthompsa{
2498203134Sthompsa	uint8_t rxchain = 0;
2499203134Sthompsa
2500203134Sthompsa	if (sc->nrxchains > 1) {
2501203134Sthompsa		if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2502203134Sthompsa			rxchain = 1;
2503203134Sthompsa		if (sc->nrxchains > 2)
2504203134Sthompsa			if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2505203134Sthompsa				rxchain = 2;
2506203134Sthompsa	}
2507209917Sthompsa	return (rxchain);
2508203134Sthompsa}
2509203134Sthompsa
2510203134Sthompsastatic void
2511203134Sthompsarun_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2512203134Sthompsa{
2513203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2514203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2515203134Sthompsa	struct ieee80211_frame *wh;
2516203134Sthompsa	struct ieee80211_node *ni;
2517203134Sthompsa	struct rt2870_rxd *rxd;
2518203134Sthompsa	struct rt2860_rxwi *rxwi;
2519203134Sthompsa	uint32_t flags;
2520258643Shselasky	uint16_t len;
2521203134Sthompsa	uint8_t ant, rssi;
2522203134Sthompsa	int8_t nf;
2523203134Sthompsa
2524203134Sthompsa	rxwi = mtod(m, struct rt2860_rxwi *);
2525203134Sthompsa	len = le16toh(rxwi->len) & 0xfff;
2526203134Sthompsa	if (__predict_false(len > dmalen)) {
2527203134Sthompsa		m_freem(m);
2528203134Sthompsa		ifp->if_ierrors++;
2529203134Sthompsa		DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2530203134Sthompsa		return;
2531203134Sthompsa	}
2532203134Sthompsa	/* Rx descriptor is located at the end */
2533203134Sthompsa	rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2534203134Sthompsa	flags = le32toh(rxd->flags);
2535203134Sthompsa
2536203134Sthompsa	if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2537203134Sthompsa		m_freem(m);
2538203134Sthompsa		ifp->if_ierrors++;
2539203134Sthompsa		DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2540203134Sthompsa		return;
2541203134Sthompsa	}
2542203134Sthompsa
2543203134Sthompsa	m->m_data += sizeof(struct rt2860_rxwi);
2544203134Sthompsa	m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2545203134Sthompsa
2546203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2547203134Sthompsa
2548209917Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2549203134Sthompsa		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2550203134Sthompsa		m->m_flags |= M_WEP;
2551203134Sthompsa	}
2552203134Sthompsa
2553209917Sthompsa	if (flags & RT2860_RX_L2PAD) {
2554203134Sthompsa		DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2555203134Sthompsa		len += 2;
2556203134Sthompsa	}
2557203134Sthompsa
2558208019Sthompsa	ni = ieee80211_find_rxnode(ic,
2559208019Sthompsa	    mtod(m, struct ieee80211_frame_min *));
2560208019Sthompsa
2561203134Sthompsa	if (__predict_false(flags & RT2860_RX_MICERR)) {
2562203134Sthompsa		/* report MIC failures to net80211 for TKIP */
2563209917Sthompsa		if (ni != NULL)
2564208019Sthompsa			ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2565203134Sthompsa		m_freem(m);
2566203134Sthompsa		ifp->if_ierrors++;
2567203134Sthompsa		DPRINTF("MIC error. Someone is lying.\n");
2568203134Sthompsa		return;
2569203134Sthompsa	}
2570203134Sthompsa
2571203134Sthompsa	ant = run_maxrssi_chain(sc, rxwi);
2572203134Sthompsa	rssi = rxwi->rssi[ant];
2573203134Sthompsa	nf = run_rssi2dbm(sc, rssi, ant);
2574203134Sthompsa
2575203134Sthompsa	m->m_pkthdr.rcvif = ifp;
2576203134Sthompsa	m->m_pkthdr.len = m->m_len = len;
2577203134Sthompsa
2578203134Sthompsa	if (ni != NULL) {
2579203134Sthompsa		(void)ieee80211_input(ni, m, rssi, nf);
2580203134Sthompsa		ieee80211_free_node(ni);
2581203134Sthompsa	} else {
2582203134Sthompsa		(void)ieee80211_input_all(ic, m, rssi, nf);
2583203134Sthompsa	}
2584203134Sthompsa
2585209917Sthompsa	if (__predict_false(ieee80211_radiotap_active(ic))) {
2586203134Sthompsa		struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2587258643Shselasky		uint16_t phy;
2588203134Sthompsa
2589203134Sthompsa		tap->wr_flags = 0;
2590236439Shselasky		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2591236439Shselasky		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2592203134Sthompsa		tap->wr_antsignal = rssi;
2593203134Sthompsa		tap->wr_antenna = ant;
2594203134Sthompsa		tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2595203134Sthompsa		tap->wr_rate = 2;	/* in case it can't be found below */
2596203134Sthompsa		phy = le16toh(rxwi->phy);
2597203134Sthompsa		switch (phy & RT2860_PHY_MODE) {
2598203134Sthompsa		case RT2860_PHY_CCK:
2599203134Sthompsa			switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2600203134Sthompsa			case 0:	tap->wr_rate =   2; break;
2601203134Sthompsa			case 1:	tap->wr_rate =   4; break;
2602203134Sthompsa			case 2:	tap->wr_rate =  11; break;
2603203134Sthompsa			case 3:	tap->wr_rate =  22; break;
2604203134Sthompsa			}
2605203134Sthompsa			if (phy & RT2860_PHY_SHPRE)
2606203134Sthompsa				tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2607203134Sthompsa			break;
2608203134Sthompsa		case RT2860_PHY_OFDM:
2609203134Sthompsa			switch (phy & RT2860_PHY_MCS) {
2610203134Sthompsa			case 0:	tap->wr_rate =  12; break;
2611203134Sthompsa			case 1:	tap->wr_rate =  18; break;
2612203134Sthompsa			case 2:	tap->wr_rate =  24; break;
2613203134Sthompsa			case 3:	tap->wr_rate =  36; break;
2614203134Sthompsa			case 4:	tap->wr_rate =  48; break;
2615203134Sthompsa			case 5:	tap->wr_rate =  72; break;
2616203134Sthompsa			case 6:	tap->wr_rate =  96; break;
2617203134Sthompsa			case 7:	tap->wr_rate = 108; break;
2618203134Sthompsa			}
2619203134Sthompsa			break;
2620203134Sthompsa		}
2621203134Sthompsa	}
2622203134Sthompsa}
2623203134Sthompsa
2624203134Sthompsastatic void
2625203134Sthompsarun_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2626203134Sthompsa{
2627203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2628203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2629203134Sthompsa	struct mbuf *m = NULL;
2630203134Sthompsa	struct mbuf *m0;
2631203134Sthompsa	uint32_t dmalen;
2632203134Sthompsa	int xferlen;
2633203134Sthompsa
2634203134Sthompsa	usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2635203134Sthompsa
2636203134Sthompsa	switch (USB_GET_STATE(xfer)) {
2637203134Sthompsa	case USB_ST_TRANSFERRED:
2638203134Sthompsa
2639203134Sthompsa		DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2640203134Sthompsa
2641233774Shselasky		if (xferlen < (int)(sizeof(uint32_t) +
2642233774Shselasky		    sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) {
2643203134Sthompsa			DPRINTF("xfer too short %d\n", xferlen);
2644203134Sthompsa			goto tr_setup;
2645203134Sthompsa		}
2646203134Sthompsa
2647203134Sthompsa		m = sc->rx_m;
2648203134Sthompsa		sc->rx_m = NULL;
2649203134Sthompsa
2650203134Sthompsa		/* FALLTHROUGH */
2651203134Sthompsa	case USB_ST_SETUP:
2652203134Sthompsatr_setup:
2653203134Sthompsa		if (sc->rx_m == NULL) {
2654243857Sglebius			sc->rx_m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
2655203134Sthompsa			    MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2656203134Sthompsa		}
2657203134Sthompsa		if (sc->rx_m == NULL) {
2658203134Sthompsa			DPRINTF("could not allocate mbuf - idle with stall\n");
2659203134Sthompsa			ifp->if_ierrors++;
2660203134Sthompsa			usbd_xfer_set_stall(xfer);
2661203134Sthompsa			usbd_xfer_set_frames(xfer, 0);
2662203134Sthompsa		} else {
2663203134Sthompsa			/*
2664203134Sthompsa			 * Directly loading a mbuf cluster into DMA to
2665203134Sthompsa			 * save some data copying. This works because
2666203134Sthompsa			 * there is only one cluster.
2667203134Sthompsa			 */
2668203134Sthompsa			usbd_xfer_set_frame_data(xfer, 0,
2669203134Sthompsa			    mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2670203134Sthompsa			usbd_xfer_set_frames(xfer, 1);
2671203134Sthompsa		}
2672203134Sthompsa		usbd_transfer_submit(xfer);
2673203134Sthompsa		break;
2674203134Sthompsa
2675203134Sthompsa	default:	/* Error */
2676203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2677203134Sthompsa			/* try to clear stall first */
2678203134Sthompsa			usbd_xfer_set_stall(xfer);
2679203134Sthompsa
2680203134Sthompsa			if (error == USB_ERR_TIMEOUT)
2681203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2682203134Sthompsa
2683203134Sthompsa			ifp->if_ierrors++;
2684203134Sthompsa
2685203134Sthompsa			goto tr_setup;
2686203134Sthompsa		}
2687209917Sthompsa		if (sc->rx_m != NULL) {
2688203134Sthompsa			m_freem(sc->rx_m);
2689203134Sthompsa			sc->rx_m = NULL;
2690203134Sthompsa		}
2691203134Sthompsa		break;
2692203134Sthompsa	}
2693203134Sthompsa
2694203134Sthompsa	if (m == NULL)
2695203134Sthompsa		return;
2696203134Sthompsa
2697203134Sthompsa	/* inputting all the frames must be last */
2698203134Sthompsa
2699203134Sthompsa	RUN_UNLOCK(sc);
2700203134Sthompsa
2701203134Sthompsa	m->m_pkthdr.len = m->m_len = xferlen;
2702203134Sthompsa
2703203134Sthompsa	/* HW can aggregate multiple 802.11 frames in a single USB xfer */
2704203134Sthompsa	for(;;) {
2705203134Sthompsa		dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2706203134Sthompsa
2707233774Shselasky		if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2708233774Shselasky		    ((dmalen & 3) != 0)) {
2709203134Sthompsa			DPRINTF("bad DMA length %u\n", dmalen);
2710203134Sthompsa			break;
2711203134Sthompsa		}
2712233774Shselasky		if ((dmalen + 8) > (uint32_t)xferlen) {
2713203134Sthompsa			DPRINTF("bad DMA length %u > %d\n",
2714203134Sthompsa			dmalen + 8, xferlen);
2715203134Sthompsa			break;
2716203134Sthompsa		}
2717203134Sthompsa
2718203134Sthompsa		/* If it is the last one or a single frame, we won't copy. */
2719209917Sthompsa		if ((xferlen -= dmalen + 8) <= 8) {
2720203134Sthompsa			/* trim 32-bit DMA-len header */
2721203134Sthompsa			m->m_data += 4;
2722203134Sthompsa			m->m_pkthdr.len = m->m_len -= 4;
2723203134Sthompsa			run_rx_frame(sc, m, dmalen);
2724257435Shselasky			m = NULL;	/* don't free source buffer */
2725203134Sthompsa			break;
2726203134Sthompsa		}
2727203134Sthompsa
2728203134Sthompsa		/* copy aggregated frames to another mbuf */
2729243857Sglebius		m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2730203134Sthompsa		if (__predict_false(m0 == NULL)) {
2731203134Sthompsa			DPRINTF("could not allocate mbuf\n");
2732203134Sthompsa			ifp->if_ierrors++;
2733203134Sthompsa			break;
2734203134Sthompsa		}
2735203134Sthompsa		m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2736203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2737203134Sthompsa		m0->m_pkthdr.len = m0->m_len =
2738203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd);
2739203134Sthompsa		run_rx_frame(sc, m0, dmalen);
2740203134Sthompsa
2741203134Sthompsa		/* update data ptr */
2742203134Sthompsa		m->m_data += dmalen + 8;
2743203134Sthompsa		m->m_pkthdr.len = m->m_len -= dmalen + 8;
2744203134Sthompsa	}
2745203134Sthompsa
2746257435Shselasky	/* make sure we free the source buffer, if any */
2747257435Shselasky	m_freem(m);
2748257435Shselasky
2749203134Sthompsa	RUN_LOCK(sc);
2750203134Sthompsa}
2751203134Sthompsa
2752203134Sthompsastatic void
2753203134Sthompsarun_tx_free(struct run_endpoint_queue *pq,
2754203134Sthompsa    struct run_tx_data *data, int txerr)
2755203134Sthompsa{
2756203134Sthompsa	if (data->m != NULL) {
2757203134Sthompsa		if (data->m->m_flags & M_TXCB)
2758203134Sthompsa			ieee80211_process_callback(data->ni, data->m,
2759203134Sthompsa			    txerr ? ETIMEDOUT : 0);
2760203134Sthompsa		m_freem(data->m);
2761203134Sthompsa		data->m = NULL;
2762203134Sthompsa
2763209917Sthompsa		if (data->ni == NULL) {
2764203134Sthompsa			DPRINTF("no node\n");
2765203134Sthompsa		} else {
2766203134Sthompsa			ieee80211_free_node(data->ni);
2767203134Sthompsa			data->ni = NULL;
2768203134Sthompsa		}
2769203134Sthompsa	}
2770203134Sthompsa
2771203134Sthompsa	STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2772203134Sthompsa	pq->tx_nfree++;
2773203134Sthompsa}
2774203134Sthompsa
2775203134Sthompsastatic void
2776257429Shselaskyrun_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, u_int index)
2777203134Sthompsa{
2778203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2779203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2780208019Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2781203134Sthompsa	struct run_tx_data *data;
2782203134Sthompsa	struct ieee80211vap *vap = NULL;
2783203134Sthompsa	struct usb_page_cache *pc;
2784203134Sthompsa	struct run_endpoint_queue *pq = &sc->sc_epq[index];
2785203134Sthompsa	struct mbuf *m;
2786203134Sthompsa	usb_frlength_t size;
2787203134Sthompsa	int actlen;
2788203134Sthompsa	int sumlen;
2789203134Sthompsa
2790203134Sthompsa	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2791203134Sthompsa
2792209917Sthompsa	switch (USB_GET_STATE(xfer)) {
2793203134Sthompsa	case USB_ST_TRANSFERRED:
2794203134Sthompsa		DPRINTFN(11, "transfer complete: %d "
2795203134Sthompsa		    "bytes @ index %d\n", actlen, index);
2796203134Sthompsa
2797203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2798203134Sthompsa
2799203134Sthompsa		run_tx_free(pq, data, 0);
2800203134Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2801203134Sthompsa
2802203134Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2803203134Sthompsa
2804203134Sthompsa		ifp->if_opackets++;
2805203134Sthompsa
2806203134Sthompsa		/* FALLTHROUGH */
2807203134Sthompsa	case USB_ST_SETUP:
2808203134Sthompsatr_setup:
2809203134Sthompsa		data = STAILQ_FIRST(&pq->tx_qh);
2810209917Sthompsa		if (data == NULL)
2811203134Sthompsa			break;
2812203134Sthompsa
2813203134Sthompsa		STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2814203134Sthompsa
2815203134Sthompsa		m = data->m;
2816228508Shselasky		if ((m->m_pkthdr.len +
2817228508Shselasky		    sizeof(data->desc) + 3 + 8) > RUN_MAX_TXSZ) {
2818203134Sthompsa			DPRINTF("data overflow, %u bytes\n",
2819203134Sthompsa			    m->m_pkthdr.len);
2820203134Sthompsa
2821203134Sthompsa			ifp->if_oerrors++;
2822203134Sthompsa
2823203134Sthompsa			run_tx_free(pq, data, 1);
2824203134Sthompsa
2825203134Sthompsa			goto tr_setup;
2826203134Sthompsa		}
2827203134Sthompsa
2828203134Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2829203134Sthompsa		size = sizeof(data->desc);
2830203134Sthompsa		usbd_copy_in(pc, 0, &data->desc, size);
2831203134Sthompsa		usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2832228508Shselasky		size += m->m_pkthdr.len;
2833228508Shselasky		/*
2834228508Shselasky		 * Align end on a 4-byte boundary, pad 8 bytes (CRC +
2835228508Shselasky		 * 4-byte padding), and be sure to zero those trailing
2836228508Shselasky		 * bytes:
2837228508Shselasky		 */
2838228508Shselasky		usbd_frame_zero(pc, size, ((-size) & 3) + 8);
2839228508Shselasky		size += ((-size) & 3) + 8;
2840203134Sthompsa
2841203134Sthompsa		vap = data->ni->ni_vap;
2842203134Sthompsa		if (ieee80211_radiotap_active_vap(vap)) {
2843203134Sthompsa			struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2844208019Sthompsa			struct rt2860_txwi *txwi =
2845208019Sthompsa			    (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2846203134Sthompsa
2847203134Sthompsa			tap->wt_flags = 0;
2848203134Sthompsa			tap->wt_rate = rt2860_rates[data->ridx].rate;
2849236439Shselasky			tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2850236439Shselasky			tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2851203134Sthompsa			tap->wt_hwqueue = index;
2852208019Sthompsa			if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2853203134Sthompsa				tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2854203134Sthompsa
2855203134Sthompsa			ieee80211_radiotap_tx(vap, m);
2856203134Sthompsa		}
2857203134Sthompsa
2858228508Shselasky		DPRINTFN(11, "sending frame len=%u/%u  @ index %d\n",
2859228508Shselasky		    m->m_pkthdr.len, size, index);
2860203134Sthompsa
2861228508Shselasky		usbd_xfer_set_frame_len(xfer, 0, size);
2862203134Sthompsa		usbd_xfer_set_priv(xfer, data);
2863203134Sthompsa
2864203134Sthompsa		usbd_transfer_submit(xfer);
2865203134Sthompsa
2866203134Sthompsa		RUN_UNLOCK(sc);
2867203134Sthompsa		run_start(ifp);
2868203134Sthompsa		RUN_LOCK(sc);
2869203134Sthompsa
2870203134Sthompsa		break;
2871203134Sthompsa
2872203134Sthompsa	default:
2873203134Sthompsa		DPRINTF("USB transfer error, %s\n",
2874203134Sthompsa		    usbd_errstr(error));
2875203134Sthompsa
2876203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2877203134Sthompsa
2878203134Sthompsa		ifp->if_oerrors++;
2879203134Sthompsa
2880203134Sthompsa		if (data != NULL) {
2881208019Sthompsa			if(data->ni != NULL)
2882208019Sthompsa				vap = data->ni->ni_vap;
2883203134Sthompsa			run_tx_free(pq, data, error);
2884203134Sthompsa			usbd_xfer_set_priv(xfer, NULL);
2885203134Sthompsa		}
2886209917Sthompsa		if (vap == NULL)
2887208019Sthompsa			vap = TAILQ_FIRST(&ic->ic_vaps);
2888203134Sthompsa
2889203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2890203134Sthompsa			if (error == USB_ERR_TIMEOUT) {
2891203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2892208019Sthompsa				uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2893208019Sthompsa				DPRINTF("cmdq_store=%d\n", i);
2894208019Sthompsa				sc->cmdq[i].func = run_usb_timeout_cb;
2895208019Sthompsa				sc->cmdq[i].arg0 = vap;
2896208019Sthompsa				ieee80211_runtask(ic, &sc->cmdq_task);
2897203134Sthompsa			}
2898203134Sthompsa
2899203134Sthompsa			/*
2900203134Sthompsa			 * Try to clear stall first, also if other
2901203134Sthompsa			 * errors occur, hence clearing stall
2902203134Sthompsa			 * introduces a 50 ms delay:
2903203134Sthompsa			 */
2904203134Sthompsa			usbd_xfer_set_stall(xfer);
2905203134Sthompsa			goto tr_setup;
2906203134Sthompsa		}
2907203134Sthompsa		break;
2908203134Sthompsa	}
2909203134Sthompsa}
2910203134Sthompsa
2911203134Sthompsastatic void
2912203134Sthompsarun_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2913203134Sthompsa{
2914203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 0);
2915203134Sthompsa}
2916203134Sthompsa
2917203134Sthompsastatic void
2918203134Sthompsarun_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2919203134Sthompsa{
2920203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 1);
2921203134Sthompsa}
2922203134Sthompsa
2923203134Sthompsastatic void
2924203134Sthompsarun_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2925203134Sthompsa{
2926203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 2);
2927203134Sthompsa}
2928203134Sthompsa
2929203134Sthompsastatic void
2930203134Sthompsarun_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2931203134Sthompsa{
2932203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 3);
2933203134Sthompsa}
2934203134Sthompsa
2935203134Sthompsastatic void
2936203134Sthompsarun_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2937203134Sthompsa{
2938203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 4);
2939203134Sthompsa}
2940203134Sthompsa
2941203134Sthompsastatic void
2942203134Sthompsarun_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2943203134Sthompsa{
2944203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 5);
2945203134Sthompsa}
2946203134Sthompsa
2947203134Sthompsastatic void
2948208019Sthompsarun_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2949203134Sthompsa{
2950203134Sthompsa	struct mbuf *m = data->m;
2951203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2952208019Sthompsa	struct ieee80211vap *vap = data->ni->ni_vap;
2953203134Sthompsa	struct ieee80211_frame *wh;
2954203134Sthompsa	struct rt2870_txd *txd;
2955203134Sthompsa	struct rt2860_txwi *txwi;
2956208019Sthompsa	uint16_t xferlen;
2957208019Sthompsa	uint16_t mcs;
2958203134Sthompsa	uint8_t ridx = data->ridx;
2959208019Sthompsa	uint8_t pad;
2960203134Sthompsa
2961203134Sthompsa	/* get MCS code from rate index */
2962208019Sthompsa	mcs = rt2860_rates[ridx].mcs;
2963203134Sthompsa
2964203134Sthompsa	xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2965203134Sthompsa
2966203134Sthompsa	/* roundup to 32-bit alignment */
2967203134Sthompsa	xferlen = (xferlen + 3) & ~3;
2968203134Sthompsa
2969203134Sthompsa	txd = (struct rt2870_txd *)&data->desc;
2970203134Sthompsa	txd->len = htole16(xferlen);
2971203134Sthompsa
2972208019Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2973208019Sthompsa
2974208019Sthompsa	/*
2975208019Sthompsa	 * Ether both are true or both are false, the header
2976208019Sthompsa	 * are nicely aligned to 32-bit. So, no L2 padding.
2977208019Sthompsa	 */
2978208019Sthompsa	if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2979208019Sthompsa		pad = 0;
2980208019Sthompsa	else
2981208019Sthompsa		pad = 2;
2982208019Sthompsa
2983203134Sthompsa	/* setup TX Wireless Information */
2984203134Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
2985203134Sthompsa	txwi->len = htole16(m->m_pkthdr.len - pad);
2986203134Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2987203134Sthompsa		txwi->phy = htole16(RT2860_PHY_CCK);
2988203134Sthompsa		if (ridx != RT2860_RIDX_CCK1 &&
2989203134Sthompsa		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2990203134Sthompsa			mcs |= RT2860_PHY_SHPRE;
2991203134Sthompsa	} else
2992203134Sthompsa		txwi->phy = htole16(RT2860_PHY_OFDM);
2993203134Sthompsa	txwi->phy |= htole16(mcs);
2994203134Sthompsa
2995203134Sthompsa	/* check if RTS/CTS or CTS-to-self protection is required */
2996203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2997203134Sthompsa	    (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2998203134Sthompsa	     ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2999203134Sthompsa	      rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
3000208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_HT;
3001203134Sthompsa	else
3002208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_BACKOFF;
3003209144Sthompsa
3004209917Sthompsa	if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
3005209144Sthompsa		txwi->xflags |= RT2860_TX_NSEQ;
3006203134Sthompsa}
3007203134Sthompsa
3008203134Sthompsa/* This function must be called locked */
3009203134Sthompsastatic int
3010203134Sthompsarun_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3011203134Sthompsa{
3012203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3013208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
3014203134Sthompsa	struct ieee80211_frame *wh;
3015208019Sthompsa	struct ieee80211_channel *chan;
3016203134Sthompsa	const struct ieee80211_txparam *tp;
3017208019Sthompsa	struct run_node *rn = (void *)ni;
3018203134Sthompsa	struct run_tx_data *data;
3019208019Sthompsa	struct rt2870_txd *txd;
3020208019Sthompsa	struct rt2860_txwi *txwi;
3021203134Sthompsa	uint16_t qos;
3022203134Sthompsa	uint16_t dur;
3023208019Sthompsa	uint16_t qid;
3024203134Sthompsa	uint8_t type;
3025203134Sthompsa	uint8_t tid;
3026208019Sthompsa	uint8_t ridx;
3027208019Sthompsa	uint8_t ctl_ridx;
3028203134Sthompsa	uint8_t qflags;
3029203134Sthompsa	uint8_t xflags = 0;
3030203134Sthompsa	int hasqos;
3031203134Sthompsa
3032203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3033203134Sthompsa
3034203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3035203134Sthompsa
3036203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3037203134Sthompsa
3038203134Sthompsa	/*
3039203134Sthompsa	 * There are 7 bulk endpoints: 1 for RX
3040203134Sthompsa	 * and 6 for TX (4 EDCAs + HCCA + Prio).
3041203134Sthompsa	 * Update 03-14-2009:  some devices like the Planex GW-US300MiniS
3042203134Sthompsa	 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
3043203134Sthompsa	 */
3044203134Sthompsa	if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
3045203134Sthompsa		uint8_t *frm;
3046203134Sthompsa
3047203134Sthompsa		if(IEEE80211_HAS_ADDR4(wh))
3048203134Sthompsa			frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
3049203134Sthompsa		else
3050203134Sthompsa			frm =((struct ieee80211_qosframe *)wh)->i_qos;
3051203134Sthompsa
3052203134Sthompsa		qos = le16toh(*(const uint16_t *)frm);
3053203134Sthompsa		tid = qos & IEEE80211_QOS_TID;
3054203134Sthompsa		qid = TID_TO_WME_AC(tid);
3055203134Sthompsa	} else {
3056203134Sthompsa		qos = 0;
3057203134Sthompsa		tid = 0;
3058203134Sthompsa		qid = WME_AC_BE;
3059203134Sthompsa	}
3060203134Sthompsa	qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
3061203134Sthompsa
3062203134Sthompsa	DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
3063203134Sthompsa	    qos, qid, tid, qflags);
3064203134Sthompsa
3065208019Sthompsa	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
3066208019Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
3067203134Sthompsa
3068203134Sthompsa	/* pickup a rate index */
3069203134Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
3070203134Sthompsa	    type != IEEE80211_FC0_TYPE_DATA) {
3071203134Sthompsa		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3072203134Sthompsa		    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3073203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3074203134Sthompsa	} else {
3075208019Sthompsa		if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3076208019Sthompsa			ridx = rn->fix_ridx;
3077208019Sthompsa		else
3078208019Sthompsa			ridx = rn->amrr_ridx;
3079203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3080203134Sthompsa	}
3081203134Sthompsa
3082203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3083203134Sthompsa	    (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3084203134Sthompsa	     IEEE80211_QOS_ACKPOLICY_NOACK)) {
3085209144Sthompsa		xflags |= RT2860_TX_ACK;
3086203134Sthompsa		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3087208019Sthompsa			dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3088203134Sthompsa		else
3089208019Sthompsa			dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3090258919Shselasky		USETW(wh->i_dur, dur);
3091203134Sthompsa	}
3092203134Sthompsa
3093203134Sthompsa	/* reserve slots for mgmt packets, just in case */
3094203134Sthompsa	if (sc->sc_epq[qid].tx_nfree < 3) {
3095203134Sthompsa		DPRINTFN(10, "tx ring %d is full\n", qid);
3096203134Sthompsa		return (-1);
3097203134Sthompsa	}
3098203134Sthompsa
3099203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3100203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3101203134Sthompsa	sc->sc_epq[qid].tx_nfree--;
3102203134Sthompsa
3103208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3104208019Sthompsa	txd->flags = qflags;
3105208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3106208019Sthompsa	txwi->xflags = xflags;
3107245047Shselasky	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3108245047Shselasky		txwi->wcid = 0;
3109245047Shselasky	} else {
3110245047Shselasky		txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
3111245047Shselasky		    1 : RUN_AID2WCID(ni->ni_associd);
3112245047Shselasky	}
3113208019Sthompsa	/* clear leftover garbage bits */
3114208019Sthompsa	txwi->flags = 0;
3115208019Sthompsa	txwi->txop = 0;
3116208019Sthompsa
3117203134Sthompsa	data->m = m;
3118203134Sthompsa	data->ni = ni;
3119203134Sthompsa	data->ridx = ridx;
3120203134Sthompsa
3121208019Sthompsa	run_set_tx_desc(sc, data);
3122203134Sthompsa
3123208019Sthompsa	/*
3124208019Sthompsa	 * The chip keeps track of 2 kind of Tx stats,
3125208019Sthompsa	 *  * TX_STAT_FIFO, for per WCID stats, and
3126208019Sthompsa	 *  * TX_STA_CNT0 for all-TX-in-one stats.
3127208019Sthompsa	 *
3128208019Sthompsa	 * To use FIFO stats, we need to store MCS into the driver-private
3129208019Sthompsa 	 * PacketID field. So that, we can tell whose stats when we read them.
3130208019Sthompsa 	 * We add 1 to the MCS because setting the PacketID field to 0 means
3131208019Sthompsa 	 * that we don't want feedback in TX_STAT_FIFO.
3132208019Sthompsa 	 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3133208019Sthompsa 	 *
3134208019Sthompsa 	 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3135208019Sthompsa 	 */
3136209917Sthompsa	if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3137209917Sthompsa	    vap->iv_opmode == IEEE80211_M_MBSS) {
3138208019Sthompsa		uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3139208019Sthompsa		txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3140208019Sthompsa
3141208019Sthompsa		/*
3142208019Sthompsa		 * Unlike PCI based devices, we don't get any interrupt from
3143208019Sthompsa		 * USB devices, so we simulate FIFO-is-full interrupt here.
3144208019Sthompsa		 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3145208019Sthompsa		 * quickly get fulled. To prevent overflow, increment a counter on
3146208019Sthompsa		 * every FIFO stat request, so we know how many slots are left.
3147208019Sthompsa		 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3148208019Sthompsa		 * are used only in those modes.
3149208019Sthompsa		 * We just drain stats. AMRR gets updated every 1 sec by
3150208019Sthompsa		 * run_ratectl_cb() via callout.
3151208019Sthompsa		 * Call it early. Otherwise overflow.
3152208019Sthompsa		 */
3153209917Sthompsa		if (sc->fifo_cnt++ == 10) {
3154208019Sthompsa			/*
3155208019Sthompsa			 * With multiple vaps or if_bridge, if_start() is called
3156208019Sthompsa			 * with a non-sleepable lock, tcpinp. So, need to defer.
3157208019Sthompsa			 */
3158208019Sthompsa			uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3159208019Sthompsa			DPRINTFN(6, "cmdq_store=%d\n", i);
3160208019Sthompsa			sc->cmdq[i].func = run_drain_fifo;
3161208019Sthompsa			sc->cmdq[i].arg0 = sc;
3162208019Sthompsa			ieee80211_runtask(ic, &sc->cmdq_task);
3163208019Sthompsa		}
3164208019Sthompsa	}
3165208019Sthompsa
3166203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3167203134Sthompsa
3168203134Sthompsa	usbd_transfer_start(sc->sc_xfer[qid]);
3169203134Sthompsa
3170258840Skevlo	DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n",
3171258840Skevlo	    m->m_pkthdr.len +
3172258840Skevlo	    (int)(sizeof(struct rt2870_txd) + sizeof(struct rt2860_txwi)),
3173203134Sthompsa	    rt2860_rates[ridx].rate, qid);
3174203134Sthompsa
3175203134Sthompsa	return (0);
3176203134Sthompsa}
3177203134Sthompsa
3178203134Sthompsastatic int
3179203134Sthompsarun_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3180203134Sthompsa{
3181203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3182203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3183208019Sthompsa	struct run_node *rn = (void *)ni;
3184203134Sthompsa	struct run_tx_data *data;
3185203134Sthompsa	struct ieee80211_frame *wh;
3186208019Sthompsa	struct rt2870_txd *txd;
3187208019Sthompsa	struct rt2860_txwi *txwi;
3188203134Sthompsa	uint16_t dur;
3189208019Sthompsa	uint8_t ridx = rn->mgt_ridx;
3190203134Sthompsa	uint8_t type;
3191203134Sthompsa	uint8_t xflags = 0;
3192208019Sthompsa	uint8_t wflags = 0;
3193203134Sthompsa
3194203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3195203134Sthompsa
3196203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3197203134Sthompsa
3198203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3199203134Sthompsa
3200208019Sthompsa	/* tell hardware to add timestamp for probe responses */
3201208019Sthompsa	if ((wh->i_fc[0] &
3202208019Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3203208019Sthompsa	    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3204208019Sthompsa		wflags |= RT2860_TX_TS;
3205208019Sthompsa	else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3206203134Sthompsa		xflags |= RT2860_TX_ACK;
3207203134Sthompsa
3208208019Sthompsa		dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3209203134Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3210258919Shselasky		USETW(wh->i_dur, dur);
3211203134Sthompsa	}
3212203134Sthompsa
3213203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3214203134Sthompsa		/* let caller free mbuf */
3215203134Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3216203134Sthompsa		return (EIO);
3217203134Sthompsa	}
3218203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3219203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3220203134Sthompsa	sc->sc_epq[0].tx_nfree--;
3221203134Sthompsa
3222208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3223208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3224208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3225208019Sthompsa	txwi->wcid = 0xff;
3226208019Sthompsa	txwi->flags = wflags;
3227208019Sthompsa	txwi->xflags = xflags;
3228208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3229208019Sthompsa
3230203134Sthompsa	data->m = m;
3231203134Sthompsa	data->ni = ni;
3232203134Sthompsa	data->ridx = ridx;
3233203134Sthompsa
3234208019Sthompsa	run_set_tx_desc(sc, data);
3235203134Sthompsa
3236203134Sthompsa	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3237258840Skevlo	    (int)(sizeof(struct rt2870_txd) + sizeof(struct rt2860_txwi)),
3238208019Sthompsa	    rt2860_rates[ridx].rate);
3239203134Sthompsa
3240203134Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3241203134Sthompsa
3242203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3243203134Sthompsa
3244203134Sthompsa	return (0);
3245203134Sthompsa}
3246203134Sthompsa
3247203134Sthompsastatic int
3248203134Sthompsarun_sendprot(struct run_softc *sc,
3249203134Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3250203134Sthompsa{
3251203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3252203134Sthompsa	struct ieee80211_frame *wh;
3253203134Sthompsa	struct run_tx_data *data;
3254208019Sthompsa	struct rt2870_txd *txd;
3255208019Sthompsa	struct rt2860_txwi *txwi;
3256203134Sthompsa	struct mbuf *mprot;
3257203134Sthompsa	int ridx;
3258203134Sthompsa	int protrate;
3259203134Sthompsa	int ackrate;
3260203134Sthompsa	int pktlen;
3261203134Sthompsa	int isshort;
3262203134Sthompsa	uint16_t dur;
3263203134Sthompsa	uint8_t type;
3264208019Sthompsa	uint8_t wflags = 0;
3265208019Sthompsa	uint8_t xflags = 0;
3266203134Sthompsa
3267203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3268203134Sthompsa
3269203134Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3270203134Sthompsa	    ("protection %d", prot));
3271203134Sthompsa
3272203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3273203134Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3274203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3275203134Sthompsa
3276203134Sthompsa	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3277203134Sthompsa	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3278203134Sthompsa
3279203134Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3280209189Sjkim	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3281203134Sthompsa	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3282203134Sthompsa	wflags = RT2860_TX_FRAG;
3283203134Sthompsa
3284203134Sthompsa	/* check that there are free slots before allocating the mbuf */
3285203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3286203134Sthompsa		/* let caller free mbuf */
3287203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3288203134Sthompsa		return (ENOBUFS);
3289203134Sthompsa	}
3290203134Sthompsa
3291203134Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
3292203134Sthompsa		/* NB: CTS is the same size as an ACK */
3293203134Sthompsa		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3294208019Sthompsa		xflags |= RT2860_TX_ACK;
3295203134Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3296203134Sthompsa	} else {
3297203134Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3298203134Sthompsa	}
3299203134Sthompsa	if (mprot == NULL) {
3300203134Sthompsa		sc->sc_ifp->if_oerrors++;
3301203134Sthompsa		DPRINTF("could not allocate mbuf\n");
3302203134Sthompsa		return (ENOBUFS);
3303203134Sthompsa	}
3304203134Sthompsa
3305203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3306203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3307203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3308203134Sthompsa
3309208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3310208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3311208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3312208019Sthompsa	txwi->wcid = 0xff;
3313208019Sthompsa	txwi->flags = wflags;
3314208019Sthompsa	txwi->xflags = xflags;
3315208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3316208019Sthompsa
3317203134Sthompsa	data->m = mprot;
3318203134Sthompsa	data->ni = ieee80211_ref_node(ni);
3319203134Sthompsa
3320203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3321203134Sthompsa		if (rt2860_rates[ridx].rate == protrate)
3322203134Sthompsa			break;
3323203134Sthompsa	data->ridx = ridx;
3324203134Sthompsa
3325208019Sthompsa	run_set_tx_desc(sc, data);
3326203134Sthompsa
3327203134Sthompsa        DPRINTFN(1, "sending prot len=%u rate=%u\n",
3328203134Sthompsa            m->m_pkthdr.len, rate);
3329203134Sthompsa
3330203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3331203134Sthompsa
3332203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3333203134Sthompsa
3334203134Sthompsa	return (0);
3335203134Sthompsa}
3336203134Sthompsa
3337203134Sthompsastatic int
3338203134Sthompsarun_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3339203134Sthompsa    const struct ieee80211_bpf_params *params)
3340203134Sthompsa{
3341203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3342203134Sthompsa	struct ieee80211_frame *wh;
3343203134Sthompsa	struct run_tx_data *data;
3344208019Sthompsa	struct rt2870_txd *txd;
3345208019Sthompsa	struct rt2860_txwi *txwi;
3346203134Sthompsa	uint8_t type;
3347208019Sthompsa	uint8_t ridx;
3348208019Sthompsa	uint8_t rate;
3349208019Sthompsa	uint8_t opflags = 0;
3350208019Sthompsa	uint8_t xflags = 0;
3351203134Sthompsa	int error;
3352203134Sthompsa
3353203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3354203134Sthompsa
3355203134Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
3356203134Sthompsa
3357203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3358203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3359203134Sthompsa
3360203134Sthompsa	rate = params->ibp_rate0;
3361203134Sthompsa	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3362203134Sthompsa		/* let caller free mbuf */
3363203134Sthompsa		return (EINVAL);
3364203134Sthompsa	}
3365203134Sthompsa
3366203134Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3367208019Sthompsa		xflags |= RT2860_TX_ACK;
3368203134Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3369203134Sthompsa		error = run_sendprot(sc, m, ni,
3370203134Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
3371203134Sthompsa			IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3372203134Sthompsa		    rate);
3373203134Sthompsa		if (error) {
3374203134Sthompsa			/* let caller free mbuf */
3375209917Sthompsa			return error;
3376203134Sthompsa		}
3377203134Sthompsa		opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3378203134Sthompsa	}
3379203134Sthompsa
3380203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3381203134Sthompsa		/* let caller free mbuf */
3382203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3383203134Sthompsa		DPRINTF("sending raw frame, but tx ring is full\n");
3384203134Sthompsa		return (EIO);
3385203134Sthompsa	}
3386203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3387203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3388203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3389203134Sthompsa
3390208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3391208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3392208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3393208019Sthompsa	txwi->wcid = 0xff;
3394208019Sthompsa	txwi->xflags = xflags;
3395208019Sthompsa	txwi->txop = opflags;
3396208019Sthompsa	txwi->flags = 0;	/* clear leftover garbage bits */
3397208019Sthompsa
3398203134Sthompsa        data->m = m;
3399203134Sthompsa        data->ni = ni;
3400203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3401203134Sthompsa		if (rt2860_rates[ridx].rate == rate)
3402203134Sthompsa			break;
3403203134Sthompsa	data->ridx = ridx;
3404203134Sthompsa
3405208019Sthompsa        run_set_tx_desc(sc, data);
3406203134Sthompsa
3407203134Sthompsa        DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3408203134Sthompsa            m->m_pkthdr.len, rate);
3409203134Sthompsa
3410203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3411203134Sthompsa
3412203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3413203134Sthompsa
3414209917Sthompsa        return (0);
3415203134Sthompsa}
3416203134Sthompsa
3417203134Sthompsastatic int
3418203134Sthompsarun_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3419203134Sthompsa    const struct ieee80211_bpf_params *params)
3420203134Sthompsa{
3421203134Sthompsa	struct ifnet *ifp = ni->ni_ic->ic_ifp;
3422203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3423208019Sthompsa	int error = 0;
3424208019Sthompsa
3425203134Sthompsa	RUN_LOCK(sc);
3426203134Sthompsa
3427203134Sthompsa	/* prevent management frames from being sent if we're not ready */
3428203134Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3429203134Sthompsa		error =  ENETDOWN;
3430208019Sthompsa		goto done;
3431203134Sthompsa	}
3432203134Sthompsa
3433203134Sthompsa	if (params == NULL) {
3434203134Sthompsa		/* tx mgt packet */
3435209917Sthompsa		if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3436203134Sthompsa			ifp->if_oerrors++;
3437203134Sthompsa			DPRINTF("mgt tx failed\n");
3438208019Sthompsa			goto done;
3439203134Sthompsa		}
3440203134Sthompsa	} else {
3441203134Sthompsa		/* tx raw packet with param */
3442209917Sthompsa		if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3443203134Sthompsa			ifp->if_oerrors++;
3444203134Sthompsa			DPRINTF("tx with param failed\n");
3445208019Sthompsa			goto done;
3446203134Sthompsa		}
3447203134Sthompsa	}
3448203134Sthompsa
3449203134Sthompsa	ifp->if_opackets++;
3450203134Sthompsa
3451208019Sthompsadone:
3452203134Sthompsa	RUN_UNLOCK(sc);
3453203134Sthompsa
3454209917Sthompsa	if (error != 0) {
3455208019Sthompsa		if(m != NULL)
3456208019Sthompsa			m_freem(m);
3457208019Sthompsa		ieee80211_free_node(ni);
3458208019Sthompsa	}
3459203134Sthompsa
3460203134Sthompsa	return (error);
3461203134Sthompsa}
3462203134Sthompsa
3463203134Sthompsastatic void
3464203134Sthompsarun_start(struct ifnet *ifp)
3465203134Sthompsa{
3466203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3467203134Sthompsa	struct ieee80211_node *ni;
3468203134Sthompsa	struct mbuf *m;
3469203134Sthompsa
3470203134Sthompsa	RUN_LOCK(sc);
3471203134Sthompsa
3472203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3473203134Sthompsa		RUN_UNLOCK(sc);
3474203134Sthompsa		return;
3475203134Sthompsa	}
3476203134Sthompsa
3477203134Sthompsa	for (;;) {
3478203134Sthompsa		/* send data frames */
3479203134Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3480203134Sthompsa		if (m == NULL)
3481203134Sthompsa			break;
3482203134Sthompsa
3483203134Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3484203134Sthompsa		if (run_tx(sc, m, ni) != 0) {
3485203134Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
3486203134Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3487203134Sthompsa			break;
3488203134Sthompsa		}
3489203134Sthompsa	}
3490203134Sthompsa
3491203134Sthompsa	RUN_UNLOCK(sc);
3492203134Sthompsa}
3493203134Sthompsa
3494203134Sthompsastatic int
3495203134Sthompsarun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3496203134Sthompsa{
3497203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3498203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3499203134Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
3500208019Sthompsa	int startall = 0;
3501246614Shselasky	int error;
3502203134Sthompsa
3503246614Shselasky	RUN_LOCK(sc);
3504246614Shselasky	error = sc->sc_detached ? ENXIO : 0;
3505246614Shselasky	RUN_UNLOCK(sc);
3506246614Shselasky	if (error)
3507246614Shselasky		return (error);
3508246614Shselasky
3509203134Sthompsa	switch (cmd) {
3510203134Sthompsa	case SIOCSIFFLAGS:
3511203134Sthompsa		RUN_LOCK(sc);
3512203134Sthompsa		if (ifp->if_flags & IFF_UP) {
3513203134Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3514208019Sthompsa				startall = 1;
3515203134Sthompsa				run_init_locked(sc);
3516203134Sthompsa			} else
3517203134Sthompsa				run_update_promisc_locked(ifp);
3518203134Sthompsa		} else {
3519209917Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3520209917Sthompsa			    (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3521208019Sthompsa					run_stop(sc);
3522208019Sthompsa			}
3523203134Sthompsa		}
3524203134Sthompsa		RUN_UNLOCK(sc);
3525209917Sthompsa		if (startall)
3526208019Sthompsa			ieee80211_start_all(ic);
3527203134Sthompsa		break;
3528203134Sthompsa	case SIOCGIFMEDIA:
3529203134Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3530203134Sthompsa		break;
3531203134Sthompsa	case SIOCGIFADDR:
3532203134Sthompsa		error = ether_ioctl(ifp, cmd, data);
3533203134Sthompsa		break;
3534203134Sthompsa	default:
3535203134Sthompsa		error = EINVAL;
3536203134Sthompsa		break;
3537203134Sthompsa	}
3538203134Sthompsa
3539203134Sthompsa	return (error);
3540203134Sthompsa}
3541203134Sthompsa
3542203134Sthompsastatic void
3543205042Sthompsarun_set_agc(struct run_softc *sc, uint8_t agc)
3544205042Sthompsa{
3545205042Sthompsa	uint8_t bbp;
3546205042Sthompsa
3547205042Sthompsa	if (sc->mac_ver == 0x3572) {
3548205042Sthompsa		run_bbp_read(sc, 27, &bbp);
3549205042Sthompsa		bbp &= ~(0x3 << 5);
3550205042Sthompsa		run_bbp_write(sc, 27, bbp | 0 << 5);	/* select Rx0 */
3551205042Sthompsa		run_bbp_write(sc, 66, agc);
3552205042Sthompsa		run_bbp_write(sc, 27, bbp | 1 << 5);	/* select Rx1 */
3553205042Sthompsa		run_bbp_write(sc, 66, agc);
3554205042Sthompsa	} else
3555205042Sthompsa		run_bbp_write(sc, 66, agc);
3556205042Sthompsa}
3557205042Sthompsa
3558205042Sthompsastatic void
3559203134Sthompsarun_select_chan_group(struct run_softc *sc, int group)
3560203134Sthompsa{
3561203134Sthompsa	uint32_t tmp;
3562205042Sthompsa	uint8_t agc;
3563203134Sthompsa
3564203134Sthompsa	run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3565203134Sthompsa	run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3566203134Sthompsa	run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3567258082Skevlo	if (sc->mac_ver < 0x3572)
3568257955Skevlo		run_bbp_write(sc, 86, 0x00);
3569203134Sthompsa
3570203134Sthompsa	if (group == 0) {
3571203134Sthompsa		if (sc->ext_2ghz_lna) {
3572257955Skevlo			if (sc->mac_ver >= 0x5390)
3573257955Skevlo				run_bbp_write(sc, 75, 0x52);
3574257955Skevlo			else {
3575257955Skevlo				run_bbp_write(sc, 82, 0x62);
3576257955Skevlo				run_bbp_write(sc, 75, 0x46);
3577257955Skevlo			}
3578203134Sthompsa		} else {
3579257955Skevlo			if (sc->mac_ver >= 0x5390)
3580257955Skevlo				run_bbp_write(sc, 75, 0x50);
3581257955Skevlo			else {
3582257955Skevlo				run_bbp_write(sc, 82, 0x84);
3583257955Skevlo				run_bbp_write(sc, 75, 0x50);
3584257955Skevlo			}
3585203134Sthompsa		}
3586203134Sthompsa	} else {
3587205042Sthompsa		if (sc->mac_ver == 0x3572)
3588205042Sthompsa			run_bbp_write(sc, 82, 0x94);
3589205042Sthompsa		else
3590203134Sthompsa			run_bbp_write(sc, 82, 0xf2);
3591205042Sthompsa		if (sc->ext_5ghz_lna)
3592203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3593205042Sthompsa		else
3594203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3595203134Sthompsa	}
3596203134Sthompsa
3597203134Sthompsa	run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3598203134Sthompsa	tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3599203134Sthompsa	tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3600203134Sthompsa	run_write(sc, RT2860_TX_BAND_CFG, tmp);
3601203134Sthompsa
3602203134Sthompsa	/* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3603208019Sthompsa	tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3604208019Sthompsa	if (sc->nrxchains > 1)
3605208019Sthompsa		tmp |= RT2860_LNA_PE1_EN;
3606203134Sthompsa	if (group == 0) {	/* 2GHz */
3607208019Sthompsa		tmp |= RT2860_PA_PE_G0_EN;
3608203134Sthompsa		if (sc->ntxchains > 1)
3609203134Sthompsa			tmp |= RT2860_PA_PE_G1_EN;
3610203134Sthompsa	} else {		/* 5GHz */
3611208019Sthompsa		tmp |= RT2860_PA_PE_A0_EN;
3612203134Sthompsa		if (sc->ntxchains > 1)
3613203134Sthompsa			tmp |= RT2860_PA_PE_A1_EN;
3614203134Sthompsa	}
3615205042Sthompsa	if (sc->mac_ver == 0x3572) {
3616205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x00);
3617205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3618205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x80);
3619205042Sthompsa	} else
3620205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3621203134Sthompsa
3622203134Sthompsa	/* set initial AGC value */
3623205042Sthompsa	if (group == 0) {	/* 2GHz band */
3624205042Sthompsa		if (sc->mac_ver >= 0x3070)
3625205042Sthompsa			agc = 0x1c + sc->lna[0] * 2;
3626205042Sthompsa		else
3627205042Sthompsa			agc = 0x2e + sc->lna[0];
3628205042Sthompsa	} else {		/* 5GHz band */
3629205042Sthompsa		if (sc->mac_ver == 0x3572)
3630205042Sthompsa			agc = 0x22 + (sc->lna[group] * 5) / 3;
3631205042Sthompsa		else
3632205042Sthompsa			agc = 0x32 + (sc->lna[group] * 5) / 3;
3633205042Sthompsa	}
3634205042Sthompsa	run_set_agc(sc, agc);
3635203134Sthompsa}
3636203134Sthompsa
3637203134Sthompsastatic void
3638257429Shselaskyrun_rt2870_set_chan(struct run_softc *sc, u_int chan)
3639203134Sthompsa{
3640203134Sthompsa	const struct rfprog *rfprog = rt2860_rf2850;
3641203134Sthompsa	uint32_t r2, r3, r4;
3642203134Sthompsa	int8_t txpow1, txpow2;
3643203134Sthompsa	int i;
3644203134Sthompsa
3645203134Sthompsa	/* find the settings for this channel (we know it exists) */
3646203134Sthompsa	for (i = 0; rfprog[i].chan != chan; i++);
3647203134Sthompsa
3648203134Sthompsa	r2 = rfprog[i].r2;
3649203134Sthompsa	if (sc->ntxchains == 1)
3650258732Skevlo		r2 |= 1 << 14;		/* 1T: disable Tx chain 2 */
3651203134Sthompsa	if (sc->nrxchains == 1)
3652258732Skevlo		r2 |= 1 << 17 | 1 << 6;	/* 1R: disable Rx chains 2 & 3 */
3653203134Sthompsa	else if (sc->nrxchains == 2)
3654258732Skevlo		r2 |= 1 << 6;		/* 2R: disable Rx chain 3 */
3655203134Sthompsa
3656203134Sthompsa	/* use Tx power values from EEPROM */
3657203134Sthompsa	txpow1 = sc->txpow1[i];
3658203134Sthompsa	txpow2 = sc->txpow2[i];
3659258732Skevlo
3660258732Skevlo	/* Initialize RF R3 and R4. */
3661258732Skevlo	r3 = rfprog[i].r3 & 0xffffc1ff;
3662258732Skevlo	r4 = (rfprog[i].r4 & ~(0x001f87c0)) | (sc->freq << 15);
3663203134Sthompsa	if (chan > 14) {
3664258732Skevlo		if (txpow1 >= 0) {
3665258732Skevlo			txpow1 = (txpow1 > 0xf) ? (0xf) : (txpow1);
3666258732Skevlo			r3 |= (txpow1 << 10) | (1 << 9);
3667258732Skevlo		} else {
3668258732Skevlo			txpow1 += 7;
3669258732Skevlo
3670258732Skevlo			/* txpow1 is not possible larger than 15. */
3671258732Skevlo			r3 |= (txpow1 << 10);
3672258732Skevlo		}
3673258732Skevlo		if (txpow2 >= 0) {
3674258732Skevlo			txpow2 = (txpow2 > 0xf) ? (0xf) : (txpow2);
3675258921Shselasky			r4 |= (txpow2 << 7) | (1 << 6);
3676258732Skevlo		} else {
3677258732Skevlo			txpow2 += 7;
3678258732Skevlo			r4 |= (txpow2 << 7);
3679258732Skevlo		}
3680258732Skevlo	} else {
3681258732Skevlo		/* Set Tx0 power. */
3682258732Skevlo		r3 |= (txpow1 << 9);
3683258732Skevlo
3684258732Skevlo		/* Set frequency offset and Tx1 power. */
3685258732Skevlo		r4 |= (txpow2 << 6);
3686203134Sthompsa	}
3687203134Sthompsa
3688258733Skevlo	run_rt2870_rf_write(sc, rfprog[i].r1);
3689258733Skevlo	run_rt2870_rf_write(sc, r2);
3690258733Skevlo	run_rt2870_rf_write(sc, r3 & ~(1 << 2));
3691258733Skevlo	run_rt2870_rf_write(sc, r4);
3692203134Sthompsa
3693203134Sthompsa	run_delay(sc, 10);
3694203134Sthompsa
3695258733Skevlo	run_rt2870_rf_write(sc, rfprog[i].r1);
3696258733Skevlo	run_rt2870_rf_write(sc, r2);
3697258733Skevlo	run_rt2870_rf_write(sc, r3 | (1 << 2));
3698258733Skevlo	run_rt2870_rf_write(sc, r4);
3699203134Sthompsa
3700203134Sthompsa	run_delay(sc, 10);
3701203134Sthompsa
3702258733Skevlo	run_rt2870_rf_write(sc, rfprog[i].r1);
3703258733Skevlo	run_rt2870_rf_write(sc, r2);
3704258733Skevlo	run_rt2870_rf_write(sc, r3 & ~(1 << 2));
3705258733Skevlo	run_rt2870_rf_write(sc, r4);
3706203134Sthompsa}
3707203134Sthompsa
3708203134Sthompsastatic void
3709257429Shselaskyrun_rt3070_set_chan(struct run_softc *sc, u_int chan)
3710203134Sthompsa{
3711203134Sthompsa	int8_t txpow1, txpow2;
3712203134Sthompsa	uint8_t rf;
3713205042Sthompsa	int i;
3714203134Sthompsa
3715205042Sthompsa	/* find the settings for this channel (we know it exists) */
3716205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3717205042Sthompsa
3718203134Sthompsa	/* use Tx power values from EEPROM */
3719205042Sthompsa	txpow1 = sc->txpow1[i];
3720205042Sthompsa	txpow2 = sc->txpow2[i];
3721203134Sthompsa
3722205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3723256720Skevlo
3724256720Skevlo	/* RT3370/RT3390: RF R3 [7:4] is not reserved bits. */
3725256720Skevlo	run_rt3070_rf_read(sc, 3, &rf);
3726256720Skevlo	rf = (rf & ~0x0f) | rt3070_freqs[i].k;
3727256720Skevlo	run_rt3070_rf_write(sc, 3, rf);
3728256720Skevlo
3729203134Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3730205042Sthompsa	rf = (rf & ~0x03) | rt3070_freqs[i].r;
3731203134Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3732203134Sthompsa
3733203134Sthompsa	/* set Tx0 power */
3734203134Sthompsa	run_rt3070_rf_read(sc, 12, &rf);
3735203134Sthompsa	rf = (rf & ~0x1f) | txpow1;
3736203134Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3737203134Sthompsa
3738203134Sthompsa	/* set Tx1 power */
3739203134Sthompsa	run_rt3070_rf_read(sc, 13, &rf);
3740203134Sthompsa	rf = (rf & ~0x1f) | txpow2;
3741203134Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3742203134Sthompsa
3743203134Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3744203134Sthompsa	rf &= ~0xfc;
3745203134Sthompsa	if (sc->ntxchains == 1)
3746203134Sthompsa		rf |= 1 << 7 | 1 << 5;	/* 1T: disable Tx chains 2 & 3 */
3747203134Sthompsa	else if (sc->ntxchains == 2)
3748203134Sthompsa		rf |= 1 << 7;		/* 2T: disable Tx chain 3 */
3749203134Sthompsa	if (sc->nrxchains == 1)
3750203134Sthompsa		rf |= 1 << 6 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3751203134Sthompsa	else if (sc->nrxchains == 2)
3752203134Sthompsa		rf |= 1 << 6;		/* 2R: disable Rx chain 3 */
3753203134Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3754203134Sthompsa
3755203134Sthompsa	/* set RF offset */
3756203134Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3757203134Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3758203134Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3759203134Sthompsa
3760203134Sthompsa	/* program RF filter */
3761205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf);	/* Tx */
3762205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3763205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);
3764205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);	/* Rx */
3765205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3766205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);
3767203134Sthompsa
3768203134Sthompsa	/* enable RF tuning */
3769203134Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3770203134Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3771203134Sthompsa}
3772203134Sthompsa
3773203134Sthompsastatic void
3774205042Sthompsarun_rt3572_set_chan(struct run_softc *sc, u_int chan)
3775205042Sthompsa{
3776205042Sthompsa	int8_t txpow1, txpow2;
3777205042Sthompsa	uint32_t tmp;
3778205042Sthompsa	uint8_t rf;
3779205042Sthompsa	int i;
3780205042Sthompsa
3781205042Sthompsa	/* find the settings for this channel (we know it exists) */
3782205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3783205042Sthompsa
3784205042Sthompsa	/* use Tx power values from EEPROM */
3785205042Sthompsa	txpow1 = sc->txpow1[i];
3786205042Sthompsa	txpow2 = sc->txpow2[i];
3787205042Sthompsa
3788205042Sthompsa	if (chan <= 14) {
3789205042Sthompsa		run_bbp_write(sc, 25, sc->bbp25);
3790205042Sthompsa		run_bbp_write(sc, 26, sc->bbp26);
3791205042Sthompsa	} else {
3792205042Sthompsa		/* enable IQ phase correction */
3793205042Sthompsa		run_bbp_write(sc, 25, 0x09);
3794205042Sthompsa		run_bbp_write(sc, 26, 0xff);
3795205042Sthompsa	}
3796205042Sthompsa
3797205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3798205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3799205042Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3800205042Sthompsa	rf  = (rf & ~0x0f) | rt3070_freqs[i].r;
3801205042Sthompsa	rf |= (chan <= 14) ? 0x08 : 0x04;
3802205042Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3803205042Sthompsa
3804205042Sthompsa	/* set PLL mode */
3805205042Sthompsa	run_rt3070_rf_read(sc, 5, &rf);
3806205042Sthompsa	rf &= ~(0x08 | 0x04);
3807205042Sthompsa	rf |= (chan <= 14) ? 0x04 : 0x08;
3808205042Sthompsa	run_rt3070_rf_write(sc, 5, rf);
3809205042Sthompsa
3810205042Sthompsa	/* set Tx power for chain 0 */
3811205042Sthompsa	if (chan <= 14)
3812205042Sthompsa		rf = 0x60 | txpow1;
3813205042Sthompsa	else
3814205042Sthompsa		rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3815205042Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3816205042Sthompsa
3817205042Sthompsa	/* set Tx power for chain 1 */
3818205042Sthompsa	if (chan <= 14)
3819205042Sthompsa		rf = 0x60 | txpow2;
3820205042Sthompsa	else
3821205042Sthompsa		rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3822205042Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3823205042Sthompsa
3824205042Sthompsa	/* set Tx/Rx streams */
3825205042Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3826205042Sthompsa	rf &= ~0xfc;
3827205042Sthompsa	if (sc->ntxchains == 1)
3828205042Sthompsa		rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3829205042Sthompsa	else if (sc->ntxchains == 2)
3830205042Sthompsa		rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3831205042Sthompsa	if (sc->nrxchains == 1)
3832205042Sthompsa		rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3833205042Sthompsa	else if (sc->nrxchains == 2)
3834205042Sthompsa		rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3835205042Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3836205042Sthompsa
3837205042Sthompsa	/* set RF offset */
3838205042Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3839205042Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3840205042Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3841205042Sthompsa
3842205042Sthompsa	/* program RF filter */
3843205042Sthompsa	rf = sc->rf24_20mhz;
3844205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);	/* Tx */
3845205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);	/* Rx */
3846205042Sthompsa
3847205042Sthompsa	/* enable RF tuning */
3848205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3849205042Sthompsa	rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3850205042Sthompsa	run_rt3070_rf_write(sc, 7, rf);
3851205042Sthompsa
3852205042Sthompsa	/* TSSI */
3853205042Sthompsa	rf = (chan <= 14) ? 0xc3 : 0xc0;
3854205042Sthompsa	run_rt3070_rf_write(sc, 9, rf);
3855205042Sthompsa
3856205042Sthompsa	/* set loop filter 1 */
3857205042Sthompsa	run_rt3070_rf_write(sc, 10, 0xf1);
3858205042Sthompsa	/* set loop filter 2 */
3859205042Sthompsa	run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3860205042Sthompsa
3861205042Sthompsa	/* set tx_mx2_ic */
3862205042Sthompsa	run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3863205042Sthompsa	/* set tx_mx1_ic */
3864205042Sthompsa	if (chan <= 14)
3865205042Sthompsa		rf = 0x48 | sc->txmixgain_2ghz;
3866205042Sthompsa	else
3867205042Sthompsa		rf = 0x78 | sc->txmixgain_5ghz;
3868205042Sthompsa	run_rt3070_rf_write(sc, 16, rf);
3869205042Sthompsa
3870205042Sthompsa	/* set tx_lo1 */
3871205042Sthompsa	run_rt3070_rf_write(sc, 17, 0x23);
3872205042Sthompsa	/* set tx_lo2 */
3873205042Sthompsa	if (chan <= 14)
3874205042Sthompsa		rf = 0x93;
3875205042Sthompsa	else if (chan <= 64)
3876205042Sthompsa		rf = 0xb7;
3877205042Sthompsa	else if (chan <= 128)
3878205042Sthompsa		rf = 0x74;
3879205042Sthompsa	else
3880205042Sthompsa		rf = 0x72;
3881205042Sthompsa	run_rt3070_rf_write(sc, 19, rf);
3882205042Sthompsa
3883205042Sthompsa	/* set rx_lo1 */
3884205042Sthompsa	if (chan <= 14)
3885205042Sthompsa		rf = 0xb3;
3886205042Sthompsa	else if (chan <= 64)
3887205042Sthompsa		rf = 0xf6;
3888205042Sthompsa	else if (chan <= 128)
3889205042Sthompsa		rf = 0xf4;
3890205042Sthompsa	else
3891205042Sthompsa		rf = 0xf3;
3892205042Sthompsa	run_rt3070_rf_write(sc, 20, rf);
3893205042Sthompsa
3894205042Sthompsa	/* set pfd_delay */
3895205042Sthompsa	if (chan <= 14)
3896205042Sthompsa		rf = 0x15;
3897205042Sthompsa	else if (chan <= 64)
3898205042Sthompsa		rf = 0x3d;
3899205042Sthompsa	else
3900205042Sthompsa		rf = 0x01;
3901205042Sthompsa	run_rt3070_rf_write(sc, 25, rf);
3902205042Sthompsa
3903205042Sthompsa	/* set rx_lo2 */
3904205042Sthompsa	run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3905205042Sthompsa	/* set ldo_rf_vc */
3906205042Sthompsa	run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3907205042Sthompsa	/* set drv_cc */
3908205042Sthompsa	run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3909205042Sthompsa
3910205042Sthompsa	run_read(sc, RT2860_GPIO_CTRL, &tmp);
3911205042Sthompsa	tmp &= ~0x8080;
3912205042Sthompsa	if (chan <= 14)
3913205042Sthompsa		tmp |= 0x80;
3914205042Sthompsa	run_write(sc, RT2860_GPIO_CTRL, tmp);
3915205042Sthompsa
3916205042Sthompsa	/* enable RF tuning */
3917205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3918205042Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3919205042Sthompsa
3920205042Sthompsa	run_delay(sc, 2);
3921205042Sthompsa}
3922205042Sthompsa
3923205042Sthompsastatic void
3924257955Skevlorun_rt5390_set_chan(struct run_softc *sc, u_int chan)
3925257955Skevlo{
3926257955Skevlo	int8_t txpow1, txpow2;
3927257955Skevlo	uint8_t rf;
3928257955Skevlo	int i;
3929257955Skevlo
3930257955Skevlo	/* find the settings for this channel (we know it exists) */
3931257955Skevlo	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3932257955Skevlo
3933257955Skevlo	/* use Tx power values from EEPROM */
3934257955Skevlo	txpow1 = sc->txpow1[i];
3935257955Skevlo	txpow2 = sc->txpow2[i];
3936257955Skevlo
3937257955Skevlo	run_rt3070_rf_write(sc, 8, rt3070_freqs[i].n);
3938257955Skevlo	run_rt3070_rf_write(sc, 9, rt3070_freqs[i].k & 0x0f);
3939257955Skevlo	run_rt3070_rf_read(sc, 11, &rf);
3940257955Skevlo	rf = (rf & ~0x03) | (rt3070_freqs[i].r & 0x03);
3941257955Skevlo	run_rt3070_rf_write(sc, 11, rf);
3942257955Skevlo
3943257955Skevlo	run_rt3070_rf_read(sc, 49, &rf);
3944257955Skevlo	rf = (rf & ~0x3f) | (txpow1 & 0x3f);
3945257955Skevlo	/* The valid range of the RF R49 is 0x00 to 0x27. */
3946257955Skevlo	if ((rf & 0x3f) > 0x27)
3947257955Skevlo		rf = (rf & ~0x3f) | 0x27;
3948257955Skevlo	run_rt3070_rf_write(sc, 49, rf);
3949257955Skevlo
3950257955Skevlo	if (sc->mac_ver == 0x5392) {
3951257955Skevlo		run_rt3070_rf_read(sc, 50, &rf);
3952257955Skevlo		rf = (rf & ~0x3f) | (txpow2 & 0x3f);
3953257955Skevlo		/* The valid range of the RF R50 is 0x00 to 0x27. */
3954257955Skevlo		if ((rf & 0x3f) > 0x27)
3955257955Skevlo			rf = (rf & ~0x3f) | 0x27;
3956257955Skevlo		run_rt3070_rf_write(sc, 50, rf);
3957257955Skevlo	}
3958257955Skevlo
3959257955Skevlo	run_rt3070_rf_read(sc, 1, &rf);
3960257955Skevlo	rf |= RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | RT3070_TX0_PD;
3961257955Skevlo	if (sc->mac_ver == 0x5392)
3962257955Skevlo		rf |= RT3070_RX1_PD | RT3070_TX1_PD;
3963257955Skevlo	run_rt3070_rf_write(sc, 1, rf);
3964257955Skevlo
3965257955Skevlo	if (sc->mac_ver != 0x5392) {
3966257955Skevlo		run_rt3070_rf_read(sc, 2, &rf);
3967257955Skevlo		rf |= 0x80;
3968257955Skevlo		run_rt3070_rf_write(sc, 2, rf);
3969257955Skevlo		run_delay(sc, 10);
3970257955Skevlo		rf &= 0x7f;
3971257955Skevlo		run_rt3070_rf_write(sc, 2, rf);
3972257955Skevlo	}
3973257955Skevlo
3974257955Skevlo	run_adjust_freq_offset(sc);
3975257955Skevlo
3976257955Skevlo	if (sc->mac_ver == 0x5392) {
3977257955Skevlo		/* Fix for RT5392C. */
3978257955Skevlo		if (sc->mac_rev >= 0x0223) {
3979259030Skevlo			if (chan <= 4)
3980257955Skevlo				rf = 0x0f;
3981259030Skevlo			else if (chan >= 5 && chan <= 7)
3982257955Skevlo				rf = 0x0e;
3983259030Skevlo			else
3984257955Skevlo				rf = 0x0d;
3985257955Skevlo			run_rt3070_rf_write(sc, 23, rf);
3986257955Skevlo
3987259030Skevlo			if (chan <= 4)
3988257955Skevlo				rf = 0x0c;
3989257955Skevlo			else if (chan == 5)
3990257955Skevlo				rf = 0x0b;
3991259030Skevlo			else if (chan >= 6 && chan <= 7)
3992257955Skevlo				rf = 0x0a;
3993259030Skevlo			else if (chan >= 8 && chan <= 10)
3994257955Skevlo				rf = 0x09;
3995259030Skevlo			else
3996257955Skevlo				rf = 0x08;
3997257955Skevlo			run_rt3070_rf_write(sc, 59, rf);
3998257955Skevlo		} else {
3999259030Skevlo			if (chan <= 11)
4000257955Skevlo				rf = 0x0f;
4001259030Skevlo			else
4002257955Skevlo				rf = 0x0b;
4003257955Skevlo			run_rt3070_rf_write(sc, 59, rf);
4004257955Skevlo		}
4005257955Skevlo	} else {
4006257955Skevlo		/* Fix for RT5390F. */
4007257955Skevlo		if (sc->mac_rev >= 0x0502) {
4008259030Skevlo			if (chan <= 11)
4009257955Skevlo				rf = 0x43;
4010259030Skevlo			else
4011257955Skevlo				rf = 0x23;
4012257955Skevlo			run_rt3070_rf_write(sc, 55, rf);
4013257955Skevlo
4014259030Skevlo			if (chan <= 11)
4015257955Skevlo				rf = 0x0f;
4016257955Skevlo			else if (chan == 12)
4017257955Skevlo				rf = 0x0d;
4018259030Skevlo			else
4019257955Skevlo				rf = 0x0b;
4020257955Skevlo			run_rt3070_rf_write(sc, 59, rf);
4021257955Skevlo		} else {
4022257955Skevlo			run_rt3070_rf_write(sc, 55, 0x44);
4023257955Skevlo			run_rt3070_rf_write(sc, 59, 0x8f);
4024257955Skevlo		}
4025257955Skevlo	}
4026257955Skevlo
4027257955Skevlo	/* Enable VCO calibration. */
4028257955Skevlo	run_rt3070_rf_read(sc, 3, &rf);
4029257955Skevlo	rf |= RT5390_VCOCAL;
4030257955Skevlo	run_rt3070_rf_write(sc, 3, rf);
4031257955Skevlo}
4032257955Skevlo
4033257955Skevlostatic void
4034203134Sthompsarun_set_rx_antenna(struct run_softc *sc, int aux)
4035203134Sthompsa{
4036203134Sthompsa	uint32_t tmp;
4037257955Skevlo	uint8_t bbp152;
4038203134Sthompsa
4039203134Sthompsa	if (aux) {
4040257955Skevlo		if (sc->rf_rev == RT5390_RF_5370) {
4041257955Skevlo			run_bbp_read(sc, 152, &bbp152);
4042257955Skevlo			run_bbp_write(sc, 152, bbp152 & ~0x80);
4043259030Skevlo		} else {
4044257955Skevlo			run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
4045257955Skevlo			run_read(sc, RT2860_GPIO_CTRL, &tmp);
4046257955Skevlo			run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
4047257955Skevlo		}
4048203134Sthompsa	} else {
4049257955Skevlo		if (sc->rf_rev == RT5390_RF_5370) {
4050257955Skevlo			run_bbp_read(sc, 152, &bbp152);
4051257955Skevlo			run_bbp_write(sc, 152, bbp152 | 0x80);
4052259030Skevlo		} else {
4053257955Skevlo			run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
4054257955Skevlo			run_read(sc, RT2860_GPIO_CTRL, &tmp);
4055257955Skevlo			run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
4056257955Skevlo		}
4057203134Sthompsa	}
4058203134Sthompsa}
4059203134Sthompsa
4060203134Sthompsastatic int
4061203134Sthompsarun_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
4062203134Sthompsa{
4063203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4064257429Shselasky	u_int chan, group;
4065203134Sthompsa
4066203134Sthompsa	chan = ieee80211_chan2ieee(ic, c);
4067203134Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
4068209917Sthompsa		return (EINVAL);
4069203134Sthompsa
4070257955Skevlo	if (sc->mac_ver >= 0x5390)
4071257955Skevlo		run_rt5390_set_chan(sc, chan);
4072257955Skevlo	else if (sc->mac_ver == 0x3572)
4073205042Sthompsa		run_rt3572_set_chan(sc, chan);
4074205042Sthompsa	else if (sc->mac_ver >= 0x3070)
4075203134Sthompsa		run_rt3070_set_chan(sc, chan);
4076203134Sthompsa	else
4077203134Sthompsa		run_rt2870_set_chan(sc, chan);
4078203134Sthompsa
4079203134Sthompsa	/* determine channel group */
4080203134Sthompsa	if (chan <= 14)
4081203134Sthompsa		group = 0;
4082203134Sthompsa	else if (chan <= 64)
4083203134Sthompsa		group = 1;
4084203134Sthompsa	else if (chan <= 128)
4085203134Sthompsa		group = 2;
4086203134Sthompsa	else
4087203134Sthompsa		group = 3;
4088203134Sthompsa
4089203134Sthompsa	/* XXX necessary only when group has changed! */
4090203134Sthompsa	run_select_chan_group(sc, group);
4091203134Sthompsa
4092203134Sthompsa	run_delay(sc, 10);
4093203134Sthompsa
4094209917Sthompsa	return (0);
4095203134Sthompsa}
4096203134Sthompsa
4097203134Sthompsastatic void
4098203134Sthompsarun_set_channel(struct ieee80211com *ic)
4099203134Sthompsa{
4100203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4101203134Sthompsa
4102203134Sthompsa	RUN_LOCK(sc);
4103203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
4104203134Sthompsa	RUN_UNLOCK(sc);
4105203134Sthompsa
4106203134Sthompsa	return;
4107203134Sthompsa}
4108203134Sthompsa
4109203134Sthompsastatic void
4110203134Sthompsarun_scan_start(struct ieee80211com *ic)
4111203134Sthompsa{
4112203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4113203134Sthompsa	uint32_t tmp;
4114203134Sthompsa
4115203134Sthompsa	RUN_LOCK(sc);
4116203134Sthompsa
4117203134Sthompsa	/* abort TSF synchronization */
4118203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4119203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG,
4120203134Sthompsa	    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4121203134Sthompsa	    RT2860_TBTT_TIMER_EN));
4122203134Sthompsa	run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
4123203134Sthompsa
4124203134Sthompsa	RUN_UNLOCK(sc);
4125203134Sthompsa
4126203134Sthompsa	return;
4127203134Sthompsa}
4128203134Sthompsa
4129203134Sthompsastatic void
4130203134Sthompsarun_scan_end(struct ieee80211com *ic)
4131203134Sthompsa{
4132203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4133203134Sthompsa
4134203134Sthompsa	RUN_LOCK(sc);
4135203134Sthompsa
4136203134Sthompsa	run_enable_tsf_sync(sc);
4137203134Sthompsa	/* XXX keep local copy */
4138203134Sthompsa	run_set_bssid(sc, sc->sc_bssid);
4139203134Sthompsa
4140203134Sthompsa	RUN_UNLOCK(sc);
4141203134Sthompsa
4142203134Sthompsa	return;
4143203134Sthompsa}
4144203134Sthompsa
4145208019Sthompsa/*
4146208019Sthompsa * Could be called from ieee80211_node_timeout()
4147208019Sthompsa * (non-sleepable thread)
4148208019Sthompsa */
4149208019Sthompsastatic void
4150208019Sthompsarun_update_beacon(struct ieee80211vap *vap, int item)
4151203134Sthompsa{
4152208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
4153208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4154218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
4155218492Sbschmidt	int mcast = 0;
4156208019Sthompsa	uint32_t i;
4157208019Sthompsa
4158218492Sbschmidt	KASSERT(vap != NULL, ("no beacon"));
4159218492Sbschmidt
4160218492Sbschmidt	switch (item) {
4161218492Sbschmidt	case IEEE80211_BEACON_ERP:
4162218492Sbschmidt		run_updateslot(ic->ic_ifp);
4163218492Sbschmidt		break;
4164218492Sbschmidt	case IEEE80211_BEACON_HTINFO:
4165218492Sbschmidt		run_updateprot(ic);
4166218492Sbschmidt		break;
4167218492Sbschmidt	case IEEE80211_BEACON_TIM:
4168218492Sbschmidt		mcast = 1;	/*TODO*/
4169218492Sbschmidt		break;
4170218492Sbschmidt	default:
4171218492Sbschmidt		break;
4172218492Sbschmidt	}
4173218492Sbschmidt
4174218492Sbschmidt	setbit(rvp->bo.bo_flags, item);
4175218492Sbschmidt	ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
4176218492Sbschmidt
4177208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
4178208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
4179208019Sthompsa	sc->cmdq[i].func = run_update_beacon_cb;
4180208019Sthompsa	sc->cmdq[i].arg0 = vap;
4181208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
4182208019Sthompsa
4183208019Sthompsa	return;
4184203134Sthompsa}
4185203134Sthompsa
4186203134Sthompsastatic void
4187208019Sthompsarun_update_beacon_cb(void *arg)
4188203134Sthompsa{
4189208019Sthompsa	struct ieee80211vap *vap = arg;
4190218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
4191203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
4192203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4193203134Sthompsa	struct rt2860_txwi txwi;
4194203134Sthompsa	struct mbuf *m;
4195208019Sthompsa	uint8_t ridx;
4196203134Sthompsa
4197209917Sthompsa	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
4198208019Sthompsa		return;
4199236439Shselasky	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
4200236439Shselasky		return;
4201208019Sthompsa
4202218492Sbschmidt	/*
4203218492Sbschmidt	 * No need to call ieee80211_beacon_update(), run_update_beacon()
4204218492Sbschmidt	 * is taking care of apropriate calls.
4205218492Sbschmidt	 */
4206218492Sbschmidt	if (rvp->beacon_mbuf == NULL) {
4207218492Sbschmidt		rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
4208218492Sbschmidt		    &rvp->bo);
4209218492Sbschmidt		if (rvp->beacon_mbuf == NULL)
4210218492Sbschmidt			return;
4211218492Sbschmidt	}
4212218492Sbschmidt	m = rvp->beacon_mbuf;
4213203134Sthompsa
4214203134Sthompsa	memset(&txwi, 0, sizeof txwi);
4215203134Sthompsa	txwi.wcid = 0xff;
4216203134Sthompsa	txwi.len = htole16(m->m_pkthdr.len);
4217203134Sthompsa	/* send beacons at the lowest available rate */
4218208019Sthompsa	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4219208019Sthompsa	    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
4220208019Sthompsa	txwi.phy = htole16(rt2860_rates[ridx].mcs);
4221208019Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
4222203134Sthompsa	        txwi.phy |= htole16(RT2860_PHY_OFDM);
4223203134Sthompsa	txwi.txop = RT2860_TX_TXOP_HT;
4224203134Sthompsa	txwi.flags = RT2860_TX_TS;
4225209144Sthompsa	txwi.xflags = RT2860_TX_NSEQ;
4226203134Sthompsa
4227218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
4228208019Sthompsa	    (uint8_t *)&txwi, sizeof txwi);
4229218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
4230203134Sthompsa	    mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);	/* roundup len */
4231203134Sthompsa
4232203134Sthompsa	return;
4233203134Sthompsa}
4234203134Sthompsa
4235203134Sthompsastatic void
4236203134Sthompsarun_updateprot(struct ieee80211com *ic)
4237203134Sthompsa{
4238203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4239218492Sbschmidt	uint32_t i;
4240218492Sbschmidt
4241218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4242218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4243218492Sbschmidt	sc->cmdq[i].func = run_updateprot_cb;
4244218492Sbschmidt	sc->cmdq[i].arg0 = ic;
4245218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4246218492Sbschmidt}
4247218492Sbschmidt
4248218492Sbschmidtstatic void
4249218492Sbschmidtrun_updateprot_cb(void *arg)
4250218492Sbschmidt{
4251218492Sbschmidt	struct ieee80211com *ic = arg;
4252218492Sbschmidt	struct run_softc *sc = ic->ic_ifp->if_softc;
4253203134Sthompsa	uint32_t tmp;
4254203134Sthompsa
4255203134Sthompsa	tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4256203134Sthompsa	/* setup protection frame rate (MCS code) */
4257203134Sthompsa	tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4258203134Sthompsa	    rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4259203134Sthompsa	    rt2860_rates[RT2860_RIDX_CCK11].mcs;
4260203134Sthompsa
4261203134Sthompsa	/* CCK frames don't require protection */
4262203134Sthompsa	run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4263203134Sthompsa	if (ic->ic_flags & IEEE80211_F_USEPROT) {
4264203134Sthompsa		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4265203134Sthompsa			tmp |= RT2860_PROT_CTRL_RTS_CTS;
4266203134Sthompsa		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4267203134Sthompsa			tmp |= RT2860_PROT_CTRL_CTS;
4268203134Sthompsa	}
4269203134Sthompsa	run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4270203134Sthompsa}
4271203134Sthompsa
4272203134Sthompsastatic void
4273208019Sthompsarun_usb_timeout_cb(void *arg)
4274203134Sthompsa{
4275208019Sthompsa	struct ieee80211vap *vap = arg;
4276208019Sthompsa	struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4277203134Sthompsa
4278208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4279203134Sthompsa
4280203134Sthompsa	if(vap->iv_state == IEEE80211_S_RUN &&
4281203134Sthompsa	    vap->iv_opmode != IEEE80211_M_STA)
4282203134Sthompsa		run_reset_livelock(sc);
4283209917Sthompsa	else if (vap->iv_state == IEEE80211_S_SCAN) {
4284203134Sthompsa		DPRINTF("timeout caused by scan\n");
4285203134Sthompsa		/* cancel bgscan */
4286203134Sthompsa		ieee80211_cancel_scan(vap);
4287203134Sthompsa	} else
4288203134Sthompsa		DPRINTF("timeout by unknown cause\n");
4289203134Sthompsa}
4290203134Sthompsa
4291203134Sthompsastatic void
4292203134Sthompsarun_reset_livelock(struct run_softc *sc)
4293203134Sthompsa{
4294203134Sthompsa	uint32_t tmp;
4295203134Sthompsa
4296208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4297208019Sthompsa
4298203134Sthompsa	/*
4299203134Sthompsa	 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4300203134Sthompsa	 * can run into a livelock and start sending CTS-to-self frames like
4301203134Sthompsa	 * crazy if protection is enabled.  Reset MAC/BBP for a while
4302203134Sthompsa	 */
4303203134Sthompsa	run_read(sc, RT2860_DEBUG, &tmp);
4304208019Sthompsa	DPRINTFN(3, "debug reg %08x\n", tmp);
4305209917Sthompsa	if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4306203134Sthompsa		DPRINTF("CTS-to-self livelock detected\n");
4307203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4308203134Sthompsa		run_delay(sc, 1);
4309203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL,
4310203134Sthompsa		    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4311203134Sthompsa	}
4312203134Sthompsa}
4313203134Sthompsa
4314203134Sthompsastatic void
4315203134Sthompsarun_update_promisc_locked(struct ifnet *ifp)
4316203134Sthompsa{
4317203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4318203134Sthompsa        uint32_t tmp;
4319203134Sthompsa
4320203134Sthompsa	run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4321203134Sthompsa
4322203134Sthompsa	tmp |= RT2860_DROP_UC_NOME;
4323203134Sthompsa        if (ifp->if_flags & IFF_PROMISC)
4324203134Sthompsa		tmp &= ~RT2860_DROP_UC_NOME;
4325203134Sthompsa
4326203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4327203134Sthompsa
4328203134Sthompsa        DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4329203134Sthompsa            "entering" : "leaving");
4330203134Sthompsa}
4331203134Sthompsa
4332203134Sthompsastatic void
4333203134Sthompsarun_update_promisc(struct ifnet *ifp)
4334203134Sthompsa{
4335203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4336203134Sthompsa
4337203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4338203134Sthompsa		return;
4339203134Sthompsa
4340203134Sthompsa	RUN_LOCK(sc);
4341203134Sthompsa	run_update_promisc_locked(ifp);
4342203134Sthompsa	RUN_UNLOCK(sc);
4343203134Sthompsa}
4344203134Sthompsa
4345203134Sthompsastatic void
4346203134Sthompsarun_enable_tsf_sync(struct run_softc *sc)
4347203134Sthompsa{
4348208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4349203134Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4350203134Sthompsa	uint32_t tmp;
4351203134Sthompsa
4352257955Skevlo	DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id,
4353257955Skevlo	    ic->ic_opmode);
4354208019Sthompsa
4355203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4356203134Sthompsa	tmp &= ~0x1fffff;
4357203134Sthompsa	tmp |= vap->iv_bss->ni_intval * 16;
4358203134Sthompsa	tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4359203134Sthompsa
4360208019Sthompsa	if (ic->ic_opmode == IEEE80211_M_STA) {
4361203134Sthompsa		/*
4362203134Sthompsa		 * Local TSF is always updated with remote TSF on beacon
4363203134Sthompsa		 * reception.
4364203134Sthompsa		 */
4365203134Sthompsa		tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4366208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4367203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4368203134Sthompsa	        /*
4369203134Sthompsa	         * Local TSF is updated with remote TSF on beacon reception
4370203134Sthompsa	         * only if the remote TSF is greater than local TSF.
4371203134Sthompsa	         */
4372203134Sthompsa	        tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4373208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4374208019Sthompsa		    ic->ic_opmode == IEEE80211_M_MBSS) {
4375203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4376203134Sthompsa	        /* SYNC with nobody */
4377203134Sthompsa	        tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4378208019Sthompsa	} else {
4379203134Sthompsa		DPRINTF("Enabling TSF failed. undefined opmode\n");
4380208019Sthompsa		return;
4381208019Sthompsa	}
4382203134Sthompsa
4383203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4384203134Sthompsa}
4385203134Sthompsa
4386203134Sthompsastatic void
4387203134Sthompsarun_enable_mrr(struct run_softc *sc)
4388203134Sthompsa{
4389203134Sthompsa#define CCK(mcs)	(mcs)
4390203134Sthompsa#define OFDM(mcs)	(1 << 3 | (mcs))
4391203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG0,
4392203134Sthompsa	    OFDM(6) << 28 |	/* 54->48 */
4393203134Sthompsa	    OFDM(5) << 24 |	/* 48->36 */
4394203134Sthompsa	    OFDM(4) << 20 |	/* 36->24 */
4395203134Sthompsa	    OFDM(3) << 16 |	/* 24->18 */
4396203134Sthompsa	    OFDM(2) << 12 |	/* 18->12 */
4397203134Sthompsa	    OFDM(1) <<  8 |	/* 12-> 9 */
4398203134Sthompsa	    OFDM(0) <<  4 |	/*  9-> 6 */
4399203134Sthompsa	    OFDM(0));		/*  6-> 6 */
4400203134Sthompsa
4401203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG1,
4402203134Sthompsa	    CCK(2) << 12 |	/* 11->5.5 */
4403203134Sthompsa	    CCK(1) <<  8 |	/* 5.5-> 2 */
4404203134Sthompsa	    CCK(0) <<  4 |	/*   2-> 1 */
4405203134Sthompsa	    CCK(0));		/*   1-> 1 */
4406203134Sthompsa#undef OFDM
4407203134Sthompsa#undef CCK
4408203134Sthompsa}
4409203134Sthompsa
4410203134Sthompsastatic void
4411203134Sthompsarun_set_txpreamble(struct run_softc *sc)
4412203134Sthompsa{
4413203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4414203134Sthompsa	uint32_t tmp;
4415203134Sthompsa
4416203134Sthompsa	run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4417203134Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4418203134Sthompsa		tmp |= RT2860_CCK_SHORT_EN;
4419203134Sthompsa	else
4420203134Sthompsa		tmp &= ~RT2860_CCK_SHORT_EN;
4421203134Sthompsa	run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4422203134Sthompsa}
4423203134Sthompsa
4424203134Sthompsastatic void
4425203134Sthompsarun_set_basicrates(struct run_softc *sc)
4426203134Sthompsa{
4427203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4428203134Sthompsa
4429203134Sthompsa	/* set basic rates mask */
4430203134Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
4431203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4432203134Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
4433203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4434203134Sthompsa	else	/* 11g */
4435203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4436203134Sthompsa}
4437203134Sthompsa
4438203134Sthompsastatic void
4439203134Sthompsarun_set_leds(struct run_softc *sc, uint16_t which)
4440203134Sthompsa{
4441203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4442203134Sthompsa	    which | (sc->leds & 0x7f));
4443203134Sthompsa}
4444203134Sthompsa
4445203134Sthompsastatic void
4446203134Sthompsarun_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4447203134Sthompsa{
4448203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW0,
4449203134Sthompsa	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4450203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW1,
4451203134Sthompsa	    bssid[4] | bssid[5] << 8);
4452203134Sthompsa}
4453203134Sthompsa
4454203134Sthompsastatic void
4455203134Sthompsarun_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4456203134Sthompsa{
4457203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW0,
4458203134Sthompsa	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4459203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW1,
4460203134Sthompsa	    addr[4] | addr[5] << 8 | 0xff << 16);
4461203134Sthompsa}
4462203134Sthompsa
4463203134Sthompsastatic void
4464203134Sthompsarun_updateslot(struct ifnet *ifp)
4465203134Sthompsa{
4466203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4467203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4468218492Sbschmidt	uint32_t i;
4469218492Sbschmidt
4470218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4471218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4472218492Sbschmidt	sc->cmdq[i].func = run_updateslot_cb;
4473218492Sbschmidt	sc->cmdq[i].arg0 = ifp;
4474218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4475218492Sbschmidt
4476218492Sbschmidt	return;
4477218492Sbschmidt}
4478218492Sbschmidt
4479218492Sbschmidt/* ARGSUSED */
4480218492Sbschmidtstatic void
4481218492Sbschmidtrun_updateslot_cb(void *arg)
4482218492Sbschmidt{
4483218492Sbschmidt	struct ifnet *ifp = arg;
4484218492Sbschmidt	struct run_softc *sc = ifp->if_softc;
4485218492Sbschmidt	struct ieee80211com *ic = ifp->if_l2com;
4486203134Sthompsa	uint32_t tmp;
4487203134Sthompsa
4488203134Sthompsa	run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4489203134Sthompsa	tmp &= ~0xff;
4490203134Sthompsa	tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4491203134Sthompsa	run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4492203134Sthompsa}
4493203134Sthompsa
4494208019Sthompsastatic void
4495208019Sthompsarun_update_mcast(struct ifnet *ifp)
4496208019Sthompsa{
4497208019Sthompsa	/* h/w filter supports getting everything or nothing */
4498208019Sthompsa	ifp->if_flags |= IFF_ALLMULTI;
4499208019Sthompsa}
4500208019Sthompsa
4501203134Sthompsastatic int8_t
4502203134Sthompsarun_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4503203134Sthompsa{
4504203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4505203134Sthompsa	struct ieee80211_channel *c = ic->ic_curchan;
4506203134Sthompsa	int delta;
4507203134Sthompsa
4508203134Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
4509257429Shselasky		u_int chan = ieee80211_chan2ieee(ic, c);
4510203134Sthompsa		delta = sc->rssi_5ghz[rxchain];
4511203134Sthompsa
4512203134Sthompsa		/* determine channel group */
4513203134Sthompsa		if (chan <= 64)
4514203134Sthompsa			delta -= sc->lna[1];
4515203134Sthompsa		else if (chan <= 128)
4516203134Sthompsa			delta -= sc->lna[2];
4517203134Sthompsa		else
4518203134Sthompsa			delta -= sc->lna[3];
4519203134Sthompsa	} else
4520203134Sthompsa		delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4521203134Sthompsa
4522209917Sthompsa	return (-12 - delta - rssi);
4523203134Sthompsa}
4524203134Sthompsa
4525257955Skevlostatic void
4526257955Skevlorun_rt5390_bbp_init(struct run_softc *sc)
4527257955Skevlo{
4528257955Skevlo	int i;
4529257955Skevlo	uint8_t bbp4;
4530257955Skevlo
4531257955Skevlo	/* Avoid data lost and CRC error. */
4532257955Skevlo	run_bbp_read(sc, 4, &bbp4);
4533259031Skevlo	run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
4534257955Skevlo
4535257955Skevlo	for (i = 0; i < nitems(rt5390_def_bbp); i++) {
4536257955Skevlo		run_bbp_write(sc, rt5390_def_bbp[i].reg,
4537257955Skevlo		    rt5390_def_bbp[i].val);
4538257955Skevlo	}
4539257955Skevlo	if (sc->mac_ver == 0x5392) {
4540257955Skevlo		run_bbp_write(sc, 88, 0x90);
4541257955Skevlo		run_bbp_write(sc, 95, 0x9a);
4542257955Skevlo		run_bbp_write(sc, 98, 0x12);
4543257955Skevlo		run_bbp_write(sc, 106, 0x12);
4544257955Skevlo		run_bbp_write(sc, 134, 0xd0);
4545257955Skevlo		run_bbp_write(sc, 135, 0xf6);
4546257955Skevlo		run_bbp_write(sc, 148, 0x84);
4547257955Skevlo	}
4548257955Skevlo
4549257955Skevlo	/* Disable hardware antenna diversity. */
4550257955Skevlo	if (sc->mac_ver == 0x5390)
4551257955Skevlo		run_bbp_write(sc, 154, 0);
4552257955Skevlo}
4553257955Skevlo
4554203134Sthompsastatic int
4555203134Sthompsarun_bbp_init(struct run_softc *sc)
4556203134Sthompsa{
4557203134Sthompsa	int i, error, ntries;
4558203134Sthompsa	uint8_t bbp0;
4559203134Sthompsa
4560203134Sthompsa	/* wait for BBP to wake up */
4561203134Sthompsa	for (ntries = 0; ntries < 20; ntries++) {
4562203134Sthompsa		if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4563203134Sthompsa			return error;
4564203134Sthompsa		if (bbp0 != 0 && bbp0 != 0xff)
4565203134Sthompsa			break;
4566203134Sthompsa	}
4567203134Sthompsa	if (ntries == 20)
4568209917Sthompsa		return (ETIMEDOUT);
4569203134Sthompsa
4570203134Sthompsa	/* initialize BBP registers to default values */
4571257955Skevlo	if (sc->mac_ver >= 0x5390)
4572257955Skevlo		run_rt5390_bbp_init(sc);
4573257955Skevlo	else {
4574257955Skevlo		for (i = 0; i < nitems(rt2860_def_bbp); i++) {
4575257955Skevlo			run_bbp_write(sc, rt2860_def_bbp[i].reg,
4576257955Skevlo			    rt2860_def_bbp[i].val);
4577257955Skevlo		}
4578203134Sthompsa	}
4579203134Sthompsa
4580203134Sthompsa	/* fix BBP84 for RT2860E */
4581205042Sthompsa	if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4582205042Sthompsa		run_bbp_write(sc, 84, 0x19);
4583203134Sthompsa
4584205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4585203134Sthompsa		run_bbp_write(sc, 79, 0x13);
4586203134Sthompsa		run_bbp_write(sc, 80, 0x05);
4587203134Sthompsa		run_bbp_write(sc, 81, 0x33);
4588205042Sthompsa	} else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4589203134Sthompsa		run_bbp_write(sc, 69, 0x16);
4590203134Sthompsa		run_bbp_write(sc, 73, 0x12);
4591203134Sthompsa	}
4592209917Sthompsa	return (0);
4593203134Sthompsa}
4594203134Sthompsa
4595203134Sthompsastatic int
4596203134Sthompsarun_rt3070_rf_init(struct run_softc *sc)
4597203134Sthompsa{
4598203134Sthompsa	uint32_t tmp;
4599256722Skevlo	uint8_t bbp4, mingain, rf, target;
4600203134Sthompsa	int i;
4601203134Sthompsa
4602203134Sthompsa	run_rt3070_rf_read(sc, 30, &rf);
4603203134Sthompsa	/* toggle RF R30 bit 7 */
4604203134Sthompsa	run_rt3070_rf_write(sc, 30, rf | 0x80);
4605203134Sthompsa	run_delay(sc, 10);
4606203134Sthompsa	run_rt3070_rf_write(sc, 30, rf & ~0x80);
4607203134Sthompsa
4608203134Sthompsa	/* initialize RF registers to default value */
4609205042Sthompsa	if (sc->mac_ver == 0x3572) {
4610257955Skevlo		for (i = 0; i < nitems(rt3572_def_rf); i++) {
4611205042Sthompsa			run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4612205042Sthompsa			    rt3572_def_rf[i].val);
4613205042Sthompsa		}
4614205042Sthompsa	} else {
4615257955Skevlo		for (i = 0; i < nitems(rt3070_def_rf); i++) {
4616205042Sthompsa			run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4617205042Sthompsa			    rt3070_def_rf[i].val);
4618205042Sthompsa		}
4619203134Sthompsa	}
4620205042Sthompsa
4621256721Skevlo	if (sc->mac_ver == 0x3070 && sc->mac_rev < 0x0201) {
4622256721Skevlo		/*
4623256721Skevlo		 * Change voltage from 1.2V to 1.35V for RT3070.
4624256721Skevlo		 * The DAC issue (RT3070_LDO_CFG0) has been fixed
4625256721Skevlo		 * in RT3070(F).
4626256721Skevlo		 */
4627203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4628203134Sthompsa		tmp = (tmp & ~0x0f000000) | 0x0d000000;
4629203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4630203134Sthompsa
4631205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4632203134Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4633203134Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4634203134Sthompsa		run_rt3070_rf_write(sc, 31, 0x14);
4635203134Sthompsa
4636203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4637203134Sthompsa		tmp &= ~0x1f000000;
4638205042Sthompsa		if (sc->mac_rev < 0x0211)
4639205042Sthompsa			tmp |= 0x0d000000;	/* 1.3V */
4640203134Sthompsa		else
4641205042Sthompsa			tmp |= 0x01000000;	/* 1.2V */
4642203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4643203134Sthompsa
4644203134Sthompsa		/* patch LNA_PE_G1 */
4645203134Sthompsa		run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4646203134Sthompsa		run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4647208019Sthompsa
4648209917Sthompsa	} else if (sc->mac_ver == 0x3572) {
4649205042Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4650205042Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4651205042Sthompsa
4652208019Sthompsa		/* increase voltage from 1.2V to 1.35V */
4653208019Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4654208019Sthompsa		tmp = (tmp & ~0x1f000000) | 0x0d000000;
4655208019Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4656203134Sthompsa
4657209917Sthompsa		if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4658203134Sthompsa			run_delay(sc, 1);	/* wait for 1msec */
4659205042Sthompsa			/* decrease voltage back to 1.2V */
4660203134Sthompsa			tmp = (tmp & ~0x1f000000) | 0x01000000;
4661203134Sthompsa			run_write(sc, RT3070_LDO_CFG0, tmp);
4662203134Sthompsa		}
4663203134Sthompsa	}
4664203134Sthompsa
4665203134Sthompsa	/* select 20MHz bandwidth */
4666203134Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4667203134Sthompsa	run_rt3070_rf_write(sc, 31, rf & ~0x20);
4668203134Sthompsa
4669203134Sthompsa	/* calibrate filter for 20MHz bandwidth */
4670203134Sthompsa	sc->rf24_20mhz = 0x1f;	/* default value */
4671205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4672205042Sthompsa	run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4673203134Sthompsa
4674203134Sthompsa	/* select 40MHz bandwidth */
4675203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4676256721Skevlo	run_bbp_write(sc, 4, (bbp4 & ~0x18) | 0x10);
4677205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4678205042Sthompsa	run_rt3070_rf_write(sc, 31, rf | 0x20);
4679203134Sthompsa
4680203134Sthompsa	/* calibrate filter for 40MHz bandwidth */
4681203134Sthompsa	sc->rf24_40mhz = 0x2f;	/* default value */
4682205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4683205042Sthompsa	run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4684203134Sthompsa
4685203134Sthompsa	/* go back to 20MHz bandwidth */
4686203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4687203134Sthompsa	run_bbp_write(sc, 4, bbp4 & ~0x18);
4688203134Sthompsa
4689205042Sthompsa	if (sc->mac_ver == 0x3572) {
4690205042Sthompsa		/* save default BBP registers 25 and 26 values */
4691205042Sthompsa		run_bbp_read(sc, 25, &sc->bbp25);
4692205042Sthompsa		run_bbp_read(sc, 26, &sc->bbp26);
4693256721Skevlo	} else if (sc->mac_rev < 0x0201 || sc->mac_rev < 0x0211)
4694203134Sthompsa		run_rt3070_rf_write(sc, 27, 0x03);
4695203134Sthompsa
4696203134Sthompsa	run_read(sc, RT3070_OPT_14, &tmp);
4697203134Sthompsa	run_write(sc, RT3070_OPT_14, tmp | 1);
4698203134Sthompsa
4699205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4700205042Sthompsa		run_rt3070_rf_read(sc, 17, &rf);
4701205042Sthompsa		rf &= ~RT3070_TX_LO1;
4702205042Sthompsa		if ((sc->mac_ver == 0x3070 ||
4703205042Sthompsa		     (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4704205042Sthompsa		    !sc->ext_2ghz_lna)
4705205042Sthompsa			rf |= 0x20;	/* fix for long range Rx issue */
4706256722Skevlo		mingain = (sc->mac_ver == 0x3070) ? 1 : 2;
4707256722Skevlo		if (sc->txmixgain_2ghz >= mingain)
4708205042Sthompsa			rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4709205042Sthompsa		run_rt3070_rf_write(sc, 17, rf);
4710205042Sthompsa	}
4711205042Sthompsa
4712205042Sthompsa	if (sc->mac_rev == 0x3071) {
4713203134Sthompsa		run_rt3070_rf_read(sc, 1, &rf);
4714203134Sthompsa		rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4715203134Sthompsa		rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4716203134Sthompsa		run_rt3070_rf_write(sc, 1, rf);
4717203134Sthompsa
4718203134Sthompsa		run_rt3070_rf_read(sc, 15, &rf);
4719203134Sthompsa		run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4720203134Sthompsa
4721203134Sthompsa		run_rt3070_rf_read(sc, 20, &rf);
4722203134Sthompsa		run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4723203134Sthompsa
4724203134Sthompsa		run_rt3070_rf_read(sc, 21, &rf);
4725203134Sthompsa		run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4726205042Sthompsa	}
4727203134Sthompsa
4728205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4729205042Sthompsa		/* fix Tx to Rx IQ glitch by raising RF voltage */
4730203134Sthompsa		run_rt3070_rf_read(sc, 27, &rf);
4731203134Sthompsa		rf &= ~0x77;
4732205042Sthompsa		if (sc->mac_rev < 0x0211)
4733203134Sthompsa			rf |= 0x03;
4734203134Sthompsa		run_rt3070_rf_write(sc, 27, rf);
4735203134Sthompsa	}
4736209917Sthompsa	return (0);
4737203134Sthompsa}
4738203134Sthompsa
4739257955Skevlostatic void
4740257955Skevlorun_rt5390_rf_init(struct run_softc *sc)
4741257955Skevlo{
4742257955Skevlo	uint32_t tmp;
4743257955Skevlo	uint8_t rf;
4744257955Skevlo	int i;
4745257955Skevlo
4746259030Skevlo	/* Toggle RF R2 to initiate calibration. */
4747259030Skevlo	if (sc->mac_ver == 0x5390) {
4748257955Skevlo		run_rt3070_rf_read(sc, 2, &rf);
4749259031Skevlo		run_rt3070_rf_write(sc, 2, rf | RT5390_RESCAL);
4750257955Skevlo		run_delay(sc, 10);
4751259031Skevlo		run_rt3070_rf_write(sc, 2, rf & ~RT5390_RESCAL);
4752259030Skevlo	} else {
4753259031Skevlo		run_rt3070_rf_write(sc, 2, RT5390_RESCAL);
4754259030Skevlo		run_delay(sc, 10);
4755257955Skevlo	}
4756257955Skevlo
4757257955Skevlo	/* Initialize RF registers to default value. */
4758257955Skevlo	if (sc->mac_ver == 0x5392) {
4759257955Skevlo		for (i = 0; i < nitems(rt5392_def_rf); i++) {
4760257955Skevlo			run_rt3070_rf_write(sc, rt5392_def_rf[i].reg,
4761257955Skevlo			    rt5392_def_rf[i].val);
4762257955Skevlo		}
4763257955Skevlo		if (sc->mac_rev >= 0x0223) {
4764257955Skevlo			run_rt3070_rf_write(sc, 23, 0x0f);
4765257955Skevlo			run_rt3070_rf_write(sc, 24, 0x3e);
4766257955Skevlo			run_rt3070_rf_write(sc, 51, 0x32);
4767257955Skevlo			run_rt3070_rf_write(sc, 53, 0x22);
4768257955Skevlo			run_rt3070_rf_write(sc, 56, 0xc1);
4769257955Skevlo			run_rt3070_rf_write(sc, 59, 0x0f);
4770257955Skevlo		}
4771257955Skevlo	} else {
4772257955Skevlo		for (i = 0; i < nitems(rt5390_def_rf); i++) {
4773257955Skevlo			run_rt3070_rf_write(sc, rt5390_def_rf[i].reg,
4774257955Skevlo			    rt5390_def_rf[i].val);
4775257955Skevlo		}
4776257955Skevlo		if (sc->mac_rev >= 0x0502) {
4777257955Skevlo			run_rt3070_rf_write(sc, 6, 0xe0);
4778257955Skevlo			run_rt3070_rf_write(sc, 25, 0x80);
4779257955Skevlo			run_rt3070_rf_write(sc, 46, 0x73);
4780257955Skevlo			run_rt3070_rf_write(sc, 53, 0x00);
4781257955Skevlo			run_rt3070_rf_write(sc, 56, 0x42);
4782257955Skevlo			run_rt3070_rf_write(sc, 61, 0xd1);
4783257955Skevlo		}
4784257955Skevlo	}
4785257955Skevlo
4786257955Skevlo	sc->rf24_20mhz = 0x1f;	/* default value */
4787257955Skevlo	sc->rf24_40mhz = 0x2f;	/* default value */
4788257955Skevlo
4789257955Skevlo	if (sc->mac_rev < 0x0211)
4790257955Skevlo		run_rt3070_rf_write(sc, 27, 0x3);
4791257955Skevlo
4792257955Skevlo	run_read(sc, RT3070_OPT_14, &tmp);
4793257955Skevlo	run_write(sc, RT3070_OPT_14, tmp | 1);
4794257955Skevlo}
4795257955Skevlo
4796203134Sthompsastatic int
4797203134Sthompsarun_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4798203134Sthompsa    uint8_t *val)
4799203134Sthompsa{
4800203134Sthompsa	uint8_t rf22, rf24;
4801203134Sthompsa	uint8_t bbp55_pb, bbp55_sb, delta;
4802203134Sthompsa	int ntries;
4803203134Sthompsa
4804203134Sthompsa	/* program filter */
4805205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf24);
4806205042Sthompsa	rf24 = (rf24 & 0xc0) | init;	/* initial filter value */
4807203134Sthompsa	run_rt3070_rf_write(sc, 24, rf24);
4808203134Sthompsa
4809203134Sthompsa	/* enable baseband loopback mode */
4810203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4811203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4812203134Sthompsa
4813203134Sthompsa	/* set power and frequency of passband test tone */
4814203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4815203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4816203134Sthompsa		/* transmit test tone */
4817203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4818203134Sthompsa		run_delay(sc, 10);
4819203134Sthompsa		/* read received power */
4820203134Sthompsa		run_bbp_read(sc, 55, &bbp55_pb);
4821203134Sthompsa		if (bbp55_pb != 0)
4822203134Sthompsa			break;
4823203134Sthompsa	}
4824203134Sthompsa	if (ntries == 100)
4825257955Skevlo		return (ETIMEDOUT);
4826203134Sthompsa
4827203134Sthompsa	/* set power and frequency of stopband test tone */
4828203134Sthompsa	run_bbp_write(sc, 24, 0x06);
4829203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4830203134Sthompsa		/* transmit test tone */
4831203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4832203134Sthompsa		run_delay(sc, 10);
4833203134Sthompsa		/* read received power */
4834203134Sthompsa		run_bbp_read(sc, 55, &bbp55_sb);
4835203134Sthompsa
4836203134Sthompsa		delta = bbp55_pb - bbp55_sb;
4837203134Sthompsa		if (delta > target)
4838203134Sthompsa			break;
4839203134Sthompsa
4840203134Sthompsa		/* reprogram filter */
4841203134Sthompsa		rf24++;
4842203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4843203134Sthompsa	}
4844203134Sthompsa	if (ntries < 100) {
4845203134Sthompsa		if (rf24 != init)
4846203134Sthompsa			rf24--;	/* backtrack */
4847203134Sthompsa		*val = rf24;
4848203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4849203134Sthompsa	}
4850203134Sthompsa
4851203134Sthompsa	/* restore initial state */
4852203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4853203134Sthompsa
4854203134Sthompsa	/* disable baseband loopback mode */
4855203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4856203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4857203134Sthompsa
4858209917Sthompsa	return (0);
4859203134Sthompsa}
4860203134Sthompsa
4861205042Sthompsastatic void
4862205042Sthompsarun_rt3070_rf_setup(struct run_softc *sc)
4863205042Sthompsa{
4864205042Sthompsa	uint8_t bbp, rf;
4865205042Sthompsa	int i;
4866205042Sthompsa
4867257955Skevlo	if (sc->mac_ver >= 0x5390) {
4868257955Skevlo		if (sc->mac_rev >= 0x0211) {
4869257955Skevlo			/* Enable DC filter. */
4870257955Skevlo			run_bbp_write(sc, 103, 0xc0);
4871257955Skevlo
4872257955Skevlo			/* Improve power consumption. */
4873257955Skevlo			run_bbp_read(sc, 31, &bbp);
4874257955Skevlo			run_bbp_write(sc, 31, bbp & ~0x03);
4875257955Skevlo		}
4876257955Skevlo
4877257955Skevlo		run_bbp_read(sc, 138, &bbp);
4878257955Skevlo		if (sc->ntxchains == 1)
4879257955Skevlo			bbp |= 0x20;	/* turn off DAC1 */
4880257955Skevlo		if (sc->nrxchains == 1)
4881257955Skevlo			bbp &= ~0x02;	/* turn off ADC1 */
4882257955Skevlo		run_bbp_write(sc, 138, bbp);
4883257955Skevlo
4884257955Skevlo		run_rt3070_rf_read(sc, 38, &rf);
4885257955Skevlo		run_rt3070_rf_write(sc, 38, rf & ~RT5390_RX_LO1);
4886257955Skevlo
4887257955Skevlo		run_rt3070_rf_read(sc, 39, &rf);
4888257955Skevlo		run_rt3070_rf_write(sc, 39, rf & ~RT5390_RX_LO2);
4889257955Skevlo
4890257955Skevlo		/* Avoid data lost and CRC error. */
4891257955Skevlo		run_bbp_read(sc, 4, &bbp);
4892259031Skevlo		run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
4893257955Skevlo
4894257955Skevlo		run_rt3070_rf_read(sc, 30, &rf);
4895257955Skevlo		rf = (rf & ~0x18) | 0x10;
4896257955Skevlo		run_rt3070_rf_write(sc, 30, rf);
4897257955Skevlo
4898257955Skevlo		run_write(sc, RT2860_TX_SW_CFG1, 0);
4899257955Skevlo		if (sc->mac_rev < 0x0211) {
4900257955Skevlo			run_write(sc, RT2860_TX_SW_CFG2,
4901257955Skevlo			    sc->patch_dac ? 0x2c : 0x0f);
4902257955Skevlo		} else
4903257955Skevlo			run_write(sc, RT2860_TX_SW_CFG2, 0);
4904257955Skevlo
4905257955Skevlo	} else if (sc->mac_ver == 0x3572) {
4906205042Sthompsa		/* enable DC filter */
4907205042Sthompsa		if (sc->mac_rev >= 0x0201)
4908205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4909205042Sthompsa
4910205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4911205042Sthompsa		if (sc->ntxchains == 1)
4912205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4913205042Sthompsa		if (sc->nrxchains == 1)
4914205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4915205042Sthompsa		run_bbp_write(sc, 138, bbp);
4916205042Sthompsa
4917205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4918205042Sthompsa			/* improve power consumption */
4919205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4920205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4921205042Sthompsa		}
4922205042Sthompsa
4923205042Sthompsa		run_rt3070_rf_read(sc, 16, &rf);
4924205042Sthompsa		rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4925205042Sthompsa		run_rt3070_rf_write(sc, 16, rf);
4926205042Sthompsa
4927205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4928257409Skevlo		if (sc->mac_rev >= 0x0211) {
4929257409Skevlo			/* enable DC filter */
4930205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4931205042Sthompsa
4932257409Skevlo			/* improve power consumption */
4933257409Skevlo			run_bbp_read(sc, 31, &bbp);
4934257409Skevlo			run_bbp_write(sc, 31, bbp & ~0x03);
4935257409Skevlo		}
4936257409Skevlo
4937205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4938205042Sthompsa		if (sc->ntxchains == 1)
4939205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4940205042Sthompsa		if (sc->nrxchains == 1)
4941205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4942205042Sthompsa		run_bbp_write(sc, 138, bbp);
4943205042Sthompsa
4944205042Sthompsa		run_write(sc, RT2860_TX_SW_CFG1, 0);
4945205042Sthompsa		if (sc->mac_rev < 0x0211) {
4946205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2,
4947205042Sthompsa			    sc->patch_dac ? 0x2c : 0x0f);
4948205042Sthompsa		} else
4949205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4950205042Sthompsa
4951205042Sthompsa	} else if (sc->mac_ver == 0x3070) {
4952205042Sthompsa		if (sc->mac_rev >= 0x0201) {
4953205042Sthompsa			/* enable DC filter */
4954205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4955205042Sthompsa
4956205042Sthompsa			/* improve power consumption */
4957205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4958205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4959205042Sthompsa		}
4960205042Sthompsa
4961256955Skevlo		if (sc->mac_rev < 0x0201) {
4962205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG1, 0);
4963205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4964205042Sthompsa		} else
4965205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4966205042Sthompsa	}
4967205042Sthompsa
4968205042Sthompsa	/* initialize RF registers from ROM for >=RT3071*/
4969257955Skevlo	if (sc->mac_ver >= 0x3071 && sc->mac_ver < 0x5390) {
4970205042Sthompsa		for (i = 0; i < 10; i++) {
4971205042Sthompsa			if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4972205042Sthompsa				continue;
4973205042Sthompsa			run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4974205042Sthompsa		}
4975205042Sthompsa	}
4976205042Sthompsa}
4977205042Sthompsa
4978203134Sthompsastatic int
4979203134Sthompsarun_txrx_enable(struct run_softc *sc)
4980203134Sthompsa{
4981203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4982203134Sthompsa	uint32_t tmp;
4983203134Sthompsa	int error, ntries;
4984203134Sthompsa
4985203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4986203134Sthompsa	for (ntries = 0; ntries < 200; ntries++) {
4987203134Sthompsa		if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4988257955Skevlo			return (error);
4989203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4990203134Sthompsa			break;
4991203134Sthompsa		run_delay(sc, 50);
4992203134Sthompsa	}
4993203134Sthompsa	if (ntries == 200)
4994257955Skevlo		return (ETIMEDOUT);
4995203134Sthompsa
4996203134Sthompsa	run_delay(sc, 50);
4997203134Sthompsa
4998203134Sthompsa	tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4999203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
5000203134Sthompsa
5001203134Sthompsa	/* enable Rx bulk aggregation (set timeout and limit) */
5002203134Sthompsa	tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
5003203134Sthompsa	    RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
5004203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, tmp);
5005203134Sthompsa
5006203134Sthompsa	/* set Rx filter */
5007203134Sthompsa	tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
5008203134Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
5009203134Sthompsa		tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
5010203134Sthompsa		    RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
5011203134Sthompsa		    RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
5012203134Sthompsa		    RT2860_DROP_CFACK | RT2860_DROP_CFEND;
5013203134Sthompsa		if (ic->ic_opmode == IEEE80211_M_STA)
5014203134Sthompsa			tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
5015203134Sthompsa	}
5016203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
5017203134Sthompsa
5018203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
5019203134Sthompsa	    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5020203134Sthompsa
5021209917Sthompsa	return (0);
5022203134Sthompsa}
5023203134Sthompsa
5024203134Sthompsastatic void
5025259030Skevlorun_adjust_freq_offset(struct run_softc *sc)
5026257955Skevlo{
5027257955Skevlo	uint8_t rf, tmp;
5028257955Skevlo
5029257955Skevlo	run_rt3070_rf_read(sc, 17, &rf);
5030257955Skevlo	tmp = rf;
5031257955Skevlo	rf = (rf & ~0x7f) | (sc->freq & 0x7f);
5032257955Skevlo	rf = MIN(rf, 0x5f);
5033257955Skevlo
5034257955Skevlo	if (tmp != rf)
5035257955Skevlo		run_mcu_cmd(sc, 0x74, (tmp << 8 ) | rf);
5036257955Skevlo}
5037257955Skevlo
5038257955Skevlostatic void
5039203134Sthompsarun_init_locked(struct run_softc *sc)
5040203134Sthompsa{
5041203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
5042203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
5043203134Sthompsa	uint32_t tmp;
5044203134Sthompsa	uint8_t bbp1, bbp3;
5045203134Sthompsa	int i;
5046203134Sthompsa	int ridx;
5047203134Sthompsa	int ntries;
5048203134Sthompsa
5049209917Sthompsa	if (ic->ic_nrunning > 1)
5050208019Sthompsa		return;
5051208019Sthompsa
5052203134Sthompsa	run_stop(sc);
5053203134Sthompsa
5054233283Sbschmidt	if (run_load_microcode(sc) != 0) {
5055233283Sbschmidt		device_printf(sc->sc_dev, "could not load 8051 microcode\n");
5056233283Sbschmidt		goto fail;
5057233283Sbschmidt	}
5058233283Sbschmidt
5059203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5060203134Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
5061203134Sthompsa			goto fail;
5062203134Sthompsa		if (tmp != 0 && tmp != 0xffffffff)
5063203134Sthompsa			break;
5064203134Sthompsa		run_delay(sc, 10);
5065203134Sthompsa	}
5066203134Sthompsa	if (ntries == 100)
5067203134Sthompsa		goto fail;
5068203134Sthompsa
5069203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
5070203134Sthompsa		run_setup_tx_list(sc, &sc->sc_epq[i]);
5071203134Sthompsa
5072203134Sthompsa	run_set_macaddr(sc, IF_LLADDR(ifp));
5073203134Sthompsa
5074203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5075203134Sthompsa		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
5076203134Sthompsa			goto fail;
5077203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
5078203134Sthompsa			break;
5079203134Sthompsa		run_delay(sc, 10);
5080203134Sthompsa	}
5081203134Sthompsa	if (ntries == 100) {
5082203138Sthompsa		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
5083203134Sthompsa		goto fail;
5084203134Sthompsa	}
5085203134Sthompsa	tmp &= 0xff0;
5086203134Sthompsa	tmp |= RT2860_TX_WB_DDONE;
5087203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
5088203134Sthompsa
5089203134Sthompsa	/* turn off PME_OEN to solve high-current issue */
5090203134Sthompsa	run_read(sc, RT2860_SYS_CTRL, &tmp);
5091203134Sthompsa	run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
5092203134Sthompsa
5093203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
5094203134Sthompsa	    RT2860_BBP_HRST | RT2860_MAC_SRST);
5095203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
5096203134Sthompsa
5097203134Sthompsa	if (run_reset(sc) != 0) {
5098203138Sthompsa		device_printf(sc->sc_dev, "could not reset chipset\n");
5099203134Sthompsa		goto fail;
5100203134Sthompsa	}
5101203134Sthompsa
5102203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
5103203134Sthompsa
5104203134Sthompsa	/* init Tx power for all Tx rates (from EEPROM) */
5105203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
5106203134Sthompsa		if (sc->txpow20mhz[ridx] == 0xffffffff)
5107203134Sthompsa			continue;
5108203134Sthompsa		run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
5109203134Sthompsa	}
5110203134Sthompsa
5111257955Skevlo	for (i = 0; i < nitems(rt2870_def_mac); i++)
5112203134Sthompsa		run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
5113203134Sthompsa	run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
5114203134Sthompsa	run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
5115203134Sthompsa	run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
5116203134Sthompsa
5117259030Skevlo	if (sc->mac_ver >= 0x5390) {
5118259030Skevlo		run_write(sc, RT2860_TX_SW_CFG0,
5119259030Skevlo		    4 << RT2860_DLY_PAPE_EN_SHIFT | 4);
5120259030Skevlo		if (sc->mac_ver >= 0x5392) {
5121259030Skevlo			run_write(sc, RT2860_MAX_LEN_CFG, 0x00002fff);
5122259030Skevlo			run_write(sc, RT2860_HT_FBK_CFG1, 0xedcb4980);
5123259030Skevlo			run_write(sc, RT2860_LG_FBK_CFG0, 0xedcba322);
5124259030Skevlo		}
5125257955Skevlo	} else if (sc->mac_ver >= 0x3070) {
5126203134Sthompsa		/* set delay of PA_PE assertion to 1us (unit of 0.25us) */
5127203134Sthompsa		run_write(sc, RT2860_TX_SW_CFG0,
5128203134Sthompsa		    4 << RT2860_DLY_PAPE_EN_SHIFT);
5129203134Sthompsa	}
5130203134Sthompsa
5131203134Sthompsa	/* wait while MAC is busy */
5132203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5133203134Sthompsa		if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
5134203134Sthompsa			goto fail;
5135203134Sthompsa		if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
5136203134Sthompsa			break;
5137203134Sthompsa		run_delay(sc, 10);
5138203134Sthompsa	}
5139203134Sthompsa	if (ntries == 100)
5140203134Sthompsa		goto fail;
5141203134Sthompsa
5142203134Sthompsa	/* clear Host to MCU mailbox */
5143203134Sthompsa	run_write(sc, RT2860_H2M_BBPAGENT, 0);
5144203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
5145203134Sthompsa	run_delay(sc, 10);
5146203134Sthompsa
5147203134Sthompsa	if (run_bbp_init(sc) != 0) {
5148203138Sthompsa		device_printf(sc->sc_dev, "could not initialize BBP\n");
5149203134Sthompsa		goto fail;
5150203134Sthompsa	}
5151203134Sthompsa
5152203134Sthompsa	/* abort TSF synchronization */
5153203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
5154203134Sthompsa	tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
5155203134Sthompsa	    RT2860_TBTT_TIMER_EN);
5156203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
5157203134Sthompsa
5158203134Sthompsa	/* clear RX WCID search table */
5159203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
5160203134Sthompsa	/* clear WCID attribute table */
5161203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
5162203134Sthompsa
5163209144Sthompsa	/* hostapd sets a key before init. So, don't clear it. */
5164209917Sthompsa	if (sc->cmdq_key_set != RUN_CMDQ_GO) {
5165209144Sthompsa		/* clear shared key table */
5166209144Sthompsa		run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
5167209144Sthompsa		/* clear shared key mode */
5168209144Sthompsa		run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
5169209144Sthompsa	}
5170209144Sthompsa
5171203134Sthompsa	run_read(sc, RT2860_US_CYC_CNT, &tmp);
5172203134Sthompsa	tmp = (tmp & ~0xff) | 0x1e;
5173203134Sthompsa	run_write(sc, RT2860_US_CYC_CNT, tmp);
5174203134Sthompsa
5175205042Sthompsa	if (sc->mac_rev != 0x0101)
5176203134Sthompsa		run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
5177203134Sthompsa
5178203134Sthompsa	run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
5179203134Sthompsa	run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
5180203134Sthompsa
5181203134Sthompsa	/* write vendor-specific BBP values (from EEPROM) */
5182257955Skevlo	if (sc->mac_ver < 0x5390) {
5183257955Skevlo		for (i = 0; i < 10; i++) {
5184257955Skevlo			if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
5185257955Skevlo				continue;
5186257955Skevlo			run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
5187257955Skevlo		}
5188203134Sthompsa	}
5189203134Sthompsa
5190203134Sthompsa	/* select Main antenna for 1T1R devices */
5191257955Skevlo	if (sc->rf_rev == RT3070_RF_3020 || sc->rf_rev == RT5390_RF_5370)
5192203134Sthompsa		run_set_rx_antenna(sc, 0);
5193203134Sthompsa
5194203134Sthompsa	/* send LEDs operating mode to microcontroller */
5195203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
5196203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
5197203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
5198203134Sthompsa
5199257955Skevlo	if (sc->mac_ver >= 0x5390)
5200257955Skevlo		run_rt5390_rf_init(sc);
5201257955Skevlo	else if (sc->mac_ver >= 0x3070)
5202205042Sthompsa		run_rt3070_rf_init(sc);
5203205042Sthompsa
5204203134Sthompsa	/* disable non-existing Rx chains */
5205203134Sthompsa	run_bbp_read(sc, 3, &bbp3);
5206203134Sthompsa	bbp3 &= ~(1 << 3 | 1 << 4);
5207203134Sthompsa	if (sc->nrxchains == 2)
5208203134Sthompsa		bbp3 |= 1 << 3;
5209203134Sthompsa	else if (sc->nrxchains == 3)
5210203134Sthompsa		bbp3 |= 1 << 4;
5211203134Sthompsa	run_bbp_write(sc, 3, bbp3);
5212203134Sthompsa
5213203134Sthompsa	/* disable non-existing Tx chains */
5214203134Sthompsa	run_bbp_read(sc, 1, &bbp1);
5215203134Sthompsa	if (sc->ntxchains == 1)
5216203134Sthompsa		bbp1 &= ~(1 << 3 | 1 << 4);
5217203134Sthompsa	run_bbp_write(sc, 1, bbp1);
5218203134Sthompsa
5219205042Sthompsa	if (sc->mac_ver >= 0x3070)
5220205042Sthompsa		run_rt3070_rf_setup(sc);
5221203134Sthompsa
5222203134Sthompsa	/* select default channel */
5223203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
5224203134Sthompsa
5225203134Sthompsa	/* setup initial protection mode */
5226218492Sbschmidt	run_updateprot_cb(ic);
5227203134Sthompsa
5228203134Sthompsa	/* turn radio LED on */
5229203134Sthompsa	run_set_leds(sc, RT2860_LED_RADIO);
5230203134Sthompsa
5231203134Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5232203134Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
5233208019Sthompsa	sc->cmdq_run = RUN_CMDQ_GO;
5234203134Sthompsa
5235209917Sthompsa	for (i = 0; i != RUN_N_XFER; i++)
5236203134Sthompsa		usbd_xfer_set_stall(sc->sc_xfer[i]);
5237203134Sthompsa
5238203134Sthompsa	usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
5239203134Sthompsa
5240203134Sthompsa	if (run_txrx_enable(sc) != 0)
5241203134Sthompsa		goto fail;
5242203134Sthompsa
5243203134Sthompsa	return;
5244203134Sthompsa
5245203134Sthompsafail:
5246203134Sthompsa	run_stop(sc);
5247203134Sthompsa}
5248203134Sthompsa
5249203134Sthompsastatic void
5250203134Sthompsarun_init(void *arg)
5251203134Sthompsa{
5252203134Sthompsa	struct run_softc *sc = arg;
5253203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
5254203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
5255203134Sthompsa
5256203134Sthompsa	RUN_LOCK(sc);
5257203134Sthompsa	run_init_locked(sc);
5258203134Sthompsa	RUN_UNLOCK(sc);
5259203134Sthompsa
5260203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
5261208019Sthompsa		ieee80211_start_all(ic);
5262203134Sthompsa}
5263203134Sthompsa
5264203134Sthompsastatic void
5265203134Sthompsarun_stop(void *arg)
5266203134Sthompsa{
5267203134Sthompsa	struct run_softc *sc = (struct run_softc *)arg;
5268203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
5269203134Sthompsa	uint32_t tmp;
5270203134Sthompsa	int i;
5271203134Sthompsa	int ntries;
5272203134Sthompsa
5273203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
5274203134Sthompsa
5275203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
5276203134Sthompsa		run_set_leds(sc, 0);	/* turn all LEDs off */
5277203134Sthompsa
5278203134Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
5279203134Sthompsa
5280208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
5281209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set;
5282208019Sthompsa
5283203134Sthompsa	RUN_UNLOCK(sc);
5284203134Sthompsa
5285203134Sthompsa	for(i = 0; i < RUN_N_XFER; i++)
5286203134Sthompsa		usbd_transfer_drain(sc->sc_xfer[i]);
5287203134Sthompsa
5288203134Sthompsa	RUN_LOCK(sc);
5289203134Sthompsa
5290209917Sthompsa	if (sc->rx_m != NULL) {
5291203134Sthompsa		m_free(sc->rx_m);
5292203134Sthompsa		sc->rx_m = NULL;
5293203134Sthompsa	}
5294203134Sthompsa
5295257955Skevlo	/* Disable Tx/Rx DMA. */
5296257955Skevlo	if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
5297257955Skevlo		return;
5298257955Skevlo	tmp &= ~(RT2860_RX_DMA_EN | RT2860_TX_DMA_EN);
5299257955Skevlo	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
5300257955Skevlo
5301258643Shselasky	for (ntries = 0; ntries < 100; ntries++) {
5302257955Skevlo		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
5303257955Skevlo			return;
5304257955Skevlo		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
5305257955Skevlo				break;
5306257955Skevlo		run_delay(sc, 10);
5307257955Skevlo	}
5308257955Skevlo	if (ntries == 100) {
5309257955Skevlo		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
5310257955Skevlo		return;
5311257955Skevlo	}
5312257955Skevlo
5313203134Sthompsa	/* disable Tx/Rx */
5314203134Sthompsa	run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
5315203134Sthompsa	tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5316203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
5317203134Sthompsa
5318203134Sthompsa	/* wait for pending Tx to complete */
5319203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5320209917Sthompsa		if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
5321203134Sthompsa			DPRINTF("Cannot read Tx queue count\n");
5322203134Sthompsa			break;
5323203134Sthompsa		}
5324209917Sthompsa		if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
5325203134Sthompsa			DPRINTF("All Tx cleared\n");
5326203134Sthompsa			break;
5327203134Sthompsa		}
5328203134Sthompsa		run_delay(sc, 10);
5329203134Sthompsa	}
5330209917Sthompsa	if (ntries >= 100)
5331203134Sthompsa		DPRINTF("There are still pending Tx\n");
5332203134Sthompsa	run_delay(sc, 10);
5333203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
5334203134Sthompsa
5335203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
5336203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
5337203134Sthompsa
5338203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
5339203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
5340203134Sthompsa}
5341203134Sthompsa
5342203134Sthompsastatic void
5343257429Shselaskyrun_delay(struct run_softc *sc, u_int ms)
5344203134Sthompsa{
5345203134Sthompsa	usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
5346203134Sthompsa	    &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
5347203134Sthompsa}
5348203134Sthompsa
5349203134Sthompsastatic device_method_t run_methods[] = {
5350203134Sthompsa	/* Device interface */
5351203134Sthompsa	DEVMETHOD(device_probe,		run_match),
5352203134Sthompsa	DEVMETHOD(device_attach,	run_attach),
5353203134Sthompsa	DEVMETHOD(device_detach,	run_detach),
5354246614Shselasky	DEVMETHOD_END
5355203134Sthompsa};
5356203134Sthompsa
5357203134Sthompsastatic driver_t run_driver = {
5358233774Shselasky	.name = "run",
5359233774Shselasky	.methods = run_methods,
5360233774Shselasky	.size = sizeof(struct run_softc)
5361203134Sthompsa};
5362203134Sthompsa
5363203134Sthompsastatic devclass_t run_devclass;
5364203134Sthompsa
5365257955SkevloDRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, NULL);
5366212122SthompsaMODULE_DEPEND(run, wlan, 1, 1, 1);
5367212122SthompsaMODULE_DEPEND(run, usb, 1, 1, 1);
5368212122SthompsaMODULE_DEPEND(run, firmware, 1, 1, 1);
5369212122SthompsaMODULE_VERSION(run, 1);
5370