if_run.c revision 258082
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 258082 2013-11-13 05:21:41Z 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 *);
347203134Sthompsastatic int	run_rt2870_rf_write(struct run_softc *, uint8_t, 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
1065203134Sthompsaint
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
1282203134Sthompsarun_rt2870_rf_write(struct run_softc *sc, uint8_t reg, 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
1296203134Sthompsa	/* RF registers are 24-bit on the RT2860 */
1297203134Sthompsa	tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1298203134Sthompsa	    (val & 0x3fffff) << 2 | (reg & 3);
1299209917Sthompsa	return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1300203134Sthompsa}
1301203134Sthompsa
1302203134Sthompsastatic int
1303203134Sthompsarun_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1304203134Sthompsa{
1305203134Sthompsa	uint32_t tmp;
1306203134Sthompsa	int error, ntries;
1307203134Sthompsa
1308203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1309203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1310209917Sthompsa			return (error);
1311203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1312203134Sthompsa			break;
1313203134Sthompsa	}
1314203134Sthompsa	if (ntries == 100)
1315209917Sthompsa		return (ETIMEDOUT);
1316203134Sthompsa
1317203134Sthompsa	tmp = RT3070_RF_KICK | reg << 8;
1318203134Sthompsa	if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1319209917Sthompsa		return (error);
1320203134Sthompsa
1321203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1322203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1323209917Sthompsa			return (error);
1324203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1325203134Sthompsa			break;
1326203134Sthompsa	}
1327203134Sthompsa	if (ntries == 100)
1328209917Sthompsa		return (ETIMEDOUT);
1329203134Sthompsa
1330203134Sthompsa	*val = tmp & 0xff;
1331209917Sthompsa	return (0);
1332203134Sthompsa}
1333203134Sthompsa
1334203134Sthompsastatic int
1335203134Sthompsarun_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1336203134Sthompsa{
1337203134Sthompsa	uint32_t tmp;
1338203134Sthompsa	int error, ntries;
1339203134Sthompsa
1340203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1341203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1342209917Sthompsa			return (error);
1343203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1344203134Sthompsa			break;
1345203134Sthompsa	}
1346203134Sthompsa	if (ntries == 10)
1347209917Sthompsa		return (ETIMEDOUT);
1348203134Sthompsa
1349203134Sthompsa	tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1350209917Sthompsa	return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1351203134Sthompsa}
1352203134Sthompsa
1353203134Sthompsastatic int
1354203134Sthompsarun_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1355203134Sthompsa{
1356203134Sthompsa	uint32_t tmp;
1357203134Sthompsa	int ntries, error;
1358203134Sthompsa
1359203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1360203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1361209917Sthompsa			return (error);
1362203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1363203134Sthompsa			break;
1364203134Sthompsa	}
1365203134Sthompsa	if (ntries == 10)
1366209917Sthompsa		return (ETIMEDOUT);
1367203134Sthompsa
1368203134Sthompsa	tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1369203134Sthompsa	if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1370209917Sthompsa		return (error);
1371203134Sthompsa
1372203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1373203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1374209917Sthompsa			return (error);
1375203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1376203134Sthompsa			break;
1377203134Sthompsa	}
1378203134Sthompsa	if (ntries == 10)
1379209917Sthompsa		return (ETIMEDOUT);
1380203134Sthompsa
1381203134Sthompsa	*val = tmp & 0xff;
1382209917Sthompsa	return (0);
1383203134Sthompsa}
1384203134Sthompsa
1385203134Sthompsastatic int
1386203134Sthompsarun_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1387203134Sthompsa{
1388203134Sthompsa	uint32_t tmp;
1389203134Sthompsa	int ntries, error;
1390203134Sthompsa
1391203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1392203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1393209917Sthompsa			return (error);
1394203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1395203134Sthompsa			break;
1396203134Sthompsa	}
1397203134Sthompsa	if (ntries == 10)
1398209917Sthompsa		return (ETIMEDOUT);
1399203134Sthompsa
1400203134Sthompsa	tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1401209917Sthompsa	return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1402203134Sthompsa}
1403203134Sthompsa
1404203134Sthompsa/*
1405203134Sthompsa * Send a command to the 8051 microcontroller unit.
1406203134Sthompsa */
1407203134Sthompsastatic int
1408203134Sthompsarun_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1409203134Sthompsa{
1410203134Sthompsa	uint32_t tmp;
1411203134Sthompsa	int error, ntries;
1412203134Sthompsa
1413203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1414203134Sthompsa		if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1415203134Sthompsa			return error;
1416203134Sthompsa		if (!(tmp & RT2860_H2M_BUSY))
1417203134Sthompsa			break;
1418203134Sthompsa	}
1419203134Sthompsa	if (ntries == 100)
1420203134Sthompsa		return ETIMEDOUT;
1421203134Sthompsa
1422203134Sthompsa	tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1423203134Sthompsa	if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1424203134Sthompsa		error = run_write(sc, RT2860_HOST_CMD, cmd);
1425209917Sthompsa	return (error);
1426203134Sthompsa}
1427203134Sthompsa
1428203134Sthompsa/*
1429203134Sthompsa * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1430203134Sthompsa * Used to adjust per-rate Tx power registers.
1431203134Sthompsa */
1432203134Sthompsastatic __inline uint32_t
1433203134Sthompsab4inc(uint32_t b32, int8_t delta)
1434203134Sthompsa{
1435203134Sthompsa	int8_t i, b4;
1436203134Sthompsa
1437203134Sthompsa	for (i = 0; i < 8; i++) {
1438203134Sthompsa		b4 = b32 & 0xf;
1439203134Sthompsa		b4 += delta;
1440203134Sthompsa		if (b4 < 0)
1441203134Sthompsa			b4 = 0;
1442203134Sthompsa		else if (b4 > 0xf)
1443203134Sthompsa			b4 = 0xf;
1444203134Sthompsa		b32 = b32 >> 4 | b4 << 28;
1445203134Sthompsa	}
1446209917Sthompsa	return (b32);
1447203134Sthompsa}
1448203134Sthompsa
1449203134Sthompsastatic const char *
1450257955Skevlorun_get_rf(uint16_t rev)
1451203134Sthompsa{
1452203134Sthompsa	switch (rev) {
1453203134Sthompsa	case RT2860_RF_2820:	return "RT2820";
1454203134Sthompsa	case RT2860_RF_2850:	return "RT2850";
1455203134Sthompsa	case RT2860_RF_2720:	return "RT2720";
1456203134Sthompsa	case RT2860_RF_2750:	return "RT2750";
1457203134Sthompsa	case RT3070_RF_3020:	return "RT3020";
1458203134Sthompsa	case RT3070_RF_2020:	return "RT2020";
1459203134Sthompsa	case RT3070_RF_3021:	return "RT3021";
1460203134Sthompsa	case RT3070_RF_3022:	return "RT3022";
1461203134Sthompsa	case RT3070_RF_3052:	return "RT3052";
1462257955Skevlo	case RT5390_RF_5370:	return "RT5370";
1463257955Skevlo	case RT5390_RF_5372:	return "RT5372";
1464203134Sthompsa	}
1465209917Sthompsa	return ("unknown");
1466203134Sthompsa}
1467203134Sthompsa
1468203134Sthompsaint
1469203134Sthompsarun_read_eeprom(struct run_softc *sc)
1470203134Sthompsa{
1471203134Sthompsa	int8_t delta_2ghz, delta_5ghz;
1472203134Sthompsa	uint32_t tmp;
1473203134Sthompsa	uint16_t val;
1474203134Sthompsa	int ridx, ant, i;
1475203134Sthompsa
1476203134Sthompsa	/* check whether the ROM is eFUSE ROM or EEPROM */
1477203134Sthompsa	sc->sc_srom_read = run_eeprom_read_2;
1478205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1479203134Sthompsa		run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1480203134Sthompsa		DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1481203134Sthompsa		if (tmp & RT3070_SEL_EFUSE)
1482203134Sthompsa			sc->sc_srom_read = run_efuse_read_2;
1483203134Sthompsa	}
1484203134Sthompsa
1485203134Sthompsa	/* read ROM version */
1486203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1487203134Sthompsa	DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1488203134Sthompsa
1489203134Sthompsa	/* read MAC address */
1490203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1491203134Sthompsa	sc->sc_bssid[0] = val & 0xff;
1492203134Sthompsa	sc->sc_bssid[1] = val >> 8;
1493203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1494203134Sthompsa	sc->sc_bssid[2] = val & 0xff;
1495203134Sthompsa	sc->sc_bssid[3] = val >> 8;
1496203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1497203134Sthompsa	sc->sc_bssid[4] = val & 0xff;
1498203134Sthompsa	sc->sc_bssid[5] = val >> 8;
1499203134Sthompsa
1500257955Skevlo	if (sc->mac_ver < 0x5390) {
1501257955Skevlo		/* read vender BBP settings */
1502205042Sthompsa		for (i = 0; i < 10; i++) {
1503257955Skevlo			run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1504257955Skevlo			sc->bbp[i].val = val & 0xff;
1505257955Skevlo			sc->bbp[i].reg = val >> 8;
1506257955Skevlo			DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg,
1507257955Skevlo			    sc->bbp[i].val);
1508205042Sthompsa		}
1509257955Skevlo		if (sc->mac_ver >= 0x3071) {
1510257955Skevlo			/* read vendor RF settings */
1511257955Skevlo			for (i = 0; i < 10; i++) {
1512257955Skevlo				run_srom_read(sc, RT3071_EEPROM_RF_BASE + i,
1513257955Skevlo				   &val);
1514257955Skevlo				sc->rf[i].val = val & 0xff;
1515257955Skevlo				sc->rf[i].reg = val >> 8;
1516257955Skevlo				DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1517257955Skevlo				    sc->rf[i].val);
1518257955Skevlo			}
1519257955Skevlo		}
1520205042Sthompsa	}
1521203134Sthompsa
1522203134Sthompsa	/* read RF frequency offset from EEPROM */
1523203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1524203134Sthompsa	sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1525203134Sthompsa	DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1526203134Sthompsa
1527205042Sthompsa	if (val >> 8 != 0xff) {
1528203134Sthompsa		/* read LEDs operating mode */
1529205042Sthompsa		sc->leds = val >> 8;
1530203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1531203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1532203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1533203134Sthompsa	} else {
1534203134Sthompsa		/* broken EEPROM, use default settings */
1535203134Sthompsa		sc->leds = 0x01;
1536203134Sthompsa		sc->led[0] = 0x5555;
1537203134Sthompsa		sc->led[1] = 0x2221;
1538203134Sthompsa		sc->led[2] = 0x5627;	/* differs from RT2860 */
1539203134Sthompsa	}
1540203134Sthompsa	DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1541203134Sthompsa	    sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1542203134Sthompsa
1543203134Sthompsa	/* read RF information */
1544257955Skevlo	if (sc->mac_ver >= 0x5390)
1545257955Skevlo		run_srom_read(sc, 0x00, &val);
1546257955Skevlo	else
1547257955Skevlo		run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1548257955Skevlo
1549203134Sthompsa	if (val == 0xffff) {
1550203134Sthompsa		DPRINTF("invalid EEPROM antenna info, using default\n");
1551205042Sthompsa		if (sc->mac_ver == 0x3572) {
1552205042Sthompsa			/* default to RF3052 2T2R */
1553205042Sthompsa			sc->rf_rev = RT3070_RF_3052;
1554205042Sthompsa			sc->ntxchains = 2;
1555205042Sthompsa			sc->nrxchains = 2;
1556205042Sthompsa		} else if (sc->mac_ver >= 0x3070) {
1557203134Sthompsa			/* default to RF3020 1T1R */
1558203134Sthompsa			sc->rf_rev = RT3070_RF_3020;
1559203134Sthompsa			sc->ntxchains = 1;
1560203134Sthompsa			sc->nrxchains = 1;
1561203134Sthompsa		} else {
1562203134Sthompsa			/* default to RF2820 1T2R */
1563203134Sthompsa			sc->rf_rev = RT2860_RF_2820;
1564203134Sthompsa			sc->ntxchains = 1;
1565203134Sthompsa			sc->nrxchains = 2;
1566203134Sthompsa		}
1567203134Sthompsa	} else {
1568257955Skevlo		if (sc->mac_ver >= 0x5390) {
1569257955Skevlo			sc->rf_rev = val;
1570257955Skevlo			run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1571257955Skevlo		} else
1572257955Skevlo			sc->rf_rev = (val >> 8) & 0xf;
1573203134Sthompsa		sc->ntxchains = (val >> 4) & 0xf;
1574203134Sthompsa		sc->nrxchains = val & 0xf;
1575203134Sthompsa	}
1576257955Skevlo	DPRINTF("EEPROM RF rev=0x%04x chains=%dT%dR\n",
1577203134Sthompsa	    sc->rf_rev, sc->ntxchains, sc->nrxchains);
1578203134Sthompsa
1579208019Sthompsa	/* check if RF supports automatic Tx access gain control */
1580203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1581203134Sthompsa	DPRINTF("EEPROM CFG 0x%04x\n", val);
1582205042Sthompsa	/* check if driver should patch the DAC issue */
1583205042Sthompsa	if ((val >> 8) != 0xff)
1584205042Sthompsa		sc->patch_dac = (val >> 15) & 1;
1585203134Sthompsa	if ((val & 0xff) != 0xff) {
1586203134Sthompsa		sc->ext_5ghz_lna = (val >> 3) & 1;
1587203134Sthompsa		sc->ext_2ghz_lna = (val >> 2) & 1;
1588205042Sthompsa		/* check if RF supports automatic Tx access gain control */
1589203134Sthompsa		sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1590205042Sthompsa		/* check if we have a hardware radio switch */
1591205042Sthompsa		sc->rfswitch = val & 1;
1592203134Sthompsa	}
1593203134Sthompsa
1594203134Sthompsa	/* read power settings for 2GHz channels */
1595203134Sthompsa	for (i = 0; i < 14; i += 2) {
1596203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1597203134Sthompsa		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1598203134Sthompsa		sc->txpow1[i + 1] = (int8_t)(val >> 8);
1599203134Sthompsa
1600257955Skevlo		if (sc->mac_ver != 0x5390) {
1601257955Skevlo			run_srom_read(sc,
1602257955Skevlo			    RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1603257955Skevlo			sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1604257955Skevlo			sc->txpow2[i + 1] = (int8_t)(val >> 8);
1605257955Skevlo		}
1606203134Sthompsa	}
1607203134Sthompsa	/* fix broken Tx power entries */
1608203134Sthompsa	for (i = 0; i < 14; i++) {
1609257955Skevlo		if (sc->mac_ver >= 0x5390) {
1610257955Skevlo			if (sc->txpow1[i] < 0 || sc->txpow1[i] > 27)
1611257955Skevlo				sc->txpow1[i] = 5;
1612257955Skevlo		} else {
1613257955Skevlo			if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1614257955Skevlo				sc->txpow1[i] = 5;
1615257955Skevlo		}
1616257955Skevlo		if (sc->mac_ver > 0x5390) {
1617257955Skevlo			if (sc->txpow2[i] < 0 || sc->txpow2[i] > 27)
1618257955Skevlo				sc->txpow2[i] = 5;
1619257955Skevlo		} else if (sc->mac_ver < 0x5390) {
1620257955Skevlo			if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1621257955Skevlo				sc->txpow2[i] = 5;
1622257955Skevlo		}
1623203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1624203134Sthompsa		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1625203134Sthompsa	}
1626203134Sthompsa	/* read power settings for 5GHz channels */
1627205042Sthompsa	for (i = 0; i < 40; i += 2) {
1628203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1629203134Sthompsa		sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1630203134Sthompsa		sc->txpow1[i + 15] = (int8_t)(val >> 8);
1631203134Sthompsa
1632203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1633203134Sthompsa		sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1634203134Sthompsa		sc->txpow2[i + 15] = (int8_t)(val >> 8);
1635203134Sthompsa	}
1636203134Sthompsa	/* fix broken Tx power entries */
1637205042Sthompsa	for (i = 0; i < 40; i++) {
1638203134Sthompsa		if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1639203134Sthompsa			sc->txpow1[14 + i] = 5;
1640203134Sthompsa		if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1641203134Sthompsa			sc->txpow2[14 + i] = 5;
1642203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1643203134Sthompsa		    rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1644203134Sthompsa		    sc->txpow2[14 + i]);
1645203134Sthompsa	}
1646203134Sthompsa
1647203134Sthompsa	/* read Tx power compensation for each Tx rate */
1648203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1649203134Sthompsa	delta_2ghz = delta_5ghz = 0;
1650203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1651203134Sthompsa		delta_2ghz = val & 0xf;
1652203134Sthompsa		if (!(val & 0x40))	/* negative number */
1653203134Sthompsa			delta_2ghz = -delta_2ghz;
1654203134Sthompsa	}
1655203134Sthompsa	val >>= 8;
1656203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1657203134Sthompsa		delta_5ghz = val & 0xf;
1658203134Sthompsa		if (!(val & 0x40))	/* negative number */
1659203134Sthompsa			delta_5ghz = -delta_5ghz;
1660203134Sthompsa	}
1661203134Sthompsa	DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1662203134Sthompsa	    delta_2ghz, delta_5ghz);
1663203134Sthompsa
1664203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
1665203134Sthompsa		uint32_t reg;
1666203134Sthompsa
1667208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1668208019Sthompsa		reg = val;
1669208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1670208019Sthompsa		reg |= (uint32_t)val << 16;
1671203134Sthompsa
1672203134Sthompsa		sc->txpow20mhz[ridx] = reg;
1673203134Sthompsa		sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1674203134Sthompsa		sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1675203134Sthompsa
1676203134Sthompsa		DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1677203134Sthompsa		    "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1678203134Sthompsa		    sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1679203134Sthompsa	}
1680203134Sthompsa
1681203134Sthompsa	/* read RSSI offsets and LNA gains from EEPROM */
1682203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1683203134Sthompsa	sc->rssi_2ghz[0] = val & 0xff;	/* Ant A */
1684203134Sthompsa	sc->rssi_2ghz[1] = val >> 8;	/* Ant B */
1685203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1686205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1687205042Sthompsa		/*
1688205042Sthompsa		 * On RT3070 chips (limited to 2 Rx chains), this ROM
1689205042Sthompsa		 * field contains the Tx mixer gain for the 2GHz band.
1690205042Sthompsa		 */
1691205042Sthompsa		if ((val & 0xff) != 0xff)
1692205042Sthompsa			sc->txmixgain_2ghz = val & 0x7;
1693205042Sthompsa		DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1694205042Sthompsa	} else
1695205042Sthompsa		sc->rssi_2ghz[2] = val & 0xff;	/* Ant C */
1696203134Sthompsa	sc->lna[2] = val >> 8;		/* channel group 2 */
1697203134Sthompsa
1698203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1699203134Sthompsa	sc->rssi_5ghz[0] = val & 0xff;	/* Ant A */
1700203134Sthompsa	sc->rssi_5ghz[1] = val >> 8;	/* Ant B */
1701203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1702205042Sthompsa	if (sc->mac_ver == 0x3572) {
1703205042Sthompsa		/*
1704205042Sthompsa		 * On RT3572 chips (limited to 2 Rx chains), this ROM
1705205042Sthompsa		 * field contains the Tx mixer gain for the 5GHz band.
1706205042Sthompsa		 */
1707205042Sthompsa		if ((val & 0xff) != 0xff)
1708205042Sthompsa			sc->txmixgain_5ghz = val & 0x7;
1709205042Sthompsa		DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1710205042Sthompsa	} else
1711205042Sthompsa		sc->rssi_5ghz[2] = val & 0xff;	/* Ant C */
1712203134Sthompsa	sc->lna[3] = val >> 8;		/* channel group 3 */
1713203134Sthompsa
1714203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1715203134Sthompsa	sc->lna[0] = val & 0xff;	/* channel group 0 */
1716203134Sthompsa	sc->lna[1] = val >> 8;		/* channel group 1 */
1717203134Sthompsa
1718203134Sthompsa	/* fix broken 5GHz LNA entries */
1719203134Sthompsa	if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1720203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 2);
1721203134Sthompsa		sc->lna[2] = sc->lna[1];
1722203134Sthompsa	}
1723203134Sthompsa	if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1724203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 3);
1725203134Sthompsa		sc->lna[3] = sc->lna[1];
1726203134Sthompsa	}
1727203134Sthompsa
1728203134Sthompsa	/* fix broken RSSI offset entries */
1729203134Sthompsa	for (ant = 0; ant < 3; ant++) {
1730203134Sthompsa		if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1731203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1732203134Sthompsa			    ant + 1, sc->rssi_2ghz[ant]);
1733203134Sthompsa			sc->rssi_2ghz[ant] = 0;
1734203134Sthompsa		}
1735203134Sthompsa		if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1736203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1737203134Sthompsa			    ant + 1, sc->rssi_5ghz[ant]);
1738203134Sthompsa			sc->rssi_5ghz[ant] = 0;
1739203134Sthompsa		}
1740203134Sthompsa	}
1741209917Sthompsa	return (0);
1742203134Sthompsa}
1743203134Sthompsa
1744218676Shselaskystatic struct ieee80211_node *
1745203134Sthompsarun_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1746203134Sthompsa{
1747203134Sthompsa	return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1748203134Sthompsa}
1749203134Sthompsa
1750203134Sthompsastatic int
1751203134Sthompsarun_media_change(struct ifnet *ifp)
1752203134Sthompsa{
1753208019Sthompsa	struct ieee80211vap *vap = ifp->if_softc;
1754208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1755203134Sthompsa	const struct ieee80211_txparam *tp;
1756208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1757203134Sthompsa	uint8_t rate, ridx;
1758203134Sthompsa	int error;
1759203134Sthompsa
1760203134Sthompsa	RUN_LOCK(sc);
1761203134Sthompsa
1762203134Sthompsa	error = ieee80211_media_change(ifp);
1763209917Sthompsa	if (error != ENETRESET) {
1764203134Sthompsa		RUN_UNLOCK(sc);
1765209917Sthompsa		return (error);
1766208019Sthompsa	}
1767203134Sthompsa
1768203134Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1769203134Sthompsa	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1770212127Sthompsa		struct ieee80211_node *ni;
1771212127Sthompsa		struct run_node	*rn;
1772212127Sthompsa
1773203134Sthompsa		rate = ic->ic_sup_rates[ic->ic_curmode].
1774203134Sthompsa		    rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1775203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1776203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
1777203134Sthompsa				break;
1778212127Sthompsa		ni = ieee80211_ref_node(vap->iv_bss);
1779212127Sthompsa		rn = (struct run_node *)ni;
1780208019Sthompsa		rn->fix_ridx = ridx;
1781208019Sthompsa		DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1782212127Sthompsa		ieee80211_free_node(ni);
1783203134Sthompsa	}
1784203134Sthompsa
1785208019Sthompsa#if 0
1786203134Sthompsa	if ((ifp->if_flags & IFF_UP) &&
1787203134Sthompsa	    (ifp->if_drv_flags &  IFF_DRV_RUNNING)){
1788203134Sthompsa		run_init_locked(sc);
1789203134Sthompsa	}
1790208019Sthompsa#endif
1791203134Sthompsa
1792203134Sthompsa	RUN_UNLOCK(sc);
1793203134Sthompsa
1794209917Sthompsa	return (0);
1795203134Sthompsa}
1796203134Sthompsa
1797203134Sthompsastatic int
1798203134Sthompsarun_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1799203134Sthompsa{
1800203134Sthompsa	const struct ieee80211_txparam *tp;
1801203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1802203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1803203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
1804203134Sthompsa	enum ieee80211_state ostate;
1805208019Sthompsa	uint32_t sta[3];
1806203134Sthompsa	uint32_t tmp;
1807208019Sthompsa	uint8_t ratectl;
1808208019Sthompsa	uint8_t restart_ratectl = 0;
1809208019Sthompsa	uint8_t bid = 1 << rvp->rvp_id;
1810203134Sthompsa
1811203134Sthompsa	ostate = vap->iv_state;
1812203134Sthompsa	DPRINTF("%s -> %s\n",
1813203134Sthompsa		ieee80211_state_name[ostate],
1814203134Sthompsa		ieee80211_state_name[nstate]);
1815203134Sthompsa
1816203134Sthompsa	IEEE80211_UNLOCK(ic);
1817203134Sthompsa	RUN_LOCK(sc);
1818203134Sthompsa
1819208019Sthompsa	ratectl = sc->ratectl_run; /* remember current state */
1820208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
1821208019Sthompsa	usb_callout_stop(&sc->ratectl_ch);
1822203134Sthompsa
1823203134Sthompsa	if (ostate == IEEE80211_S_RUN) {
1824203134Sthompsa		/* turn link LED off */
1825203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO);
1826203134Sthompsa	}
1827203134Sthompsa
1828203134Sthompsa	switch (nstate) {
1829203134Sthompsa	case IEEE80211_S_INIT:
1830208019Sthompsa		restart_ratectl = 1;
1831208019Sthompsa
1832208019Sthompsa		if (ostate != IEEE80211_S_RUN)
1833208019Sthompsa			break;
1834208019Sthompsa
1835208019Sthompsa		ratectl &= ~bid;
1836208019Sthompsa		sc->runbmap &= ~bid;
1837208019Sthompsa
1838208019Sthompsa		/* abort TSF synchronization if there is no vap running */
1839209917Sthompsa		if (--sc->running == 0) {
1840203134Sthompsa			run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1841203134Sthompsa			run_write(sc, RT2860_BCN_TIME_CFG,
1842203134Sthompsa			    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1843203134Sthompsa			    RT2860_TBTT_TIMER_EN));
1844203134Sthompsa		}
1845203134Sthompsa		break;
1846203134Sthompsa
1847203134Sthompsa	case IEEE80211_S_RUN:
1848209917Sthompsa		if (!(sc->runbmap & bid)) {
1849208019Sthompsa			if(sc->running++)
1850208019Sthompsa				restart_ratectl = 1;
1851208019Sthompsa			sc->runbmap |= bid;
1852208019Sthompsa		}
1853203134Sthompsa
1854218492Sbschmidt		m_freem(rvp->beacon_mbuf);
1855218492Sbschmidt		rvp->beacon_mbuf = NULL;
1856218492Sbschmidt
1857209917Sthompsa		switch (vap->iv_opmode) {
1858208019Sthompsa		case IEEE80211_M_HOSTAP:
1859208019Sthompsa		case IEEE80211_M_MBSS:
1860208019Sthompsa			sc->ap_running |= bid;
1861208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1862208019Sthompsa			run_update_beacon_cb(vap);
1863208019Sthompsa			break;
1864208019Sthompsa		case IEEE80211_M_IBSS:
1865208019Sthompsa			sc->adhoc_running |= bid;
1866209917Sthompsa			if (!sc->ap_running)
1867208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1868208019Sthompsa			run_update_beacon_cb(vap);
1869208019Sthompsa			break;
1870208019Sthompsa		case IEEE80211_M_STA:
1871208019Sthompsa			sc->sta_running |= bid;
1872209917Sthompsa			if (!sc->ap_running && !sc->adhoc_running)
1873208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1874208019Sthompsa
1875208019Sthompsa			/* read statistic counters (clear on read) */
1876208019Sthompsa			run_read_region_1(sc, RT2860_TX_STA_CNT0,
1877208019Sthompsa			    (uint8_t *)sta, sizeof sta);
1878208019Sthompsa
1879208019Sthompsa			break;
1880208019Sthompsa		default:
1881208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1882208019Sthompsa			break;
1883208019Sthompsa		}
1884208019Sthompsa
1885203134Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1886212127Sthompsa			struct ieee80211_node *ni;
1887212127Sthompsa
1888236439Shselasky			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
1889236439Shselasky				RUN_UNLOCK(sc);
1890236439Shselasky				IEEE80211_LOCK(ic);
1891236439Shselasky				return (-1);
1892236439Shselasky			}
1893203134Sthompsa			run_updateslot(ic->ic_ifp);
1894203134Sthompsa			run_enable_mrr(sc);
1895203134Sthompsa			run_set_txpreamble(sc);
1896203134Sthompsa			run_set_basicrates(sc);
1897212127Sthompsa			ni = ieee80211_ref_node(vap->iv_bss);
1898203134Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1899203134Sthompsa			run_set_bssid(sc, ni->ni_bssid);
1900212127Sthompsa			ieee80211_free_node(ni);
1901208019Sthompsa			run_enable_tsf_sync(sc);
1902203134Sthompsa
1903208019Sthompsa			/* enable automatic rate adaptation */
1904208019Sthompsa			tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1905208019Sthompsa			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1906208019Sthompsa				ratectl |= bid;
1907203134Sthompsa		}
1908203134Sthompsa
1909203134Sthompsa		/* turn link LED on */
1910203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO |
1911208019Sthompsa		    (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1912203134Sthompsa		     RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1913203134Sthompsa
1914203134Sthompsa		break;
1915203134Sthompsa	default:
1916203134Sthompsa		DPRINTFN(6, "undefined case\n");
1917203134Sthompsa		break;
1918203134Sthompsa	}
1919203134Sthompsa
1920208019Sthompsa	/* restart amrr for running VAPs */
1921209917Sthompsa	if ((sc->ratectl_run = ratectl) && restart_ratectl)
1922208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1923208019Sthompsa
1924203134Sthompsa	RUN_UNLOCK(sc);
1925203134Sthompsa	IEEE80211_LOCK(ic);
1926203134Sthompsa
1927203134Sthompsa	return(rvp->newstate(vap, nstate, arg));
1928203134Sthompsa}
1929203134Sthompsa
1930203134Sthompsa/* ARGSUSED */
1931203134Sthompsastatic void
1932208019Sthompsarun_wme_update_cb(void *arg)
1933203134Sthompsa{
1934203134Sthompsa	struct ieee80211com *ic = arg;
1935203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1936203134Sthompsa	struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1937203134Sthompsa	int aci, error = 0;
1938203134Sthompsa
1939208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1940203134Sthompsa
1941203134Sthompsa	/* update MAC TX configuration registers */
1942203134Sthompsa	for (aci = 0; aci < WME_NUM_AC; aci++) {
1943203134Sthompsa		error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1944203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmax << 16 |
1945203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmin << 12 |
1946203134Sthompsa		    wmesp->wme_params[aci].wmep_aifsn  <<  8 |
1947203134Sthompsa		    wmesp->wme_params[aci].wmep_txopLimit);
1948209917Sthompsa		if (error) goto err;
1949203134Sthompsa	}
1950203134Sthompsa
1951203134Sthompsa	/* update SCH/DMA registers too */
1952203134Sthompsa	error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1953203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_aifsn  << 12 |
1954203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_aifsn  <<  8 |
1955203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_aifsn  <<  4 |
1956203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1957209917Sthompsa	if (error) goto err;
1958203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1959203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1960203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmin <<  8 |
1961203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmin <<  4 |
1962203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1963209917Sthompsa	if (error) goto err;
1964203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1965203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1966203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmax <<  8 |
1967203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmax <<  4 |
1968203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1969209917Sthompsa	if (error) goto err;
1970203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1971203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1972203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1973209917Sthompsa	if (error) goto err;
1974203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1975203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1976203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1977203134Sthompsa
1978203134Sthompsaerr:
1979209917Sthompsa	if (error)
1980203134Sthompsa		DPRINTF("WME update failed\n");
1981203134Sthompsa
1982203134Sthompsa	return;
1983203134Sthompsa}
1984203134Sthompsa
1985208019Sthompsastatic int
1986208019Sthompsarun_wme_update(struct ieee80211com *ic)
1987208019Sthompsa{
1988208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1989208019Sthompsa
1990208019Sthompsa	/* sometime called wothout lock */
1991209917Sthompsa	if (mtx_owned(&ic->ic_comlock.mtx)) {
1992208019Sthompsa		uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1993208019Sthompsa		DPRINTF("cmdq_store=%d\n", i);
1994208019Sthompsa		sc->cmdq[i].func = run_wme_update_cb;
1995208019Sthompsa		sc->cmdq[i].arg0 = ic;
1996208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
1997209918Sthompsa		return (0);
1998208019Sthompsa	}
1999208019Sthompsa
2000208019Sthompsa	RUN_LOCK(sc);
2001208019Sthompsa	run_wme_update_cb(ic);
2002208019Sthompsa	RUN_UNLOCK(sc);
2003208019Sthompsa
2004208019Sthompsa	/* return whatever, upper layer desn't care anyway */
2005208019Sthompsa	return (0);
2006208019Sthompsa}
2007208019Sthompsa
2008203134Sthompsastatic void
2009203134Sthompsarun_key_update_begin(struct ieee80211vap *vap)
2010203134Sthompsa{
2011203134Sthompsa	/*
2012208019Sthompsa	 * To avoid out-of-order events, both run_key_set() and
2013208019Sthompsa	 * _delete() are deferred and handled by run_cmdq_cb().
2014208019Sthompsa	 * So, there is nothing we need to do here.
2015203134Sthompsa	 */
2016203134Sthompsa}
2017203134Sthompsa
2018203134Sthompsastatic void
2019203134Sthompsarun_key_update_end(struct ieee80211vap *vap)
2020203134Sthompsa{
2021203134Sthompsa	/* null */
2022203134Sthompsa}
2023203134Sthompsa
2024208019Sthompsastatic void
2025208019Sthompsarun_key_set_cb(void *arg)
2026203134Sthompsa{
2027208019Sthompsa	struct run_cmdq *cmdq = arg;
2028208019Sthompsa	struct ieee80211vap *vap = cmdq->arg1;
2029208019Sthompsa	struct ieee80211_key *k = cmdq->k;
2030203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2031208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2032203134Sthompsa	struct ieee80211_node *ni;
2033203134Sthompsa	uint32_t attr;
2034203134Sthompsa	uint16_t base, associd;
2035209144Sthompsa	uint8_t mode, wcid, iv[8];
2036203134Sthompsa
2037208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2038203134Sthompsa
2039209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2040208019Sthompsa		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
2041209144Sthompsa	else
2042203134Sthompsa		ni = vap->iv_bss;
2043208019Sthompsa	associd = (ni != NULL) ? ni->ni_associd : 0;
2044203134Sthompsa
2045203134Sthompsa	/* map net80211 cipher to RT2860 security mode */
2046203134Sthompsa	switch (k->wk_cipher->ic_cipher) {
2047203134Sthompsa	case IEEE80211_CIPHER_WEP:
2048203134Sthompsa		if(k->wk_keylen < 8)
2049203134Sthompsa			mode = RT2860_MODE_WEP40;
2050203134Sthompsa		else
2051203134Sthompsa			mode = RT2860_MODE_WEP104;
2052203134Sthompsa		break;
2053203134Sthompsa	case IEEE80211_CIPHER_TKIP:
2054203134Sthompsa		mode = RT2860_MODE_TKIP;
2055203134Sthompsa		break;
2056203134Sthompsa	case IEEE80211_CIPHER_AES_CCM:
2057203134Sthompsa		mode = RT2860_MODE_AES_CCMP;
2058203134Sthompsa		break;
2059203134Sthompsa	default:
2060203134Sthompsa		DPRINTF("undefined case\n");
2061208019Sthompsa		return;
2062203134Sthompsa	}
2063203134Sthompsa
2064208019Sthompsa	DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2065203134Sthompsa	    associd, k->wk_keyix, mode,
2066208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2067208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2068208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2069203134Sthompsa
2070203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2071203134Sthompsa		wcid = 0;	/* NB: update WCID0 for group keys */
2072208019Sthompsa		base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2073203134Sthompsa	} else {
2074245047Shselasky		wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2075245047Shselasky		    1 : RUN_AID2WCID(associd);
2076203134Sthompsa		base = RT2860_PKEY(wcid);
2077203134Sthompsa	}
2078203134Sthompsa
2079203134Sthompsa	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2080203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, 16))
2081208019Sthompsa			return;
2082209144Sthompsa		if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8))	/* wk_txmic */
2083208019Sthompsa			return;
2084209144Sthompsa		if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8))	/* wk_rxmic */
2085208019Sthompsa			return;
2086203134Sthompsa	} else {
2087203134Sthompsa		/* roundup len to 16-bit: XXX fix write_region_1() instead */
2088203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2089208019Sthompsa			return;
2090203134Sthompsa	}
2091203134Sthompsa
2092203134Sthompsa	if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2093203134Sthompsa	    (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2094203134Sthompsa		/* set initial packet number in IV+EIV */
2095209917Sthompsa		if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2096203134Sthompsa			memset(iv, 0, sizeof iv);
2097208019Sthompsa			iv[3] = vap->iv_def_txkey << 6;
2098203134Sthompsa		} else {
2099203134Sthompsa			if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2100203134Sthompsa				iv[0] = k->wk_keytsc >> 8;
2101203134Sthompsa				iv[1] = (iv[0] | 0x20) & 0x7f;
2102203134Sthompsa				iv[2] = k->wk_keytsc;
2103203134Sthompsa			} else /* CCMP */ {
2104203134Sthompsa				iv[0] = k->wk_keytsc;
2105203134Sthompsa				iv[1] = k->wk_keytsc >> 8;
2106203134Sthompsa				iv[2] = 0;
2107203134Sthompsa			}
2108203134Sthompsa			iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2109203134Sthompsa			iv[4] = k->wk_keytsc >> 16;
2110203134Sthompsa			iv[5] = k->wk_keytsc >> 24;
2111203134Sthompsa			iv[6] = k->wk_keytsc >> 32;
2112203134Sthompsa			iv[7] = k->wk_keytsc >> 40;
2113203134Sthompsa		}
2114209917Sthompsa		if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2115208019Sthompsa			return;
2116203134Sthompsa	}
2117203134Sthompsa
2118203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2119203134Sthompsa		/* install group key */
2120209917Sthompsa		if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2121208019Sthompsa			return;
2122203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2123203134Sthompsa		attr |= mode << (k->wk_keyix * 4);
2124209917Sthompsa		if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2125208019Sthompsa			return;
2126203134Sthompsa	} else {
2127203134Sthompsa		/* install pairwise key */
2128209917Sthompsa		if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2129208019Sthompsa			return;
2130203134Sthompsa		attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2131209917Sthompsa		if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2132208019Sthompsa			return;
2133203134Sthompsa	}
2134203134Sthompsa
2135203134Sthompsa	/* TODO create a pass-thru key entry? */
2136203134Sthompsa
2137208019Sthompsa	/* need wcid to delete the right key later */
2138208019Sthompsa	k->wk_pad = wcid;
2139203134Sthompsa}
2140203134Sthompsa
2141203134Sthompsa/*
2142208019Sthompsa * Don't have to be deferred, but in order to keep order of
2143208019Sthompsa * execution, i.e. with run_key_delete(), defer this and let
2144208019Sthompsa * run_cmdq_cb() maintain the order.
2145208019Sthompsa *
2146203134Sthompsa * return 0 on error
2147203134Sthompsa */
2148203134Sthompsastatic int
2149208019Sthompsarun_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2150208019Sthompsa		const uint8_t mac[IEEE80211_ADDR_LEN])
2151203134Sthompsa{
2152203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2153203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2154208019Sthompsa	uint32_t i;
2155208019Sthompsa
2156208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2157208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2158208019Sthompsa	sc->cmdq[i].func = run_key_set_cb;
2159208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2160208019Sthompsa	sc->cmdq[i].arg1 = vap;
2161208019Sthompsa	sc->cmdq[i].k = k;
2162208019Sthompsa	IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2163208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2164208019Sthompsa
2165209144Sthompsa	/*
2166209144Sthompsa	 * To make sure key will be set when hostapd
2167209144Sthompsa	 * calls iv_key_set() before if_init().
2168209144Sthompsa	 */
2169209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2170209144Sthompsa		RUN_LOCK(sc);
2171209144Sthompsa		sc->cmdq_key_set = RUN_CMDQ_GO;
2172209144Sthompsa		RUN_UNLOCK(sc);
2173209144Sthompsa	}
2174209144Sthompsa
2175209917Sthompsa	return (1);
2176208019Sthompsa}
2177208019Sthompsa
2178208019Sthompsa/*
2179208019Sthompsa * If wlan is destroyed without being brought down i.e. without
2180208019Sthompsa * wlan down or wpa_cli terminate, this function is called after
2181208019Sthompsa * vap is gone. Don't refer it.
2182208019Sthompsa */
2183208019Sthompsastatic void
2184208019Sthompsarun_key_delete_cb(void *arg)
2185208019Sthompsa{
2186208019Sthompsa	struct run_cmdq *cmdq = arg;
2187208019Sthompsa	struct run_softc *sc = cmdq->arg1;
2188208019Sthompsa	struct ieee80211_key *k = &cmdq->key;
2189203134Sthompsa	uint32_t attr;
2190203134Sthompsa	uint8_t wcid;
2191203134Sthompsa
2192208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2193203134Sthompsa
2194203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2195203134Sthompsa		/* remove group key */
2196208019Sthompsa		DPRINTF("removing group key\n");
2197208019Sthompsa		run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2198203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2199208019Sthompsa		run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2200203134Sthompsa	} else {
2201203134Sthompsa		/* remove pairwise key */
2202208019Sthompsa		DPRINTF("removing key for wcid %x\n", k->wk_pad);
2203208019Sthompsa		/* matching wcid was written to wk_pad in run_key_set() */
2204208019Sthompsa		wcid = k->wk_pad;
2205208019Sthompsa		run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2206203134Sthompsa		attr &= ~0xf;
2207208019Sthompsa		run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2208208019Sthompsa		run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2209203134Sthompsa	}
2210203134Sthompsa
2211208019Sthompsa	k->wk_pad = 0;
2212203134Sthompsa}
2213203134Sthompsa
2214208019Sthompsa/*
2215208019Sthompsa * return 0 on error
2216208019Sthompsa */
2217208019Sthompsastatic int
2218208019Sthompsarun_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2219203134Sthompsa{
2220208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2221208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2222208019Sthompsa	struct ieee80211_key *k0;
2223208019Sthompsa	uint32_t i;
2224203134Sthompsa
2225208019Sthompsa	/*
2226208019Sthompsa	 * When called back, key might be gone. So, make a copy
2227208019Sthompsa	 * of some values need to delete keys before deferring.
2228208019Sthompsa	 * But, because of LOR with node lock, cannot use lock here.
2229208019Sthompsa	 * So, use atomic instead.
2230208019Sthompsa	 */
2231208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2232208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2233208019Sthompsa	sc->cmdq[i].func = run_key_delete_cb;
2234208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2235208019Sthompsa	sc->cmdq[i].arg1 = sc;
2236208019Sthompsa	k0 = &sc->cmdq[i].key;
2237208019Sthompsa	k0->wk_flags = k->wk_flags;
2238208019Sthompsa	k0->wk_keyix = k->wk_keyix;
2239208019Sthompsa	/* matching wcid was written to wk_pad in run_key_set() */
2240208019Sthompsa	k0->wk_pad = k->wk_pad;
2241208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2242208019Sthompsa	return (1);	/* return fake success */
2243203134Sthompsa
2244203134Sthompsa}
2245203134Sthompsa
2246203134Sthompsastatic void
2247206358Srpaulorun_ratectl_to(void *arg)
2248203134Sthompsa{
2249208019Sthompsa	struct run_softc *sc = arg;
2250203134Sthompsa
2251203134Sthompsa	/* do it in a process context, so it can go sleep */
2252208019Sthompsa	ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2253203134Sthompsa	/* next timeout will be rescheduled in the callback task */
2254203134Sthompsa}
2255203134Sthompsa
2256203134Sthompsa/* ARGSUSED */
2257203134Sthompsastatic void
2258206358Srpaulorun_ratectl_cb(void *arg, int pending)
2259203134Sthompsa{
2260208019Sthompsa	struct run_softc *sc = arg;
2261208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2262208019Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2263203134Sthompsa
2264209917Sthompsa	if (vap == NULL)
2265208019Sthompsa		return;
2266208019Sthompsa
2267209917Sthompsa	if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2268208019Sthompsa		run_iter_func(sc, vap->iv_bss);
2269203134Sthompsa	else {
2270203134Sthompsa		/*
2271203134Sthompsa		 * run_reset_livelock() doesn't do anything with AMRR,
2272203134Sthompsa		 * but Ralink wants us to call it every 1 sec. So, we
2273203134Sthompsa		 * piggyback here rather than creating another callout.
2274203134Sthompsa		 * Livelock may occur only in HOSTAP or IBSS mode
2275203134Sthompsa		 * (when h/w is sending beacons).
2276203134Sthompsa		 */
2277203134Sthompsa		RUN_LOCK(sc);
2278203134Sthompsa		run_reset_livelock(sc);
2279208019Sthompsa		/* just in case, there are some stats to drain */
2280208019Sthompsa		run_drain_fifo(sc);
2281203134Sthompsa		RUN_UNLOCK(sc);
2282208019Sthompsa		ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2283203134Sthompsa	}
2284203134Sthompsa
2285257712Shselasky	RUN_LOCK(sc);
2286208019Sthompsa	if(sc->ratectl_run != RUN_RATECTL_OFF)
2287208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2288257712Shselasky	RUN_UNLOCK(sc);
2289203134Sthompsa}
2290203134Sthompsa
2291203134Sthompsastatic void
2292208019Sthompsarun_drain_fifo(void *arg)
2293203134Sthompsa{
2294208019Sthompsa	struct run_softc *sc = arg;
2295208019Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2296208019Sthompsa	uint32_t stat;
2297218676Shselasky	uint16_t (*wstat)[3];
2298203134Sthompsa	uint8_t wcid, mcs, pid;
2299218676Shselasky	int8_t retry;
2300203134Sthompsa
2301208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2302203134Sthompsa
2303208019Sthompsa	for (;;) {
2304203134Sthompsa		/* drain Tx status FIFO (maxsize = 16) */
2305203134Sthompsa		run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2306208019Sthompsa		DPRINTFN(4, "tx stat 0x%08x\n", stat);
2307209917Sthompsa		if (!(stat & RT2860_TXQ_VLD))
2308208019Sthompsa			break;
2309203134Sthompsa
2310208019Sthompsa		wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2311203134Sthompsa
2312208019Sthompsa		/* if no ACK was requested, no feedback is available */
2313208019Sthompsa		if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2314208019Sthompsa		    wcid == 0)
2315208019Sthompsa			continue;
2316203134Sthompsa
2317218676Shselasky		/*
2318218676Shselasky		 * Even though each stat is Tx-complete-status like format,
2319218676Shselasky		 * the device can poll stats. Because there is no guarantee
2320218676Shselasky		 * that the referring node is still around when read the stats.
2321218676Shselasky		 * So that, if we use ieee80211_ratectl_tx_update(), we will
2322218676Shselasky		 * have hard time not to refer already freed node.
2323218676Shselasky		 *
2324218676Shselasky		 * To eliminate such page faults, we poll stats in softc.
2325218676Shselasky		 * Then, update the rates later with ieee80211_ratectl_tx_update().
2326218676Shselasky		 */
2327218676Shselasky		wstat = &(sc->wcid_stats[wcid]);
2328218676Shselasky		(*wstat)[RUN_TXCNT]++;
2329218676Shselasky		if (stat & RT2860_TXQ_OK)
2330218676Shselasky			(*wstat)[RUN_SUCCESS]++;
2331218676Shselasky		else
2332208019Sthompsa			ifp->if_oerrors++;
2333218676Shselasky		/*
2334218676Shselasky		 * Check if there were retries, ie if the Tx success rate is
2335218676Shselasky		 * different from the requested rate. Note that it works only
2336218676Shselasky		 * because we do not allow rate fallback from OFDM to CCK.
2337218676Shselasky		 */
2338218676Shselasky		mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2339218676Shselasky		pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2340218676Shselasky		if ((retry = pid -1 - mcs) > 0) {
2341218676Shselasky			(*wstat)[RUN_TXCNT] += retry;
2342218676Shselasky			(*wstat)[RUN_RETRY] += retry;
2343203134Sthompsa		}
2344208019Sthompsa	}
2345208019Sthompsa	DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2346208019Sthompsa
2347208019Sthompsa	sc->fifo_cnt = 0;
2348208019Sthompsa}
2349208019Sthompsa
2350208019Sthompsastatic void
2351208019Sthompsarun_iter_func(void *arg, struct ieee80211_node *ni)
2352208019Sthompsa{
2353208019Sthompsa	struct run_softc *sc = arg;
2354208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2355208019Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2356208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2357208019Sthompsa	struct run_node *rn = (void *)ni;
2358218676Shselasky	union run_stats sta[2];
2359218676Shselasky	uint16_t (*wstat)[3];
2360218676Shselasky	int txcnt, success, retrycnt, error;
2361208019Sthompsa
2362218676Shselasky	RUN_LOCK(sc);
2363218676Shselasky
2364209917Sthompsa	if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2365209917Sthompsa	    vap->iv_opmode == IEEE80211_M_STA)) {
2366203134Sthompsa		/* read statistic counters (clear on read) and update AMRR state */
2367203134Sthompsa		error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2368203134Sthompsa		    sizeof sta);
2369203134Sthompsa		if (error != 0)
2370218676Shselasky			goto fail;
2371203134Sthompsa
2372203134Sthompsa		/* count failed TX as errors */
2373218676Shselasky		ifp->if_oerrors += le16toh(sta[0].error.fail);
2374203134Sthompsa
2375218676Shselasky		retrycnt = le16toh(sta[1].tx.retry);
2376218676Shselasky		success = le16toh(sta[1].tx.success);
2377218676Shselasky		txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2378203134Sthompsa
2379218676Shselasky		DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2380218676Shselasky			retrycnt, success, le16toh(sta[0].error.fail));
2381218676Shselasky	} else {
2382218676Shselasky		wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2383203134Sthompsa
2384218676Shselasky		if (wstat == &(sc->wcid_stats[0]) ||
2385218676Shselasky		    wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2386218676Shselasky			goto fail;
2387208019Sthompsa
2388218676Shselasky		txcnt = (*wstat)[RUN_TXCNT];
2389218676Shselasky		success = (*wstat)[RUN_SUCCESS];
2390218676Shselasky		retrycnt = (*wstat)[RUN_RETRY];
2391218676Shselasky		DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2392218676Shselasky		    retrycnt, txcnt, success);
2393208019Sthompsa
2394218676Shselasky		memset(wstat, 0, sizeof(*wstat));
2395203134Sthompsa	}
2396203134Sthompsa
2397218676Shselasky	ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2398208019Sthompsa	rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2399218676Shselasky
2400218676Shselaskyfail:
2401218676Shselasky	RUN_UNLOCK(sc);
2402218676Shselasky
2403208019Sthompsa	DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2404208019Sthompsa}
2405203134Sthompsa
2406208019Sthompsastatic void
2407208019Sthompsarun_newassoc_cb(void *arg)
2408208019Sthompsa{
2409208019Sthompsa	struct run_cmdq *cmdq = arg;
2410208019Sthompsa	struct ieee80211_node *ni = cmdq->arg1;
2411208019Sthompsa	struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2412208019Sthompsa	uint8_t wcid = cmdq->wcid;
2413203134Sthompsa
2414208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2415208019Sthompsa
2416208019Sthompsa	run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2417208019Sthompsa	    ni->ni_macaddr, IEEE80211_ADDR_LEN);
2418218676Shselasky
2419218676Shselasky	memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2420203134Sthompsa}
2421203134Sthompsa
2422203134Sthompsastatic void
2423203134Sthompsarun_newassoc(struct ieee80211_node *ni, int isnew)
2424203134Sthompsa{
2425203134Sthompsa	struct run_node *rn = (void *)ni;
2426203134Sthompsa	struct ieee80211_rateset *rs = &ni->ni_rates;
2427208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2428208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2429208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2430203134Sthompsa	uint8_t rate;
2431208019Sthompsa	uint8_t ridx;
2432245047Shselasky	uint8_t wcid;
2433208019Sthompsa	int i, j;
2434203134Sthompsa
2435245047Shselasky	wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2436245047Shselasky	    1 : RUN_AID2WCID(ni->ni_associd);
2437245047Shselasky
2438209917Sthompsa	if (wcid > RT2870_WCID_MAX) {
2439208019Sthompsa		device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2440208019Sthompsa		return;
2441208019Sthompsa	}
2442203134Sthompsa
2443208019Sthompsa	/* only interested in true associations */
2444209917Sthompsa	if (isnew && ni->ni_associd != 0) {
2445208019Sthompsa
2446208019Sthompsa		/*
2447208019Sthompsa		 * This function could is called though timeout function.
2448208019Sthompsa		 * Need to defer.
2449208019Sthompsa		 */
2450208019Sthompsa		uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2451208019Sthompsa		DPRINTF("cmdq_store=%d\n", cnt);
2452208019Sthompsa		sc->cmdq[cnt].func = run_newassoc_cb;
2453208019Sthompsa		sc->cmdq[cnt].arg0 = NULL;
2454208019Sthompsa		sc->cmdq[cnt].arg1 = ni;
2455208019Sthompsa		sc->cmdq[cnt].wcid = wcid;
2456208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
2457208019Sthompsa	}
2458208019Sthompsa
2459208019Sthompsa	DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2460208019Sthompsa	    isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2461208019Sthompsa
2462203134Sthompsa	for (i = 0; i < rs->rs_nrates; i++) {
2463203134Sthompsa		rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2464203134Sthompsa		/* convert 802.11 rate to hardware rate index */
2465203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2466203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
2467203134Sthompsa				break;
2468203134Sthompsa		rn->ridx[i] = ridx;
2469203134Sthompsa		/* determine rate of control response frames */
2470203134Sthompsa		for (j = i; j >= 0; j--) {
2471203134Sthompsa			if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2472203134Sthompsa			    rt2860_rates[rn->ridx[i]].phy ==
2473203134Sthompsa			    rt2860_rates[rn->ridx[j]].phy)
2474203134Sthompsa				break;
2475203134Sthompsa		}
2476203134Sthompsa		if (j >= 0) {
2477203134Sthompsa			rn->ctl_ridx[i] = rn->ridx[j];
2478203134Sthompsa		} else {
2479203134Sthompsa			/* no basic rate found, use mandatory one */
2480203134Sthompsa			rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2481203134Sthompsa		}
2482203134Sthompsa		DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2483203134Sthompsa		    rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2484203134Sthompsa	}
2485208019Sthompsa	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2486208019Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2487208019Sthompsa		if (rt2860_rates[ridx].rate == rate)
2488208019Sthompsa			break;
2489208019Sthompsa	rn->mgt_ridx = ridx;
2490208019Sthompsa	DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2491208019Sthompsa
2492208019Sthompsa	usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2493203134Sthompsa}
2494203134Sthompsa
2495203134Sthompsa/*
2496203134Sthompsa * Return the Rx chain with the highest RSSI for a given frame.
2497203134Sthompsa */
2498203134Sthompsastatic __inline uint8_t
2499203134Sthompsarun_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2500203134Sthompsa{
2501203134Sthompsa	uint8_t rxchain = 0;
2502203134Sthompsa
2503203134Sthompsa	if (sc->nrxchains > 1) {
2504203134Sthompsa		if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2505203134Sthompsa			rxchain = 1;
2506203134Sthompsa		if (sc->nrxchains > 2)
2507203134Sthompsa			if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2508203134Sthompsa				rxchain = 2;
2509203134Sthompsa	}
2510209917Sthompsa	return (rxchain);
2511203134Sthompsa}
2512203134Sthompsa
2513203134Sthompsastatic void
2514203134Sthompsarun_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2515203134Sthompsa{
2516203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2517203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2518203134Sthompsa	struct ieee80211_frame *wh;
2519203134Sthompsa	struct ieee80211_node *ni;
2520203134Sthompsa	struct rt2870_rxd *rxd;
2521203134Sthompsa	struct rt2860_rxwi *rxwi;
2522203134Sthompsa	uint32_t flags;
2523203134Sthompsa	uint16_t len, phy;
2524203134Sthompsa	uint8_t ant, rssi;
2525203134Sthompsa	int8_t nf;
2526203134Sthompsa
2527203134Sthompsa	rxwi = mtod(m, struct rt2860_rxwi *);
2528203134Sthompsa	len = le16toh(rxwi->len) & 0xfff;
2529203134Sthompsa	if (__predict_false(len > dmalen)) {
2530203134Sthompsa		m_freem(m);
2531203134Sthompsa		ifp->if_ierrors++;
2532203134Sthompsa		DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2533203134Sthompsa		return;
2534203134Sthompsa	}
2535203134Sthompsa	/* Rx descriptor is located at the end */
2536203134Sthompsa	rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2537203134Sthompsa	flags = le32toh(rxd->flags);
2538203134Sthompsa
2539203134Sthompsa	if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2540203134Sthompsa		m_freem(m);
2541203134Sthompsa		ifp->if_ierrors++;
2542203134Sthompsa		DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2543203134Sthompsa		return;
2544203134Sthompsa	}
2545203134Sthompsa
2546203134Sthompsa	m->m_data += sizeof(struct rt2860_rxwi);
2547203134Sthompsa	m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2548203134Sthompsa
2549203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2550203134Sthompsa
2551209917Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2552203134Sthompsa		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2553203134Sthompsa		m->m_flags |= M_WEP;
2554203134Sthompsa	}
2555203134Sthompsa
2556209917Sthompsa	if (flags & RT2860_RX_L2PAD) {
2557203134Sthompsa		DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2558203134Sthompsa		len += 2;
2559203134Sthompsa	}
2560203134Sthompsa
2561208019Sthompsa	ni = ieee80211_find_rxnode(ic,
2562208019Sthompsa	    mtod(m, struct ieee80211_frame_min *));
2563208019Sthompsa
2564203134Sthompsa	if (__predict_false(flags & RT2860_RX_MICERR)) {
2565203134Sthompsa		/* report MIC failures to net80211 for TKIP */
2566209917Sthompsa		if (ni != NULL)
2567208019Sthompsa			ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2568203134Sthompsa		m_freem(m);
2569203134Sthompsa		ifp->if_ierrors++;
2570203134Sthompsa		DPRINTF("MIC error. Someone is lying.\n");
2571203134Sthompsa		return;
2572203134Sthompsa	}
2573203134Sthompsa
2574203134Sthompsa	ant = run_maxrssi_chain(sc, rxwi);
2575203134Sthompsa	rssi = rxwi->rssi[ant];
2576203134Sthompsa	nf = run_rssi2dbm(sc, rssi, ant);
2577203134Sthompsa
2578203134Sthompsa	m->m_pkthdr.rcvif = ifp;
2579203134Sthompsa	m->m_pkthdr.len = m->m_len = len;
2580203134Sthompsa
2581203134Sthompsa	if (ni != NULL) {
2582203134Sthompsa		(void)ieee80211_input(ni, m, rssi, nf);
2583203134Sthompsa		ieee80211_free_node(ni);
2584203134Sthompsa	} else {
2585203134Sthompsa		(void)ieee80211_input_all(ic, m, rssi, nf);
2586203134Sthompsa	}
2587203134Sthompsa
2588209917Sthompsa	if (__predict_false(ieee80211_radiotap_active(ic))) {
2589203134Sthompsa		struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2590203134Sthompsa
2591203134Sthompsa		tap->wr_flags = 0;
2592236439Shselasky		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2593236439Shselasky		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2594203134Sthompsa		tap->wr_antsignal = rssi;
2595203134Sthompsa		tap->wr_antenna = ant;
2596203134Sthompsa		tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2597203134Sthompsa		tap->wr_rate = 2;	/* in case it can't be found below */
2598203134Sthompsa		phy = le16toh(rxwi->phy);
2599203134Sthompsa		switch (phy & RT2860_PHY_MODE) {
2600203134Sthompsa		case RT2860_PHY_CCK:
2601203134Sthompsa			switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2602203134Sthompsa			case 0:	tap->wr_rate =   2; break;
2603203134Sthompsa			case 1:	tap->wr_rate =   4; break;
2604203134Sthompsa			case 2:	tap->wr_rate =  11; break;
2605203134Sthompsa			case 3:	tap->wr_rate =  22; break;
2606203134Sthompsa			}
2607203134Sthompsa			if (phy & RT2860_PHY_SHPRE)
2608203134Sthompsa				tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2609203134Sthompsa			break;
2610203134Sthompsa		case RT2860_PHY_OFDM:
2611203134Sthompsa			switch (phy & RT2860_PHY_MCS) {
2612203134Sthompsa			case 0:	tap->wr_rate =  12; break;
2613203134Sthompsa			case 1:	tap->wr_rate =  18; break;
2614203134Sthompsa			case 2:	tap->wr_rate =  24; break;
2615203134Sthompsa			case 3:	tap->wr_rate =  36; break;
2616203134Sthompsa			case 4:	tap->wr_rate =  48; break;
2617203134Sthompsa			case 5:	tap->wr_rate =  72; break;
2618203134Sthompsa			case 6:	tap->wr_rate =  96; break;
2619203134Sthompsa			case 7:	tap->wr_rate = 108; break;
2620203134Sthompsa			}
2621203134Sthompsa			break;
2622203134Sthompsa		}
2623203134Sthompsa	}
2624203134Sthompsa}
2625203134Sthompsa
2626203134Sthompsastatic void
2627203134Sthompsarun_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2628203134Sthompsa{
2629203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2630203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2631203134Sthompsa	struct mbuf *m = NULL;
2632203134Sthompsa	struct mbuf *m0;
2633203134Sthompsa	uint32_t dmalen;
2634203134Sthompsa	int xferlen;
2635203134Sthompsa
2636203134Sthompsa	usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2637203134Sthompsa
2638203134Sthompsa	switch (USB_GET_STATE(xfer)) {
2639203134Sthompsa	case USB_ST_TRANSFERRED:
2640203134Sthompsa
2641203134Sthompsa		DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2642203134Sthompsa
2643233774Shselasky		if (xferlen < (int)(sizeof(uint32_t) +
2644233774Shselasky		    sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) {
2645203134Sthompsa			DPRINTF("xfer too short %d\n", xferlen);
2646203134Sthompsa			goto tr_setup;
2647203134Sthompsa		}
2648203134Sthompsa
2649203134Sthompsa		m = sc->rx_m;
2650203134Sthompsa		sc->rx_m = NULL;
2651203134Sthompsa
2652203134Sthompsa		/* FALLTHROUGH */
2653203134Sthompsa	case USB_ST_SETUP:
2654203134Sthompsatr_setup:
2655203134Sthompsa		if (sc->rx_m == NULL) {
2656243857Sglebius			sc->rx_m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
2657203134Sthompsa			    MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2658203134Sthompsa		}
2659203134Sthompsa		if (sc->rx_m == NULL) {
2660203134Sthompsa			DPRINTF("could not allocate mbuf - idle with stall\n");
2661203134Sthompsa			ifp->if_ierrors++;
2662203134Sthompsa			usbd_xfer_set_stall(xfer);
2663203134Sthompsa			usbd_xfer_set_frames(xfer, 0);
2664203134Sthompsa		} else {
2665203134Sthompsa			/*
2666203134Sthompsa			 * Directly loading a mbuf cluster into DMA to
2667203134Sthompsa			 * save some data copying. This works because
2668203134Sthompsa			 * there is only one cluster.
2669203134Sthompsa			 */
2670203134Sthompsa			usbd_xfer_set_frame_data(xfer, 0,
2671203134Sthompsa			    mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2672203134Sthompsa			usbd_xfer_set_frames(xfer, 1);
2673203134Sthompsa		}
2674203134Sthompsa		usbd_transfer_submit(xfer);
2675203134Sthompsa		break;
2676203134Sthompsa
2677203134Sthompsa	default:	/* Error */
2678203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2679203134Sthompsa			/* try to clear stall first */
2680203134Sthompsa			usbd_xfer_set_stall(xfer);
2681203134Sthompsa
2682203134Sthompsa			if (error == USB_ERR_TIMEOUT)
2683203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2684203134Sthompsa
2685203134Sthompsa			ifp->if_ierrors++;
2686203134Sthompsa
2687203134Sthompsa			goto tr_setup;
2688203134Sthompsa		}
2689209917Sthompsa		if (sc->rx_m != NULL) {
2690203134Sthompsa			m_freem(sc->rx_m);
2691203134Sthompsa			sc->rx_m = NULL;
2692203134Sthompsa		}
2693203134Sthompsa		break;
2694203134Sthompsa	}
2695203134Sthompsa
2696203134Sthompsa	if (m == NULL)
2697203134Sthompsa		return;
2698203134Sthompsa
2699203134Sthompsa	/* inputting all the frames must be last */
2700203134Sthompsa
2701203134Sthompsa	RUN_UNLOCK(sc);
2702203134Sthompsa
2703203134Sthompsa	m->m_pkthdr.len = m->m_len = xferlen;
2704203134Sthompsa
2705203134Sthompsa	/* HW can aggregate multiple 802.11 frames in a single USB xfer */
2706203134Sthompsa	for(;;) {
2707203134Sthompsa		dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2708203134Sthompsa
2709233774Shselasky		if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2710233774Shselasky		    ((dmalen & 3) != 0)) {
2711203134Sthompsa			DPRINTF("bad DMA length %u\n", dmalen);
2712203134Sthompsa			break;
2713203134Sthompsa		}
2714233774Shselasky		if ((dmalen + 8) > (uint32_t)xferlen) {
2715203134Sthompsa			DPRINTF("bad DMA length %u > %d\n",
2716203134Sthompsa			dmalen + 8, xferlen);
2717203134Sthompsa			break;
2718203134Sthompsa		}
2719203134Sthompsa
2720203134Sthompsa		/* If it is the last one or a single frame, we won't copy. */
2721209917Sthompsa		if ((xferlen -= dmalen + 8) <= 8) {
2722203134Sthompsa			/* trim 32-bit DMA-len header */
2723203134Sthompsa			m->m_data += 4;
2724203134Sthompsa			m->m_pkthdr.len = m->m_len -= 4;
2725203134Sthompsa			run_rx_frame(sc, m, dmalen);
2726257435Shselasky			m = NULL;	/* don't free source buffer */
2727203134Sthompsa			break;
2728203134Sthompsa		}
2729203134Sthompsa
2730203134Sthompsa		/* copy aggregated frames to another mbuf */
2731243857Sglebius		m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2732203134Sthompsa		if (__predict_false(m0 == NULL)) {
2733203134Sthompsa			DPRINTF("could not allocate mbuf\n");
2734203134Sthompsa			ifp->if_ierrors++;
2735203134Sthompsa			break;
2736203134Sthompsa		}
2737203134Sthompsa		m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2738203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2739203134Sthompsa		m0->m_pkthdr.len = m0->m_len =
2740203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd);
2741203134Sthompsa		run_rx_frame(sc, m0, dmalen);
2742203134Sthompsa
2743203134Sthompsa		/* update data ptr */
2744203134Sthompsa		m->m_data += dmalen + 8;
2745203134Sthompsa		m->m_pkthdr.len = m->m_len -= dmalen + 8;
2746203134Sthompsa	}
2747203134Sthompsa
2748257435Shselasky	/* make sure we free the source buffer, if any */
2749257435Shselasky	m_freem(m);
2750257435Shselasky
2751203134Sthompsa	RUN_LOCK(sc);
2752203134Sthompsa}
2753203134Sthompsa
2754203134Sthompsastatic void
2755203134Sthompsarun_tx_free(struct run_endpoint_queue *pq,
2756203134Sthompsa    struct run_tx_data *data, int txerr)
2757203134Sthompsa{
2758203134Sthompsa	if (data->m != NULL) {
2759203134Sthompsa		if (data->m->m_flags & M_TXCB)
2760203134Sthompsa			ieee80211_process_callback(data->ni, data->m,
2761203134Sthompsa			    txerr ? ETIMEDOUT : 0);
2762203134Sthompsa		m_freem(data->m);
2763203134Sthompsa		data->m = NULL;
2764203134Sthompsa
2765209917Sthompsa		if (data->ni == NULL) {
2766203134Sthompsa			DPRINTF("no node\n");
2767203134Sthompsa		} else {
2768203134Sthompsa			ieee80211_free_node(data->ni);
2769203134Sthompsa			data->ni = NULL;
2770203134Sthompsa		}
2771203134Sthompsa	}
2772203134Sthompsa
2773203134Sthompsa	STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2774203134Sthompsa	pq->tx_nfree++;
2775203134Sthompsa}
2776203134Sthompsa
2777203134Sthompsastatic void
2778257429Shselaskyrun_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, u_int index)
2779203134Sthompsa{
2780203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2781203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2782208019Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2783203134Sthompsa	struct run_tx_data *data;
2784203134Sthompsa	struct ieee80211vap *vap = NULL;
2785203134Sthompsa	struct usb_page_cache *pc;
2786203134Sthompsa	struct run_endpoint_queue *pq = &sc->sc_epq[index];
2787203134Sthompsa	struct mbuf *m;
2788203134Sthompsa	usb_frlength_t size;
2789203134Sthompsa	int actlen;
2790203134Sthompsa	int sumlen;
2791203134Sthompsa
2792203134Sthompsa	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2793203134Sthompsa
2794209917Sthompsa	switch (USB_GET_STATE(xfer)) {
2795203134Sthompsa	case USB_ST_TRANSFERRED:
2796203134Sthompsa		DPRINTFN(11, "transfer complete: %d "
2797203134Sthompsa		    "bytes @ index %d\n", actlen, index);
2798203134Sthompsa
2799203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2800203134Sthompsa
2801203134Sthompsa		run_tx_free(pq, data, 0);
2802203134Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2803203134Sthompsa
2804203134Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2805203134Sthompsa
2806203134Sthompsa		ifp->if_opackets++;
2807203134Sthompsa
2808203134Sthompsa		/* FALLTHROUGH */
2809203134Sthompsa	case USB_ST_SETUP:
2810203134Sthompsatr_setup:
2811203134Sthompsa		data = STAILQ_FIRST(&pq->tx_qh);
2812209917Sthompsa		if (data == NULL)
2813203134Sthompsa			break;
2814203134Sthompsa
2815203134Sthompsa		STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2816203134Sthompsa
2817203134Sthompsa		m = data->m;
2818228508Shselasky		if ((m->m_pkthdr.len +
2819228508Shselasky		    sizeof(data->desc) + 3 + 8) > RUN_MAX_TXSZ) {
2820203134Sthompsa			DPRINTF("data overflow, %u bytes\n",
2821203134Sthompsa			    m->m_pkthdr.len);
2822203134Sthompsa
2823203134Sthompsa			ifp->if_oerrors++;
2824203134Sthompsa
2825203134Sthompsa			run_tx_free(pq, data, 1);
2826203134Sthompsa
2827203134Sthompsa			goto tr_setup;
2828203134Sthompsa		}
2829203134Sthompsa
2830203134Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2831203134Sthompsa		size = sizeof(data->desc);
2832203134Sthompsa		usbd_copy_in(pc, 0, &data->desc, size);
2833203134Sthompsa		usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2834228508Shselasky		size += m->m_pkthdr.len;
2835228508Shselasky		/*
2836228508Shselasky		 * Align end on a 4-byte boundary, pad 8 bytes (CRC +
2837228508Shselasky		 * 4-byte padding), and be sure to zero those trailing
2838228508Shselasky		 * bytes:
2839228508Shselasky		 */
2840228508Shselasky		usbd_frame_zero(pc, size, ((-size) & 3) + 8);
2841228508Shselasky		size += ((-size) & 3) + 8;
2842203134Sthompsa
2843203134Sthompsa		vap = data->ni->ni_vap;
2844203134Sthompsa		if (ieee80211_radiotap_active_vap(vap)) {
2845203134Sthompsa			struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2846208019Sthompsa			struct rt2860_txwi *txwi =
2847208019Sthompsa			    (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2848203134Sthompsa
2849203134Sthompsa			tap->wt_flags = 0;
2850203134Sthompsa			tap->wt_rate = rt2860_rates[data->ridx].rate;
2851236439Shselasky			tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2852236439Shselasky			tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2853203134Sthompsa			tap->wt_hwqueue = index;
2854208019Sthompsa			if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2855203134Sthompsa				tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2856203134Sthompsa
2857203134Sthompsa			ieee80211_radiotap_tx(vap, m);
2858203134Sthompsa		}
2859203134Sthompsa
2860228508Shselasky		DPRINTFN(11, "sending frame len=%u/%u  @ index %d\n",
2861228508Shselasky		    m->m_pkthdr.len, size, index);
2862203134Sthompsa
2863228508Shselasky		usbd_xfer_set_frame_len(xfer, 0, size);
2864203134Sthompsa		usbd_xfer_set_priv(xfer, data);
2865203134Sthompsa
2866203134Sthompsa		usbd_transfer_submit(xfer);
2867203134Sthompsa
2868203134Sthompsa		RUN_UNLOCK(sc);
2869203134Sthompsa		run_start(ifp);
2870203134Sthompsa		RUN_LOCK(sc);
2871203134Sthompsa
2872203134Sthompsa		break;
2873203134Sthompsa
2874203134Sthompsa	default:
2875203134Sthompsa		DPRINTF("USB transfer error, %s\n",
2876203134Sthompsa		    usbd_errstr(error));
2877203134Sthompsa
2878203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2879203134Sthompsa
2880203134Sthompsa		ifp->if_oerrors++;
2881203134Sthompsa
2882203134Sthompsa		if (data != NULL) {
2883208019Sthompsa			if(data->ni != NULL)
2884208019Sthompsa				vap = data->ni->ni_vap;
2885203134Sthompsa			run_tx_free(pq, data, error);
2886203134Sthompsa			usbd_xfer_set_priv(xfer, NULL);
2887203134Sthompsa		}
2888209917Sthompsa		if (vap == NULL)
2889208019Sthompsa			vap = TAILQ_FIRST(&ic->ic_vaps);
2890203134Sthompsa
2891203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2892203134Sthompsa			if (error == USB_ERR_TIMEOUT) {
2893203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2894208019Sthompsa				uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2895208019Sthompsa				DPRINTF("cmdq_store=%d\n", i);
2896208019Sthompsa				sc->cmdq[i].func = run_usb_timeout_cb;
2897208019Sthompsa				sc->cmdq[i].arg0 = vap;
2898208019Sthompsa				ieee80211_runtask(ic, &sc->cmdq_task);
2899203134Sthompsa			}
2900203134Sthompsa
2901203134Sthompsa			/*
2902203134Sthompsa			 * Try to clear stall first, also if other
2903203134Sthompsa			 * errors occur, hence clearing stall
2904203134Sthompsa			 * introduces a 50 ms delay:
2905203134Sthompsa			 */
2906203134Sthompsa			usbd_xfer_set_stall(xfer);
2907203134Sthompsa			goto tr_setup;
2908203134Sthompsa		}
2909203134Sthompsa		break;
2910203134Sthompsa	}
2911203134Sthompsa}
2912203134Sthompsa
2913203134Sthompsastatic void
2914203134Sthompsarun_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2915203134Sthompsa{
2916203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 0);
2917203134Sthompsa}
2918203134Sthompsa
2919203134Sthompsastatic void
2920203134Sthompsarun_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2921203134Sthompsa{
2922203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 1);
2923203134Sthompsa}
2924203134Sthompsa
2925203134Sthompsastatic void
2926203134Sthompsarun_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2927203134Sthompsa{
2928203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 2);
2929203134Sthompsa}
2930203134Sthompsa
2931203134Sthompsastatic void
2932203134Sthompsarun_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2933203134Sthompsa{
2934203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 3);
2935203134Sthompsa}
2936203134Sthompsa
2937203134Sthompsastatic void
2938203134Sthompsarun_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2939203134Sthompsa{
2940203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 4);
2941203134Sthompsa}
2942203134Sthompsa
2943203134Sthompsastatic void
2944203134Sthompsarun_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2945203134Sthompsa{
2946203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 5);
2947203134Sthompsa}
2948203134Sthompsa
2949203134Sthompsastatic void
2950208019Sthompsarun_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2951203134Sthompsa{
2952203134Sthompsa	struct mbuf *m = data->m;
2953203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2954208019Sthompsa	struct ieee80211vap *vap = data->ni->ni_vap;
2955203134Sthompsa	struct ieee80211_frame *wh;
2956203134Sthompsa	struct rt2870_txd *txd;
2957203134Sthompsa	struct rt2860_txwi *txwi;
2958208019Sthompsa	uint16_t xferlen;
2959208019Sthompsa	uint16_t mcs;
2960203134Sthompsa	uint8_t ridx = data->ridx;
2961208019Sthompsa	uint8_t pad;
2962203134Sthompsa
2963203134Sthompsa	/* get MCS code from rate index */
2964208019Sthompsa	mcs = rt2860_rates[ridx].mcs;
2965203134Sthompsa
2966203134Sthompsa	xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2967203134Sthompsa
2968203134Sthompsa	/* roundup to 32-bit alignment */
2969203134Sthompsa	xferlen = (xferlen + 3) & ~3;
2970203134Sthompsa
2971203134Sthompsa	txd = (struct rt2870_txd *)&data->desc;
2972203134Sthompsa	txd->len = htole16(xferlen);
2973203134Sthompsa
2974208019Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2975208019Sthompsa
2976208019Sthompsa	/*
2977208019Sthompsa	 * Ether both are true or both are false, the header
2978208019Sthompsa	 * are nicely aligned to 32-bit. So, no L2 padding.
2979208019Sthompsa	 */
2980208019Sthompsa	if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2981208019Sthompsa		pad = 0;
2982208019Sthompsa	else
2983208019Sthompsa		pad = 2;
2984208019Sthompsa
2985203134Sthompsa	/* setup TX Wireless Information */
2986203134Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
2987203134Sthompsa	txwi->len = htole16(m->m_pkthdr.len - pad);
2988203134Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2989203134Sthompsa		txwi->phy = htole16(RT2860_PHY_CCK);
2990203134Sthompsa		if (ridx != RT2860_RIDX_CCK1 &&
2991203134Sthompsa		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2992203134Sthompsa			mcs |= RT2860_PHY_SHPRE;
2993203134Sthompsa	} else
2994203134Sthompsa		txwi->phy = htole16(RT2860_PHY_OFDM);
2995203134Sthompsa	txwi->phy |= htole16(mcs);
2996203134Sthompsa
2997203134Sthompsa	/* check if RTS/CTS or CTS-to-self protection is required */
2998203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2999203134Sthompsa	    (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
3000203134Sthompsa	     ((ic->ic_flags & IEEE80211_F_USEPROT) &&
3001203134Sthompsa	      rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
3002208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_HT;
3003203134Sthompsa	else
3004208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_BACKOFF;
3005209144Sthompsa
3006209917Sthompsa	if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
3007209144Sthompsa		txwi->xflags |= RT2860_TX_NSEQ;
3008203134Sthompsa}
3009203134Sthompsa
3010203134Sthompsa/* This function must be called locked */
3011203134Sthompsastatic int
3012203134Sthompsarun_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3013203134Sthompsa{
3014203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3015208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
3016203134Sthompsa	struct ieee80211_frame *wh;
3017208019Sthompsa	struct ieee80211_channel *chan;
3018203134Sthompsa	const struct ieee80211_txparam *tp;
3019208019Sthompsa	struct run_node *rn = (void *)ni;
3020203134Sthompsa	struct run_tx_data *data;
3021208019Sthompsa	struct rt2870_txd *txd;
3022208019Sthompsa	struct rt2860_txwi *txwi;
3023203134Sthompsa	uint16_t qos;
3024203134Sthompsa	uint16_t dur;
3025208019Sthompsa	uint16_t qid;
3026203134Sthompsa	uint8_t type;
3027203134Sthompsa	uint8_t tid;
3028208019Sthompsa	uint8_t ridx;
3029208019Sthompsa	uint8_t ctl_ridx;
3030203134Sthompsa	uint8_t qflags;
3031203134Sthompsa	uint8_t xflags = 0;
3032203134Sthompsa	int hasqos;
3033203134Sthompsa
3034203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3035203134Sthompsa
3036203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3037203134Sthompsa
3038203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3039203134Sthompsa
3040203134Sthompsa	/*
3041203134Sthompsa	 * There are 7 bulk endpoints: 1 for RX
3042203134Sthompsa	 * and 6 for TX (4 EDCAs + HCCA + Prio).
3043203134Sthompsa	 * Update 03-14-2009:  some devices like the Planex GW-US300MiniS
3044203134Sthompsa	 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
3045203134Sthompsa	 */
3046203134Sthompsa	if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
3047203134Sthompsa		uint8_t *frm;
3048203134Sthompsa
3049203134Sthompsa		if(IEEE80211_HAS_ADDR4(wh))
3050203134Sthompsa			frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
3051203134Sthompsa		else
3052203134Sthompsa			frm =((struct ieee80211_qosframe *)wh)->i_qos;
3053203134Sthompsa
3054203134Sthompsa		qos = le16toh(*(const uint16_t *)frm);
3055203134Sthompsa		tid = qos & IEEE80211_QOS_TID;
3056203134Sthompsa		qid = TID_TO_WME_AC(tid);
3057203134Sthompsa	} else {
3058203134Sthompsa		qos = 0;
3059203134Sthompsa		tid = 0;
3060203134Sthompsa		qid = WME_AC_BE;
3061203134Sthompsa	}
3062203134Sthompsa	qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
3063203134Sthompsa
3064203134Sthompsa	DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
3065203134Sthompsa	    qos, qid, tid, qflags);
3066203134Sthompsa
3067208019Sthompsa	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
3068208019Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
3069203134Sthompsa
3070203134Sthompsa	/* pickup a rate index */
3071203134Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
3072203134Sthompsa	    type != IEEE80211_FC0_TYPE_DATA) {
3073203134Sthompsa		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3074203134Sthompsa		    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3075203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3076203134Sthompsa	} else {
3077208019Sthompsa		if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3078208019Sthompsa			ridx = rn->fix_ridx;
3079208019Sthompsa		else
3080208019Sthompsa			ridx = rn->amrr_ridx;
3081203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3082203134Sthompsa	}
3083203134Sthompsa
3084203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3085203134Sthompsa	    (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3086203134Sthompsa	     IEEE80211_QOS_ACKPOLICY_NOACK)) {
3087209144Sthompsa		xflags |= RT2860_TX_ACK;
3088203134Sthompsa		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3089208019Sthompsa			dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3090203134Sthompsa		else
3091208019Sthompsa			dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3092205042Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3093203134Sthompsa	}
3094203134Sthompsa
3095203134Sthompsa	/* reserve slots for mgmt packets, just in case */
3096203134Sthompsa	if (sc->sc_epq[qid].tx_nfree < 3) {
3097203134Sthompsa		DPRINTFN(10, "tx ring %d is full\n", qid);
3098203134Sthompsa		return (-1);
3099203134Sthompsa	}
3100203134Sthompsa
3101203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3102203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3103203134Sthompsa	sc->sc_epq[qid].tx_nfree--;
3104203134Sthompsa
3105208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3106208019Sthompsa	txd->flags = qflags;
3107208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3108208019Sthompsa	txwi->xflags = xflags;
3109245047Shselasky	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3110245047Shselasky		txwi->wcid = 0;
3111245047Shselasky	} else {
3112245047Shselasky		txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
3113245047Shselasky		    1 : RUN_AID2WCID(ni->ni_associd);
3114245047Shselasky	}
3115208019Sthompsa	/* clear leftover garbage bits */
3116208019Sthompsa	txwi->flags = 0;
3117208019Sthompsa	txwi->txop = 0;
3118208019Sthompsa
3119203134Sthompsa	data->m = m;
3120203134Sthompsa	data->ni = ni;
3121203134Sthompsa	data->ridx = ridx;
3122203134Sthompsa
3123208019Sthompsa	run_set_tx_desc(sc, data);
3124203134Sthompsa
3125208019Sthompsa	/*
3126208019Sthompsa	 * The chip keeps track of 2 kind of Tx stats,
3127208019Sthompsa	 *  * TX_STAT_FIFO, for per WCID stats, and
3128208019Sthompsa	 *  * TX_STA_CNT0 for all-TX-in-one stats.
3129208019Sthompsa	 *
3130208019Sthompsa	 * To use FIFO stats, we need to store MCS into the driver-private
3131208019Sthompsa 	 * PacketID field. So that, we can tell whose stats when we read them.
3132208019Sthompsa 	 * We add 1 to the MCS because setting the PacketID field to 0 means
3133208019Sthompsa 	 * that we don't want feedback in TX_STAT_FIFO.
3134208019Sthompsa 	 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3135208019Sthompsa 	 *
3136208019Sthompsa 	 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3137208019Sthompsa 	 */
3138209917Sthompsa	if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3139209917Sthompsa	    vap->iv_opmode == IEEE80211_M_MBSS) {
3140208019Sthompsa		uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3141208019Sthompsa		txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3142208019Sthompsa
3143208019Sthompsa		/*
3144208019Sthompsa		 * Unlike PCI based devices, we don't get any interrupt from
3145208019Sthompsa		 * USB devices, so we simulate FIFO-is-full interrupt here.
3146208019Sthompsa		 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3147208019Sthompsa		 * quickly get fulled. To prevent overflow, increment a counter on
3148208019Sthompsa		 * every FIFO stat request, so we know how many slots are left.
3149208019Sthompsa		 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3150208019Sthompsa		 * are used only in those modes.
3151208019Sthompsa		 * We just drain stats. AMRR gets updated every 1 sec by
3152208019Sthompsa		 * run_ratectl_cb() via callout.
3153208019Sthompsa		 * Call it early. Otherwise overflow.
3154208019Sthompsa		 */
3155209917Sthompsa		if (sc->fifo_cnt++ == 10) {
3156208019Sthompsa			/*
3157208019Sthompsa			 * With multiple vaps or if_bridge, if_start() is called
3158208019Sthompsa			 * with a non-sleepable lock, tcpinp. So, need to defer.
3159208019Sthompsa			 */
3160208019Sthompsa			uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3161208019Sthompsa			DPRINTFN(6, "cmdq_store=%d\n", i);
3162208019Sthompsa			sc->cmdq[i].func = run_drain_fifo;
3163208019Sthompsa			sc->cmdq[i].arg0 = sc;
3164208019Sthompsa			ieee80211_runtask(ic, &sc->cmdq_task);
3165208019Sthompsa		}
3166208019Sthompsa	}
3167208019Sthompsa
3168203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3169203134Sthompsa
3170203134Sthompsa	usbd_transfer_start(sc->sc_xfer[qid]);
3171203134Sthompsa
3172203134Sthompsa	DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3173203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3174203134Sthompsa	    rt2860_rates[ridx].rate, qid);
3175203134Sthompsa
3176203134Sthompsa	return (0);
3177203134Sthompsa}
3178203134Sthompsa
3179203134Sthompsastatic int
3180203134Sthompsarun_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3181203134Sthompsa{
3182203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3183203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3184208019Sthompsa	struct run_node *rn = (void *)ni;
3185203134Sthompsa	struct run_tx_data *data;
3186203134Sthompsa	struct ieee80211_frame *wh;
3187208019Sthompsa	struct rt2870_txd *txd;
3188208019Sthompsa	struct rt2860_txwi *txwi;
3189203134Sthompsa	uint16_t dur;
3190208019Sthompsa	uint8_t ridx = rn->mgt_ridx;
3191203134Sthompsa	uint8_t type;
3192203134Sthompsa	uint8_t xflags = 0;
3193208019Sthompsa	uint8_t wflags = 0;
3194203134Sthompsa
3195203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3196203134Sthompsa
3197203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3198203134Sthompsa
3199203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3200203134Sthompsa
3201208019Sthompsa	/* tell hardware to add timestamp for probe responses */
3202208019Sthompsa	if ((wh->i_fc[0] &
3203208019Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3204208019Sthompsa	    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3205208019Sthompsa		wflags |= RT2860_TX_TS;
3206208019Sthompsa	else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3207203134Sthompsa		xflags |= RT2860_TX_ACK;
3208203134Sthompsa
3209208019Sthompsa		dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3210203134Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3211203134Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3212203134Sthompsa	}
3213203134Sthompsa
3214203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3215203134Sthompsa		/* let caller free mbuf */
3216203134Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3217203134Sthompsa		return (EIO);
3218203134Sthompsa	}
3219203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3220203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3221203134Sthompsa	sc->sc_epq[0].tx_nfree--;
3222203134Sthompsa
3223208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3224208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3225208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3226208019Sthompsa	txwi->wcid = 0xff;
3227208019Sthompsa	txwi->flags = wflags;
3228208019Sthompsa	txwi->xflags = xflags;
3229208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3230208019Sthompsa
3231203134Sthompsa	data->m = m;
3232203134Sthompsa	data->ni = ni;
3233203134Sthompsa	data->ridx = ridx;
3234203134Sthompsa
3235208019Sthompsa	run_set_tx_desc(sc, data);
3236203134Sthompsa
3237203134Sthompsa	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3238203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3239208019Sthompsa	    rt2860_rates[ridx].rate);
3240203134Sthompsa
3241203134Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3242203134Sthompsa
3243203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3244203134Sthompsa
3245203134Sthompsa	return (0);
3246203134Sthompsa}
3247203134Sthompsa
3248203134Sthompsastatic int
3249203134Sthompsarun_sendprot(struct run_softc *sc,
3250203134Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3251203134Sthompsa{
3252203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3253203134Sthompsa	struct ieee80211_frame *wh;
3254203134Sthompsa	struct run_tx_data *data;
3255208019Sthompsa	struct rt2870_txd *txd;
3256208019Sthompsa	struct rt2860_txwi *txwi;
3257203134Sthompsa	struct mbuf *mprot;
3258203134Sthompsa	int ridx;
3259203134Sthompsa	int protrate;
3260203134Sthompsa	int ackrate;
3261203134Sthompsa	int pktlen;
3262203134Sthompsa	int isshort;
3263203134Sthompsa	uint16_t dur;
3264203134Sthompsa	uint8_t type;
3265208019Sthompsa	uint8_t wflags = 0;
3266208019Sthompsa	uint8_t xflags = 0;
3267203134Sthompsa
3268203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3269203134Sthompsa
3270203134Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3271203134Sthompsa	    ("protection %d", prot));
3272203134Sthompsa
3273203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3274203134Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3275203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3276203134Sthompsa
3277203134Sthompsa	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3278203134Sthompsa	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3279203134Sthompsa
3280203134Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3281209189Sjkim	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3282203134Sthompsa	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3283203134Sthompsa	wflags = RT2860_TX_FRAG;
3284203134Sthompsa
3285203134Sthompsa	/* check that there are free slots before allocating the mbuf */
3286203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3287203134Sthompsa		/* let caller free mbuf */
3288203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3289203134Sthompsa		return (ENOBUFS);
3290203134Sthompsa	}
3291203134Sthompsa
3292203134Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
3293203134Sthompsa		/* NB: CTS is the same size as an ACK */
3294203134Sthompsa		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3295208019Sthompsa		xflags |= RT2860_TX_ACK;
3296203134Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3297203134Sthompsa	} else {
3298203134Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3299203134Sthompsa	}
3300203134Sthompsa	if (mprot == NULL) {
3301203134Sthompsa		sc->sc_ifp->if_oerrors++;
3302203134Sthompsa		DPRINTF("could not allocate mbuf\n");
3303203134Sthompsa		return (ENOBUFS);
3304203134Sthompsa	}
3305203134Sthompsa
3306203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3307203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3308203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3309203134Sthompsa
3310208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3311208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3312208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3313208019Sthompsa	txwi->wcid = 0xff;
3314208019Sthompsa	txwi->flags = wflags;
3315208019Sthompsa	txwi->xflags = xflags;
3316208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3317208019Sthompsa
3318203134Sthompsa	data->m = mprot;
3319203134Sthompsa	data->ni = ieee80211_ref_node(ni);
3320203134Sthompsa
3321203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3322203134Sthompsa		if (rt2860_rates[ridx].rate == protrate)
3323203134Sthompsa			break;
3324203134Sthompsa	data->ridx = ridx;
3325203134Sthompsa
3326208019Sthompsa	run_set_tx_desc(sc, data);
3327203134Sthompsa
3328203134Sthompsa        DPRINTFN(1, "sending prot len=%u rate=%u\n",
3329203134Sthompsa            m->m_pkthdr.len, rate);
3330203134Sthompsa
3331203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3332203134Sthompsa
3333203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3334203134Sthompsa
3335203134Sthompsa	return (0);
3336203134Sthompsa}
3337203134Sthompsa
3338203134Sthompsastatic int
3339203134Sthompsarun_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3340203134Sthompsa    const struct ieee80211_bpf_params *params)
3341203134Sthompsa{
3342203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3343203134Sthompsa	struct ieee80211_frame *wh;
3344203134Sthompsa	struct run_tx_data *data;
3345208019Sthompsa	struct rt2870_txd *txd;
3346208019Sthompsa	struct rt2860_txwi *txwi;
3347203134Sthompsa	uint8_t type;
3348208019Sthompsa	uint8_t ridx;
3349208019Sthompsa	uint8_t rate;
3350208019Sthompsa	uint8_t opflags = 0;
3351208019Sthompsa	uint8_t xflags = 0;
3352203134Sthompsa	int error;
3353203134Sthompsa
3354203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3355203134Sthompsa
3356203134Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
3357203134Sthompsa
3358203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3359203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3360203134Sthompsa
3361203134Sthompsa	rate = params->ibp_rate0;
3362203134Sthompsa	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3363203134Sthompsa		/* let caller free mbuf */
3364203134Sthompsa		return (EINVAL);
3365203134Sthompsa	}
3366203134Sthompsa
3367203134Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3368208019Sthompsa		xflags |= RT2860_TX_ACK;
3369203134Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3370203134Sthompsa		error = run_sendprot(sc, m, ni,
3371203134Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
3372203134Sthompsa			IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3373203134Sthompsa		    rate);
3374203134Sthompsa		if (error) {
3375203134Sthompsa			/* let caller free mbuf */
3376209917Sthompsa			return error;
3377203134Sthompsa		}
3378203134Sthompsa		opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3379203134Sthompsa	}
3380203134Sthompsa
3381203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3382203134Sthompsa		/* let caller free mbuf */
3383203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3384203134Sthompsa		DPRINTF("sending raw frame, but tx ring is full\n");
3385203134Sthompsa		return (EIO);
3386203134Sthompsa	}
3387203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3388203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3389203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3390203134Sthompsa
3391208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3392208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3393208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3394208019Sthompsa	txwi->wcid = 0xff;
3395208019Sthompsa	txwi->xflags = xflags;
3396208019Sthompsa	txwi->txop = opflags;
3397208019Sthompsa	txwi->flags = 0;	/* clear leftover garbage bits */
3398208019Sthompsa
3399203134Sthompsa        data->m = m;
3400203134Sthompsa        data->ni = ni;
3401203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3402203134Sthompsa		if (rt2860_rates[ridx].rate == rate)
3403203134Sthompsa			break;
3404203134Sthompsa	data->ridx = ridx;
3405203134Sthompsa
3406208019Sthompsa        run_set_tx_desc(sc, data);
3407203134Sthompsa
3408203134Sthompsa        DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3409203134Sthompsa            m->m_pkthdr.len, rate);
3410203134Sthompsa
3411203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3412203134Sthompsa
3413203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3414203134Sthompsa
3415209917Sthompsa        return (0);
3416203134Sthompsa}
3417203134Sthompsa
3418203134Sthompsastatic int
3419203134Sthompsarun_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3420203134Sthompsa    const struct ieee80211_bpf_params *params)
3421203134Sthompsa{
3422203134Sthompsa	struct ifnet *ifp = ni->ni_ic->ic_ifp;
3423203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3424208019Sthompsa	int error = 0;
3425208019Sthompsa
3426203134Sthompsa	RUN_LOCK(sc);
3427203134Sthompsa
3428203134Sthompsa	/* prevent management frames from being sent if we're not ready */
3429203134Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3430203134Sthompsa		error =  ENETDOWN;
3431208019Sthompsa		goto done;
3432203134Sthompsa	}
3433203134Sthompsa
3434203134Sthompsa	if (params == NULL) {
3435203134Sthompsa		/* tx mgt packet */
3436209917Sthompsa		if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3437203134Sthompsa			ifp->if_oerrors++;
3438203134Sthompsa			DPRINTF("mgt tx failed\n");
3439208019Sthompsa			goto done;
3440203134Sthompsa		}
3441203134Sthompsa	} else {
3442203134Sthompsa		/* tx raw packet with param */
3443209917Sthompsa		if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3444203134Sthompsa			ifp->if_oerrors++;
3445203134Sthompsa			DPRINTF("tx with param failed\n");
3446208019Sthompsa			goto done;
3447203134Sthompsa		}
3448203134Sthompsa	}
3449203134Sthompsa
3450203134Sthompsa	ifp->if_opackets++;
3451203134Sthompsa
3452208019Sthompsadone:
3453203134Sthompsa	RUN_UNLOCK(sc);
3454203134Sthompsa
3455209917Sthompsa	if (error != 0) {
3456208019Sthompsa		if(m != NULL)
3457208019Sthompsa			m_freem(m);
3458208019Sthompsa		ieee80211_free_node(ni);
3459208019Sthompsa	}
3460203134Sthompsa
3461203134Sthompsa	return (error);
3462203134Sthompsa}
3463203134Sthompsa
3464203134Sthompsastatic void
3465203134Sthompsarun_start(struct ifnet *ifp)
3466203134Sthompsa{
3467203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3468203134Sthompsa	struct ieee80211_node *ni;
3469203134Sthompsa	struct mbuf *m;
3470203134Sthompsa
3471203134Sthompsa	RUN_LOCK(sc);
3472203134Sthompsa
3473203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3474203134Sthompsa		RUN_UNLOCK(sc);
3475203134Sthompsa		return;
3476203134Sthompsa	}
3477203134Sthompsa
3478203134Sthompsa	for (;;) {
3479203134Sthompsa		/* send data frames */
3480203134Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3481203134Sthompsa		if (m == NULL)
3482203134Sthompsa			break;
3483203134Sthompsa
3484203134Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3485203134Sthompsa		if (run_tx(sc, m, ni) != 0) {
3486203134Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
3487203134Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3488203134Sthompsa			break;
3489203134Sthompsa		}
3490203134Sthompsa	}
3491203134Sthompsa
3492203134Sthompsa	RUN_UNLOCK(sc);
3493203134Sthompsa}
3494203134Sthompsa
3495203134Sthompsastatic int
3496203134Sthompsarun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3497203134Sthompsa{
3498203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3499203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3500203134Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
3501208019Sthompsa	int startall = 0;
3502246614Shselasky	int error;
3503203134Sthompsa
3504246614Shselasky	RUN_LOCK(sc);
3505246614Shselasky	error = sc->sc_detached ? ENXIO : 0;
3506246614Shselasky	RUN_UNLOCK(sc);
3507246614Shselasky	if (error)
3508246614Shselasky		return (error);
3509246614Shselasky
3510203134Sthompsa	switch (cmd) {
3511203134Sthompsa	case SIOCSIFFLAGS:
3512203134Sthompsa		RUN_LOCK(sc);
3513203134Sthompsa		if (ifp->if_flags & IFF_UP) {
3514203134Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3515208019Sthompsa				startall = 1;
3516203134Sthompsa				run_init_locked(sc);
3517203134Sthompsa			} else
3518203134Sthompsa				run_update_promisc_locked(ifp);
3519203134Sthompsa		} else {
3520209917Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3521209917Sthompsa			    (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3522208019Sthompsa					run_stop(sc);
3523208019Sthompsa			}
3524203134Sthompsa		}
3525203134Sthompsa		RUN_UNLOCK(sc);
3526209917Sthompsa		if (startall)
3527208019Sthompsa			ieee80211_start_all(ic);
3528203134Sthompsa		break;
3529203134Sthompsa	case SIOCGIFMEDIA:
3530203134Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3531203134Sthompsa		break;
3532203134Sthompsa	case SIOCGIFADDR:
3533203134Sthompsa		error = ether_ioctl(ifp, cmd, data);
3534203134Sthompsa		break;
3535203134Sthompsa	default:
3536203134Sthompsa		error = EINVAL;
3537203134Sthompsa		break;
3538203134Sthompsa	}
3539203134Sthompsa
3540203134Sthompsa	return (error);
3541203134Sthompsa}
3542203134Sthompsa
3543203134Sthompsastatic void
3544205042Sthompsarun_set_agc(struct run_softc *sc, uint8_t agc)
3545205042Sthompsa{
3546205042Sthompsa	uint8_t bbp;
3547205042Sthompsa
3548205042Sthompsa	if (sc->mac_ver == 0x3572) {
3549205042Sthompsa		run_bbp_read(sc, 27, &bbp);
3550205042Sthompsa		bbp &= ~(0x3 << 5);
3551205042Sthompsa		run_bbp_write(sc, 27, bbp | 0 << 5);	/* select Rx0 */
3552205042Sthompsa		run_bbp_write(sc, 66, agc);
3553205042Sthompsa		run_bbp_write(sc, 27, bbp | 1 << 5);	/* select Rx1 */
3554205042Sthompsa		run_bbp_write(sc, 66, agc);
3555205042Sthompsa	} else
3556205042Sthompsa		run_bbp_write(sc, 66, agc);
3557205042Sthompsa}
3558205042Sthompsa
3559205042Sthompsastatic void
3560203134Sthompsarun_select_chan_group(struct run_softc *sc, int group)
3561203134Sthompsa{
3562203134Sthompsa	uint32_t tmp;
3563205042Sthompsa	uint8_t agc;
3564203134Sthompsa
3565203134Sthompsa	run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3566203134Sthompsa	run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3567203134Sthompsa	run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3568258082Skevlo	if (sc->mac_ver < 0x3572)
3569257955Skevlo		run_bbp_write(sc, 86, 0x00);
3570203134Sthompsa
3571203134Sthompsa	if (group == 0) {
3572203134Sthompsa		if (sc->ext_2ghz_lna) {
3573257955Skevlo			if (sc->mac_ver >= 0x5390)
3574257955Skevlo				run_bbp_write(sc, 75, 0x52);
3575257955Skevlo			else {
3576257955Skevlo				run_bbp_write(sc, 82, 0x62);
3577257955Skevlo				run_bbp_write(sc, 75, 0x46);
3578257955Skevlo			}
3579203134Sthompsa		} else {
3580257955Skevlo			if (sc->mac_ver >= 0x5390)
3581257955Skevlo				run_bbp_write(sc, 75, 0x50);
3582257955Skevlo			else {
3583257955Skevlo				run_bbp_write(sc, 82, 0x84);
3584257955Skevlo				run_bbp_write(sc, 75, 0x50);
3585257955Skevlo			}
3586203134Sthompsa		}
3587203134Sthompsa	} else {
3588205042Sthompsa		if (sc->mac_ver == 0x3572)
3589205042Sthompsa			run_bbp_write(sc, 82, 0x94);
3590205042Sthompsa		else
3591203134Sthompsa			run_bbp_write(sc, 82, 0xf2);
3592205042Sthompsa		if (sc->ext_5ghz_lna)
3593203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3594205042Sthompsa		else
3595203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3596203134Sthompsa	}
3597203134Sthompsa
3598203134Sthompsa	run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3599203134Sthompsa	tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3600203134Sthompsa	tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3601203134Sthompsa	run_write(sc, RT2860_TX_BAND_CFG, tmp);
3602203134Sthompsa
3603203134Sthompsa	/* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3604208019Sthompsa	tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3605208019Sthompsa	if (sc->nrxchains > 1)
3606208019Sthompsa		tmp |= RT2860_LNA_PE1_EN;
3607203134Sthompsa	if (group == 0) {	/* 2GHz */
3608208019Sthompsa		tmp |= RT2860_PA_PE_G0_EN;
3609203134Sthompsa		if (sc->ntxchains > 1)
3610203134Sthompsa			tmp |= RT2860_PA_PE_G1_EN;
3611203134Sthompsa	} else {		/* 5GHz */
3612208019Sthompsa		tmp |= RT2860_PA_PE_A0_EN;
3613203134Sthompsa		if (sc->ntxchains > 1)
3614203134Sthompsa			tmp |= RT2860_PA_PE_A1_EN;
3615203134Sthompsa	}
3616205042Sthompsa	if (sc->mac_ver == 0x3572) {
3617205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x00);
3618205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3619205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x80);
3620205042Sthompsa	} else
3621205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3622203134Sthompsa
3623203134Sthompsa	/* set initial AGC value */
3624205042Sthompsa	if (group == 0) {	/* 2GHz band */
3625205042Sthompsa		if (sc->mac_ver >= 0x3070)
3626205042Sthompsa			agc = 0x1c + sc->lna[0] * 2;
3627205042Sthompsa		else
3628205042Sthompsa			agc = 0x2e + sc->lna[0];
3629205042Sthompsa	} else {		/* 5GHz band */
3630205042Sthompsa		if (sc->mac_ver == 0x3572)
3631205042Sthompsa			agc = 0x22 + (sc->lna[group] * 5) / 3;
3632205042Sthompsa		else
3633205042Sthompsa			agc = 0x32 + (sc->lna[group] * 5) / 3;
3634205042Sthompsa	}
3635205042Sthompsa	run_set_agc(sc, agc);
3636203134Sthompsa}
3637203134Sthompsa
3638203134Sthompsastatic void
3639257429Shselaskyrun_rt2870_set_chan(struct run_softc *sc, u_int chan)
3640203134Sthompsa{
3641203134Sthompsa	const struct rfprog *rfprog = rt2860_rf2850;
3642203134Sthompsa	uint32_t r2, r3, r4;
3643203134Sthompsa	int8_t txpow1, txpow2;
3644203134Sthompsa	int i;
3645203134Sthompsa
3646203134Sthompsa	/* find the settings for this channel (we know it exists) */
3647203134Sthompsa	for (i = 0; rfprog[i].chan != chan; i++);
3648203134Sthompsa
3649203134Sthompsa	r2 = rfprog[i].r2;
3650203134Sthompsa	if (sc->ntxchains == 1)
3651203134Sthompsa		r2 |= 1 << 12;		/* 1T: disable Tx chain 2 */
3652203134Sthompsa	if (sc->nrxchains == 1)
3653203134Sthompsa		r2 |= 1 << 15 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3654203134Sthompsa	else if (sc->nrxchains == 2)
3655203134Sthompsa		r2 |= 1 << 4;		/* 2R: disable Rx chain 3 */
3656203134Sthompsa
3657203134Sthompsa	/* use Tx power values from EEPROM */
3658203134Sthompsa	txpow1 = sc->txpow1[i];
3659203134Sthompsa	txpow2 = sc->txpow2[i];
3660203134Sthompsa	if (chan > 14) {
3661203134Sthompsa		if (txpow1 >= 0)
3662208019Sthompsa			txpow1 = txpow1 << 1 | 1;
3663203134Sthompsa		else
3664208019Sthompsa			txpow1 = (7 + txpow1) << 1;
3665203134Sthompsa		if (txpow2 >= 0)
3666208019Sthompsa			txpow2 = txpow2 << 1 | 1;
3667203134Sthompsa		else
3668208019Sthompsa			txpow2 = (7 + txpow2) << 1;
3669203134Sthompsa	}
3670203134Sthompsa	r3 = rfprog[i].r3 | txpow1 << 7;
3671203134Sthompsa	r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3672203134Sthompsa
3673203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3674203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3675203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3676203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3677203134Sthompsa
3678203134Sthompsa	run_delay(sc, 10);
3679203134Sthompsa
3680203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3681203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3682203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3683203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3684203134Sthompsa
3685203134Sthompsa	run_delay(sc, 10);
3686203134Sthompsa
3687203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3688203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3689203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3690203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3691203134Sthompsa}
3692203134Sthompsa
3693203134Sthompsastatic void
3694257429Shselaskyrun_rt3070_set_chan(struct run_softc *sc, u_int chan)
3695203134Sthompsa{
3696203134Sthompsa	int8_t txpow1, txpow2;
3697203134Sthompsa	uint8_t rf;
3698205042Sthompsa	int i;
3699203134Sthompsa
3700203134Sthompsa	/* RT3070 is 2GHz only */
3701203134Sthompsa	KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3702203134Sthompsa
3703205042Sthompsa	/* find the settings for this channel (we know it exists) */
3704205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3705205042Sthompsa
3706203134Sthompsa	/* use Tx power values from EEPROM */
3707205042Sthompsa	txpow1 = sc->txpow1[i];
3708205042Sthompsa	txpow2 = sc->txpow2[i];
3709203134Sthompsa
3710205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3711256720Skevlo
3712256720Skevlo	/* RT3370/RT3390: RF R3 [7:4] is not reserved bits. */
3713256720Skevlo	run_rt3070_rf_read(sc, 3, &rf);
3714256720Skevlo	rf = (rf & ~0x0f) | rt3070_freqs[i].k;
3715256720Skevlo	run_rt3070_rf_write(sc, 3, rf);
3716256720Skevlo
3717203134Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3718205042Sthompsa	rf = (rf & ~0x03) | rt3070_freqs[i].r;
3719203134Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3720203134Sthompsa
3721203134Sthompsa	/* set Tx0 power */
3722203134Sthompsa	run_rt3070_rf_read(sc, 12, &rf);
3723203134Sthompsa	rf = (rf & ~0x1f) | txpow1;
3724203134Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3725203134Sthompsa
3726203134Sthompsa	/* set Tx1 power */
3727203134Sthompsa	run_rt3070_rf_read(sc, 13, &rf);
3728203134Sthompsa	rf = (rf & ~0x1f) | txpow2;
3729203134Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3730203134Sthompsa
3731203134Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3732203134Sthompsa	rf &= ~0xfc;
3733203134Sthompsa	if (sc->ntxchains == 1)
3734203134Sthompsa		rf |= 1 << 7 | 1 << 5;	/* 1T: disable Tx chains 2 & 3 */
3735203134Sthompsa	else if (sc->ntxchains == 2)
3736203134Sthompsa		rf |= 1 << 7;		/* 2T: disable Tx chain 3 */
3737203134Sthompsa	if (sc->nrxchains == 1)
3738203134Sthompsa		rf |= 1 << 6 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3739203134Sthompsa	else if (sc->nrxchains == 2)
3740203134Sthompsa		rf |= 1 << 6;		/* 2R: disable Rx chain 3 */
3741203134Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3742203134Sthompsa
3743203134Sthompsa	/* set RF offset */
3744203134Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3745203134Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3746203134Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3747203134Sthompsa
3748203134Sthompsa	/* program RF filter */
3749205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf);	/* Tx */
3750205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3751205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);
3752205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);	/* Rx */
3753205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3754205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);
3755203134Sthompsa
3756203134Sthompsa	/* enable RF tuning */
3757203134Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3758203134Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3759203134Sthompsa}
3760203134Sthompsa
3761203134Sthompsastatic void
3762205042Sthompsarun_rt3572_set_chan(struct run_softc *sc, u_int chan)
3763205042Sthompsa{
3764205042Sthompsa	int8_t txpow1, txpow2;
3765205042Sthompsa	uint32_t tmp;
3766205042Sthompsa	uint8_t rf;
3767205042Sthompsa	int i;
3768205042Sthompsa
3769205042Sthompsa	/* find the settings for this channel (we know it exists) */
3770205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3771205042Sthompsa
3772205042Sthompsa	/* use Tx power values from EEPROM */
3773205042Sthompsa	txpow1 = sc->txpow1[i];
3774205042Sthompsa	txpow2 = sc->txpow2[i];
3775205042Sthompsa
3776205042Sthompsa	if (chan <= 14) {
3777205042Sthompsa		run_bbp_write(sc, 25, sc->bbp25);
3778205042Sthompsa		run_bbp_write(sc, 26, sc->bbp26);
3779205042Sthompsa	} else {
3780205042Sthompsa		/* enable IQ phase correction */
3781205042Sthompsa		run_bbp_write(sc, 25, 0x09);
3782205042Sthompsa		run_bbp_write(sc, 26, 0xff);
3783205042Sthompsa	}
3784205042Sthompsa
3785205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3786205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3787205042Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3788205042Sthompsa	rf  = (rf & ~0x0f) | rt3070_freqs[i].r;
3789205042Sthompsa	rf |= (chan <= 14) ? 0x08 : 0x04;
3790205042Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3791205042Sthompsa
3792205042Sthompsa	/* set PLL mode */
3793205042Sthompsa	run_rt3070_rf_read(sc, 5, &rf);
3794205042Sthompsa	rf &= ~(0x08 | 0x04);
3795205042Sthompsa	rf |= (chan <= 14) ? 0x04 : 0x08;
3796205042Sthompsa	run_rt3070_rf_write(sc, 5, rf);
3797205042Sthompsa
3798205042Sthompsa	/* set Tx power for chain 0 */
3799205042Sthompsa	if (chan <= 14)
3800205042Sthompsa		rf = 0x60 | txpow1;
3801205042Sthompsa	else
3802205042Sthompsa		rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3803205042Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3804205042Sthompsa
3805205042Sthompsa	/* set Tx power for chain 1 */
3806205042Sthompsa	if (chan <= 14)
3807205042Sthompsa		rf = 0x60 | txpow2;
3808205042Sthompsa	else
3809205042Sthompsa		rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3810205042Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3811205042Sthompsa
3812205042Sthompsa	/* set Tx/Rx streams */
3813205042Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3814205042Sthompsa	rf &= ~0xfc;
3815205042Sthompsa	if (sc->ntxchains == 1)
3816205042Sthompsa		rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3817205042Sthompsa	else if (sc->ntxchains == 2)
3818205042Sthompsa		rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3819205042Sthompsa	if (sc->nrxchains == 1)
3820205042Sthompsa		rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3821205042Sthompsa	else if (sc->nrxchains == 2)
3822205042Sthompsa		rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3823205042Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3824205042Sthompsa
3825205042Sthompsa	/* set RF offset */
3826205042Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3827205042Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3828205042Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3829205042Sthompsa
3830205042Sthompsa	/* program RF filter */
3831205042Sthompsa	rf = sc->rf24_20mhz;
3832205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);	/* Tx */
3833205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);	/* Rx */
3834205042Sthompsa
3835205042Sthompsa	/* enable RF tuning */
3836205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3837205042Sthompsa	rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3838205042Sthompsa	run_rt3070_rf_write(sc, 7, rf);
3839205042Sthompsa
3840205042Sthompsa	/* TSSI */
3841205042Sthompsa	rf = (chan <= 14) ? 0xc3 : 0xc0;
3842205042Sthompsa	run_rt3070_rf_write(sc, 9, rf);
3843205042Sthompsa
3844205042Sthompsa	/* set loop filter 1 */
3845205042Sthompsa	run_rt3070_rf_write(sc, 10, 0xf1);
3846205042Sthompsa	/* set loop filter 2 */
3847205042Sthompsa	run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3848205042Sthompsa
3849205042Sthompsa	/* set tx_mx2_ic */
3850205042Sthompsa	run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3851205042Sthompsa	/* set tx_mx1_ic */
3852205042Sthompsa	if (chan <= 14)
3853205042Sthompsa		rf = 0x48 | sc->txmixgain_2ghz;
3854205042Sthompsa	else
3855205042Sthompsa		rf = 0x78 | sc->txmixgain_5ghz;
3856205042Sthompsa	run_rt3070_rf_write(sc, 16, rf);
3857205042Sthompsa
3858205042Sthompsa	/* set tx_lo1 */
3859205042Sthompsa	run_rt3070_rf_write(sc, 17, 0x23);
3860205042Sthompsa	/* set tx_lo2 */
3861205042Sthompsa	if (chan <= 14)
3862205042Sthompsa		rf = 0x93;
3863205042Sthompsa	else if (chan <= 64)
3864205042Sthompsa		rf = 0xb7;
3865205042Sthompsa	else if (chan <= 128)
3866205042Sthompsa		rf = 0x74;
3867205042Sthompsa	else
3868205042Sthompsa		rf = 0x72;
3869205042Sthompsa	run_rt3070_rf_write(sc, 19, rf);
3870205042Sthompsa
3871205042Sthompsa	/* set rx_lo1 */
3872205042Sthompsa	if (chan <= 14)
3873205042Sthompsa		rf = 0xb3;
3874205042Sthompsa	else if (chan <= 64)
3875205042Sthompsa		rf = 0xf6;
3876205042Sthompsa	else if (chan <= 128)
3877205042Sthompsa		rf = 0xf4;
3878205042Sthompsa	else
3879205042Sthompsa		rf = 0xf3;
3880205042Sthompsa	run_rt3070_rf_write(sc, 20, rf);
3881205042Sthompsa
3882205042Sthompsa	/* set pfd_delay */
3883205042Sthompsa	if (chan <= 14)
3884205042Sthompsa		rf = 0x15;
3885205042Sthompsa	else if (chan <= 64)
3886205042Sthompsa		rf = 0x3d;
3887205042Sthompsa	else
3888205042Sthompsa		rf = 0x01;
3889205042Sthompsa	run_rt3070_rf_write(sc, 25, rf);
3890205042Sthompsa
3891205042Sthompsa	/* set rx_lo2 */
3892205042Sthompsa	run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3893205042Sthompsa	/* set ldo_rf_vc */
3894205042Sthompsa	run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3895205042Sthompsa	/* set drv_cc */
3896205042Sthompsa	run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3897205042Sthompsa
3898205042Sthompsa	run_read(sc, RT2860_GPIO_CTRL, &tmp);
3899205042Sthompsa	tmp &= ~0x8080;
3900205042Sthompsa	if (chan <= 14)
3901205042Sthompsa		tmp |= 0x80;
3902205042Sthompsa	run_write(sc, RT2860_GPIO_CTRL, tmp);
3903205042Sthompsa
3904205042Sthompsa	/* enable RF tuning */
3905205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3906205042Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3907205042Sthompsa
3908205042Sthompsa	run_delay(sc, 2);
3909205042Sthompsa}
3910205042Sthompsa
3911205042Sthompsastatic void
3912257955Skevlorun_rt5390_set_chan(struct run_softc *sc, u_int chan)
3913257955Skevlo{
3914257955Skevlo	int8_t txpow1, txpow2;
3915257955Skevlo	uint8_t rf;
3916257955Skevlo	int i;
3917257955Skevlo
3918257955Skevlo	/* find the settings for this channel (we know it exists) */
3919257955Skevlo	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3920257955Skevlo
3921257955Skevlo	/* use Tx power values from EEPROM */
3922257955Skevlo	txpow1 = sc->txpow1[i];
3923257955Skevlo	txpow2 = sc->txpow2[i];
3924257955Skevlo
3925257955Skevlo	run_rt3070_rf_write(sc, 8, rt3070_freqs[i].n);
3926257955Skevlo	run_rt3070_rf_write(sc, 9, rt3070_freqs[i].k & 0x0f);
3927257955Skevlo	run_rt3070_rf_read(sc, 11, &rf);
3928257955Skevlo	rf = (rf & ~0x03) | (rt3070_freqs[i].r & 0x03);
3929257955Skevlo	run_rt3070_rf_write(sc, 11, rf);
3930257955Skevlo
3931257955Skevlo	run_rt3070_rf_read(sc, 49, &rf);
3932257955Skevlo	rf = (rf & ~0x3f) | (txpow1 & 0x3f);
3933257955Skevlo	/* The valid range of the RF R49 is 0x00 to 0x27. */
3934257955Skevlo	if ((rf & 0x3f) > 0x27)
3935257955Skevlo		rf = (rf & ~0x3f) | 0x27;
3936257955Skevlo	run_rt3070_rf_write(sc, 49, rf);
3937257955Skevlo
3938257955Skevlo	if (sc->mac_ver == 0x5392) {
3939257955Skevlo		run_rt3070_rf_read(sc, 50, &rf);
3940257955Skevlo		rf = (rf & ~0x3f) | (txpow2 & 0x3f);
3941257955Skevlo		/* The valid range of the RF R50 is 0x00 to 0x27. */
3942257955Skevlo		if ((rf & 0x3f) > 0x27)
3943257955Skevlo			rf = (rf & ~0x3f) | 0x27;
3944257955Skevlo		run_rt3070_rf_write(sc, 50, rf);
3945257955Skevlo	}
3946257955Skevlo
3947257955Skevlo	run_rt3070_rf_read(sc, 1, &rf);
3948257955Skevlo	rf |= RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | RT3070_TX0_PD;
3949257955Skevlo	if (sc->mac_ver == 0x5392)
3950257955Skevlo		rf |= RT3070_RX1_PD | RT3070_TX1_PD;
3951257955Skevlo	run_rt3070_rf_write(sc, 1, rf);
3952257955Skevlo
3953257955Skevlo	if (sc->mac_ver != 0x5392) {
3954257955Skevlo		run_rt3070_rf_read(sc, 2, &rf);
3955257955Skevlo		rf |= 0x80;
3956257955Skevlo		run_rt3070_rf_write(sc, 2, rf);
3957257955Skevlo		run_delay(sc, 10);
3958257955Skevlo		rf &= 0x7f;
3959257955Skevlo		run_rt3070_rf_write(sc, 2, rf);
3960257955Skevlo	}
3961257955Skevlo
3962257955Skevlo	run_adjust_freq_offset(sc);
3963257955Skevlo
3964257955Skevlo	if (sc->mac_ver == 0x5392) {
3965257955Skevlo		/* Fix for RT5392C. */
3966257955Skevlo		if (sc->mac_rev >= 0x0223) {
3967257955Skevlo			if ((chan >= 1) && (chan <= 4))
3968257955Skevlo				rf = 0x0f;
3969257955Skevlo			else if ((chan >= 5) && (chan <= 7))
3970257955Skevlo				rf = 0x0e;
3971257955Skevlo			else if ((chan >= 8) && (chan <= 14))
3972257955Skevlo				rf = 0x0d;
3973257955Skevlo			run_rt3070_rf_write(sc, 23, rf);
3974257955Skevlo
3975257955Skevlo			if ((chan >= 1) && (chan <= 4))
3976257955Skevlo				rf = 0x0c;
3977257955Skevlo			else if (chan == 5)
3978257955Skevlo				rf = 0x0b;
3979257955Skevlo			else if ((chan >= 6) && (chan <= 7))
3980257955Skevlo				rf = 0x0a;
3981257955Skevlo			else if ((chan >= 8) && (chan <= 10))
3982257955Skevlo				rf = 0x09;
3983257955Skevlo			else if ((chan >= 11) && (chan <= 14))
3984257955Skevlo				rf = 0x08;
3985257955Skevlo			run_rt3070_rf_write(sc, 59, rf);
3986257955Skevlo		} else {
3987257955Skevlo			if ((chan >= 1) && (chan <= 11))
3988257955Skevlo				rf = 0x0f;
3989257955Skevlo			else if ((chan >= 12) && (chan <= 14))
3990257955Skevlo				rf = 0x0b;
3991257955Skevlo			run_rt3070_rf_write(sc, 59, rf);
3992257955Skevlo		}
3993257955Skevlo	} else {
3994257955Skevlo		/* Fix for RT5390F. */
3995257955Skevlo		if (sc->mac_rev >= 0x0502) {
3996257955Skevlo			if ((chan >= 1) && (chan <= 11))
3997257955Skevlo				rf = 0x43;
3998257955Skevlo			else if ((chan >= 12) && (chan <= 14))
3999257955Skevlo				rf = 0x23;
4000257955Skevlo			run_rt3070_rf_write(sc, 55, rf);
4001257955Skevlo
4002257955Skevlo			if ((chan >= 1) && (chan <= 11))
4003257955Skevlo				rf = 0x0f;
4004257955Skevlo			else if (chan == 12)
4005257955Skevlo				rf = 0x0d;
4006257955Skevlo			else if ((chan >= 13) && (chan <= 14))
4007257955Skevlo				rf = 0x0b;
4008257955Skevlo			run_rt3070_rf_write(sc, 59, rf);
4009257955Skevlo		} else {
4010257955Skevlo			run_rt3070_rf_write(sc, 55, 0x44);
4011257955Skevlo			run_rt3070_rf_write(sc, 59, 0x8f);
4012257955Skevlo		}
4013257955Skevlo	}
4014257955Skevlo
4015257955Skevlo	/* Enable VCO calibration. */
4016257955Skevlo	run_rt3070_rf_read(sc, 3, &rf);
4017257955Skevlo	rf |= RT5390_VCOCAL;
4018257955Skevlo	run_rt3070_rf_write(sc, 3, rf);
4019257955Skevlo}
4020257955Skevlo
4021257955Skevlostatic void
4022203134Sthompsarun_set_rx_antenna(struct run_softc *sc, int aux)
4023203134Sthompsa{
4024203134Sthompsa	uint32_t tmp;
4025257955Skevlo	uint8_t bbp152;
4026203134Sthompsa
4027203134Sthompsa	if (aux) {
4028257955Skevlo		if (sc->rf_rev == RT5390_RF_5370) {
4029257955Skevlo			run_bbp_read(sc, 152, &bbp152);
4030257955Skevlo			run_bbp_write(sc, 152, bbp152 & ~0x80);
4031257955Skevlo		}
4032257955Skevlo		if (sc->rf_rev == RT3070_RF_3020) {
4033257955Skevlo			run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
4034257955Skevlo			run_read(sc, RT2860_GPIO_CTRL, &tmp);
4035257955Skevlo			run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
4036257955Skevlo		}
4037203134Sthompsa	} else {
4038257955Skevlo		if (sc->rf_rev == RT5390_RF_5370) {
4039257955Skevlo			run_bbp_read(sc, 152, &bbp152);
4040257955Skevlo			run_bbp_write(sc, 152, bbp152 | 0x80);
4041257955Skevlo		}
4042257955Skevlo		if (sc->rf_rev == RT3070_RF_3020) {
4043257955Skevlo			run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
4044257955Skevlo			run_read(sc, RT2860_GPIO_CTRL, &tmp);
4045257955Skevlo			run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
4046257955Skevlo		}
4047203134Sthompsa	}
4048203134Sthompsa}
4049203134Sthompsa
4050203134Sthompsastatic int
4051203134Sthompsarun_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
4052203134Sthompsa{
4053203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4054257429Shselasky	u_int chan, group;
4055203134Sthompsa
4056203134Sthompsa	chan = ieee80211_chan2ieee(ic, c);
4057203134Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
4058209917Sthompsa		return (EINVAL);
4059203134Sthompsa
4060257955Skevlo	if (sc->mac_ver >= 0x5390)
4061257955Skevlo		run_rt5390_set_chan(sc, chan);
4062257955Skevlo	else if (sc->mac_ver == 0x3572)
4063205042Sthompsa		run_rt3572_set_chan(sc, chan);
4064205042Sthompsa	else if (sc->mac_ver >= 0x3070)
4065203134Sthompsa		run_rt3070_set_chan(sc, chan);
4066203134Sthompsa	else
4067203134Sthompsa		run_rt2870_set_chan(sc, chan);
4068203134Sthompsa
4069203134Sthompsa	/* determine channel group */
4070203134Sthompsa	if (chan <= 14)
4071203134Sthompsa		group = 0;
4072203134Sthompsa	else if (chan <= 64)
4073203134Sthompsa		group = 1;
4074203134Sthompsa	else if (chan <= 128)
4075203134Sthompsa		group = 2;
4076203134Sthompsa	else
4077203134Sthompsa		group = 3;
4078203134Sthompsa
4079203134Sthompsa	/* XXX necessary only when group has changed! */
4080203134Sthompsa	run_select_chan_group(sc, group);
4081203134Sthompsa
4082203134Sthompsa	run_delay(sc, 10);
4083203134Sthompsa
4084209917Sthompsa	return (0);
4085203134Sthompsa}
4086203134Sthompsa
4087203134Sthompsastatic void
4088203134Sthompsarun_set_channel(struct ieee80211com *ic)
4089203134Sthompsa{
4090203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4091203134Sthompsa
4092203134Sthompsa	RUN_LOCK(sc);
4093203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
4094203134Sthompsa	RUN_UNLOCK(sc);
4095203134Sthompsa
4096203134Sthompsa	return;
4097203134Sthompsa}
4098203134Sthompsa
4099203134Sthompsastatic void
4100203134Sthompsarun_scan_start(struct ieee80211com *ic)
4101203134Sthompsa{
4102203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4103203134Sthompsa	uint32_t tmp;
4104203134Sthompsa
4105203134Sthompsa	RUN_LOCK(sc);
4106203134Sthompsa
4107203134Sthompsa	/* abort TSF synchronization */
4108203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4109203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG,
4110203134Sthompsa	    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4111203134Sthompsa	    RT2860_TBTT_TIMER_EN));
4112203134Sthompsa	run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
4113203134Sthompsa
4114203134Sthompsa	RUN_UNLOCK(sc);
4115203134Sthompsa
4116203134Sthompsa	return;
4117203134Sthompsa}
4118203134Sthompsa
4119203134Sthompsastatic void
4120203134Sthompsarun_scan_end(struct ieee80211com *ic)
4121203134Sthompsa{
4122203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4123203134Sthompsa
4124203134Sthompsa	RUN_LOCK(sc);
4125203134Sthompsa
4126203134Sthompsa	run_enable_tsf_sync(sc);
4127203134Sthompsa	/* XXX keep local copy */
4128203134Sthompsa	run_set_bssid(sc, sc->sc_bssid);
4129203134Sthompsa
4130203134Sthompsa	RUN_UNLOCK(sc);
4131203134Sthompsa
4132203134Sthompsa	return;
4133203134Sthompsa}
4134203134Sthompsa
4135208019Sthompsa/*
4136208019Sthompsa * Could be called from ieee80211_node_timeout()
4137208019Sthompsa * (non-sleepable thread)
4138208019Sthompsa */
4139208019Sthompsastatic void
4140208019Sthompsarun_update_beacon(struct ieee80211vap *vap, int item)
4141203134Sthompsa{
4142208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
4143208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4144218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
4145218492Sbschmidt	int mcast = 0;
4146208019Sthompsa	uint32_t i;
4147208019Sthompsa
4148218492Sbschmidt	KASSERT(vap != NULL, ("no beacon"));
4149218492Sbschmidt
4150218492Sbschmidt	switch (item) {
4151218492Sbschmidt	case IEEE80211_BEACON_ERP:
4152218492Sbschmidt		run_updateslot(ic->ic_ifp);
4153218492Sbschmidt		break;
4154218492Sbschmidt	case IEEE80211_BEACON_HTINFO:
4155218492Sbschmidt		run_updateprot(ic);
4156218492Sbschmidt		break;
4157218492Sbschmidt	case IEEE80211_BEACON_TIM:
4158218492Sbschmidt		mcast = 1;	/*TODO*/
4159218492Sbschmidt		break;
4160218492Sbschmidt	default:
4161218492Sbschmidt		break;
4162218492Sbschmidt	}
4163218492Sbschmidt
4164218492Sbschmidt	setbit(rvp->bo.bo_flags, item);
4165218492Sbschmidt	ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
4166218492Sbschmidt
4167208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
4168208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
4169208019Sthompsa	sc->cmdq[i].func = run_update_beacon_cb;
4170208019Sthompsa	sc->cmdq[i].arg0 = vap;
4171208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
4172208019Sthompsa
4173208019Sthompsa	return;
4174203134Sthompsa}
4175203134Sthompsa
4176203134Sthompsastatic void
4177208019Sthompsarun_update_beacon_cb(void *arg)
4178203134Sthompsa{
4179208019Sthompsa	struct ieee80211vap *vap = arg;
4180218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
4181203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
4182203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4183203134Sthompsa	struct rt2860_txwi txwi;
4184203134Sthompsa	struct mbuf *m;
4185208019Sthompsa	uint8_t ridx;
4186203134Sthompsa
4187209917Sthompsa	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
4188208019Sthompsa		return;
4189236439Shselasky	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
4190236439Shselasky		return;
4191208019Sthompsa
4192218492Sbschmidt	/*
4193218492Sbschmidt	 * No need to call ieee80211_beacon_update(), run_update_beacon()
4194218492Sbschmidt	 * is taking care of apropriate calls.
4195218492Sbschmidt	 */
4196218492Sbschmidt	if (rvp->beacon_mbuf == NULL) {
4197218492Sbschmidt		rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
4198218492Sbschmidt		    &rvp->bo);
4199218492Sbschmidt		if (rvp->beacon_mbuf == NULL)
4200218492Sbschmidt			return;
4201218492Sbschmidt	}
4202218492Sbschmidt	m = rvp->beacon_mbuf;
4203203134Sthompsa
4204203134Sthompsa	memset(&txwi, 0, sizeof txwi);
4205203134Sthompsa	txwi.wcid = 0xff;
4206203134Sthompsa	txwi.len = htole16(m->m_pkthdr.len);
4207203134Sthompsa	/* send beacons at the lowest available rate */
4208208019Sthompsa	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4209208019Sthompsa	    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
4210208019Sthompsa	txwi.phy = htole16(rt2860_rates[ridx].mcs);
4211208019Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
4212203134Sthompsa	        txwi.phy |= htole16(RT2860_PHY_OFDM);
4213203134Sthompsa	txwi.txop = RT2860_TX_TXOP_HT;
4214203134Sthompsa	txwi.flags = RT2860_TX_TS;
4215209144Sthompsa	txwi.xflags = RT2860_TX_NSEQ;
4216203134Sthompsa
4217218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
4218208019Sthompsa	    (uint8_t *)&txwi, sizeof txwi);
4219218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
4220203134Sthompsa	    mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);	/* roundup len */
4221203134Sthompsa
4222203134Sthompsa	return;
4223203134Sthompsa}
4224203134Sthompsa
4225203134Sthompsastatic void
4226203134Sthompsarun_updateprot(struct ieee80211com *ic)
4227203134Sthompsa{
4228203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4229218492Sbschmidt	uint32_t i;
4230218492Sbschmidt
4231218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4232218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4233218492Sbschmidt	sc->cmdq[i].func = run_updateprot_cb;
4234218492Sbschmidt	sc->cmdq[i].arg0 = ic;
4235218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4236218492Sbschmidt}
4237218492Sbschmidt
4238218492Sbschmidtstatic void
4239218492Sbschmidtrun_updateprot_cb(void *arg)
4240218492Sbschmidt{
4241218492Sbschmidt	struct ieee80211com *ic = arg;
4242218492Sbschmidt	struct run_softc *sc = ic->ic_ifp->if_softc;
4243203134Sthompsa	uint32_t tmp;
4244203134Sthompsa
4245203134Sthompsa	tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4246203134Sthompsa	/* setup protection frame rate (MCS code) */
4247203134Sthompsa	tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4248203134Sthompsa	    rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4249203134Sthompsa	    rt2860_rates[RT2860_RIDX_CCK11].mcs;
4250203134Sthompsa
4251203134Sthompsa	/* CCK frames don't require protection */
4252203134Sthompsa	run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4253203134Sthompsa	if (ic->ic_flags & IEEE80211_F_USEPROT) {
4254203134Sthompsa		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4255203134Sthompsa			tmp |= RT2860_PROT_CTRL_RTS_CTS;
4256203134Sthompsa		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4257203134Sthompsa			tmp |= RT2860_PROT_CTRL_CTS;
4258203134Sthompsa	}
4259203134Sthompsa	run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4260203134Sthompsa}
4261203134Sthompsa
4262203134Sthompsastatic void
4263208019Sthompsarun_usb_timeout_cb(void *arg)
4264203134Sthompsa{
4265208019Sthompsa	struct ieee80211vap *vap = arg;
4266208019Sthompsa	struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4267203134Sthompsa
4268208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4269203134Sthompsa
4270203134Sthompsa	if(vap->iv_state == IEEE80211_S_RUN &&
4271203134Sthompsa	    vap->iv_opmode != IEEE80211_M_STA)
4272203134Sthompsa		run_reset_livelock(sc);
4273209917Sthompsa	else if (vap->iv_state == IEEE80211_S_SCAN) {
4274203134Sthompsa		DPRINTF("timeout caused by scan\n");
4275203134Sthompsa		/* cancel bgscan */
4276203134Sthompsa		ieee80211_cancel_scan(vap);
4277203134Sthompsa	} else
4278203134Sthompsa		DPRINTF("timeout by unknown cause\n");
4279203134Sthompsa}
4280203134Sthompsa
4281203134Sthompsastatic void
4282203134Sthompsarun_reset_livelock(struct run_softc *sc)
4283203134Sthompsa{
4284203134Sthompsa	uint32_t tmp;
4285203134Sthompsa
4286208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4287208019Sthompsa
4288203134Sthompsa	/*
4289203134Sthompsa	 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4290203134Sthompsa	 * can run into a livelock and start sending CTS-to-self frames like
4291203134Sthompsa	 * crazy if protection is enabled.  Reset MAC/BBP for a while
4292203134Sthompsa	 */
4293203134Sthompsa	run_read(sc, RT2860_DEBUG, &tmp);
4294208019Sthompsa	DPRINTFN(3, "debug reg %08x\n", tmp);
4295209917Sthompsa	if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4296203134Sthompsa		DPRINTF("CTS-to-self livelock detected\n");
4297203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4298203134Sthompsa		run_delay(sc, 1);
4299203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL,
4300203134Sthompsa		    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4301203134Sthompsa	}
4302203134Sthompsa}
4303203134Sthompsa
4304203134Sthompsastatic void
4305203134Sthompsarun_update_promisc_locked(struct ifnet *ifp)
4306203134Sthompsa{
4307203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4308203134Sthompsa        uint32_t tmp;
4309203134Sthompsa
4310203134Sthompsa	run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4311203134Sthompsa
4312203134Sthompsa	tmp |= RT2860_DROP_UC_NOME;
4313203134Sthompsa        if (ifp->if_flags & IFF_PROMISC)
4314203134Sthompsa		tmp &= ~RT2860_DROP_UC_NOME;
4315203134Sthompsa
4316203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4317203134Sthompsa
4318203134Sthompsa        DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4319203134Sthompsa            "entering" : "leaving");
4320203134Sthompsa}
4321203134Sthompsa
4322203134Sthompsastatic void
4323203134Sthompsarun_update_promisc(struct ifnet *ifp)
4324203134Sthompsa{
4325203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4326203134Sthompsa
4327203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4328203134Sthompsa		return;
4329203134Sthompsa
4330203134Sthompsa	RUN_LOCK(sc);
4331203134Sthompsa	run_update_promisc_locked(ifp);
4332203134Sthompsa	RUN_UNLOCK(sc);
4333203134Sthompsa}
4334203134Sthompsa
4335203134Sthompsastatic void
4336203134Sthompsarun_enable_tsf_sync(struct run_softc *sc)
4337203134Sthompsa{
4338208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4339203134Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4340203134Sthompsa	uint32_t tmp;
4341203134Sthompsa
4342257955Skevlo	DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id,
4343257955Skevlo	    ic->ic_opmode);
4344208019Sthompsa
4345203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4346203134Sthompsa	tmp &= ~0x1fffff;
4347203134Sthompsa	tmp |= vap->iv_bss->ni_intval * 16;
4348203134Sthompsa	tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4349203134Sthompsa
4350208019Sthompsa	if (ic->ic_opmode == IEEE80211_M_STA) {
4351203134Sthompsa		/*
4352203134Sthompsa		 * Local TSF is always updated with remote TSF on beacon
4353203134Sthompsa		 * reception.
4354203134Sthompsa		 */
4355203134Sthompsa		tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4356208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4357203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4358203134Sthompsa	        /*
4359203134Sthompsa	         * Local TSF is updated with remote TSF on beacon reception
4360203134Sthompsa	         * only if the remote TSF is greater than local TSF.
4361203134Sthompsa	         */
4362203134Sthompsa	        tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4363208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4364208019Sthompsa		    ic->ic_opmode == IEEE80211_M_MBSS) {
4365203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4366203134Sthompsa	        /* SYNC with nobody */
4367203134Sthompsa	        tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4368208019Sthompsa	} else {
4369203134Sthompsa		DPRINTF("Enabling TSF failed. undefined opmode\n");
4370208019Sthompsa		return;
4371208019Sthompsa	}
4372203134Sthompsa
4373203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4374203134Sthompsa}
4375203134Sthompsa
4376203134Sthompsastatic void
4377203134Sthompsarun_enable_mrr(struct run_softc *sc)
4378203134Sthompsa{
4379203134Sthompsa#define CCK(mcs)	(mcs)
4380203134Sthompsa#define OFDM(mcs)	(1 << 3 | (mcs))
4381203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG0,
4382203134Sthompsa	    OFDM(6) << 28 |	/* 54->48 */
4383203134Sthompsa	    OFDM(5) << 24 |	/* 48->36 */
4384203134Sthompsa	    OFDM(4) << 20 |	/* 36->24 */
4385203134Sthompsa	    OFDM(3) << 16 |	/* 24->18 */
4386203134Sthompsa	    OFDM(2) << 12 |	/* 18->12 */
4387203134Sthompsa	    OFDM(1) <<  8 |	/* 12-> 9 */
4388203134Sthompsa	    OFDM(0) <<  4 |	/*  9-> 6 */
4389203134Sthompsa	    OFDM(0));		/*  6-> 6 */
4390203134Sthompsa
4391203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG1,
4392203134Sthompsa	    CCK(2) << 12 |	/* 11->5.5 */
4393203134Sthompsa	    CCK(1) <<  8 |	/* 5.5-> 2 */
4394203134Sthompsa	    CCK(0) <<  4 |	/*   2-> 1 */
4395203134Sthompsa	    CCK(0));		/*   1-> 1 */
4396203134Sthompsa#undef OFDM
4397203134Sthompsa#undef CCK
4398203134Sthompsa}
4399203134Sthompsa
4400203134Sthompsastatic void
4401203134Sthompsarun_set_txpreamble(struct run_softc *sc)
4402203134Sthompsa{
4403203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4404203134Sthompsa	uint32_t tmp;
4405203134Sthompsa
4406203134Sthompsa	run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4407203134Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4408203134Sthompsa		tmp |= RT2860_CCK_SHORT_EN;
4409203134Sthompsa	else
4410203134Sthompsa		tmp &= ~RT2860_CCK_SHORT_EN;
4411203134Sthompsa	run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4412203134Sthompsa}
4413203134Sthompsa
4414203134Sthompsastatic void
4415203134Sthompsarun_set_basicrates(struct run_softc *sc)
4416203134Sthompsa{
4417203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4418203134Sthompsa
4419203134Sthompsa	/* set basic rates mask */
4420203134Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
4421203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4422203134Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
4423203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4424203134Sthompsa	else	/* 11g */
4425203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4426203134Sthompsa}
4427203134Sthompsa
4428203134Sthompsastatic void
4429203134Sthompsarun_set_leds(struct run_softc *sc, uint16_t which)
4430203134Sthompsa{
4431203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4432203134Sthompsa	    which | (sc->leds & 0x7f));
4433203134Sthompsa}
4434203134Sthompsa
4435203134Sthompsastatic void
4436203134Sthompsarun_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4437203134Sthompsa{
4438203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW0,
4439203134Sthompsa	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4440203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW1,
4441203134Sthompsa	    bssid[4] | bssid[5] << 8);
4442203134Sthompsa}
4443203134Sthompsa
4444203134Sthompsastatic void
4445203134Sthompsarun_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4446203134Sthompsa{
4447203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW0,
4448203134Sthompsa	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4449203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW1,
4450203134Sthompsa	    addr[4] | addr[5] << 8 | 0xff << 16);
4451203134Sthompsa}
4452203134Sthompsa
4453203134Sthompsastatic void
4454203134Sthompsarun_updateslot(struct ifnet *ifp)
4455203134Sthompsa{
4456203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4457203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4458218492Sbschmidt	uint32_t i;
4459218492Sbschmidt
4460218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4461218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4462218492Sbschmidt	sc->cmdq[i].func = run_updateslot_cb;
4463218492Sbschmidt	sc->cmdq[i].arg0 = ifp;
4464218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4465218492Sbschmidt
4466218492Sbschmidt	return;
4467218492Sbschmidt}
4468218492Sbschmidt
4469218492Sbschmidt/* ARGSUSED */
4470218492Sbschmidtstatic void
4471218492Sbschmidtrun_updateslot_cb(void *arg)
4472218492Sbschmidt{
4473218492Sbschmidt	struct ifnet *ifp = arg;
4474218492Sbschmidt	struct run_softc *sc = ifp->if_softc;
4475218492Sbschmidt	struct ieee80211com *ic = ifp->if_l2com;
4476203134Sthompsa	uint32_t tmp;
4477203134Sthompsa
4478203134Sthompsa	run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4479203134Sthompsa	tmp &= ~0xff;
4480203134Sthompsa	tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4481203134Sthompsa	run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4482203134Sthompsa}
4483203134Sthompsa
4484208019Sthompsastatic void
4485208019Sthompsarun_update_mcast(struct ifnet *ifp)
4486208019Sthompsa{
4487208019Sthompsa	/* h/w filter supports getting everything or nothing */
4488208019Sthompsa	ifp->if_flags |= IFF_ALLMULTI;
4489208019Sthompsa}
4490208019Sthompsa
4491203134Sthompsastatic int8_t
4492203134Sthompsarun_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4493203134Sthompsa{
4494203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4495203134Sthompsa	struct ieee80211_channel *c = ic->ic_curchan;
4496203134Sthompsa	int delta;
4497203134Sthompsa
4498203134Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
4499257429Shselasky		u_int chan = ieee80211_chan2ieee(ic, c);
4500203134Sthompsa		delta = sc->rssi_5ghz[rxchain];
4501203134Sthompsa
4502203134Sthompsa		/* determine channel group */
4503203134Sthompsa		if (chan <= 64)
4504203134Sthompsa			delta -= sc->lna[1];
4505203134Sthompsa		else if (chan <= 128)
4506203134Sthompsa			delta -= sc->lna[2];
4507203134Sthompsa		else
4508203134Sthompsa			delta -= sc->lna[3];
4509203134Sthompsa	} else
4510203134Sthompsa		delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4511203134Sthompsa
4512209917Sthompsa	return (-12 - delta - rssi);
4513203134Sthompsa}
4514203134Sthompsa
4515257955Skevlostatic void
4516257955Skevlorun_rt5390_bbp_init(struct run_softc *sc)
4517257955Skevlo{
4518257955Skevlo	int i;
4519257955Skevlo	uint8_t bbp4;
4520257955Skevlo
4521257955Skevlo	/* Avoid data lost and CRC error. */
4522257955Skevlo	run_bbp_read(sc, 4, &bbp4);
4523257955Skevlo	run_bbp_write(sc, 4, bbp4 | 0x40);
4524257955Skevlo
4525257955Skevlo	for (i = 0; i < nitems(rt5390_def_bbp); i++) {
4526257955Skevlo		run_bbp_write(sc, rt5390_def_bbp[i].reg,
4527257955Skevlo		    rt5390_def_bbp[i].val);
4528257955Skevlo	}
4529257955Skevlo	if (sc->mac_ver == 0x5392) {
4530257955Skevlo		run_bbp_write(sc, 88, 0x90);
4531257955Skevlo		run_bbp_write(sc, 95, 0x9a);
4532257955Skevlo		run_bbp_write(sc, 98, 0x12);
4533257955Skevlo		run_bbp_write(sc, 106, 0x12);
4534257955Skevlo		run_bbp_write(sc, 134, 0xd0);
4535257955Skevlo		run_bbp_write(sc, 135, 0xf6);
4536257955Skevlo		run_bbp_write(sc, 148, 0x84);
4537257955Skevlo	}
4538257955Skevlo
4539257955Skevlo	/* Disable hardware antenna diversity. */
4540257955Skevlo	if (sc->mac_ver == 0x5390)
4541257955Skevlo		run_bbp_write(sc, 154, 0);
4542257955Skevlo}
4543257955Skevlo
4544203134Sthompsastatic int
4545203134Sthompsarun_bbp_init(struct run_softc *sc)
4546203134Sthompsa{
4547203134Sthompsa	int i, error, ntries;
4548203134Sthompsa	uint8_t bbp0;
4549203134Sthompsa
4550203134Sthompsa	/* wait for BBP to wake up */
4551203134Sthompsa	for (ntries = 0; ntries < 20; ntries++) {
4552203134Sthompsa		if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4553203134Sthompsa			return error;
4554203134Sthompsa		if (bbp0 != 0 && bbp0 != 0xff)
4555203134Sthompsa			break;
4556203134Sthompsa	}
4557203134Sthompsa	if (ntries == 20)
4558209917Sthompsa		return (ETIMEDOUT);
4559203134Sthompsa
4560203134Sthompsa	/* initialize BBP registers to default values */
4561257955Skevlo	if (sc->mac_ver >= 0x5390)
4562257955Skevlo		run_rt5390_bbp_init(sc);
4563257955Skevlo	else {
4564257955Skevlo		for (i = 0; i < nitems(rt2860_def_bbp); i++) {
4565257955Skevlo			run_bbp_write(sc, rt2860_def_bbp[i].reg,
4566257955Skevlo			    rt2860_def_bbp[i].val);
4567257955Skevlo		}
4568203134Sthompsa	}
4569203134Sthompsa
4570203134Sthompsa	/* fix BBP84 for RT2860E */
4571205042Sthompsa	if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4572205042Sthompsa		run_bbp_write(sc, 84, 0x19);
4573203134Sthompsa
4574205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4575203134Sthompsa		run_bbp_write(sc, 79, 0x13);
4576203134Sthompsa		run_bbp_write(sc, 80, 0x05);
4577203134Sthompsa		run_bbp_write(sc, 81, 0x33);
4578205042Sthompsa	} else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4579203134Sthompsa		run_bbp_write(sc, 69, 0x16);
4580203134Sthompsa		run_bbp_write(sc, 73, 0x12);
4581203134Sthompsa	}
4582209917Sthompsa	return (0);
4583203134Sthompsa}
4584203134Sthompsa
4585203134Sthompsastatic int
4586203134Sthompsarun_rt3070_rf_init(struct run_softc *sc)
4587203134Sthompsa{
4588203134Sthompsa	uint32_t tmp;
4589256722Skevlo	uint8_t bbp4, mingain, rf, target;
4590203134Sthompsa	int i;
4591203134Sthompsa
4592203134Sthompsa	run_rt3070_rf_read(sc, 30, &rf);
4593203134Sthompsa	/* toggle RF R30 bit 7 */
4594203134Sthompsa	run_rt3070_rf_write(sc, 30, rf | 0x80);
4595203134Sthompsa	run_delay(sc, 10);
4596203134Sthompsa	run_rt3070_rf_write(sc, 30, rf & ~0x80);
4597203134Sthompsa
4598203134Sthompsa	/* initialize RF registers to default value */
4599205042Sthompsa	if (sc->mac_ver == 0x3572) {
4600257955Skevlo		for (i = 0; i < nitems(rt3572_def_rf); i++) {
4601205042Sthompsa			run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4602205042Sthompsa			    rt3572_def_rf[i].val);
4603205042Sthompsa		}
4604205042Sthompsa	} else {
4605257955Skevlo		for (i = 0; i < nitems(rt3070_def_rf); i++) {
4606205042Sthompsa			run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4607205042Sthompsa			    rt3070_def_rf[i].val);
4608205042Sthompsa		}
4609203134Sthompsa	}
4610205042Sthompsa
4611256721Skevlo	if (sc->mac_ver == 0x3070 && sc->mac_rev < 0x0201) {
4612256721Skevlo		/*
4613256721Skevlo		 * Change voltage from 1.2V to 1.35V for RT3070.
4614256721Skevlo		 * The DAC issue (RT3070_LDO_CFG0) has been fixed
4615256721Skevlo		 * in RT3070(F).
4616256721Skevlo		 */
4617203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4618203134Sthompsa		tmp = (tmp & ~0x0f000000) | 0x0d000000;
4619203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4620203134Sthompsa
4621205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4622203134Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4623203134Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4624203134Sthompsa		run_rt3070_rf_write(sc, 31, 0x14);
4625203134Sthompsa
4626203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4627203134Sthompsa		tmp &= ~0x1f000000;
4628205042Sthompsa		if (sc->mac_rev < 0x0211)
4629205042Sthompsa			tmp |= 0x0d000000;	/* 1.3V */
4630203134Sthompsa		else
4631205042Sthompsa			tmp |= 0x01000000;	/* 1.2V */
4632203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4633203134Sthompsa
4634203134Sthompsa		/* patch LNA_PE_G1 */
4635203134Sthompsa		run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4636203134Sthompsa		run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4637208019Sthompsa
4638209917Sthompsa	} else if (sc->mac_ver == 0x3572) {
4639205042Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4640205042Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4641205042Sthompsa
4642208019Sthompsa		/* increase voltage from 1.2V to 1.35V */
4643208019Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4644208019Sthompsa		tmp = (tmp & ~0x1f000000) | 0x0d000000;
4645208019Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4646203134Sthompsa
4647209917Sthompsa		if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4648203134Sthompsa			run_delay(sc, 1);	/* wait for 1msec */
4649205042Sthompsa			/* decrease voltage back to 1.2V */
4650203134Sthompsa			tmp = (tmp & ~0x1f000000) | 0x01000000;
4651203134Sthompsa			run_write(sc, RT3070_LDO_CFG0, tmp);
4652203134Sthompsa		}
4653203134Sthompsa	}
4654203134Sthompsa
4655203134Sthompsa	/* select 20MHz bandwidth */
4656203134Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4657203134Sthompsa	run_rt3070_rf_write(sc, 31, rf & ~0x20);
4658203134Sthompsa
4659203134Sthompsa	/* calibrate filter for 20MHz bandwidth */
4660203134Sthompsa	sc->rf24_20mhz = 0x1f;	/* default value */
4661205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4662205042Sthompsa	run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4663203134Sthompsa
4664203134Sthompsa	/* select 40MHz bandwidth */
4665203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4666256721Skevlo	run_bbp_write(sc, 4, (bbp4 & ~0x18) | 0x10);
4667205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4668205042Sthompsa	run_rt3070_rf_write(sc, 31, rf | 0x20);
4669203134Sthompsa
4670203134Sthompsa	/* calibrate filter for 40MHz bandwidth */
4671203134Sthompsa	sc->rf24_40mhz = 0x2f;	/* default value */
4672205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4673205042Sthompsa	run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4674203134Sthompsa
4675203134Sthompsa	/* go back to 20MHz bandwidth */
4676203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4677203134Sthompsa	run_bbp_write(sc, 4, bbp4 & ~0x18);
4678203134Sthompsa
4679205042Sthompsa	if (sc->mac_ver == 0x3572) {
4680205042Sthompsa		/* save default BBP registers 25 and 26 values */
4681205042Sthompsa		run_bbp_read(sc, 25, &sc->bbp25);
4682205042Sthompsa		run_bbp_read(sc, 26, &sc->bbp26);
4683256721Skevlo	} else if (sc->mac_rev < 0x0201 || sc->mac_rev < 0x0211)
4684203134Sthompsa		run_rt3070_rf_write(sc, 27, 0x03);
4685203134Sthompsa
4686203134Sthompsa	run_read(sc, RT3070_OPT_14, &tmp);
4687203134Sthompsa	run_write(sc, RT3070_OPT_14, tmp | 1);
4688203134Sthompsa
4689205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4690205042Sthompsa		run_rt3070_rf_read(sc, 17, &rf);
4691205042Sthompsa		rf &= ~RT3070_TX_LO1;
4692205042Sthompsa		if ((sc->mac_ver == 0x3070 ||
4693205042Sthompsa		     (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4694205042Sthompsa		    !sc->ext_2ghz_lna)
4695205042Sthompsa			rf |= 0x20;	/* fix for long range Rx issue */
4696256722Skevlo		mingain = (sc->mac_ver == 0x3070) ? 1 : 2;
4697256722Skevlo		if (sc->txmixgain_2ghz >= mingain)
4698205042Sthompsa			rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4699205042Sthompsa		run_rt3070_rf_write(sc, 17, rf);
4700205042Sthompsa	}
4701205042Sthompsa
4702205042Sthompsa	if (sc->mac_rev == 0x3071) {
4703203134Sthompsa		run_rt3070_rf_read(sc, 1, &rf);
4704203134Sthompsa		rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4705203134Sthompsa		rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4706203134Sthompsa		run_rt3070_rf_write(sc, 1, rf);
4707203134Sthompsa
4708203134Sthompsa		run_rt3070_rf_read(sc, 15, &rf);
4709203134Sthompsa		run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4710203134Sthompsa
4711203134Sthompsa		run_rt3070_rf_read(sc, 20, &rf);
4712203134Sthompsa		run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4713203134Sthompsa
4714203134Sthompsa		run_rt3070_rf_read(sc, 21, &rf);
4715203134Sthompsa		run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4716205042Sthompsa	}
4717203134Sthompsa
4718205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4719205042Sthompsa		/* fix Tx to Rx IQ glitch by raising RF voltage */
4720203134Sthompsa		run_rt3070_rf_read(sc, 27, &rf);
4721203134Sthompsa		rf &= ~0x77;
4722205042Sthompsa		if (sc->mac_rev < 0x0211)
4723203134Sthompsa			rf |= 0x03;
4724203134Sthompsa		run_rt3070_rf_write(sc, 27, rf);
4725203134Sthompsa	}
4726209917Sthompsa	return (0);
4727203134Sthompsa}
4728203134Sthompsa
4729257955Skevlostatic void
4730257955Skevlorun_rt5390_rf_init(struct run_softc *sc)
4731257955Skevlo{
4732257955Skevlo	uint32_t tmp;
4733257955Skevlo	uint8_t rf;
4734257955Skevlo	int i;
4735257955Skevlo
4736257955Skevlo	if (sc->mac_ver == 0x5392)
4737257955Skevlo		run_rt3070_rf_write(sc, 2, 0x80);
4738257955Skevlo	else {
4739257955Skevlo		run_rt3070_rf_read(sc, 2, &rf);
4740257955Skevlo		/* Toggle RF R2 to initiate calibration. */
4741257955Skevlo		run_rt3070_rf_write(sc, 2, rf | 0x80);
4742257955Skevlo		run_delay(sc, 10);
4743257955Skevlo		run_rt3070_rf_write(sc, 2, rf & ~0x80);
4744257955Skevlo	}
4745257955Skevlo
4746257955Skevlo	/* Initialize RF registers to default value. */
4747257955Skevlo	if (sc->mac_ver == 0x5392) {
4748257955Skevlo		for (i = 0; i < nitems(rt5392_def_rf); i++) {
4749257955Skevlo			run_rt3070_rf_write(sc, rt5392_def_rf[i].reg,
4750257955Skevlo			    rt5392_def_rf[i].val);
4751257955Skevlo		}
4752257955Skevlo		if (sc->mac_rev >= 0x0223) {
4753257955Skevlo			run_rt3070_rf_write(sc, 23, 0x0f);
4754257955Skevlo			run_rt3070_rf_write(sc, 24, 0x3e);
4755257955Skevlo			run_rt3070_rf_write(sc, 51, 0x32);
4756257955Skevlo			run_rt3070_rf_write(sc, 53, 0x22);
4757257955Skevlo			run_rt3070_rf_write(sc, 56, 0xc1);
4758257955Skevlo			run_rt3070_rf_write(sc, 59, 0x0f);
4759257955Skevlo		}
4760257955Skevlo	} else {
4761257955Skevlo		for (i = 0; i < nitems(rt5390_def_rf); i++) {
4762257955Skevlo			run_rt3070_rf_write(sc, rt5390_def_rf[i].reg,
4763257955Skevlo			    rt5390_def_rf[i].val);
4764257955Skevlo		}
4765257955Skevlo		if (sc->mac_rev >= 0x0502) {
4766257955Skevlo			run_rt3070_rf_write(sc, 6, 0xe0);
4767257955Skevlo			run_rt3070_rf_write(sc, 25, 0x80);
4768257955Skevlo			run_rt3070_rf_write(sc, 46, 0x73);
4769257955Skevlo			run_rt3070_rf_write(sc, 53, 0x00);
4770257955Skevlo			run_rt3070_rf_write(sc, 56, 0x42);
4771257955Skevlo			run_rt3070_rf_write(sc, 61, 0xd1);
4772257955Skevlo		}
4773257955Skevlo	}
4774257955Skevlo
4775257955Skevlo	sc->rf24_20mhz = 0x1f;	/* default value */
4776257955Skevlo	sc->rf24_40mhz = 0x2f;	/* default value */
4777257955Skevlo
4778257955Skevlo	if (sc->mac_rev < 0x0211)
4779257955Skevlo		run_rt3070_rf_write(sc, 27, 0x3);
4780257955Skevlo
4781257955Skevlo	run_read(sc, RT3070_OPT_14, &tmp);
4782257955Skevlo	run_write(sc, RT3070_OPT_14, tmp | 1);
4783257955Skevlo}
4784257955Skevlo
4785203134Sthompsastatic int
4786203134Sthompsarun_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4787203134Sthompsa    uint8_t *val)
4788203134Sthompsa{
4789203134Sthompsa	uint8_t rf22, rf24;
4790203134Sthompsa	uint8_t bbp55_pb, bbp55_sb, delta;
4791203134Sthompsa	int ntries;
4792203134Sthompsa
4793203134Sthompsa	/* program filter */
4794205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf24);
4795205042Sthompsa	rf24 = (rf24 & 0xc0) | init;	/* initial filter value */
4796203134Sthompsa	run_rt3070_rf_write(sc, 24, rf24);
4797203134Sthompsa
4798203134Sthompsa	/* enable baseband loopback mode */
4799203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4800203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4801203134Sthompsa
4802203134Sthompsa	/* set power and frequency of passband test tone */
4803203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4804203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4805203134Sthompsa		/* transmit test tone */
4806203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4807203134Sthompsa		run_delay(sc, 10);
4808203134Sthompsa		/* read received power */
4809203134Sthompsa		run_bbp_read(sc, 55, &bbp55_pb);
4810203134Sthompsa		if (bbp55_pb != 0)
4811203134Sthompsa			break;
4812203134Sthompsa	}
4813203134Sthompsa	if (ntries == 100)
4814257955Skevlo		return (ETIMEDOUT);
4815203134Sthompsa
4816203134Sthompsa	/* set power and frequency of stopband test tone */
4817203134Sthompsa	run_bbp_write(sc, 24, 0x06);
4818203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4819203134Sthompsa		/* transmit test tone */
4820203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4821203134Sthompsa		run_delay(sc, 10);
4822203134Sthompsa		/* read received power */
4823203134Sthompsa		run_bbp_read(sc, 55, &bbp55_sb);
4824203134Sthompsa
4825203134Sthompsa		delta = bbp55_pb - bbp55_sb;
4826203134Sthompsa		if (delta > target)
4827203134Sthompsa			break;
4828203134Sthompsa
4829203134Sthompsa		/* reprogram filter */
4830203134Sthompsa		rf24++;
4831203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4832203134Sthompsa	}
4833203134Sthompsa	if (ntries < 100) {
4834203134Sthompsa		if (rf24 != init)
4835203134Sthompsa			rf24--;	/* backtrack */
4836203134Sthompsa		*val = rf24;
4837203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4838203134Sthompsa	}
4839203134Sthompsa
4840203134Sthompsa	/* restore initial state */
4841203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4842203134Sthompsa
4843203134Sthompsa	/* disable baseband loopback mode */
4844203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4845203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4846203134Sthompsa
4847209917Sthompsa	return (0);
4848203134Sthompsa}
4849203134Sthompsa
4850205042Sthompsastatic void
4851205042Sthompsarun_rt3070_rf_setup(struct run_softc *sc)
4852205042Sthompsa{
4853205042Sthompsa	uint8_t bbp, rf;
4854205042Sthompsa	int i;
4855205042Sthompsa
4856257955Skevlo	if (sc->mac_ver >= 0x5390) {
4857257955Skevlo		if (sc->mac_rev >= 0x0211) {
4858257955Skevlo			/* Enable DC filter. */
4859257955Skevlo			run_bbp_write(sc, 103, 0xc0);
4860257955Skevlo
4861257955Skevlo			/* Improve power consumption. */
4862257955Skevlo			run_bbp_read(sc, 31, &bbp);
4863257955Skevlo			run_bbp_write(sc, 31, bbp & ~0x03);
4864257955Skevlo		}
4865257955Skevlo
4866257955Skevlo		run_bbp_read(sc, 138, &bbp);
4867257955Skevlo		if (sc->ntxchains == 1)
4868257955Skevlo			bbp |= 0x20;	/* turn off DAC1 */
4869257955Skevlo		if (sc->nrxchains == 1)
4870257955Skevlo			bbp &= ~0x02;	/* turn off ADC1 */
4871257955Skevlo		run_bbp_write(sc, 138, bbp);
4872257955Skevlo
4873257955Skevlo		run_rt3070_rf_read(sc, 38, &rf);
4874257955Skevlo		run_rt3070_rf_write(sc, 38, rf & ~RT5390_RX_LO1);
4875257955Skevlo
4876257955Skevlo		run_rt3070_rf_read(sc, 39, &rf);
4877257955Skevlo		run_rt3070_rf_write(sc, 39, rf & ~RT5390_RX_LO2);
4878257955Skevlo
4879257955Skevlo		/* Avoid data lost and CRC error. */
4880257955Skevlo		run_bbp_read(sc, 4, &bbp);
4881257955Skevlo		run_bbp_write(sc, 4, bbp | 0x40);
4882257955Skevlo
4883257955Skevlo		run_rt3070_rf_read(sc, 30, &rf);
4884257955Skevlo		rf = (rf & ~0x18) | 0x10;
4885257955Skevlo		run_rt3070_rf_write(sc, 30, rf);
4886257955Skevlo
4887257955Skevlo		run_write(sc, RT2860_TX_SW_CFG1, 0);
4888257955Skevlo		if (sc->mac_rev < 0x0211) {
4889257955Skevlo			run_write(sc, RT2860_TX_SW_CFG2,
4890257955Skevlo			    sc->patch_dac ? 0x2c : 0x0f);
4891257955Skevlo		} else
4892257955Skevlo			run_write(sc, RT2860_TX_SW_CFG2, 0);
4893257955Skevlo
4894257955Skevlo	} else if (sc->mac_ver == 0x3572) {
4895205042Sthompsa		/* enable DC filter */
4896205042Sthompsa		if (sc->mac_rev >= 0x0201)
4897205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4898205042Sthompsa
4899205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4900205042Sthompsa		if (sc->ntxchains == 1)
4901205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4902205042Sthompsa		if (sc->nrxchains == 1)
4903205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4904205042Sthompsa		run_bbp_write(sc, 138, bbp);
4905205042Sthompsa
4906205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4907205042Sthompsa			/* improve power consumption */
4908205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4909205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4910205042Sthompsa		}
4911205042Sthompsa
4912205042Sthompsa		run_rt3070_rf_read(sc, 16, &rf);
4913205042Sthompsa		rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4914205042Sthompsa		run_rt3070_rf_write(sc, 16, rf);
4915205042Sthompsa
4916205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4917257409Skevlo		if (sc->mac_rev >= 0x0211) {
4918257409Skevlo			/* enable DC filter */
4919205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4920205042Sthompsa
4921257409Skevlo			/* improve power consumption */
4922257409Skevlo			run_bbp_read(sc, 31, &bbp);
4923257409Skevlo			run_bbp_write(sc, 31, bbp & ~0x03);
4924257409Skevlo		}
4925257409Skevlo
4926205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4927205042Sthompsa		if (sc->ntxchains == 1)
4928205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4929205042Sthompsa		if (sc->nrxchains == 1)
4930205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4931205042Sthompsa		run_bbp_write(sc, 138, bbp);
4932205042Sthompsa
4933205042Sthompsa		run_write(sc, RT2860_TX_SW_CFG1, 0);
4934205042Sthompsa		if (sc->mac_rev < 0x0211) {
4935205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2,
4936205042Sthompsa			    sc->patch_dac ? 0x2c : 0x0f);
4937205042Sthompsa		} else
4938205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4939205042Sthompsa
4940205042Sthompsa	} else if (sc->mac_ver == 0x3070) {
4941205042Sthompsa		if (sc->mac_rev >= 0x0201) {
4942205042Sthompsa			/* enable DC filter */
4943205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4944205042Sthompsa
4945205042Sthompsa			/* improve power consumption */
4946205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4947205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4948205042Sthompsa		}
4949205042Sthompsa
4950256955Skevlo		if (sc->mac_rev < 0x0201) {
4951205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG1, 0);
4952205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4953205042Sthompsa		} else
4954205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4955205042Sthompsa	}
4956205042Sthompsa
4957205042Sthompsa	/* initialize RF registers from ROM for >=RT3071*/
4958257955Skevlo	if (sc->mac_ver >= 0x3071 && sc->mac_ver < 0x5390) {
4959205042Sthompsa		for (i = 0; i < 10; i++) {
4960205042Sthompsa			if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4961205042Sthompsa				continue;
4962205042Sthompsa			run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4963205042Sthompsa		}
4964205042Sthompsa	}
4965205042Sthompsa}
4966205042Sthompsa
4967203134Sthompsastatic int
4968203134Sthompsarun_txrx_enable(struct run_softc *sc)
4969203134Sthompsa{
4970203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4971203134Sthompsa	uint32_t tmp;
4972203134Sthompsa	int error, ntries;
4973203134Sthompsa
4974203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4975203134Sthompsa	for (ntries = 0; ntries < 200; ntries++) {
4976203134Sthompsa		if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4977257955Skevlo			return (error);
4978203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4979203134Sthompsa			break;
4980203134Sthompsa		run_delay(sc, 50);
4981203134Sthompsa	}
4982203134Sthompsa	if (ntries == 200)
4983257955Skevlo		return (ETIMEDOUT);
4984203134Sthompsa
4985203134Sthompsa	run_delay(sc, 50);
4986203134Sthompsa
4987203134Sthompsa	tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4988203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4989203134Sthompsa
4990203134Sthompsa	/* enable Rx bulk aggregation (set timeout and limit) */
4991203134Sthompsa	tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
4992203134Sthompsa	    RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
4993203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, tmp);
4994203134Sthompsa
4995203134Sthompsa	/* set Rx filter */
4996203134Sthompsa	tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
4997203134Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
4998203134Sthompsa		tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
4999203134Sthompsa		    RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
5000203134Sthompsa		    RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
5001203134Sthompsa		    RT2860_DROP_CFACK | RT2860_DROP_CFEND;
5002203134Sthompsa		if (ic->ic_opmode == IEEE80211_M_STA)
5003203134Sthompsa			tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
5004203134Sthompsa	}
5005203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
5006203134Sthompsa
5007203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
5008203134Sthompsa	    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5009203134Sthompsa
5010209917Sthompsa	return (0);
5011203134Sthompsa}
5012203134Sthompsa
5013203134Sthompsastatic void
5014257955Skevlorun_adjust_freq_offset(struct run_softc * sc)
5015257955Skevlo{
5016257955Skevlo	uint8_t rf, tmp;
5017257955Skevlo
5018257955Skevlo	run_rt3070_rf_read(sc, 17, &rf);
5019257955Skevlo	tmp = rf;
5020257955Skevlo	rf = (rf & ~0x7f) | (sc->freq & 0x7f);
5021257955Skevlo	rf = MIN(rf, 0x5f);
5022257955Skevlo
5023257955Skevlo	if (tmp != rf)
5024257955Skevlo		run_mcu_cmd(sc, 0x74, (tmp << 8 ) | rf);
5025257955Skevlo}
5026257955Skevlo
5027257955Skevlostatic void
5028203134Sthompsarun_init_locked(struct run_softc *sc)
5029203134Sthompsa{
5030203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
5031203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
5032203134Sthompsa	uint32_t tmp;
5033203134Sthompsa	uint8_t bbp1, bbp3;
5034203134Sthompsa	int i;
5035203134Sthompsa	int ridx;
5036203134Sthompsa	int ntries;
5037203134Sthompsa
5038209917Sthompsa	if (ic->ic_nrunning > 1)
5039208019Sthompsa		return;
5040208019Sthompsa
5041203134Sthompsa	run_stop(sc);
5042203134Sthompsa
5043233283Sbschmidt	if (run_load_microcode(sc) != 0) {
5044233283Sbschmidt		device_printf(sc->sc_dev, "could not load 8051 microcode\n");
5045233283Sbschmidt		goto fail;
5046233283Sbschmidt	}
5047233283Sbschmidt
5048203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5049203134Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
5050203134Sthompsa			goto fail;
5051203134Sthompsa		if (tmp != 0 && tmp != 0xffffffff)
5052203134Sthompsa			break;
5053203134Sthompsa		run_delay(sc, 10);
5054203134Sthompsa	}
5055203134Sthompsa	if (ntries == 100)
5056203134Sthompsa		goto fail;
5057203134Sthompsa
5058203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
5059203134Sthompsa		run_setup_tx_list(sc, &sc->sc_epq[i]);
5060203134Sthompsa
5061203134Sthompsa	run_set_macaddr(sc, IF_LLADDR(ifp));
5062203134Sthompsa
5063203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5064203134Sthompsa		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
5065203134Sthompsa			goto fail;
5066203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
5067203134Sthompsa			break;
5068203134Sthompsa		run_delay(sc, 10);
5069203134Sthompsa	}
5070203134Sthompsa	if (ntries == 100) {
5071203138Sthompsa		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
5072203134Sthompsa		goto fail;
5073203134Sthompsa	}
5074203134Sthompsa	tmp &= 0xff0;
5075203134Sthompsa	tmp |= RT2860_TX_WB_DDONE;
5076203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
5077203134Sthompsa
5078203134Sthompsa	/* turn off PME_OEN to solve high-current issue */
5079203134Sthompsa	run_read(sc, RT2860_SYS_CTRL, &tmp);
5080203134Sthompsa	run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
5081203134Sthompsa
5082203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
5083203134Sthompsa	    RT2860_BBP_HRST | RT2860_MAC_SRST);
5084203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
5085203134Sthompsa
5086203134Sthompsa	if (run_reset(sc) != 0) {
5087203138Sthompsa		device_printf(sc->sc_dev, "could not reset chipset\n");
5088203134Sthompsa		goto fail;
5089203134Sthompsa	}
5090203134Sthompsa
5091203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
5092203134Sthompsa
5093203134Sthompsa	/* init Tx power for all Tx rates (from EEPROM) */
5094203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
5095203134Sthompsa		if (sc->txpow20mhz[ridx] == 0xffffffff)
5096203134Sthompsa			continue;
5097203134Sthompsa		run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
5098203134Sthompsa	}
5099203134Sthompsa
5100257955Skevlo	for (i = 0; i < nitems(rt2870_def_mac); i++)
5101203134Sthompsa		run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
5102203134Sthompsa	run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
5103203134Sthompsa	run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
5104203134Sthompsa	run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
5105203134Sthompsa
5106257955Skevlo	if (sc->mac_ver == 0x5392) {
5107257955Skevlo		run_write(sc, RT2860_TX_SW_CFG0, 0x00000404);
5108257955Skevlo		run_write(sc, RT2860_MAX_LEN_CFG, 0x00002fff);
5109257955Skevlo		run_write(sc, RT2860_HT_FBK_CFG1, 0xedcb4980);
5110257955Skevlo		run_write(sc, RT2860_LG_FBK_CFG0, 0xedcba322);
5111257955Skevlo	} else if (sc->mac_ver == 0x5390) {
5112257955Skevlo		run_write(sc, RT2860_TX_SW_CFG0, 0x00000404);
5113257955Skevlo	} else if (sc->mac_ver >= 0x3070) {
5114203134Sthompsa		/* set delay of PA_PE assertion to 1us (unit of 0.25us) */
5115203134Sthompsa		run_write(sc, RT2860_TX_SW_CFG0,
5116203134Sthompsa		    4 << RT2860_DLY_PAPE_EN_SHIFT);
5117203134Sthompsa	}
5118203134Sthompsa
5119203134Sthompsa	/* wait while MAC is busy */
5120203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5121203134Sthompsa		if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
5122203134Sthompsa			goto fail;
5123203134Sthompsa		if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
5124203134Sthompsa			break;
5125203134Sthompsa		run_delay(sc, 10);
5126203134Sthompsa	}
5127203134Sthompsa	if (ntries == 100)
5128203134Sthompsa		goto fail;
5129203134Sthompsa
5130203134Sthompsa	/* clear Host to MCU mailbox */
5131203134Sthompsa	run_write(sc, RT2860_H2M_BBPAGENT, 0);
5132203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
5133203134Sthompsa	run_delay(sc, 10);
5134203134Sthompsa
5135203134Sthompsa	if (run_bbp_init(sc) != 0) {
5136203138Sthompsa		device_printf(sc->sc_dev, "could not initialize BBP\n");
5137203134Sthompsa		goto fail;
5138203134Sthompsa	}
5139203134Sthompsa
5140203134Sthompsa	/* abort TSF synchronization */
5141203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
5142203134Sthompsa	tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
5143203134Sthompsa	    RT2860_TBTT_TIMER_EN);
5144203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
5145203134Sthompsa
5146203134Sthompsa	/* clear RX WCID search table */
5147203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
5148203134Sthompsa	/* clear WCID attribute table */
5149203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
5150203134Sthompsa
5151209144Sthompsa	/* hostapd sets a key before init. So, don't clear it. */
5152209917Sthompsa	if (sc->cmdq_key_set != RUN_CMDQ_GO) {
5153209144Sthompsa		/* clear shared key table */
5154209144Sthompsa		run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
5155209144Sthompsa		/* clear shared key mode */
5156209144Sthompsa		run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
5157209144Sthompsa	}
5158209144Sthompsa
5159203134Sthompsa	run_read(sc, RT2860_US_CYC_CNT, &tmp);
5160203134Sthompsa	tmp = (tmp & ~0xff) | 0x1e;
5161203134Sthompsa	run_write(sc, RT2860_US_CYC_CNT, tmp);
5162203134Sthompsa
5163205042Sthompsa	if (sc->mac_rev != 0x0101)
5164203134Sthompsa		run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
5165203134Sthompsa
5166203134Sthompsa	run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
5167203134Sthompsa	run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
5168203134Sthompsa
5169203134Sthompsa	/* write vendor-specific BBP values (from EEPROM) */
5170257955Skevlo	if (sc->mac_ver < 0x5390) {
5171257955Skevlo		for (i = 0; i < 10; i++) {
5172257955Skevlo			if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
5173257955Skevlo				continue;
5174257955Skevlo			run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
5175257955Skevlo		}
5176203134Sthompsa	}
5177203134Sthompsa
5178203134Sthompsa	/* select Main antenna for 1T1R devices */
5179257955Skevlo	if (sc->rf_rev == RT3070_RF_3020 || sc->rf_rev == RT5390_RF_5370)
5180203134Sthompsa		run_set_rx_antenna(sc, 0);
5181203134Sthompsa
5182203134Sthompsa	/* send LEDs operating mode to microcontroller */
5183203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
5184203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
5185203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
5186203134Sthompsa
5187257955Skevlo	if (sc->mac_ver >= 0x5390)
5188257955Skevlo		run_rt5390_rf_init(sc);
5189257955Skevlo	else if (sc->mac_ver >= 0x3070)
5190205042Sthompsa		run_rt3070_rf_init(sc);
5191205042Sthompsa
5192203134Sthompsa	/* disable non-existing Rx chains */
5193203134Sthompsa	run_bbp_read(sc, 3, &bbp3);
5194203134Sthompsa	bbp3 &= ~(1 << 3 | 1 << 4);
5195203134Sthompsa	if (sc->nrxchains == 2)
5196203134Sthompsa		bbp3 |= 1 << 3;
5197203134Sthompsa	else if (sc->nrxchains == 3)
5198203134Sthompsa		bbp3 |= 1 << 4;
5199203134Sthompsa	run_bbp_write(sc, 3, bbp3);
5200203134Sthompsa
5201203134Sthompsa	/* disable non-existing Tx chains */
5202203134Sthompsa	run_bbp_read(sc, 1, &bbp1);
5203203134Sthompsa	if (sc->ntxchains == 1)
5204203134Sthompsa		bbp1 &= ~(1 << 3 | 1 << 4);
5205203134Sthompsa	run_bbp_write(sc, 1, bbp1);
5206203134Sthompsa
5207205042Sthompsa	if (sc->mac_ver >= 0x3070)
5208205042Sthompsa		run_rt3070_rf_setup(sc);
5209203134Sthompsa
5210203134Sthompsa	/* select default channel */
5211203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
5212203134Sthompsa
5213203134Sthompsa	/* setup initial protection mode */
5214218492Sbschmidt	run_updateprot_cb(ic);
5215203134Sthompsa
5216203134Sthompsa	/* turn radio LED on */
5217203134Sthompsa	run_set_leds(sc, RT2860_LED_RADIO);
5218203134Sthompsa
5219203134Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5220203134Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
5221208019Sthompsa	sc->cmdq_run = RUN_CMDQ_GO;
5222203134Sthompsa
5223209917Sthompsa	for (i = 0; i != RUN_N_XFER; i++)
5224203134Sthompsa		usbd_xfer_set_stall(sc->sc_xfer[i]);
5225203134Sthompsa
5226203134Sthompsa	usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
5227203134Sthompsa
5228203134Sthompsa	if (run_txrx_enable(sc) != 0)
5229203134Sthompsa		goto fail;
5230203134Sthompsa
5231203134Sthompsa	return;
5232203134Sthompsa
5233203134Sthompsafail:
5234203134Sthompsa	run_stop(sc);
5235203134Sthompsa}
5236203134Sthompsa
5237203134Sthompsastatic void
5238203134Sthompsarun_init(void *arg)
5239203134Sthompsa{
5240203134Sthompsa	struct run_softc *sc = arg;
5241203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
5242203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
5243203134Sthompsa
5244203134Sthompsa	RUN_LOCK(sc);
5245203134Sthompsa	run_init_locked(sc);
5246203134Sthompsa	RUN_UNLOCK(sc);
5247203134Sthompsa
5248203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
5249208019Sthompsa		ieee80211_start_all(ic);
5250203134Sthompsa}
5251203134Sthompsa
5252203134Sthompsastatic void
5253203134Sthompsarun_stop(void *arg)
5254203134Sthompsa{
5255203134Sthompsa	struct run_softc *sc = (struct run_softc *)arg;
5256203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
5257203134Sthompsa	uint32_t tmp;
5258203134Sthompsa	int i;
5259203134Sthompsa	int ntries;
5260203134Sthompsa
5261203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
5262203134Sthompsa
5263203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
5264203134Sthompsa		run_set_leds(sc, 0);	/* turn all LEDs off */
5265203134Sthompsa
5266203134Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
5267203134Sthompsa
5268208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
5269209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set;
5270208019Sthompsa
5271203134Sthompsa	RUN_UNLOCK(sc);
5272203134Sthompsa
5273203134Sthompsa	for(i = 0; i < RUN_N_XFER; i++)
5274203134Sthompsa		usbd_transfer_drain(sc->sc_xfer[i]);
5275203134Sthompsa
5276203134Sthompsa	RUN_LOCK(sc);
5277203134Sthompsa
5278209917Sthompsa	if (sc->rx_m != NULL) {
5279203134Sthompsa		m_free(sc->rx_m);
5280203134Sthompsa		sc->rx_m = NULL;
5281203134Sthompsa	}
5282203134Sthompsa
5283257955Skevlo	/* Disable Tx/Rx DMA. */
5284257955Skevlo	if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
5285257955Skevlo		return;
5286257955Skevlo	tmp &= ~(RT2860_RX_DMA_EN | RT2860_TX_DMA_EN);
5287257955Skevlo	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
5288257955Skevlo
5289257955Skevlo        for (ntries = 0; ntries < 100; ntries++) {
5290257955Skevlo		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
5291257955Skevlo			return;
5292257955Skevlo		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
5293257955Skevlo				break;
5294257955Skevlo		run_delay(sc, 10);
5295257955Skevlo	}
5296257955Skevlo	if (ntries == 100) {
5297257955Skevlo		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
5298257955Skevlo		return;
5299257955Skevlo	}
5300257955Skevlo
5301203134Sthompsa	/* disable Tx/Rx */
5302203134Sthompsa	run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
5303203134Sthompsa	tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5304203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
5305203134Sthompsa
5306203134Sthompsa	/* wait for pending Tx to complete */
5307203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
5308209917Sthompsa		if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
5309203134Sthompsa			DPRINTF("Cannot read Tx queue count\n");
5310203134Sthompsa			break;
5311203134Sthompsa		}
5312209917Sthompsa		if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
5313203134Sthompsa			DPRINTF("All Tx cleared\n");
5314203134Sthompsa			break;
5315203134Sthompsa		}
5316203134Sthompsa		run_delay(sc, 10);
5317203134Sthompsa	}
5318209917Sthompsa	if (ntries >= 100)
5319203134Sthompsa		DPRINTF("There are still pending Tx\n");
5320203134Sthompsa	run_delay(sc, 10);
5321203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
5322203134Sthompsa
5323203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
5324203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
5325203134Sthompsa
5326203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
5327203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
5328203134Sthompsa}
5329203134Sthompsa
5330203134Sthompsastatic void
5331257429Shselaskyrun_delay(struct run_softc *sc, u_int ms)
5332203134Sthompsa{
5333203134Sthompsa	usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
5334203134Sthompsa	    &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
5335203134Sthompsa}
5336203134Sthompsa
5337203134Sthompsastatic device_method_t run_methods[] = {
5338203134Sthompsa	/* Device interface */
5339203134Sthompsa	DEVMETHOD(device_probe,		run_match),
5340203134Sthompsa	DEVMETHOD(device_attach,	run_attach),
5341203134Sthompsa	DEVMETHOD(device_detach,	run_detach),
5342246614Shselasky	DEVMETHOD_END
5343203134Sthompsa};
5344203134Sthompsa
5345203134Sthompsastatic driver_t run_driver = {
5346233774Shselasky	.name = "run",
5347233774Shselasky	.methods = run_methods,
5348233774Shselasky	.size = sizeof(struct run_softc)
5349203134Sthompsa};
5350203134Sthompsa
5351203134Sthompsastatic devclass_t run_devclass;
5352203134Sthompsa
5353257955SkevloDRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, NULL);
5354212122SthompsaMODULE_DEPEND(run, wlan, 1, 1, 1);
5355212122SthompsaMODULE_DEPEND(run, usb, 1, 1, 1);
5356212122SthompsaMODULE_DEPEND(run, firmware, 1, 1, 1);
5357212122SthompsaMODULE_VERSION(run, 1);
5358