if_run.c revision 246614
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 246614 2013-02-10 10:36:16Z hselasky $");
21203134Sthompsa
22203134Sthompsa/*-
23203134Sthompsa * Ralink Technology RT2700U/RT2800U/RT3000U 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>
50203134Sthompsa#include <net/if_arp.h>
51203134Sthompsa#include <net/ethernet.h>
52203134Sthompsa#include <net/if_dl.h>
53203134Sthompsa#include <net/if_media.h>
54203134Sthompsa#include <net/if_types.h>
55203134Sthompsa
56203134Sthompsa#include <netinet/in.h>
57203134Sthompsa#include <netinet/in_systm.h>
58203134Sthompsa#include <netinet/in_var.h>
59203134Sthompsa#include <netinet/if_ether.h>
60203134Sthompsa#include <netinet/ip.h>
61203134Sthompsa
62203134Sthompsa#include <net80211/ieee80211_var.h>
63203134Sthompsa#include <net80211/ieee80211_regdomain.h>
64203134Sthompsa#include <net80211/ieee80211_radiotap.h>
65206358Srpaulo#include <net80211/ieee80211_ratectl.h>
66203134Sthompsa
67203134Sthompsa#include <dev/usb/usb.h>
68203134Sthompsa#include <dev/usb/usbdi.h>
69203134Sthompsa#include "usbdevs.h"
70203134Sthompsa
71203134Sthompsa#define USB_DEBUG_VAR run_debug
72203134Sthompsa#include <dev/usb/usb_debug.h>
73203134Sthompsa
74220235Skevlo#include <dev/usb/wlan/if_runreg.h>
75220235Skevlo#include <dev/usb/wlan/if_runvar.h>
76203134Sthompsa
77233774Shselasky#define	N(_a) ((int)(sizeof((_a)) / sizeof((_a)[0])))
78203134Sthompsa
79207077Sthompsa#ifdef	USB_DEBUG
80203134Sthompsa#define RUN_DEBUG
81203134Sthompsa#endif
82203134Sthompsa
83203134Sthompsa#ifdef	RUN_DEBUG
84203134Sthompsaint run_debug = 0;
85227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run");
86203134SthompsaSYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0,
87203134Sthompsa    "run debug level");
88203134Sthompsa#endif
89203134Sthompsa
90203134Sthompsa#define IEEE80211_HAS_ADDR4(wh) \
91203134Sthompsa	(((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
92203134Sthompsa
93208019Sthompsa/*
94208019Sthompsa * Because of LOR in run_key_delete(), use atomic instead.
95208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[].
96208019Sthompsa */
97208019Sthompsa#define RUN_CMDQ_GET(c)	(atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
98208019Sthompsa
99223486Shselaskystatic const STRUCT_USB_HOST_ID run_devs[] = {
100209918Sthompsa#define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
101209918Sthompsa    RUN_DEV(ABOCOM,		RT2770),
102209918Sthompsa    RUN_DEV(ABOCOM,		RT2870),
103209918Sthompsa    RUN_DEV(ABOCOM,		RT3070),
104209918Sthompsa    RUN_DEV(ABOCOM,		RT3071),
105209918Sthompsa    RUN_DEV(ABOCOM,		RT3072),
106209918Sthompsa    RUN_DEV(ABOCOM2,		RT2870_1),
107209918Sthompsa    RUN_DEV(ACCTON,		RT2770),
108209918Sthompsa    RUN_DEV(ACCTON,		RT2870_1),
109209918Sthompsa    RUN_DEV(ACCTON,		RT2870_2),
110209918Sthompsa    RUN_DEV(ACCTON,		RT2870_3),
111209918Sthompsa    RUN_DEV(ACCTON,		RT2870_4),
112209918Sthompsa    RUN_DEV(ACCTON,		RT2870_5),
113209918Sthompsa    RUN_DEV(ACCTON,		RT3070),
114209918Sthompsa    RUN_DEV(ACCTON,		RT3070_1),
115209918Sthompsa    RUN_DEV(ACCTON,		RT3070_2),
116209918Sthompsa    RUN_DEV(ACCTON,		RT3070_3),
117209918Sthompsa    RUN_DEV(ACCTON,		RT3070_4),
118209918Sthompsa    RUN_DEV(ACCTON,		RT3070_5),
119209918Sthompsa    RUN_DEV(AIRTIES,		RT3070),
120209918Sthompsa    RUN_DEV(ALLWIN,		RT2070),
121209918Sthompsa    RUN_DEV(ALLWIN,		RT2770),
122209918Sthompsa    RUN_DEV(ALLWIN,		RT2870),
123209918Sthompsa    RUN_DEV(ALLWIN,		RT3070),
124209918Sthompsa    RUN_DEV(ALLWIN,		RT3071),
125209918Sthompsa    RUN_DEV(ALLWIN,		RT3072),
126209918Sthompsa    RUN_DEV(ALLWIN,		RT3572),
127209918Sthompsa    RUN_DEV(AMIGO,		RT2870_1),
128209918Sthompsa    RUN_DEV(AMIGO,		RT2870_2),
129209918Sthompsa    RUN_DEV(AMIT,		CGWLUSB2GNR),
130209918Sthompsa    RUN_DEV(AMIT,		RT2870_1),
131209918Sthompsa    RUN_DEV(AMIT2,		RT2870),
132209918Sthompsa    RUN_DEV(ASUS,		RT2870_1),
133209918Sthompsa    RUN_DEV(ASUS,		RT2870_2),
134209918Sthompsa    RUN_DEV(ASUS,		RT2870_3),
135209918Sthompsa    RUN_DEV(ASUS,		RT2870_4),
136209918Sthompsa    RUN_DEV(ASUS,		RT2870_5),
137209918Sthompsa    RUN_DEV(ASUS,		USBN13),
138209918Sthompsa    RUN_DEV(ASUS,		RT3070_1),
139239358Shselasky    RUN_DEV(ASUS,		USB_N53),
140209918Sthompsa    RUN_DEV(ASUS2,		USBN11),
141209918Sthompsa    RUN_DEV(AZUREWAVE,		RT2870_1),
142209918Sthompsa    RUN_DEV(AZUREWAVE,		RT2870_2),
143209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_1),
144209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_2),
145209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_3),
146209918Sthompsa    RUN_DEV(BELKIN,		F5D8053V3),
147209918Sthompsa    RUN_DEV(BELKIN,		F5D8055),
148226534Shselasky    RUN_DEV(BELKIN,		F5D8055V2),
149209918Sthompsa    RUN_DEV(BELKIN,		F6D4050V1),
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),
174209918Sthompsa    RUN_DEV(DLINK2,		DWA130),
175209918Sthompsa    RUN_DEV(DLINK2,		RT2870_1),
176209918Sthompsa    RUN_DEV(DLINK2,		RT2870_2),
177209918Sthompsa    RUN_DEV(DLINK2,		RT3070_1),
178209918Sthompsa    RUN_DEV(DLINK2,		RT3070_2),
179209918Sthompsa    RUN_DEV(DLINK2,		RT3070_3),
180209918Sthompsa    RUN_DEV(DLINK2,		RT3070_4),
181209918Sthompsa    RUN_DEV(DLINK2,		RT3070_5),
182209918Sthompsa    RUN_DEV(DLINK2,		RT3072),
183209918Sthompsa    RUN_DEV(DLINK2,		RT3072_1),
184209918Sthompsa    RUN_DEV(EDIMAX,		EW7717),
185209918Sthompsa    RUN_DEV(EDIMAX,		EW7718),
186209918Sthompsa    RUN_DEV(EDIMAX,		RT2870_1),
187209918Sthompsa    RUN_DEV(ENCORE,		RT3070_1),
188209918Sthompsa    RUN_DEV(ENCORE,		RT3070_2),
189209918Sthompsa    RUN_DEV(ENCORE,		RT3070_3),
190209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB31N),
191209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB32L),
192209918Sthompsa    RUN_DEV(GIGABYTE,		RT2870_1),
193209918Sthompsa    RUN_DEV(GIGASET,		RT3070_1),
194209918Sthompsa    RUN_DEV(GIGASET,		RT3070_2),
195209918Sthompsa    RUN_DEV(GUILLEMOT,		HWNU300),
196209918Sthompsa    RUN_DEV(HAWKING,		HWUN2),
197209918Sthompsa    RUN_DEV(HAWKING,		RT2870_1),
198209918Sthompsa    RUN_DEV(HAWKING,		RT2870_2),
199209918Sthompsa    RUN_DEV(HAWKING,		RT3070),
200209918Sthompsa    RUN_DEV(IODATA,		RT3072_1),
201209918Sthompsa    RUN_DEV(IODATA,		RT3072_2),
202209918Sthompsa    RUN_DEV(IODATA,		RT3072_3),
203209918Sthompsa    RUN_DEV(IODATA,		RT3072_4),
204209918Sthompsa    RUN_DEV(LINKSYS4,		RT3070),
205209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB100),
206209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB54GCV3),
207209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600N),
208209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600NV2),
209209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_1),
210209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_2),
211209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_3),
212230333Shselasky    RUN_DEV(LOGITEC,		LANW300NU2),
213238274Shrs    RUN_DEV(LOGITEC,		LANW150NU2),
214209918Sthompsa    RUN_DEV(MELCO,		RT2870_1),
215209918Sthompsa    RUN_DEV(MELCO,		RT2870_2),
216209918Sthompsa    RUN_DEV(MELCO,		WLIUCAG300N),
217209918Sthompsa    RUN_DEV(MELCO,		WLIUCG300N),
218219257Sdaichi    RUN_DEV(MELCO,		WLIUCG301N),
219209918Sthompsa    RUN_DEV(MELCO,		WLIUCGN),
220227781Shselasky    RUN_DEV(MELCO,		WLIUCGNM),
221238274Shrs    RUN_DEV(MELCO,		WLIUCGNM2),
222209918Sthompsa    RUN_DEV(MOTOROLA4,		RT2770),
223209918Sthompsa    RUN_DEV(MOTOROLA4,		RT3070),
224209918Sthompsa    RUN_DEV(MSI,		RT3070_1),
225209918Sthompsa    RUN_DEV(MSI,		RT3070_2),
226209918Sthompsa    RUN_DEV(MSI,		RT3070_3),
227209918Sthompsa    RUN_DEV(MSI,		RT3070_4),
228209918Sthompsa    RUN_DEV(MSI,		RT3070_5),
229209918Sthompsa    RUN_DEV(MSI,		RT3070_6),
230209918Sthompsa    RUN_DEV(MSI,		RT3070_7),
231209918Sthompsa    RUN_DEV(MSI,		RT3070_8),
232209918Sthompsa    RUN_DEV(MSI,		RT3070_9),
233209918Sthompsa    RUN_DEV(MSI,		RT3070_10),
234209918Sthompsa    RUN_DEV(MSI,		RT3070_11),
235209918Sthompsa    RUN_DEV(OVISLINK,		RT3072),
236209918Sthompsa    RUN_DEV(PARA,		RT3070),
237209918Sthompsa    RUN_DEV(PEGATRON,		RT2870),
238209918Sthompsa    RUN_DEV(PEGATRON,		RT3070),
239209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_2),
240209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_3),
241209918Sthompsa    RUN_DEV(PHILIPS,		RT2870),
242209918Sthompsa    RUN_DEV(PLANEX2,		GWUS300MINIS),
243209918Sthompsa    RUN_DEV(PLANEX2,		GWUSMICRON),
244209918Sthompsa    RUN_DEV(PLANEX2,		RT2870),
245209918Sthompsa    RUN_DEV(PLANEX2,		RT3070),
246209918Sthompsa    RUN_DEV(QCOM,		RT2870),
247209918Sthompsa    RUN_DEV(QUANTA,		RT3070),
248209918Sthompsa    RUN_DEV(RALINK,		RT2070),
249209918Sthompsa    RUN_DEV(RALINK,		RT2770),
250209918Sthompsa    RUN_DEV(RALINK,		RT2870),
251209918Sthompsa    RUN_DEV(RALINK,		RT3070),
252209918Sthompsa    RUN_DEV(RALINK,		RT3071),
253209918Sthompsa    RUN_DEV(RALINK,		RT3072),
254209918Sthompsa    RUN_DEV(RALINK,		RT3370),
255209918Sthompsa    RUN_DEV(RALINK,		RT3572),
256209918Sthompsa    RUN_DEV(RALINK,		RT8070),
257226534Shselasky    RUN_DEV(SAMSUNG,		WIS09ABGN),
258209918Sthompsa    RUN_DEV(SAMSUNG2,		RT2870_1),
259209918Sthompsa    RUN_DEV(SENAO,		RT2870_1),
260209918Sthompsa    RUN_DEV(SENAO,		RT2870_2),
261209918Sthompsa    RUN_DEV(SENAO,		RT2870_3),
262209918Sthompsa    RUN_DEV(SENAO,		RT2870_4),
263209918Sthompsa    RUN_DEV(SENAO,		RT3070),
264209918Sthompsa    RUN_DEV(SENAO,		RT3071),
265209918Sthompsa    RUN_DEV(SENAO,		RT3072_1),
266209918Sthompsa    RUN_DEV(SENAO,		RT3072_2),
267209918Sthompsa    RUN_DEV(SENAO,		RT3072_3),
268209918Sthompsa    RUN_DEV(SENAO,		RT3072_4),
269209918Sthompsa    RUN_DEV(SENAO,		RT3072_5),
270209918Sthompsa    RUN_DEV(SITECOMEU,		RT2770),
271209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_1),
272209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_2),
273209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_3),
274209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_4),
275209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070),
276209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_2),
277209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_3),
278209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_4),
279209918Sthompsa    RUN_DEV(SITECOMEU,		RT3071),
280209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_1),
281209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_2),
282209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_3),
283209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_4),
284209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_5),
285209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_6),
286209918Sthompsa    RUN_DEV(SITECOMEU,		WL608),
287209918Sthompsa    RUN_DEV(SPARKLAN,		RT2870_1),
288209918Sthompsa    RUN_DEV(SPARKLAN,		RT3070),
289209918Sthompsa    RUN_DEV(SWEEX2,		LW153),
290209918Sthompsa    RUN_DEV(SWEEX2,		LW303),
291209918Sthompsa    RUN_DEV(SWEEX2,		LW313),
292209918Sthompsa    RUN_DEV(TOSHIBA,		RT3070),
293209918Sthompsa    RUN_DEV(UMEDIA,		RT2870_1),
294209918Sthompsa    RUN_DEV(ZCOM,		RT2870_1),
295209918Sthompsa    RUN_DEV(ZCOM,		RT2870_2),
296209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_1),
297209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_2),
298209918Sthompsa    RUN_DEV(ZINWELL,		RT3070),
299209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_1),
300209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_2),
301209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_1),
302209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_2),
303209918Sthompsa#undef RUN_DEV
304203134Sthompsa};
305203134Sthompsa
306203134Sthompsastatic device_probe_t	run_match;
307203134Sthompsastatic device_attach_t	run_attach;
308203134Sthompsastatic device_detach_t	run_detach;
309203134Sthompsa
310203134Sthompsastatic usb_callback_t	run_bulk_rx_callback;
311203134Sthompsastatic usb_callback_t	run_bulk_tx_callback0;
312203134Sthompsastatic usb_callback_t	run_bulk_tx_callback1;
313203134Sthompsastatic usb_callback_t	run_bulk_tx_callback2;
314203134Sthompsastatic usb_callback_t	run_bulk_tx_callback3;
315203134Sthompsastatic usb_callback_t	run_bulk_tx_callback4;
316203134Sthompsastatic usb_callback_t	run_bulk_tx_callback5;
317203134Sthompsa
318203134Sthompsastatic void	run_bulk_tx_callbackN(struct usb_xfer *xfer,
319203134Sthompsa		    usb_error_t error, unsigned int index);
320203134Sthompsastatic struct ieee80211vap *run_vap_create(struct ieee80211com *,
321228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
322228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
323228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
324203134Sthompsastatic void	run_vap_delete(struct ieee80211vap *);
325208019Sthompsastatic void	run_cmdq_cb(void *, int);
326203134Sthompsastatic void	run_setup_tx_list(struct run_softc *,
327203134Sthompsa		    struct run_endpoint_queue *);
328203134Sthompsastatic void	run_unsetup_tx_list(struct run_softc *,
329203134Sthompsa		    struct run_endpoint_queue *);
330203134Sthompsastatic int	run_load_microcode(struct run_softc *);
331203134Sthompsastatic int	run_reset(struct run_softc *);
332203134Sthompsastatic usb_error_t run_do_request(struct run_softc *,
333203134Sthompsa		    struct usb_device_request *, void *);
334203134Sthompsastatic int	run_read(struct run_softc *, uint16_t, uint32_t *);
335203134Sthompsastatic int	run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
336203134Sthompsastatic int	run_write_2(struct run_softc *, uint16_t, uint16_t);
337203134Sthompsastatic int	run_write(struct run_softc *, uint16_t, uint32_t);
338203134Sthompsastatic int	run_write_region_1(struct run_softc *, uint16_t,
339203134Sthompsa		    const uint8_t *, int);
340203134Sthompsastatic int	run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
341203134Sthompsastatic int	run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
342203134Sthompsastatic int	run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
343203134Sthompsastatic int	run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t);
344203134Sthompsastatic int	run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
345203134Sthompsastatic int	run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
346203134Sthompsastatic int	run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
347203134Sthompsastatic int	run_bbp_write(struct run_softc *, uint8_t, uint8_t);
348203134Sthompsastatic int	run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
349203134Sthompsastatic const char *run_get_rf(int);
350203134Sthompsastatic int	run_read_eeprom(struct run_softc *);
351203134Sthompsastatic struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
352203134Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
353203134Sthompsastatic int	run_media_change(struct ifnet *);
354203134Sthompsastatic int	run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
355203134Sthompsastatic int	run_wme_update(struct ieee80211com *);
356208019Sthompsastatic void	run_wme_update_cb(void *);
357203134Sthompsastatic void	run_key_update_begin(struct ieee80211vap *);
358203134Sthompsastatic void	run_key_update_end(struct ieee80211vap *);
359208019Sthompsastatic void	run_key_set_cb(void *);
360208019Sthompsastatic int	run_key_set(struct ieee80211vap *, struct ieee80211_key *,
361203134Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
362208019Sthompsastatic void	run_key_delete_cb(void *);
363208019Sthompsastatic int	run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
364206358Srpaulostatic void	run_ratectl_to(void *);
365206358Srpaulostatic void	run_ratectl_cb(void *, int);
366208019Sthompsastatic void	run_drain_fifo(void *);
367203134Sthompsastatic void	run_iter_func(void *, struct ieee80211_node *);
368208019Sthompsastatic void	run_newassoc_cb(void *);
369203134Sthompsastatic void	run_newassoc(struct ieee80211_node *, int);
370203134Sthompsastatic void	run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
371203134Sthompsastatic void	run_tx_free(struct run_endpoint_queue *pq,
372203134Sthompsa		    struct run_tx_data *, int);
373208019Sthompsastatic void	run_set_tx_desc(struct run_softc *, struct run_tx_data *);
374203134Sthompsastatic int	run_tx(struct run_softc *, struct mbuf *,
375203134Sthompsa		    struct ieee80211_node *);
376203134Sthompsastatic int	run_tx_mgt(struct run_softc *, struct mbuf *,
377203134Sthompsa		    struct ieee80211_node *);
378203134Sthompsastatic int	run_sendprot(struct run_softc *, const struct mbuf *,
379203134Sthompsa		    struct ieee80211_node *, int, int);
380203134Sthompsastatic int	run_tx_param(struct run_softc *, struct mbuf *,
381203134Sthompsa		    struct ieee80211_node *,
382203134Sthompsa		    const struct ieee80211_bpf_params *);
383203134Sthompsastatic int	run_raw_xmit(struct ieee80211_node *, struct mbuf *,
384203134Sthompsa		    const struct ieee80211_bpf_params *);
385203134Sthompsastatic void	run_start(struct ifnet *);
386203134Sthompsastatic int	run_ioctl(struct ifnet *, u_long, caddr_t);
387205042Sthompsastatic void	run_set_agc(struct run_softc *, uint8_t);
388203134Sthompsastatic void	run_select_chan_group(struct run_softc *, int);
389203134Sthompsastatic void	run_set_rx_antenna(struct run_softc *, int);
390203134Sthompsastatic void	run_rt2870_set_chan(struct run_softc *, u_int);
391203134Sthompsastatic void	run_rt3070_set_chan(struct run_softc *, u_int);
392205042Sthompsastatic void	run_rt3572_set_chan(struct run_softc *, u_int);
393203134Sthompsastatic int	run_set_chan(struct run_softc *, struct ieee80211_channel *);
394203134Sthompsastatic void	run_set_channel(struct ieee80211com *);
395203134Sthompsastatic void	run_scan_start(struct ieee80211com *);
396203134Sthompsastatic void	run_scan_end(struct ieee80211com *);
397203134Sthompsastatic void	run_update_beacon(struct ieee80211vap *, int);
398208019Sthompsastatic void	run_update_beacon_cb(void *);
399203134Sthompsastatic void	run_updateprot(struct ieee80211com *);
400218492Sbschmidtstatic void	run_updateprot_cb(void *);
401208019Sthompsastatic void	run_usb_timeout_cb(void *);
402203134Sthompsastatic void	run_reset_livelock(struct run_softc *);
403203134Sthompsastatic void	run_enable_tsf_sync(struct run_softc *);
404203134Sthompsastatic void	run_enable_mrr(struct run_softc *);
405203134Sthompsastatic void	run_set_txpreamble(struct run_softc *);
406203134Sthompsastatic void	run_set_basicrates(struct run_softc *);
407203134Sthompsastatic void	run_set_leds(struct run_softc *, uint16_t);
408203134Sthompsastatic void	run_set_bssid(struct run_softc *, const uint8_t *);
409203134Sthompsastatic void	run_set_macaddr(struct run_softc *, const uint8_t *);
410203134Sthompsastatic void	run_updateslot(struct ifnet *);
411218492Sbschmidtstatic void	run_updateslot_cb(void *);
412208019Sthompsastatic void	run_update_mcast(struct ifnet *);
413203134Sthompsastatic int8_t	run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
414203134Sthompsastatic void	run_update_promisc_locked(struct ifnet *);
415203134Sthompsastatic void	run_update_promisc(struct ifnet *);
416203134Sthompsastatic int	run_bbp_init(struct run_softc *);
417203134Sthompsastatic int	run_rt3070_rf_init(struct run_softc *);
418203134Sthompsastatic int	run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
419203134Sthompsa		    uint8_t *);
420205042Sthompsastatic void	run_rt3070_rf_setup(struct run_softc *);
421203134Sthompsastatic int	run_txrx_enable(struct run_softc *);
422203134Sthompsastatic void	run_init(void *);
423203134Sthompsastatic void	run_init_locked(struct run_softc *);
424203134Sthompsastatic void	run_stop(void *);
425203134Sthompsastatic void	run_delay(struct run_softc *, unsigned int);
426203134Sthompsa
427203134Sthompsastatic const struct {
428208019Sthompsa	uint16_t	reg;
429203134Sthompsa	uint32_t	val;
430203134Sthompsa} rt2870_def_mac[] = {
431203134Sthompsa	RT2870_DEF_MAC
432203134Sthompsa};
433203134Sthompsa
434203134Sthompsastatic const struct {
435203134Sthompsa	uint8_t	reg;
436203134Sthompsa	uint8_t	val;
437203134Sthompsa} rt2860_def_bbp[] = {
438203134Sthompsa	RT2860_DEF_BBP
439203134Sthompsa};
440203134Sthompsa
441203134Sthompsastatic const struct rfprog {
442203134Sthompsa	uint8_t		chan;
443203134Sthompsa	uint32_t	r1, r2, r3, r4;
444203134Sthompsa} rt2860_rf2850[] = {
445203134Sthompsa	RT2860_RF2850
446203134Sthompsa};
447203134Sthompsa
448203134Sthompsastruct {
449203134Sthompsa	uint8_t	n, r, k;
450205042Sthompsa} rt3070_freqs[] = {
451205042Sthompsa	RT3070_RF3052
452203134Sthompsa};
453203134Sthompsa
454203134Sthompsastatic const struct {
455203134Sthompsa	uint8_t	reg;
456203134Sthompsa	uint8_t	val;
457203134Sthompsa} rt3070_def_rf[] = {
458203134Sthompsa	RT3070_DEF_RF
459205042Sthompsa},rt3572_def_rf[] = {
460205042Sthompsa	RT3572_DEF_RF
461203134Sthompsa};
462203134Sthompsa
463203134Sthompsastatic const struct usb_config run_config[RUN_N_XFER] = {
464203134Sthompsa    [RUN_BULK_TX_BE] = {
465203134Sthompsa	.type = UE_BULK,
466203134Sthompsa	.endpoint = UE_ADDR_ANY,
467203134Sthompsa	.ep_index = 0,
468203134Sthompsa	.direction = UE_DIR_OUT,
469203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
470203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
471203134Sthompsa	.callback = run_bulk_tx_callback0,
472203134Sthompsa	.timeout = 5000,	/* ms */
473203134Sthompsa    },
474203134Sthompsa    [RUN_BULK_TX_BK] = {
475203134Sthompsa	.type = UE_BULK,
476203134Sthompsa	.endpoint = UE_ADDR_ANY,
477203134Sthompsa	.direction = UE_DIR_OUT,
478203134Sthompsa	.ep_index = 1,
479203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
480203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
481203134Sthompsa	.callback = run_bulk_tx_callback1,
482203134Sthompsa	.timeout = 5000,	/* ms */
483203134Sthompsa    },
484203134Sthompsa    [RUN_BULK_TX_VI] = {
485203134Sthompsa	.type = UE_BULK,
486203134Sthompsa	.endpoint = UE_ADDR_ANY,
487203134Sthompsa	.direction = UE_DIR_OUT,
488203134Sthompsa	.ep_index = 2,
489203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
490203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
491203134Sthompsa	.callback = run_bulk_tx_callback2,
492203134Sthompsa	.timeout = 5000,	/* ms */
493203134Sthompsa    },
494203134Sthompsa    [RUN_BULK_TX_VO] = {
495203134Sthompsa	.type = UE_BULK,
496203134Sthompsa	.endpoint = UE_ADDR_ANY,
497203134Sthompsa	.direction = UE_DIR_OUT,
498203134Sthompsa	.ep_index = 3,
499203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
500203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
501203134Sthompsa	.callback = run_bulk_tx_callback3,
502203134Sthompsa	.timeout = 5000,	/* ms */
503203134Sthompsa    },
504203134Sthompsa    [RUN_BULK_TX_HCCA] = {
505203134Sthompsa	.type = UE_BULK,
506203134Sthompsa	.endpoint = UE_ADDR_ANY,
507203134Sthompsa	.direction = UE_DIR_OUT,
508203134Sthompsa	.ep_index = 4,
509203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
510203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
511203134Sthompsa	.callback = run_bulk_tx_callback4,
512203134Sthompsa	.timeout = 5000,	/* ms */
513203134Sthompsa    },
514203134Sthompsa    [RUN_BULK_TX_PRIO] = {
515203134Sthompsa	.type = UE_BULK,
516203134Sthompsa	.endpoint = UE_ADDR_ANY,
517203134Sthompsa	.direction = UE_DIR_OUT,
518203134Sthompsa	.ep_index = 5,
519203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
520203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
521203134Sthompsa	.callback = run_bulk_tx_callback5,
522203134Sthompsa	.timeout = 5000,	/* ms */
523203134Sthompsa    },
524203134Sthompsa    [RUN_BULK_RX] = {
525203134Sthompsa	.type = UE_BULK,
526203134Sthompsa	.endpoint = UE_ADDR_ANY,
527203134Sthompsa	.direction = UE_DIR_IN,
528203134Sthompsa	.bufsize = RUN_MAX_RXSZ,
529203134Sthompsa	.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
530203134Sthompsa	.callback = run_bulk_rx_callback,
531203134Sthompsa    }
532203134Sthompsa};
533203134Sthompsa
534220235Skevlostatic int
535203134Sthompsarun_match(device_t self)
536203134Sthompsa{
537203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
538203134Sthompsa
539203134Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
540203134Sthompsa		return (ENXIO);
541203134Sthompsa	if (uaa->info.bConfigIndex != 0)
542203134Sthompsa		return (ENXIO);
543203134Sthompsa	if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
544203134Sthompsa		return (ENXIO);
545203134Sthompsa
546203134Sthompsa	return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
547203134Sthompsa}
548203134Sthompsa
549203134Sthompsastatic int
550203134Sthompsarun_attach(device_t self)
551203134Sthompsa{
552203134Sthompsa	struct run_softc *sc = device_get_softc(self);
553203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
554203134Sthompsa	struct ieee80211com *ic;
555203134Sthompsa	struct ifnet *ifp;
556205042Sthompsa	uint32_t ver;
557203134Sthompsa	int i, ntries, error;
558203134Sthompsa	uint8_t iface_index, bands;
559203134Sthompsa
560203134Sthompsa	device_set_usb_desc(self);
561203134Sthompsa	sc->sc_udev = uaa->device;
562203134Sthompsa	sc->sc_dev = self;
563203134Sthompsa
564203134Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
565203134Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
566203134Sthompsa
567203134Sthompsa	iface_index = RT2860_IFACE_INDEX;
568208019Sthompsa
569203134Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
570203134Sthompsa	    sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
571203134Sthompsa	if (error) {
572205042Sthompsa		device_printf(self, "could not allocate USB transfers, "
573203134Sthompsa		    "err=%s\n", usbd_errstr(error));
574203134Sthompsa		goto detach;
575203134Sthompsa	}
576203134Sthompsa
577203134Sthompsa	RUN_LOCK(sc);
578203134Sthompsa
579203134Sthompsa	/* wait for the chip to settle */
580203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
581209917Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
582203134Sthompsa			RUN_UNLOCK(sc);
583203134Sthompsa			goto detach;
584203134Sthompsa		}
585205042Sthompsa		if (ver != 0 && ver != 0xffffffff)
586203134Sthompsa			break;
587203134Sthompsa		run_delay(sc, 10);
588203134Sthompsa	}
589203134Sthompsa	if (ntries == 100) {
590203138Sthompsa		device_printf(sc->sc_dev,
591203138Sthompsa		    "timeout waiting for NIC to initialize\n");
592203134Sthompsa		RUN_UNLOCK(sc);
593203134Sthompsa		goto detach;
594203134Sthompsa	}
595205042Sthompsa	sc->mac_ver = ver >> 16;
596205042Sthompsa	sc->mac_rev = ver & 0xffff;
597203134Sthompsa
598203134Sthompsa	/* retrieve RF rev. no and various other things from EEPROM */
599203134Sthompsa	run_read_eeprom(sc);
600203134Sthompsa
601203138Sthompsa	device_printf(sc->sc_dev,
602203138Sthompsa	    "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
603205042Sthompsa	    sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
604203138Sthompsa	    sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
605203134Sthompsa
606203134Sthompsa	RUN_UNLOCK(sc);
607203134Sthompsa
608203134Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
609220235Skevlo	if (ifp == NULL) {
610203138Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
611203134Sthompsa		goto detach;
612203134Sthompsa	}
613203134Sthompsa	ic = ifp->if_l2com;
614203134Sthompsa
615203134Sthompsa	ifp->if_softc = sc;
616203134Sthompsa	if_initname(ifp, "run", device_get_unit(sc->sc_dev));
617203134Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
618203134Sthompsa	ifp->if_init = run_init;
619203134Sthompsa	ifp->if_ioctl = run_ioctl;
620203134Sthompsa	ifp->if_start = run_start;
621207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
622207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
623203134Sthompsa	IFQ_SET_READY(&ifp->if_snd);
624203134Sthompsa
625203134Sthompsa	ic->ic_ifp = ifp;
626203134Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
627203134Sthompsa	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
628208019Sthompsa
629203134Sthompsa	/* set device capabilities */
630203134Sthompsa	ic->ic_caps =
631203134Sthompsa	    IEEE80211_C_STA |		/* station mode supported */
632203134Sthompsa	    IEEE80211_C_MONITOR |	/* monitor mode supported */
633203134Sthompsa	    IEEE80211_C_IBSS |
634203134Sthompsa	    IEEE80211_C_HOSTAP |
635208019Sthompsa	    IEEE80211_C_WDS |		/* 4-address traffic works */
636208019Sthompsa	    IEEE80211_C_MBSS |
637203134Sthompsa	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
638203134Sthompsa	    IEEE80211_C_SHSLOT |	/* short slot time supported */
639203134Sthompsa	    IEEE80211_C_WME |		/* WME */
640214894Sbschmidt	    IEEE80211_C_WPA;		/* WPA1|WPA2(RSN) */
641203134Sthompsa
642203134Sthompsa	ic->ic_cryptocaps =
643203134Sthompsa	    IEEE80211_CRYPTO_WEP |
644203134Sthompsa	    IEEE80211_CRYPTO_AES_CCM |
645203134Sthompsa	    IEEE80211_CRYPTO_TKIPMIC |
646203134Sthompsa	    IEEE80211_CRYPTO_TKIP;
647203134Sthompsa
648203134Sthompsa	ic->ic_flags |= IEEE80211_F_DATAPAD;
649203134Sthompsa	ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
650203134Sthompsa
651203134Sthompsa	bands = 0;
652203134Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
653203134Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
654203134Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
655203134Sthompsa
656203134Sthompsa	/*
657203134Sthompsa	 * Do this by own because h/w supports
658203134Sthompsa	 * more channels than ieee80211_init_channels()
659203134Sthompsa	 */
660205042Sthompsa	if (sc->rf_rev == RT2860_RF_2750 ||
661205042Sthompsa	    sc->rf_rev == RT2860_RF_2850 ||
662205042Sthompsa	    sc->rf_rev == RT3070_RF_3052) {
663203134Sthompsa		/* set supported .11a rates */
664233774Shselasky		for (i = 14; i < N(rt2860_rf2850); i++) {
665203134Sthompsa			uint8_t chan = rt2860_rf2850[i].chan;
666203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_freq =
667203134Sthompsa			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A);
668203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_ieee = chan;
669203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A;
670203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_extieee = 0;
671203134Sthompsa			ic->ic_nchans++;
672203134Sthompsa		}
673203134Sthompsa	}
674203134Sthompsa
675203134Sthompsa	ieee80211_ifattach(ic, sc->sc_bssid);
676203134Sthompsa
677203134Sthompsa	ic->ic_scan_start = run_scan_start;
678203134Sthompsa	ic->ic_scan_end = run_scan_end;
679203134Sthompsa	ic->ic_set_channel = run_set_channel;
680203134Sthompsa	ic->ic_node_alloc = run_node_alloc;
681203134Sthompsa	ic->ic_newassoc = run_newassoc;
682218492Sbschmidt	ic->ic_updateslot = run_updateslot;
683208019Sthompsa	ic->ic_update_mcast = run_update_mcast;
684203134Sthompsa	ic->ic_wme.wme_update = run_wme_update;
685203134Sthompsa	ic->ic_raw_xmit = run_raw_xmit;
686203134Sthompsa	ic->ic_update_promisc = run_update_promisc;
687203134Sthompsa
688203134Sthompsa	ic->ic_vap_create = run_vap_create;
689203134Sthompsa	ic->ic_vap_delete = run_vap_delete;
690203134Sthompsa
691203134Sthompsa	ieee80211_radiotap_attach(ic,
692203134Sthompsa	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
693203134Sthompsa		RUN_TX_RADIOTAP_PRESENT,
694203134Sthompsa	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
695203134Sthompsa		RUN_RX_RADIOTAP_PRESENT);
696203134Sthompsa
697208019Sthompsa	TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
698208019Sthompsa	TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
699208019Sthompsa	callout_init((struct callout *)&sc->ratectl_ch, 1);
700208019Sthompsa
701203134Sthompsa	if (bootverbose)
702203134Sthompsa		ieee80211_announce(ic);
703203134Sthompsa
704209917Sthompsa	return (0);
705203134Sthompsa
706203134Sthompsadetach:
707203134Sthompsa	run_detach(self);
708209917Sthompsa	return (ENXIO);
709203134Sthompsa}
710203134Sthompsa
711203134Sthompsastatic int
712203134Sthompsarun_detach(device_t self)
713203134Sthompsa{
714203134Sthompsa	struct run_softc *sc = device_get_softc(self);
715203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
716203134Sthompsa	struct ieee80211com *ic;
717203134Sthompsa	int i;
718203134Sthompsa
719246614Shselasky	RUN_LOCK(sc);
720246614Shselasky	sc->sc_detached = 1;
721246614Shselasky	RUN_UNLOCK(sc);
722246614Shselasky
723203134Sthompsa	/* stop all USB transfers */
724203134Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
725203134Sthompsa
726203134Sthompsa	RUN_LOCK(sc);
727209144Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
728209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
729209144Sthompsa
730203134Sthompsa	/* free TX list, if any */
731203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
732203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
733203134Sthompsa	RUN_UNLOCK(sc);
734203134Sthompsa
735203134Sthompsa	if (ifp) {
736203134Sthompsa		ic = ifp->if_l2com;
737208019Sthompsa		/* drain tasks */
738208019Sthompsa		usb_callout_drain(&sc->ratectl_ch);
739208019Sthompsa		ieee80211_draintask(ic, &sc->cmdq_task);
740208019Sthompsa		ieee80211_draintask(ic, &sc->ratectl_task);
741203134Sthompsa		ieee80211_ifdetach(ic);
742203134Sthompsa		if_free(ifp);
743203134Sthompsa	}
744203134Sthompsa
745203134Sthompsa	mtx_destroy(&sc->sc_mtx);
746203134Sthompsa
747203134Sthompsa	return (0);
748203134Sthompsa}
749203134Sthompsa
750203134Sthompsastatic struct ieee80211vap *
751228621Sbschmidtrun_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
752228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
753203134Sthompsa    const uint8_t bssid[IEEE80211_ADDR_LEN],
754203134Sthompsa    const uint8_t mac[IEEE80211_ADDR_LEN])
755203134Sthompsa{
756208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
757208019Sthompsa	struct run_softc *sc = ifp->if_softc;
758203134Sthompsa	struct run_vap *rvp;
759203134Sthompsa	struct ieee80211vap *vap;
760208019Sthompsa	int i;
761203134Sthompsa
762209917Sthompsa	if (sc->rvp_cnt >= RUN_VAP_MAX) {
763208019Sthompsa		if_printf(ifp, "number of VAPs maxed out\n");
764209917Sthompsa		return (NULL);
765208019Sthompsa	}
766208019Sthompsa
767208019Sthompsa	switch (opmode) {
768208019Sthompsa	case IEEE80211_M_STA:
769208019Sthompsa		/* enable s/w bmiss handling for sta mode */
770208019Sthompsa		flags |= IEEE80211_CLONE_NOBEACONS;
771208019Sthompsa		/* fall though */
772208019Sthompsa	case IEEE80211_M_IBSS:
773208019Sthompsa	case IEEE80211_M_MONITOR:
774208019Sthompsa	case IEEE80211_M_HOSTAP:
775208019Sthompsa	case IEEE80211_M_MBSS:
776208019Sthompsa		/* other than WDS vaps, only one at a time */
777208019Sthompsa		if (!TAILQ_EMPTY(&ic->ic_vaps))
778209917Sthompsa			return (NULL);
779208019Sthompsa		break;
780208019Sthompsa	case IEEE80211_M_WDS:
781208019Sthompsa		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
782208019Sthompsa			if(vap->iv_opmode != IEEE80211_M_HOSTAP)
783208019Sthompsa				continue;
784208019Sthompsa			/* WDS vap's always share the local mac address. */
785208019Sthompsa			flags &= ~IEEE80211_CLONE_BSSID;
786208019Sthompsa			break;
787208019Sthompsa		}
788209917Sthompsa		if (vap == NULL) {
789208019Sthompsa			if_printf(ifp, "wds only supported in ap mode\n");
790209917Sthompsa			return (NULL);
791208019Sthompsa		}
792208019Sthompsa		break;
793208019Sthompsa	default:
794208019Sthompsa		if_printf(ifp, "unknown opmode %d\n", opmode);
795209917Sthompsa		return (NULL);
796208019Sthompsa	}
797208019Sthompsa
798208019Sthompsa	rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
799203134Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
800203134Sthompsa	if (rvp == NULL)
801209917Sthompsa		return (NULL);
802203134Sthompsa	vap = &rvp->vap;
803208019Sthompsa	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
804203134Sthompsa
805203134Sthompsa	vap->iv_key_update_begin = run_key_update_begin;
806203134Sthompsa	vap->iv_key_update_end = run_key_update_end;
807203134Sthompsa	vap->iv_update_beacon = run_update_beacon;
808208019Sthompsa	vap->iv_max_aid = RT2870_WCID_MAX;
809208019Sthompsa	/*
810208019Sthompsa	 * To delete the right key from h/w, we need wcid.
811208019Sthompsa	 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
812208019Sthompsa	 * and matching wcid will be written into there. So, cast
813208019Sthompsa	 * some spells to remove 'const' from ieee80211_key{}
814208019Sthompsa	 */
815208019Sthompsa	vap->iv_key_delete = (void *)run_key_delete;
816208019Sthompsa	vap->iv_key_set = (void *)run_key_set;
817203134Sthompsa
818203134Sthompsa	/* override state transition machine */
819203134Sthompsa	rvp->newstate = vap->iv_newstate;
820203134Sthompsa	vap->iv_newstate = run_newstate;
821203134Sthompsa
822206358Srpaulo	ieee80211_ratectl_init(vap);
823206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
824203134Sthompsa
825203134Sthompsa	/* complete setup */
826203134Sthompsa	ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
827208019Sthompsa
828208019Sthompsa	/* make sure id is always unique */
829209917Sthompsa	for (i = 0; i < RUN_VAP_MAX; i++) {
830208019Sthompsa		if((sc->rvp_bmap & 1 << i) == 0){
831208019Sthompsa			sc->rvp_bmap |= 1 << i;
832208019Sthompsa			rvp->rvp_id = i;
833208019Sthompsa			break;
834208019Sthompsa		}
835208019Sthompsa	}
836209917Sthompsa	if (sc->rvp_cnt++ == 0)
837208019Sthompsa		ic->ic_opmode = opmode;
838208019Sthompsa
839209917Sthompsa	if (opmode == IEEE80211_M_HOSTAP)
840209144Sthompsa		sc->cmdq_run = RUN_CMDQ_GO;
841209144Sthompsa
842208019Sthompsa	DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
843208019Sthompsa	    rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
844208019Sthompsa
845209917Sthompsa	return (vap);
846203134Sthompsa}
847203134Sthompsa
848203134Sthompsastatic void
849203134Sthompsarun_vap_delete(struct ieee80211vap *vap)
850203134Sthompsa{
851203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
852203134Sthompsa	struct ifnet *ifp;
853203134Sthompsa	struct ieee80211com *ic;
854203134Sthompsa	struct run_softc *sc;
855208019Sthompsa	uint8_t rvp_id;
856203134Sthompsa
857209917Sthompsa	if (vap == NULL)
858203134Sthompsa		return;
859203134Sthompsa
860203134Sthompsa	ic = vap->iv_ic;
861203134Sthompsa	ifp = ic->ic_ifp;
862203134Sthompsa
863203134Sthompsa	sc = ifp->if_softc;
864203134Sthompsa
865205042Sthompsa	RUN_LOCK(sc);
866208019Sthompsa
867218492Sbschmidt	m_freem(rvp->beacon_mbuf);
868218492Sbschmidt	rvp->beacon_mbuf = NULL;
869218492Sbschmidt
870208019Sthompsa	rvp_id = rvp->rvp_id;
871208019Sthompsa	sc->ratectl_run &= ~(1 << rvp_id);
872208019Sthompsa	sc->rvp_bmap &= ~(1 << rvp_id);
873208019Sthompsa	run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
874208019Sthompsa	run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
875208019Sthompsa	--sc->rvp_cnt;
876208019Sthompsa
877208019Sthompsa	DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
878208019Sthompsa	    vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
879208019Sthompsa
880205042Sthompsa	RUN_UNLOCK(sc);
881203134Sthompsa
882206358Srpaulo	ieee80211_ratectl_deinit(vap);
883203134Sthompsa	ieee80211_vap_detach(vap);
884203134Sthompsa	free(rvp, M_80211_VAP);
885203134Sthompsa}
886203134Sthompsa
887208019Sthompsa/*
888208019Sthompsa * There are numbers of functions need to be called in context thread.
889208019Sthompsa * Rather than creating taskqueue event for each of those functions,
890208019Sthompsa * here is all-for-one taskqueue callback function. This function
891208019Sthompsa * gurantees deferred functions are executed in the same order they
892208019Sthompsa * were enqueued.
893208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[].
894208019Sthompsa */
895203134Sthompsastatic void
896208019Sthompsarun_cmdq_cb(void *arg, int pending)
897208019Sthompsa{
898208019Sthompsa	struct run_softc *sc = arg;
899208019Sthompsa	uint8_t i;
900208019Sthompsa
901208019Sthompsa	/* call cmdq[].func locked */
902208019Sthompsa	RUN_LOCK(sc);
903209917Sthompsa	for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
904209917Sthompsa	    i = sc->cmdq_exec, pending--) {
905208019Sthompsa		DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
906209917Sthompsa		if (sc->cmdq_run == RUN_CMDQ_GO) {
907208019Sthompsa			/*
908208019Sthompsa			 * If arg0 is NULL, callback func needs more
909208019Sthompsa			 * than one arg. So, pass ptr to cmdq struct.
910208019Sthompsa			 */
911209917Sthompsa			if (sc->cmdq[i].arg0)
912208019Sthompsa				sc->cmdq[i].func(sc->cmdq[i].arg0);
913208019Sthompsa			else
914208019Sthompsa				sc->cmdq[i].func(&sc->cmdq[i]);
915208019Sthompsa		}
916208019Sthompsa		sc->cmdq[i].arg0 = NULL;
917208019Sthompsa		sc->cmdq[i].func = NULL;
918208019Sthompsa		sc->cmdq_exec++;
919208019Sthompsa		sc->cmdq_exec &= RUN_CMDQ_MASQ;
920208019Sthompsa	}
921208019Sthompsa	RUN_UNLOCK(sc);
922208019Sthompsa}
923208019Sthompsa
924208019Sthompsastatic void
925203134Sthompsarun_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
926203134Sthompsa{
927203134Sthompsa	struct run_tx_data *data;
928203134Sthompsa
929203134Sthompsa	memset(pq, 0, sizeof(*pq));
930203134Sthompsa
931203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
932203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
933203134Sthompsa
934203134Sthompsa	for (data = &pq->tx_data[0];
935203134Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
936203134Sthompsa		data->sc = sc;
937203134Sthompsa		STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
938203134Sthompsa	}
939203134Sthompsa	pq->tx_nfree = RUN_TX_RING_COUNT;
940203134Sthompsa}
941203134Sthompsa
942203134Sthompsastatic void
943203134Sthompsarun_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
944203134Sthompsa{
945203134Sthompsa	struct run_tx_data *data;
946203134Sthompsa
947203134Sthompsa	/* make sure any subsequent use of the queues will fail */
948203134Sthompsa	pq->tx_nfree = 0;
949203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
950203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
951203134Sthompsa
952203134Sthompsa	/* free up all node references and mbufs */
953203134Sthompsa	for (data = &pq->tx_data[0];
954209917Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
955203134Sthompsa		if (data->m != NULL) {
956203134Sthompsa			m_freem(data->m);
957203134Sthompsa			data->m = NULL;
958203134Sthompsa		}
959203134Sthompsa		if (data->ni != NULL) {
960203134Sthompsa			ieee80211_free_node(data->ni);
961203134Sthompsa			data->ni = NULL;
962203134Sthompsa		}
963203134Sthompsa	}
964203134Sthompsa}
965203134Sthompsa
966220235Skevlostatic int
967203134Sthompsarun_load_microcode(struct run_softc *sc)
968203134Sthompsa{
969203134Sthompsa	usb_device_request_t req;
970203137Sthompsa	const struct firmware *fw;
971203134Sthompsa	const u_char *base;
972203134Sthompsa	uint32_t tmp;
973203134Sthompsa	int ntries, error;
974203134Sthompsa	const uint64_t *temp;
975203134Sthompsa	uint64_t bytes;
976203134Sthompsa
977205042Sthompsa	RUN_UNLOCK(sc);
978203137Sthompsa	fw = firmware_get("runfw");
979205042Sthompsa	RUN_LOCK(sc);
980209917Sthompsa	if (fw == NULL) {
981203138Sthompsa		device_printf(sc->sc_dev,
982203138Sthompsa		    "failed loadfirmware of file %s\n", "runfw");
983203134Sthompsa		return ENOENT;
984203134Sthompsa	}
985203134Sthompsa
986203137Sthompsa	if (fw->datasize != 8192) {
987203138Sthompsa		device_printf(sc->sc_dev,
988203138Sthompsa		    "invalid firmware size (should be 8KB)\n");
989203137Sthompsa		error = EINVAL;
990203137Sthompsa		goto fail;
991203134Sthompsa	}
992203134Sthompsa
993203134Sthompsa	/*
994203134Sthompsa	 * RT3071/RT3072 use a different firmware
995203134Sthompsa	 * run-rt2870 (8KB) contains both,
996203134Sthompsa	 * first half (4KB) is for rt2870,
997203134Sthompsa	 * last half is for rt3071.
998203134Sthompsa	 */
999203137Sthompsa	base = fw->data;
1000205042Sthompsa	if ((sc->mac_ver) != 0x2860 &&
1001205042Sthompsa	    (sc->mac_ver) != 0x2872 &&
1002209917Sthompsa	    (sc->mac_ver) != 0x3070) {
1003203134Sthompsa		base += 4096;
1004205042Sthompsa	}
1005203134Sthompsa
1006203134Sthompsa	/* cheap sanity check */
1007203137Sthompsa	temp = fw->data;
1008203134Sthompsa	bytes = *temp;
1009209917Sthompsa	if (bytes != be64toh(0xffffff0210280210)) {
1010203138Sthompsa		device_printf(sc->sc_dev, "firmware checksum failed\n");
1011203137Sthompsa		error = EINVAL;
1012203137Sthompsa		goto fail;
1013203137Sthompsa	}
1014203134Sthompsa
1015203134Sthompsa	run_read(sc, RT2860_ASIC_VER_ID, &tmp);
1016203134Sthompsa	/* write microcode image */
1017203134Sthompsa	run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1018203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1019203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1020203134Sthompsa
1021203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1022203134Sthompsa	req.bRequest = RT2870_RESET;
1023203134Sthompsa	USETW(req.wValue, 8);
1024203134Sthompsa	USETW(req.wIndex, 0);
1025203134Sthompsa	USETW(req.wLength, 0);
1026220235Skevlo	if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL))
1027220235Skevlo	    != 0) {
1028203138Sthompsa		device_printf(sc->sc_dev, "firmware reset failed\n");
1029203137Sthompsa		goto fail;
1030203137Sthompsa	}
1031203134Sthompsa
1032203134Sthompsa	run_delay(sc, 10);
1033203134Sthompsa
1034203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
1035205042Sthompsa	if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1036203137Sthompsa		goto fail;
1037203134Sthompsa
1038203134Sthompsa	/* wait until microcontroller is ready */
1039203134Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
1040203137Sthompsa		if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1041203137Sthompsa			goto fail;
1042203137Sthompsa		}
1043203134Sthompsa		if (tmp & RT2860_MCU_READY)
1044203134Sthompsa			break;
1045203134Sthompsa		run_delay(sc, 10);
1046203134Sthompsa	}
1047203134Sthompsa	if (ntries == 1000) {
1048203138Sthompsa		device_printf(sc->sc_dev,
1049203138Sthompsa		    "timeout waiting for MCU to initialize\n");
1050203137Sthompsa		error = ETIMEDOUT;
1051203137Sthompsa		goto fail;
1052203134Sthompsa	}
1053233283Sbschmidt	device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n",
1054233283Sbschmidt	    (base == fw->data) ? "RT2870" : "RT3071",
1055233283Sbschmidt	    *(base + 4092), *(base + 4093));
1056203134Sthompsa
1057203137Sthompsafail:
1058203137Sthompsa	firmware_put(fw, FIRMWARE_UNLOAD);
1059203137Sthompsa	return (error);
1060203134Sthompsa}
1061203134Sthompsa
1062203134Sthompsaint
1063203134Sthompsarun_reset(struct run_softc *sc)
1064203134Sthompsa{
1065203134Sthompsa	usb_device_request_t req;
1066203134Sthompsa
1067203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1068203134Sthompsa	req.bRequest = RT2870_RESET;
1069203134Sthompsa	USETW(req.wValue, 1);
1070203134Sthompsa	USETW(req.wIndex, 0);
1071203134Sthompsa	USETW(req.wLength, 0);
1072209917Sthompsa	return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1073203134Sthompsa}
1074203134Sthompsa
1075203134Sthompsastatic usb_error_t
1076203134Sthompsarun_do_request(struct run_softc *sc,
1077203134Sthompsa    struct usb_device_request *req, void *data)
1078203134Sthompsa{
1079203134Sthompsa	usb_error_t err;
1080203134Sthompsa	int ntries = 10;
1081203134Sthompsa
1082203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1083203134Sthompsa
1084203134Sthompsa	while (ntries--) {
1085203134Sthompsa		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1086203134Sthompsa		    req, data, 0, NULL, 250 /* ms */);
1087203134Sthompsa		if (err == 0)
1088203134Sthompsa			break;
1089203134Sthompsa		DPRINTFN(1, "Control request failed, %s (retrying)\n",
1090203134Sthompsa		    usbd_errstr(err));
1091203134Sthompsa		run_delay(sc, 10);
1092203134Sthompsa	}
1093203134Sthompsa	return (err);
1094203134Sthompsa}
1095203134Sthompsa
1096203134Sthompsastatic int
1097203134Sthompsarun_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1098203134Sthompsa{
1099203134Sthompsa	uint32_t tmp;
1100203134Sthompsa	int error;
1101203134Sthompsa
1102203134Sthompsa	error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1103203134Sthompsa	if (error == 0)
1104203134Sthompsa		*val = le32toh(tmp);
1105203134Sthompsa	else
1106203134Sthompsa		*val = 0xffffffff;
1107209917Sthompsa	return (error);
1108203134Sthompsa}
1109203134Sthompsa
1110203134Sthompsastatic int
1111203134Sthompsarun_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1112203134Sthompsa{
1113203134Sthompsa	usb_device_request_t req;
1114203134Sthompsa
1115203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1116203134Sthompsa	req.bRequest = RT2870_READ_REGION_1;
1117203134Sthompsa	USETW(req.wValue, 0);
1118203134Sthompsa	USETW(req.wIndex, reg);
1119203134Sthompsa	USETW(req.wLength, len);
1120203134Sthompsa
1121209917Sthompsa	return (run_do_request(sc, &req, buf));
1122203134Sthompsa}
1123203134Sthompsa
1124203134Sthompsastatic int
1125203134Sthompsarun_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1126203134Sthompsa{
1127203134Sthompsa	usb_device_request_t req;
1128203134Sthompsa
1129203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1130203134Sthompsa	req.bRequest = RT2870_WRITE_2;
1131203134Sthompsa	USETW(req.wValue, val);
1132203134Sthompsa	USETW(req.wIndex, reg);
1133203134Sthompsa	USETW(req.wLength, 0);
1134203134Sthompsa
1135209917Sthompsa	return (run_do_request(sc, &req, NULL));
1136203134Sthompsa}
1137203134Sthompsa
1138203134Sthompsastatic int
1139203134Sthompsarun_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1140203134Sthompsa{
1141203134Sthompsa	int error;
1142203134Sthompsa
1143203134Sthompsa	if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1144203134Sthompsa		error = run_write_2(sc, reg + 2, val >> 16);
1145209917Sthompsa	return (error);
1146203134Sthompsa}
1147203134Sthompsa
1148203134Sthompsastatic int
1149203134Sthompsarun_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1150203134Sthompsa    int len)
1151203134Sthompsa{
1152203134Sthompsa#if 1
1153203134Sthompsa	int i, error = 0;
1154203134Sthompsa	/*
1155203134Sthompsa	 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1156203134Sthompsa	 * We thus issue multiple WRITE_2 commands instead.
1157203134Sthompsa	 */
1158203134Sthompsa	KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1159203134Sthompsa	for (i = 0; i < len && error == 0; i += 2)
1160203134Sthompsa		error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1161209917Sthompsa	return (error);
1162203134Sthompsa#else
1163203134Sthompsa	usb_device_request_t req;
1164203134Sthompsa
1165203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1166203134Sthompsa	req.bRequest = RT2870_WRITE_REGION_1;
1167203134Sthompsa	USETW(req.wValue, 0);
1168203134Sthompsa	USETW(req.wIndex, reg);
1169203134Sthompsa	USETW(req.wLength, len);
1170209917Sthompsa	return (run_do_request(sc, &req, buf));
1171203134Sthompsa#endif
1172203134Sthompsa}
1173203134Sthompsa
1174203134Sthompsastatic int
1175203134Sthompsarun_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1176203134Sthompsa{
1177203134Sthompsa	int i, error = 0;
1178203134Sthompsa
1179203134Sthompsa	KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1180203134Sthompsa	for (i = 0; i < len && error == 0; i += 4)
1181203134Sthompsa		error = run_write(sc, reg + i, val);
1182209917Sthompsa	return (error);
1183203134Sthompsa}
1184203134Sthompsa
1185203134Sthompsa/* Read 16-bit from eFUSE ROM (RT3070 only.) */
1186203134Sthompsastatic int
1187203134Sthompsarun_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1188203134Sthompsa{
1189203134Sthompsa	uint32_t tmp;
1190203134Sthompsa	uint16_t reg;
1191203134Sthompsa	int error, ntries;
1192203134Sthompsa
1193203134Sthompsa	if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1194209917Sthompsa		return (error);
1195203134Sthompsa
1196203134Sthompsa	addr *= 2;
1197203134Sthompsa	/*-
1198203134Sthompsa	 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1199203134Sthompsa	 * DATA0: F E D C
1200203134Sthompsa	 * DATA1: B A 9 8
1201203134Sthompsa	 * DATA2: 7 6 5 4
1202203134Sthompsa	 * DATA3: 3 2 1 0
1203203134Sthompsa	 */
1204203134Sthompsa	tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1205203134Sthompsa	tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1206203134Sthompsa	run_write(sc, RT3070_EFUSE_CTRL, tmp);
1207203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1208203134Sthompsa		if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1209209917Sthompsa			return (error);
1210203134Sthompsa		if (!(tmp & RT3070_EFSROM_KICK))
1211203134Sthompsa			break;
1212203134Sthompsa		run_delay(sc, 2);
1213203134Sthompsa	}
1214203134Sthompsa	if (ntries == 100)
1215209917Sthompsa		return (ETIMEDOUT);
1216203134Sthompsa
1217203134Sthompsa	if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1218203134Sthompsa		*val = 0xffff;	/* address not found */
1219209917Sthompsa		return (0);
1220203134Sthompsa	}
1221203134Sthompsa	/* determine to which 32-bit register our 16-bit word belongs */
1222203134Sthompsa	reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1223203134Sthompsa	if ((error = run_read(sc, reg, &tmp)) != 0)
1224209917Sthompsa		return (error);
1225203134Sthompsa
1226203134Sthompsa	*val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1227209917Sthompsa	return (0);
1228203134Sthompsa}
1229203134Sthompsa
1230203134Sthompsastatic int
1231203134Sthompsarun_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1232203134Sthompsa{
1233203134Sthompsa	usb_device_request_t req;
1234203134Sthompsa	uint16_t tmp;
1235203134Sthompsa	int error;
1236203134Sthompsa
1237203134Sthompsa	addr *= 2;
1238203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1239203134Sthompsa	req.bRequest = RT2870_EEPROM_READ;
1240203134Sthompsa	USETW(req.wValue, 0);
1241203134Sthompsa	USETW(req.wIndex, addr);
1242203134Sthompsa	USETW(req.wLength, sizeof tmp);
1243203134Sthompsa
1244203134Sthompsa	error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1245203134Sthompsa	if (error == 0)
1246203134Sthompsa		*val = le16toh(tmp);
1247203134Sthompsa	else
1248203134Sthompsa		*val = 0xffff;
1249209917Sthompsa	return (error);
1250203134Sthompsa}
1251203134Sthompsa
1252203134Sthompsastatic __inline int
1253203134Sthompsarun_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1254203134Sthompsa{
1255203134Sthompsa	/* either eFUSE ROM or EEPROM */
1256203134Sthompsa	return sc->sc_srom_read(sc, addr, val);
1257203134Sthompsa}
1258203134Sthompsa
1259203134Sthompsastatic int
1260203134Sthompsarun_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val)
1261203134Sthompsa{
1262203134Sthompsa	uint32_t tmp;
1263203134Sthompsa	int error, ntries;
1264203134Sthompsa
1265203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1266203134Sthompsa		if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1267209917Sthompsa			return (error);
1268203134Sthompsa		if (!(tmp & RT2860_RF_REG_CTRL))
1269203134Sthompsa			break;
1270203134Sthompsa	}
1271203134Sthompsa	if (ntries == 10)
1272209917Sthompsa		return (ETIMEDOUT);
1273203134Sthompsa
1274203134Sthompsa	/* RF registers are 24-bit on the RT2860 */
1275203134Sthompsa	tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1276203134Sthompsa	    (val & 0x3fffff) << 2 | (reg & 3);
1277209917Sthompsa	return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1278203134Sthompsa}
1279203134Sthompsa
1280203134Sthompsastatic int
1281203134Sthompsarun_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1282203134Sthompsa{
1283203134Sthompsa	uint32_t tmp;
1284203134Sthompsa	int error, ntries;
1285203134Sthompsa
1286203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1287203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1288209917Sthompsa			return (error);
1289203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1290203134Sthompsa			break;
1291203134Sthompsa	}
1292203134Sthompsa	if (ntries == 100)
1293209917Sthompsa		return (ETIMEDOUT);
1294203134Sthompsa
1295203134Sthompsa	tmp = RT3070_RF_KICK | reg << 8;
1296203134Sthompsa	if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1297209917Sthompsa		return (error);
1298203134Sthompsa
1299203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1300203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1301209917Sthompsa			return (error);
1302203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1303203134Sthompsa			break;
1304203134Sthompsa	}
1305203134Sthompsa	if (ntries == 100)
1306209917Sthompsa		return (ETIMEDOUT);
1307203134Sthompsa
1308203134Sthompsa	*val = tmp & 0xff;
1309209917Sthompsa	return (0);
1310203134Sthompsa}
1311203134Sthompsa
1312203134Sthompsastatic int
1313203134Sthompsarun_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1314203134Sthompsa{
1315203134Sthompsa	uint32_t tmp;
1316203134Sthompsa	int error, ntries;
1317203134Sthompsa
1318203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1319203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1320209917Sthompsa			return (error);
1321203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1322203134Sthompsa			break;
1323203134Sthompsa	}
1324203134Sthompsa	if (ntries == 10)
1325209917Sthompsa		return (ETIMEDOUT);
1326203134Sthompsa
1327203134Sthompsa	tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1328209917Sthompsa	return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1329203134Sthompsa}
1330203134Sthompsa
1331203134Sthompsastatic int
1332203134Sthompsarun_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1333203134Sthompsa{
1334203134Sthompsa	uint32_t tmp;
1335203134Sthompsa	int ntries, error;
1336203134Sthompsa
1337203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1338203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1339209917Sthompsa			return (error);
1340203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1341203134Sthompsa			break;
1342203134Sthompsa	}
1343203134Sthompsa	if (ntries == 10)
1344209917Sthompsa		return (ETIMEDOUT);
1345203134Sthompsa
1346203134Sthompsa	tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1347203134Sthompsa	if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1348209917Sthompsa		return (error);
1349203134Sthompsa
1350203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1351203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1352209917Sthompsa			return (error);
1353203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1354203134Sthompsa			break;
1355203134Sthompsa	}
1356203134Sthompsa	if (ntries == 10)
1357209917Sthompsa		return (ETIMEDOUT);
1358203134Sthompsa
1359203134Sthompsa	*val = tmp & 0xff;
1360209917Sthompsa	return (0);
1361203134Sthompsa}
1362203134Sthompsa
1363203134Sthompsastatic int
1364203134Sthompsarun_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1365203134Sthompsa{
1366203134Sthompsa	uint32_t tmp;
1367203134Sthompsa	int ntries, error;
1368203134Sthompsa
1369203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1370203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1371209917Sthompsa			return (error);
1372203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1373203134Sthompsa			break;
1374203134Sthompsa	}
1375203134Sthompsa	if (ntries == 10)
1376209917Sthompsa		return (ETIMEDOUT);
1377203134Sthompsa
1378203134Sthompsa	tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1379209917Sthompsa	return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1380203134Sthompsa}
1381203134Sthompsa
1382203134Sthompsa/*
1383203134Sthompsa * Send a command to the 8051 microcontroller unit.
1384203134Sthompsa */
1385203134Sthompsastatic int
1386203134Sthompsarun_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1387203134Sthompsa{
1388203134Sthompsa	uint32_t tmp;
1389203134Sthompsa	int error, ntries;
1390203134Sthompsa
1391203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1392203134Sthompsa		if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1393203134Sthompsa			return error;
1394203134Sthompsa		if (!(tmp & RT2860_H2M_BUSY))
1395203134Sthompsa			break;
1396203134Sthompsa	}
1397203134Sthompsa	if (ntries == 100)
1398203134Sthompsa		return ETIMEDOUT;
1399203134Sthompsa
1400203134Sthompsa	tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1401203134Sthompsa	if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1402203134Sthompsa		error = run_write(sc, RT2860_HOST_CMD, cmd);
1403209917Sthompsa	return (error);
1404203134Sthompsa}
1405203134Sthompsa
1406203134Sthompsa/*
1407203134Sthompsa * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1408203134Sthompsa * Used to adjust per-rate Tx power registers.
1409203134Sthompsa */
1410203134Sthompsastatic __inline uint32_t
1411203134Sthompsab4inc(uint32_t b32, int8_t delta)
1412203134Sthompsa{
1413203134Sthompsa	int8_t i, b4;
1414203134Sthompsa
1415203134Sthompsa	for (i = 0; i < 8; i++) {
1416203134Sthompsa		b4 = b32 & 0xf;
1417203134Sthompsa		b4 += delta;
1418203134Sthompsa		if (b4 < 0)
1419203134Sthompsa			b4 = 0;
1420203134Sthompsa		else if (b4 > 0xf)
1421203134Sthompsa			b4 = 0xf;
1422203134Sthompsa		b32 = b32 >> 4 | b4 << 28;
1423203134Sthompsa	}
1424209917Sthompsa	return (b32);
1425203134Sthompsa}
1426203134Sthompsa
1427203134Sthompsastatic const char *
1428203134Sthompsarun_get_rf(int rev)
1429203134Sthompsa{
1430203134Sthompsa	switch (rev) {
1431203134Sthompsa	case RT2860_RF_2820:	return "RT2820";
1432203134Sthompsa	case RT2860_RF_2850:	return "RT2850";
1433203134Sthompsa	case RT2860_RF_2720:	return "RT2720";
1434203134Sthompsa	case RT2860_RF_2750:	return "RT2750";
1435203134Sthompsa	case RT3070_RF_3020:	return "RT3020";
1436203134Sthompsa	case RT3070_RF_2020:	return "RT2020";
1437203134Sthompsa	case RT3070_RF_3021:	return "RT3021";
1438203134Sthompsa	case RT3070_RF_3022:	return "RT3022";
1439203134Sthompsa	case RT3070_RF_3052:	return "RT3052";
1440203134Sthompsa	}
1441209917Sthompsa	return ("unknown");
1442203134Sthompsa}
1443203134Sthompsa
1444203134Sthompsaint
1445203134Sthompsarun_read_eeprom(struct run_softc *sc)
1446203134Sthompsa{
1447203134Sthompsa	int8_t delta_2ghz, delta_5ghz;
1448203134Sthompsa	uint32_t tmp;
1449203134Sthompsa	uint16_t val;
1450203134Sthompsa	int ridx, ant, i;
1451203134Sthompsa
1452203134Sthompsa	/* check whether the ROM is eFUSE ROM or EEPROM */
1453203134Sthompsa	sc->sc_srom_read = run_eeprom_read_2;
1454205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1455203134Sthompsa		run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1456203134Sthompsa		DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1457203134Sthompsa		if (tmp & RT3070_SEL_EFUSE)
1458203134Sthompsa			sc->sc_srom_read = run_efuse_read_2;
1459203134Sthompsa	}
1460203134Sthompsa
1461203134Sthompsa	/* read ROM version */
1462203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1463203134Sthompsa	DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1464203134Sthompsa
1465203134Sthompsa	/* read MAC address */
1466203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1467203134Sthompsa	sc->sc_bssid[0] = val & 0xff;
1468203134Sthompsa	sc->sc_bssid[1] = val >> 8;
1469203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1470203134Sthompsa	sc->sc_bssid[2] = val & 0xff;
1471203134Sthompsa	sc->sc_bssid[3] = val >> 8;
1472203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1473203134Sthompsa	sc->sc_bssid[4] = val & 0xff;
1474203134Sthompsa	sc->sc_bssid[5] = val >> 8;
1475203134Sthompsa
1476205042Sthompsa	/* read vender BBP settings */
1477205042Sthompsa	for (i = 0; i < 10; i++) {
1478203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1479203134Sthompsa		sc->bbp[i].val = val & 0xff;
1480203134Sthompsa		sc->bbp[i].reg = val >> 8;
1481203134Sthompsa		DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val);
1482203134Sthompsa	}
1483205042Sthompsa	if (sc->mac_ver >= 0x3071) {
1484205042Sthompsa		/* read vendor RF settings */
1485205042Sthompsa		for (i = 0; i < 10; i++) {
1486205042Sthompsa			run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val);
1487205042Sthompsa			sc->rf[i].val = val & 0xff;
1488205042Sthompsa			sc->rf[i].reg = val >> 8;
1489205042Sthompsa			DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1490205042Sthompsa			    sc->rf[i].val);
1491205042Sthompsa		}
1492205042Sthompsa	}
1493203134Sthompsa
1494203134Sthompsa	/* read RF frequency offset from EEPROM */
1495203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1496203134Sthompsa	sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1497203134Sthompsa	DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1498203134Sthompsa
1499205042Sthompsa	if (val >> 8 != 0xff) {
1500203134Sthompsa		/* read LEDs operating mode */
1501205042Sthompsa		sc->leds = val >> 8;
1502203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1503203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1504203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1505203134Sthompsa	} else {
1506203134Sthompsa		/* broken EEPROM, use default settings */
1507203134Sthompsa		sc->leds = 0x01;
1508203134Sthompsa		sc->led[0] = 0x5555;
1509203134Sthompsa		sc->led[1] = 0x2221;
1510203134Sthompsa		sc->led[2] = 0x5627;	/* differs from RT2860 */
1511203134Sthompsa	}
1512203134Sthompsa	DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1513203134Sthompsa	    sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1514203134Sthompsa
1515203134Sthompsa	/* read RF information */
1516203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1517203134Sthompsa	if (val == 0xffff) {
1518203134Sthompsa		DPRINTF("invalid EEPROM antenna info, using default\n");
1519205042Sthompsa		if (sc->mac_ver == 0x3572) {
1520205042Sthompsa			/* default to RF3052 2T2R */
1521205042Sthompsa			sc->rf_rev = RT3070_RF_3052;
1522205042Sthompsa			sc->ntxchains = 2;
1523205042Sthompsa			sc->nrxchains = 2;
1524205042Sthompsa		} else if (sc->mac_ver >= 0x3070) {
1525203134Sthompsa			/* default to RF3020 1T1R */
1526203134Sthompsa			sc->rf_rev = RT3070_RF_3020;
1527203134Sthompsa			sc->ntxchains = 1;
1528203134Sthompsa			sc->nrxchains = 1;
1529203134Sthompsa		} else {
1530203134Sthompsa			/* default to RF2820 1T2R */
1531203134Sthompsa			sc->rf_rev = RT2860_RF_2820;
1532203134Sthompsa			sc->ntxchains = 1;
1533203134Sthompsa			sc->nrxchains = 2;
1534203134Sthompsa		}
1535203134Sthompsa	} else {
1536203134Sthompsa		sc->rf_rev = (val >> 8) & 0xf;
1537203134Sthompsa		sc->ntxchains = (val >> 4) & 0xf;
1538203134Sthompsa		sc->nrxchains = val & 0xf;
1539203134Sthompsa	}
1540203134Sthompsa	DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n",
1541203134Sthompsa	    sc->rf_rev, sc->ntxchains, sc->nrxchains);
1542203134Sthompsa
1543208019Sthompsa	/* check if RF supports automatic Tx access gain control */
1544203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1545203134Sthompsa	DPRINTF("EEPROM CFG 0x%04x\n", val);
1546205042Sthompsa	/* check if driver should patch the DAC issue */
1547205042Sthompsa	if ((val >> 8) != 0xff)
1548205042Sthompsa		sc->patch_dac = (val >> 15) & 1;
1549203134Sthompsa	if ((val & 0xff) != 0xff) {
1550203134Sthompsa		sc->ext_5ghz_lna = (val >> 3) & 1;
1551203134Sthompsa		sc->ext_2ghz_lna = (val >> 2) & 1;
1552205042Sthompsa		/* check if RF supports automatic Tx access gain control */
1553203134Sthompsa		sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1554205042Sthompsa		/* check if we have a hardware radio switch */
1555205042Sthompsa		sc->rfswitch = val & 1;
1556203134Sthompsa	}
1557203134Sthompsa
1558203134Sthompsa	/* read power settings for 2GHz channels */
1559203134Sthompsa	for (i = 0; i < 14; i += 2) {
1560203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1561203134Sthompsa		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1562203134Sthompsa		sc->txpow1[i + 1] = (int8_t)(val >> 8);
1563203134Sthompsa
1564203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1565203134Sthompsa		sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1566203134Sthompsa		sc->txpow2[i + 1] = (int8_t)(val >> 8);
1567203134Sthompsa	}
1568203134Sthompsa	/* fix broken Tx power entries */
1569203134Sthompsa	for (i = 0; i < 14; i++) {
1570203134Sthompsa		if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1571203134Sthompsa			sc->txpow1[i] = 5;
1572203134Sthompsa		if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1573203134Sthompsa			sc->txpow2[i] = 5;
1574203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1575203134Sthompsa		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1576203134Sthompsa	}
1577203134Sthompsa	/* read power settings for 5GHz channels */
1578205042Sthompsa	for (i = 0; i < 40; i += 2) {
1579203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1580203134Sthompsa		sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1581203134Sthompsa		sc->txpow1[i + 15] = (int8_t)(val >> 8);
1582203134Sthompsa
1583203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1584203134Sthompsa		sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1585203134Sthompsa		sc->txpow2[i + 15] = (int8_t)(val >> 8);
1586203134Sthompsa	}
1587203134Sthompsa	/* fix broken Tx power entries */
1588205042Sthompsa	for (i = 0; i < 40; i++) {
1589203134Sthompsa		if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1590203134Sthompsa			sc->txpow1[14 + i] = 5;
1591203134Sthompsa		if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1592203134Sthompsa			sc->txpow2[14 + i] = 5;
1593203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1594203134Sthompsa		    rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1595203134Sthompsa		    sc->txpow2[14 + i]);
1596203134Sthompsa	}
1597203134Sthompsa
1598203134Sthompsa	/* read Tx power compensation for each Tx rate */
1599203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1600203134Sthompsa	delta_2ghz = delta_5ghz = 0;
1601203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1602203134Sthompsa		delta_2ghz = val & 0xf;
1603203134Sthompsa		if (!(val & 0x40))	/* negative number */
1604203134Sthompsa			delta_2ghz = -delta_2ghz;
1605203134Sthompsa	}
1606203134Sthompsa	val >>= 8;
1607203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1608203134Sthompsa		delta_5ghz = val & 0xf;
1609203134Sthompsa		if (!(val & 0x40))	/* negative number */
1610203134Sthompsa			delta_5ghz = -delta_5ghz;
1611203134Sthompsa	}
1612203134Sthompsa	DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1613203134Sthompsa	    delta_2ghz, delta_5ghz);
1614203134Sthompsa
1615203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
1616203134Sthompsa		uint32_t reg;
1617203134Sthompsa
1618208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1619208019Sthompsa		reg = val;
1620208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1621208019Sthompsa		reg |= (uint32_t)val << 16;
1622203134Sthompsa
1623203134Sthompsa		sc->txpow20mhz[ridx] = reg;
1624203134Sthompsa		sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1625203134Sthompsa		sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1626203134Sthompsa
1627203134Sthompsa		DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1628203134Sthompsa		    "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1629203134Sthompsa		    sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1630203134Sthompsa	}
1631203134Sthompsa
1632203134Sthompsa	/* read RSSI offsets and LNA gains from EEPROM */
1633203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1634203134Sthompsa	sc->rssi_2ghz[0] = val & 0xff;	/* Ant A */
1635203134Sthompsa	sc->rssi_2ghz[1] = val >> 8;	/* Ant B */
1636203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1637205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1638205042Sthompsa		/*
1639205042Sthompsa		 * On RT3070 chips (limited to 2 Rx chains), this ROM
1640205042Sthompsa		 * field contains the Tx mixer gain for the 2GHz band.
1641205042Sthompsa		 */
1642205042Sthompsa		if ((val & 0xff) != 0xff)
1643205042Sthompsa			sc->txmixgain_2ghz = val & 0x7;
1644205042Sthompsa		DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1645205042Sthompsa	} else
1646205042Sthompsa		sc->rssi_2ghz[2] = val & 0xff;	/* Ant C */
1647203134Sthompsa	sc->lna[2] = val >> 8;		/* channel group 2 */
1648203134Sthompsa
1649203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1650203134Sthompsa	sc->rssi_5ghz[0] = val & 0xff;	/* Ant A */
1651203134Sthompsa	sc->rssi_5ghz[1] = val >> 8;	/* Ant B */
1652203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1653205042Sthompsa	if (sc->mac_ver == 0x3572) {
1654205042Sthompsa		/*
1655205042Sthompsa		 * On RT3572 chips (limited to 2 Rx chains), this ROM
1656205042Sthompsa		 * field contains the Tx mixer gain for the 5GHz band.
1657205042Sthompsa		 */
1658205042Sthompsa		if ((val & 0xff) != 0xff)
1659205042Sthompsa			sc->txmixgain_5ghz = val & 0x7;
1660205042Sthompsa		DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1661205042Sthompsa	} else
1662205042Sthompsa		sc->rssi_5ghz[2] = val & 0xff;	/* Ant C */
1663203134Sthompsa	sc->lna[3] = val >> 8;		/* channel group 3 */
1664203134Sthompsa
1665203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1666203134Sthompsa	sc->lna[0] = val & 0xff;	/* channel group 0 */
1667203134Sthompsa	sc->lna[1] = val >> 8;		/* channel group 1 */
1668203134Sthompsa
1669203134Sthompsa	/* fix broken 5GHz LNA entries */
1670203134Sthompsa	if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1671203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 2);
1672203134Sthompsa		sc->lna[2] = sc->lna[1];
1673203134Sthompsa	}
1674203134Sthompsa	if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1675203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 3);
1676203134Sthompsa		sc->lna[3] = sc->lna[1];
1677203134Sthompsa	}
1678203134Sthompsa
1679203134Sthompsa	/* fix broken RSSI offset entries */
1680203134Sthompsa	for (ant = 0; ant < 3; ant++) {
1681203134Sthompsa		if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1682203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1683203134Sthompsa			    ant + 1, sc->rssi_2ghz[ant]);
1684203134Sthompsa			sc->rssi_2ghz[ant] = 0;
1685203134Sthompsa		}
1686203134Sthompsa		if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1687203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1688203134Sthompsa			    ant + 1, sc->rssi_5ghz[ant]);
1689203134Sthompsa			sc->rssi_5ghz[ant] = 0;
1690203134Sthompsa		}
1691203134Sthompsa	}
1692209917Sthompsa	return (0);
1693203134Sthompsa}
1694203134Sthompsa
1695218676Shselaskystatic struct ieee80211_node *
1696203134Sthompsarun_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1697203134Sthompsa{
1698203134Sthompsa	return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1699203134Sthompsa}
1700203134Sthompsa
1701203134Sthompsastatic int
1702203134Sthompsarun_media_change(struct ifnet *ifp)
1703203134Sthompsa{
1704208019Sthompsa	struct ieee80211vap *vap = ifp->if_softc;
1705208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1706203134Sthompsa	const struct ieee80211_txparam *tp;
1707208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1708203134Sthompsa	uint8_t rate, ridx;
1709203134Sthompsa	int error;
1710203134Sthompsa
1711203134Sthompsa	RUN_LOCK(sc);
1712203134Sthompsa
1713203134Sthompsa	error = ieee80211_media_change(ifp);
1714209917Sthompsa	if (error != ENETRESET) {
1715203134Sthompsa		RUN_UNLOCK(sc);
1716209917Sthompsa		return (error);
1717208019Sthompsa	}
1718203134Sthompsa
1719203134Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1720203134Sthompsa	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1721212127Sthompsa		struct ieee80211_node *ni;
1722212127Sthompsa		struct run_node	*rn;
1723212127Sthompsa
1724203134Sthompsa		rate = ic->ic_sup_rates[ic->ic_curmode].
1725203134Sthompsa		    rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1726203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1727203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
1728203134Sthompsa				break;
1729212127Sthompsa		ni = ieee80211_ref_node(vap->iv_bss);
1730212127Sthompsa		rn = (struct run_node *)ni;
1731208019Sthompsa		rn->fix_ridx = ridx;
1732208019Sthompsa		DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1733212127Sthompsa		ieee80211_free_node(ni);
1734203134Sthompsa	}
1735203134Sthompsa
1736208019Sthompsa#if 0
1737203134Sthompsa	if ((ifp->if_flags & IFF_UP) &&
1738203134Sthompsa	    (ifp->if_drv_flags &  IFF_DRV_RUNNING)){
1739203134Sthompsa		run_init_locked(sc);
1740203134Sthompsa	}
1741208019Sthompsa#endif
1742203134Sthompsa
1743203134Sthompsa	RUN_UNLOCK(sc);
1744203134Sthompsa
1745209917Sthompsa	return (0);
1746203134Sthompsa}
1747203134Sthompsa
1748203134Sthompsastatic int
1749203134Sthompsarun_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1750203134Sthompsa{
1751203134Sthompsa	const struct ieee80211_txparam *tp;
1752203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1753203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1754203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
1755203134Sthompsa	enum ieee80211_state ostate;
1756208019Sthompsa	uint32_t sta[3];
1757203134Sthompsa	uint32_t tmp;
1758208019Sthompsa	uint8_t ratectl;
1759208019Sthompsa	uint8_t restart_ratectl = 0;
1760208019Sthompsa	uint8_t bid = 1 << rvp->rvp_id;
1761203134Sthompsa
1762203134Sthompsa	ostate = vap->iv_state;
1763203134Sthompsa	DPRINTF("%s -> %s\n",
1764203134Sthompsa		ieee80211_state_name[ostate],
1765203134Sthompsa		ieee80211_state_name[nstate]);
1766203134Sthompsa
1767203134Sthompsa	IEEE80211_UNLOCK(ic);
1768203134Sthompsa	RUN_LOCK(sc);
1769203134Sthompsa
1770208019Sthompsa	ratectl = sc->ratectl_run; /* remember current state */
1771208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
1772208019Sthompsa	usb_callout_stop(&sc->ratectl_ch);
1773203134Sthompsa
1774203134Sthompsa	if (ostate == IEEE80211_S_RUN) {
1775203134Sthompsa		/* turn link LED off */
1776203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO);
1777203134Sthompsa	}
1778203134Sthompsa
1779203134Sthompsa	switch (nstate) {
1780203134Sthompsa	case IEEE80211_S_INIT:
1781208019Sthompsa		restart_ratectl = 1;
1782208019Sthompsa
1783208019Sthompsa		if (ostate != IEEE80211_S_RUN)
1784208019Sthompsa			break;
1785208019Sthompsa
1786208019Sthompsa		ratectl &= ~bid;
1787208019Sthompsa		sc->runbmap &= ~bid;
1788208019Sthompsa
1789208019Sthompsa		/* abort TSF synchronization if there is no vap running */
1790209917Sthompsa		if (--sc->running == 0) {
1791203134Sthompsa			run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1792203134Sthompsa			run_write(sc, RT2860_BCN_TIME_CFG,
1793203134Sthompsa			    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1794203134Sthompsa			    RT2860_TBTT_TIMER_EN));
1795203134Sthompsa		}
1796203134Sthompsa		break;
1797203134Sthompsa
1798203134Sthompsa	case IEEE80211_S_RUN:
1799209917Sthompsa		if (!(sc->runbmap & bid)) {
1800208019Sthompsa			if(sc->running++)
1801208019Sthompsa				restart_ratectl = 1;
1802208019Sthompsa			sc->runbmap |= bid;
1803208019Sthompsa		}
1804203134Sthompsa
1805218492Sbschmidt		m_freem(rvp->beacon_mbuf);
1806218492Sbschmidt		rvp->beacon_mbuf = NULL;
1807218492Sbschmidt
1808209917Sthompsa		switch (vap->iv_opmode) {
1809208019Sthompsa		case IEEE80211_M_HOSTAP:
1810208019Sthompsa		case IEEE80211_M_MBSS:
1811208019Sthompsa			sc->ap_running |= bid;
1812208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1813208019Sthompsa			run_update_beacon_cb(vap);
1814208019Sthompsa			break;
1815208019Sthompsa		case IEEE80211_M_IBSS:
1816208019Sthompsa			sc->adhoc_running |= bid;
1817209917Sthompsa			if (!sc->ap_running)
1818208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1819208019Sthompsa			run_update_beacon_cb(vap);
1820208019Sthompsa			break;
1821208019Sthompsa		case IEEE80211_M_STA:
1822208019Sthompsa			sc->sta_running |= bid;
1823209917Sthompsa			if (!sc->ap_running && !sc->adhoc_running)
1824208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1825208019Sthompsa
1826208019Sthompsa			/* read statistic counters (clear on read) */
1827208019Sthompsa			run_read_region_1(sc, RT2860_TX_STA_CNT0,
1828208019Sthompsa			    (uint8_t *)sta, sizeof sta);
1829208019Sthompsa
1830208019Sthompsa			break;
1831208019Sthompsa		default:
1832208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1833208019Sthompsa			break;
1834208019Sthompsa		}
1835208019Sthompsa
1836203134Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1837212127Sthompsa			struct ieee80211_node *ni;
1838212127Sthompsa
1839236439Shselasky			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
1840236439Shselasky				RUN_UNLOCK(sc);
1841236439Shselasky				IEEE80211_LOCK(ic);
1842236439Shselasky				return (-1);
1843236439Shselasky			}
1844203134Sthompsa			run_updateslot(ic->ic_ifp);
1845203134Sthompsa			run_enable_mrr(sc);
1846203134Sthompsa			run_set_txpreamble(sc);
1847203134Sthompsa			run_set_basicrates(sc);
1848212127Sthompsa			ni = ieee80211_ref_node(vap->iv_bss);
1849203134Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1850203134Sthompsa			run_set_bssid(sc, ni->ni_bssid);
1851212127Sthompsa			ieee80211_free_node(ni);
1852208019Sthompsa			run_enable_tsf_sync(sc);
1853203134Sthompsa
1854208019Sthompsa			/* enable automatic rate adaptation */
1855208019Sthompsa			tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1856208019Sthompsa			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1857208019Sthompsa				ratectl |= bid;
1858203134Sthompsa		}
1859203134Sthompsa
1860203134Sthompsa		/* turn link LED on */
1861203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO |
1862208019Sthompsa		    (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1863203134Sthompsa		     RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1864203134Sthompsa
1865203134Sthompsa		break;
1866203134Sthompsa	default:
1867203134Sthompsa		DPRINTFN(6, "undefined case\n");
1868203134Sthompsa		break;
1869203134Sthompsa	}
1870203134Sthompsa
1871208019Sthompsa	/* restart amrr for running VAPs */
1872209917Sthompsa	if ((sc->ratectl_run = ratectl) && restart_ratectl)
1873208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1874208019Sthompsa
1875203134Sthompsa	RUN_UNLOCK(sc);
1876203134Sthompsa	IEEE80211_LOCK(ic);
1877203134Sthompsa
1878203134Sthompsa	return(rvp->newstate(vap, nstate, arg));
1879203134Sthompsa}
1880203134Sthompsa
1881203134Sthompsa/* ARGSUSED */
1882203134Sthompsastatic void
1883208019Sthompsarun_wme_update_cb(void *arg)
1884203134Sthompsa{
1885203134Sthompsa	struct ieee80211com *ic = arg;
1886203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1887203134Sthompsa	struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1888203134Sthompsa	int aci, error = 0;
1889203134Sthompsa
1890208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1891203134Sthompsa
1892203134Sthompsa	/* update MAC TX configuration registers */
1893203134Sthompsa	for (aci = 0; aci < WME_NUM_AC; aci++) {
1894203134Sthompsa		error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1895203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmax << 16 |
1896203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmin << 12 |
1897203134Sthompsa		    wmesp->wme_params[aci].wmep_aifsn  <<  8 |
1898203134Sthompsa		    wmesp->wme_params[aci].wmep_txopLimit);
1899209917Sthompsa		if (error) goto err;
1900203134Sthompsa	}
1901203134Sthompsa
1902203134Sthompsa	/* update SCH/DMA registers too */
1903203134Sthompsa	error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1904203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_aifsn  << 12 |
1905203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_aifsn  <<  8 |
1906203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_aifsn  <<  4 |
1907203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1908209917Sthompsa	if (error) goto err;
1909203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1910203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1911203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmin <<  8 |
1912203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmin <<  4 |
1913203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1914209917Sthompsa	if (error) goto err;
1915203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1916203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1917203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmax <<  8 |
1918203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmax <<  4 |
1919203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1920209917Sthompsa	if (error) goto err;
1921203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1922203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1923203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1924209917Sthompsa	if (error) goto err;
1925203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1926203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1927203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1928203134Sthompsa
1929203134Sthompsaerr:
1930209917Sthompsa	if (error)
1931203134Sthompsa		DPRINTF("WME update failed\n");
1932203134Sthompsa
1933203134Sthompsa	return;
1934203134Sthompsa}
1935203134Sthompsa
1936208019Sthompsastatic int
1937208019Sthompsarun_wme_update(struct ieee80211com *ic)
1938208019Sthompsa{
1939208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1940208019Sthompsa
1941208019Sthompsa	/* sometime called wothout lock */
1942209917Sthompsa	if (mtx_owned(&ic->ic_comlock.mtx)) {
1943208019Sthompsa		uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1944208019Sthompsa		DPRINTF("cmdq_store=%d\n", i);
1945208019Sthompsa		sc->cmdq[i].func = run_wme_update_cb;
1946208019Sthompsa		sc->cmdq[i].arg0 = ic;
1947208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
1948209918Sthompsa		return (0);
1949208019Sthompsa	}
1950208019Sthompsa
1951208019Sthompsa	RUN_LOCK(sc);
1952208019Sthompsa	run_wme_update_cb(ic);
1953208019Sthompsa	RUN_UNLOCK(sc);
1954208019Sthompsa
1955208019Sthompsa	/* return whatever, upper layer desn't care anyway */
1956208019Sthompsa	return (0);
1957208019Sthompsa}
1958208019Sthompsa
1959203134Sthompsastatic void
1960203134Sthompsarun_key_update_begin(struct ieee80211vap *vap)
1961203134Sthompsa{
1962203134Sthompsa	/*
1963208019Sthompsa	 * To avoid out-of-order events, both run_key_set() and
1964208019Sthompsa	 * _delete() are deferred and handled by run_cmdq_cb().
1965208019Sthompsa	 * So, there is nothing we need to do here.
1966203134Sthompsa	 */
1967203134Sthompsa}
1968203134Sthompsa
1969203134Sthompsastatic void
1970203134Sthompsarun_key_update_end(struct ieee80211vap *vap)
1971203134Sthompsa{
1972203134Sthompsa	/* null */
1973203134Sthompsa}
1974203134Sthompsa
1975208019Sthompsastatic void
1976208019Sthompsarun_key_set_cb(void *arg)
1977203134Sthompsa{
1978208019Sthompsa	struct run_cmdq *cmdq = arg;
1979208019Sthompsa	struct ieee80211vap *vap = cmdq->arg1;
1980208019Sthompsa	struct ieee80211_key *k = cmdq->k;
1981203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1982208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1983203134Sthompsa	struct ieee80211_node *ni;
1984203134Sthompsa	uint32_t attr;
1985203134Sthompsa	uint16_t base, associd;
1986209144Sthompsa	uint8_t mode, wcid, iv[8];
1987203134Sthompsa
1988208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1989203134Sthompsa
1990209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1991208019Sthompsa		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
1992209144Sthompsa	else
1993203134Sthompsa		ni = vap->iv_bss;
1994208019Sthompsa	associd = (ni != NULL) ? ni->ni_associd : 0;
1995203134Sthompsa
1996203134Sthompsa	/* map net80211 cipher to RT2860 security mode */
1997203134Sthompsa	switch (k->wk_cipher->ic_cipher) {
1998203134Sthompsa	case IEEE80211_CIPHER_WEP:
1999203134Sthompsa		if(k->wk_keylen < 8)
2000203134Sthompsa			mode = RT2860_MODE_WEP40;
2001203134Sthompsa		else
2002203134Sthompsa			mode = RT2860_MODE_WEP104;
2003203134Sthompsa		break;
2004203134Sthompsa	case IEEE80211_CIPHER_TKIP:
2005203134Sthompsa		mode = RT2860_MODE_TKIP;
2006203134Sthompsa		break;
2007203134Sthompsa	case IEEE80211_CIPHER_AES_CCM:
2008203134Sthompsa		mode = RT2860_MODE_AES_CCMP;
2009203134Sthompsa		break;
2010203134Sthompsa	default:
2011203134Sthompsa		DPRINTF("undefined case\n");
2012208019Sthompsa		return;
2013203134Sthompsa	}
2014203134Sthompsa
2015208019Sthompsa	DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2016203134Sthompsa	    associd, k->wk_keyix, mode,
2017208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2018208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2019208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2020203134Sthompsa
2021203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2022203134Sthompsa		wcid = 0;	/* NB: update WCID0 for group keys */
2023208019Sthompsa		base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2024203134Sthompsa	} else {
2025245047Shselasky		wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2026245047Shselasky		    1 : RUN_AID2WCID(associd);
2027203134Sthompsa		base = RT2860_PKEY(wcid);
2028203134Sthompsa	}
2029203134Sthompsa
2030203134Sthompsa	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2031203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, 16))
2032208019Sthompsa			return;
2033209144Sthompsa		if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8))	/* wk_txmic */
2034208019Sthompsa			return;
2035209144Sthompsa		if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8))	/* wk_rxmic */
2036208019Sthompsa			return;
2037203134Sthompsa	} else {
2038203134Sthompsa		/* roundup len to 16-bit: XXX fix write_region_1() instead */
2039203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2040208019Sthompsa			return;
2041203134Sthompsa	}
2042203134Sthompsa
2043203134Sthompsa	if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2044203134Sthompsa	    (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2045203134Sthompsa		/* set initial packet number in IV+EIV */
2046209917Sthompsa		if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2047203134Sthompsa			memset(iv, 0, sizeof iv);
2048208019Sthompsa			iv[3] = vap->iv_def_txkey << 6;
2049203134Sthompsa		} else {
2050203134Sthompsa			if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2051203134Sthompsa				iv[0] = k->wk_keytsc >> 8;
2052203134Sthompsa				iv[1] = (iv[0] | 0x20) & 0x7f;
2053203134Sthompsa				iv[2] = k->wk_keytsc;
2054203134Sthompsa			} else /* CCMP */ {
2055203134Sthompsa				iv[0] = k->wk_keytsc;
2056203134Sthompsa				iv[1] = k->wk_keytsc >> 8;
2057203134Sthompsa				iv[2] = 0;
2058203134Sthompsa			}
2059203134Sthompsa			iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2060203134Sthompsa			iv[4] = k->wk_keytsc >> 16;
2061203134Sthompsa			iv[5] = k->wk_keytsc >> 24;
2062203134Sthompsa			iv[6] = k->wk_keytsc >> 32;
2063203134Sthompsa			iv[7] = k->wk_keytsc >> 40;
2064203134Sthompsa		}
2065209917Sthompsa		if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2066208019Sthompsa			return;
2067203134Sthompsa	}
2068203134Sthompsa
2069203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2070203134Sthompsa		/* install group key */
2071209917Sthompsa		if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2072208019Sthompsa			return;
2073203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2074203134Sthompsa		attr |= mode << (k->wk_keyix * 4);
2075209917Sthompsa		if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2076208019Sthompsa			return;
2077203134Sthompsa	} else {
2078203134Sthompsa		/* install pairwise key */
2079209917Sthompsa		if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2080208019Sthompsa			return;
2081203134Sthompsa		attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2082209917Sthompsa		if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2083208019Sthompsa			return;
2084203134Sthompsa	}
2085203134Sthompsa
2086203134Sthompsa	/* TODO create a pass-thru key entry? */
2087203134Sthompsa
2088208019Sthompsa	/* need wcid to delete the right key later */
2089208019Sthompsa	k->wk_pad = wcid;
2090203134Sthompsa}
2091203134Sthompsa
2092203134Sthompsa/*
2093208019Sthompsa * Don't have to be deferred, but in order to keep order of
2094208019Sthompsa * execution, i.e. with run_key_delete(), defer this and let
2095208019Sthompsa * run_cmdq_cb() maintain the order.
2096208019Sthompsa *
2097203134Sthompsa * return 0 on error
2098203134Sthompsa */
2099203134Sthompsastatic int
2100208019Sthompsarun_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2101208019Sthompsa		const uint8_t mac[IEEE80211_ADDR_LEN])
2102203134Sthompsa{
2103203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2104203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2105208019Sthompsa	uint32_t i;
2106208019Sthompsa
2107208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2108208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2109208019Sthompsa	sc->cmdq[i].func = run_key_set_cb;
2110208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2111208019Sthompsa	sc->cmdq[i].arg1 = vap;
2112208019Sthompsa	sc->cmdq[i].k = k;
2113208019Sthompsa	IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2114208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2115208019Sthompsa
2116209144Sthompsa	/*
2117209144Sthompsa	 * To make sure key will be set when hostapd
2118209144Sthompsa	 * calls iv_key_set() before if_init().
2119209144Sthompsa	 */
2120209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2121209144Sthompsa		RUN_LOCK(sc);
2122209144Sthompsa		sc->cmdq_key_set = RUN_CMDQ_GO;
2123209144Sthompsa		RUN_UNLOCK(sc);
2124209144Sthompsa	}
2125209144Sthompsa
2126209917Sthompsa	return (1);
2127208019Sthompsa}
2128208019Sthompsa
2129208019Sthompsa/*
2130208019Sthompsa * If wlan is destroyed without being brought down i.e. without
2131208019Sthompsa * wlan down or wpa_cli terminate, this function is called after
2132208019Sthompsa * vap is gone. Don't refer it.
2133208019Sthompsa */
2134208019Sthompsastatic void
2135208019Sthompsarun_key_delete_cb(void *arg)
2136208019Sthompsa{
2137208019Sthompsa	struct run_cmdq *cmdq = arg;
2138208019Sthompsa	struct run_softc *sc = cmdq->arg1;
2139208019Sthompsa	struct ieee80211_key *k = &cmdq->key;
2140203134Sthompsa	uint32_t attr;
2141203134Sthompsa	uint8_t wcid;
2142203134Sthompsa
2143208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2144203134Sthompsa
2145203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2146203134Sthompsa		/* remove group key */
2147208019Sthompsa		DPRINTF("removing group key\n");
2148208019Sthompsa		run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2149203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2150208019Sthompsa		run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2151203134Sthompsa	} else {
2152203134Sthompsa		/* remove pairwise key */
2153208019Sthompsa		DPRINTF("removing key for wcid %x\n", k->wk_pad);
2154208019Sthompsa		/* matching wcid was written to wk_pad in run_key_set() */
2155208019Sthompsa		wcid = k->wk_pad;
2156208019Sthompsa		run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2157203134Sthompsa		attr &= ~0xf;
2158208019Sthompsa		run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2159208019Sthompsa		run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2160203134Sthompsa	}
2161203134Sthompsa
2162208019Sthompsa	k->wk_pad = 0;
2163203134Sthompsa}
2164203134Sthompsa
2165208019Sthompsa/*
2166208019Sthompsa * return 0 on error
2167208019Sthompsa */
2168208019Sthompsastatic int
2169208019Sthompsarun_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2170203134Sthompsa{
2171208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2172208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2173208019Sthompsa	struct ieee80211_key *k0;
2174208019Sthompsa	uint32_t i;
2175203134Sthompsa
2176208019Sthompsa	/*
2177208019Sthompsa	 * When called back, key might be gone. So, make a copy
2178208019Sthompsa	 * of some values need to delete keys before deferring.
2179208019Sthompsa	 * But, because of LOR with node lock, cannot use lock here.
2180208019Sthompsa	 * So, use atomic instead.
2181208019Sthompsa	 */
2182208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2183208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2184208019Sthompsa	sc->cmdq[i].func = run_key_delete_cb;
2185208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2186208019Sthompsa	sc->cmdq[i].arg1 = sc;
2187208019Sthompsa	k0 = &sc->cmdq[i].key;
2188208019Sthompsa	k0->wk_flags = k->wk_flags;
2189208019Sthompsa	k0->wk_keyix = k->wk_keyix;
2190208019Sthompsa	/* matching wcid was written to wk_pad in run_key_set() */
2191208019Sthompsa	k0->wk_pad = k->wk_pad;
2192208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2193208019Sthompsa	return (1);	/* return fake success */
2194203134Sthompsa
2195203134Sthompsa}
2196203134Sthompsa
2197203134Sthompsastatic void
2198206358Srpaulorun_ratectl_to(void *arg)
2199203134Sthompsa{
2200208019Sthompsa	struct run_softc *sc = arg;
2201203134Sthompsa
2202203134Sthompsa	/* do it in a process context, so it can go sleep */
2203208019Sthompsa	ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2204203134Sthompsa	/* next timeout will be rescheduled in the callback task */
2205203134Sthompsa}
2206203134Sthompsa
2207203134Sthompsa/* ARGSUSED */
2208203134Sthompsastatic void
2209206358Srpaulorun_ratectl_cb(void *arg, int pending)
2210203134Sthompsa{
2211208019Sthompsa	struct run_softc *sc = arg;
2212208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2213208019Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2214203134Sthompsa
2215209917Sthompsa	if (vap == NULL)
2216208019Sthompsa		return;
2217208019Sthompsa
2218209917Sthompsa	if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2219208019Sthompsa		run_iter_func(sc, vap->iv_bss);
2220203134Sthompsa	else {
2221203134Sthompsa		/*
2222203134Sthompsa		 * run_reset_livelock() doesn't do anything with AMRR,
2223203134Sthompsa		 * but Ralink wants us to call it every 1 sec. So, we
2224203134Sthompsa		 * piggyback here rather than creating another callout.
2225203134Sthompsa		 * Livelock may occur only in HOSTAP or IBSS mode
2226203134Sthompsa		 * (when h/w is sending beacons).
2227203134Sthompsa		 */
2228203134Sthompsa		RUN_LOCK(sc);
2229203134Sthompsa		run_reset_livelock(sc);
2230208019Sthompsa		/* just in case, there are some stats to drain */
2231208019Sthompsa		run_drain_fifo(sc);
2232203134Sthompsa		RUN_UNLOCK(sc);
2233208019Sthompsa		ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2234203134Sthompsa	}
2235203134Sthompsa
2236208019Sthompsa	if(sc->ratectl_run != RUN_RATECTL_OFF)
2237208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2238203134Sthompsa}
2239203134Sthompsa
2240203134Sthompsastatic void
2241208019Sthompsarun_drain_fifo(void *arg)
2242203134Sthompsa{
2243208019Sthompsa	struct run_softc *sc = arg;
2244208019Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2245208019Sthompsa	uint32_t stat;
2246218676Shselasky	uint16_t (*wstat)[3];
2247203134Sthompsa	uint8_t wcid, mcs, pid;
2248218676Shselasky	int8_t retry;
2249203134Sthompsa
2250208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2251203134Sthompsa
2252208019Sthompsa	for (;;) {
2253203134Sthompsa		/* drain Tx status FIFO (maxsize = 16) */
2254203134Sthompsa		run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2255208019Sthompsa		DPRINTFN(4, "tx stat 0x%08x\n", stat);
2256209917Sthompsa		if (!(stat & RT2860_TXQ_VLD))
2257208019Sthompsa			break;
2258203134Sthompsa
2259208019Sthompsa		wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2260203134Sthompsa
2261208019Sthompsa		/* if no ACK was requested, no feedback is available */
2262208019Sthompsa		if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2263208019Sthompsa		    wcid == 0)
2264208019Sthompsa			continue;
2265203134Sthompsa
2266218676Shselasky		/*
2267218676Shselasky		 * Even though each stat is Tx-complete-status like format,
2268218676Shselasky		 * the device can poll stats. Because there is no guarantee
2269218676Shselasky		 * that the referring node is still around when read the stats.
2270218676Shselasky		 * So that, if we use ieee80211_ratectl_tx_update(), we will
2271218676Shselasky		 * have hard time not to refer already freed node.
2272218676Shselasky		 *
2273218676Shselasky		 * To eliminate such page faults, we poll stats in softc.
2274218676Shselasky		 * Then, update the rates later with ieee80211_ratectl_tx_update().
2275218676Shselasky		 */
2276218676Shselasky		wstat = &(sc->wcid_stats[wcid]);
2277218676Shselasky		(*wstat)[RUN_TXCNT]++;
2278218676Shselasky		if (stat & RT2860_TXQ_OK)
2279218676Shselasky			(*wstat)[RUN_SUCCESS]++;
2280218676Shselasky		else
2281208019Sthompsa			ifp->if_oerrors++;
2282218676Shselasky		/*
2283218676Shselasky		 * Check if there were retries, ie if the Tx success rate is
2284218676Shselasky		 * different from the requested rate. Note that it works only
2285218676Shselasky		 * because we do not allow rate fallback from OFDM to CCK.
2286218676Shselasky		 */
2287218676Shselasky		mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2288218676Shselasky		pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2289218676Shselasky		if ((retry = pid -1 - mcs) > 0) {
2290218676Shselasky			(*wstat)[RUN_TXCNT] += retry;
2291218676Shselasky			(*wstat)[RUN_RETRY] += retry;
2292203134Sthompsa		}
2293208019Sthompsa	}
2294208019Sthompsa	DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2295208019Sthompsa
2296208019Sthompsa	sc->fifo_cnt = 0;
2297208019Sthompsa}
2298208019Sthompsa
2299208019Sthompsastatic void
2300208019Sthompsarun_iter_func(void *arg, struct ieee80211_node *ni)
2301208019Sthompsa{
2302208019Sthompsa	struct run_softc *sc = arg;
2303208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2304208019Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2305208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2306208019Sthompsa	struct run_node *rn = (void *)ni;
2307218676Shselasky	union run_stats sta[2];
2308218676Shselasky	uint16_t (*wstat)[3];
2309218676Shselasky	int txcnt, success, retrycnt, error;
2310208019Sthompsa
2311218676Shselasky	RUN_LOCK(sc);
2312218676Shselasky
2313209917Sthompsa	if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2314209917Sthompsa	    vap->iv_opmode == IEEE80211_M_STA)) {
2315203134Sthompsa		/* read statistic counters (clear on read) and update AMRR state */
2316203134Sthompsa		error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2317203134Sthompsa		    sizeof sta);
2318203134Sthompsa		if (error != 0)
2319218676Shselasky			goto fail;
2320203134Sthompsa
2321203134Sthompsa		/* count failed TX as errors */
2322218676Shselasky		ifp->if_oerrors += le16toh(sta[0].error.fail);
2323203134Sthompsa
2324218676Shselasky		retrycnt = le16toh(sta[1].tx.retry);
2325218676Shselasky		success = le16toh(sta[1].tx.success);
2326218676Shselasky		txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2327203134Sthompsa
2328218676Shselasky		DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2329218676Shselasky			retrycnt, success, le16toh(sta[0].error.fail));
2330218676Shselasky	} else {
2331218676Shselasky		wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2332203134Sthompsa
2333218676Shselasky		if (wstat == &(sc->wcid_stats[0]) ||
2334218676Shselasky		    wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2335218676Shselasky			goto fail;
2336208019Sthompsa
2337218676Shselasky		txcnt = (*wstat)[RUN_TXCNT];
2338218676Shselasky		success = (*wstat)[RUN_SUCCESS];
2339218676Shselasky		retrycnt = (*wstat)[RUN_RETRY];
2340218676Shselasky		DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2341218676Shselasky		    retrycnt, txcnt, success);
2342208019Sthompsa
2343218676Shselasky		memset(wstat, 0, sizeof(*wstat));
2344203134Sthompsa	}
2345203134Sthompsa
2346218676Shselasky	ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2347208019Sthompsa	rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2348218676Shselasky
2349218676Shselaskyfail:
2350218676Shselasky	RUN_UNLOCK(sc);
2351218676Shselasky
2352208019Sthompsa	DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2353208019Sthompsa}
2354203134Sthompsa
2355208019Sthompsastatic void
2356208019Sthompsarun_newassoc_cb(void *arg)
2357208019Sthompsa{
2358208019Sthompsa	struct run_cmdq *cmdq = arg;
2359208019Sthompsa	struct ieee80211_node *ni = cmdq->arg1;
2360208019Sthompsa	struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2361208019Sthompsa	uint8_t wcid = cmdq->wcid;
2362203134Sthompsa
2363208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2364208019Sthompsa
2365208019Sthompsa	run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2366208019Sthompsa	    ni->ni_macaddr, IEEE80211_ADDR_LEN);
2367218676Shselasky
2368218676Shselasky	memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2369203134Sthompsa}
2370203134Sthompsa
2371203134Sthompsastatic void
2372203134Sthompsarun_newassoc(struct ieee80211_node *ni, int isnew)
2373203134Sthompsa{
2374203134Sthompsa	struct run_node *rn = (void *)ni;
2375203134Sthompsa	struct ieee80211_rateset *rs = &ni->ni_rates;
2376208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2377208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2378208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2379203134Sthompsa	uint8_t rate;
2380208019Sthompsa	uint8_t ridx;
2381245047Shselasky	uint8_t wcid;
2382208019Sthompsa	int i, j;
2383203134Sthompsa
2384245047Shselasky	wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2385245047Shselasky	    1 : RUN_AID2WCID(ni->ni_associd);
2386245047Shselasky
2387209917Sthompsa	if (wcid > RT2870_WCID_MAX) {
2388208019Sthompsa		device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2389208019Sthompsa		return;
2390208019Sthompsa	}
2391203134Sthompsa
2392208019Sthompsa	/* only interested in true associations */
2393209917Sthompsa	if (isnew && ni->ni_associd != 0) {
2394208019Sthompsa
2395208019Sthompsa		/*
2396208019Sthompsa		 * This function could is called though timeout function.
2397208019Sthompsa		 * Need to defer.
2398208019Sthompsa		 */
2399208019Sthompsa		uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2400208019Sthompsa		DPRINTF("cmdq_store=%d\n", cnt);
2401208019Sthompsa		sc->cmdq[cnt].func = run_newassoc_cb;
2402208019Sthompsa		sc->cmdq[cnt].arg0 = NULL;
2403208019Sthompsa		sc->cmdq[cnt].arg1 = ni;
2404208019Sthompsa		sc->cmdq[cnt].wcid = wcid;
2405208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
2406208019Sthompsa	}
2407208019Sthompsa
2408208019Sthompsa	DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2409208019Sthompsa	    isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2410208019Sthompsa
2411203134Sthompsa	for (i = 0; i < rs->rs_nrates; i++) {
2412203134Sthompsa		rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2413203134Sthompsa		/* convert 802.11 rate to hardware rate index */
2414203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2415203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
2416203134Sthompsa				break;
2417203134Sthompsa		rn->ridx[i] = ridx;
2418203134Sthompsa		/* determine rate of control response frames */
2419203134Sthompsa		for (j = i; j >= 0; j--) {
2420203134Sthompsa			if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2421203134Sthompsa			    rt2860_rates[rn->ridx[i]].phy ==
2422203134Sthompsa			    rt2860_rates[rn->ridx[j]].phy)
2423203134Sthompsa				break;
2424203134Sthompsa		}
2425203134Sthompsa		if (j >= 0) {
2426203134Sthompsa			rn->ctl_ridx[i] = rn->ridx[j];
2427203134Sthompsa		} else {
2428203134Sthompsa			/* no basic rate found, use mandatory one */
2429203134Sthompsa			rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2430203134Sthompsa		}
2431203134Sthompsa		DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2432203134Sthompsa		    rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2433203134Sthompsa	}
2434208019Sthompsa	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2435208019Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2436208019Sthompsa		if (rt2860_rates[ridx].rate == rate)
2437208019Sthompsa			break;
2438208019Sthompsa	rn->mgt_ridx = ridx;
2439208019Sthompsa	DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2440208019Sthompsa
2441208019Sthompsa	usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2442203134Sthompsa}
2443203134Sthompsa
2444203134Sthompsa/*
2445203134Sthompsa * Return the Rx chain with the highest RSSI for a given frame.
2446203134Sthompsa */
2447203134Sthompsastatic __inline uint8_t
2448203134Sthompsarun_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2449203134Sthompsa{
2450203134Sthompsa	uint8_t rxchain = 0;
2451203134Sthompsa
2452203134Sthompsa	if (sc->nrxchains > 1) {
2453203134Sthompsa		if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2454203134Sthompsa			rxchain = 1;
2455203134Sthompsa		if (sc->nrxchains > 2)
2456203134Sthompsa			if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2457203134Sthompsa				rxchain = 2;
2458203134Sthompsa	}
2459209917Sthompsa	return (rxchain);
2460203134Sthompsa}
2461203134Sthompsa
2462203134Sthompsastatic void
2463203134Sthompsarun_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2464203134Sthompsa{
2465203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2466203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2467203134Sthompsa	struct ieee80211_frame *wh;
2468203134Sthompsa	struct ieee80211_node *ni;
2469203134Sthompsa	struct rt2870_rxd *rxd;
2470203134Sthompsa	struct rt2860_rxwi *rxwi;
2471203134Sthompsa	uint32_t flags;
2472203134Sthompsa	uint16_t len, phy;
2473203134Sthompsa	uint8_t ant, rssi;
2474203134Sthompsa	int8_t nf;
2475203134Sthompsa
2476203134Sthompsa	rxwi = mtod(m, struct rt2860_rxwi *);
2477203134Sthompsa	len = le16toh(rxwi->len) & 0xfff;
2478203134Sthompsa	if (__predict_false(len > dmalen)) {
2479203134Sthompsa		m_freem(m);
2480203134Sthompsa		ifp->if_ierrors++;
2481203134Sthompsa		DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2482203134Sthompsa		return;
2483203134Sthompsa	}
2484203134Sthompsa	/* Rx descriptor is located at the end */
2485203134Sthompsa	rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2486203134Sthompsa	flags = le32toh(rxd->flags);
2487203134Sthompsa
2488203134Sthompsa	if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2489203134Sthompsa		m_freem(m);
2490203134Sthompsa		ifp->if_ierrors++;
2491203134Sthompsa		DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2492203134Sthompsa		return;
2493203134Sthompsa	}
2494203134Sthompsa
2495203134Sthompsa	m->m_data += sizeof(struct rt2860_rxwi);
2496203134Sthompsa	m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2497203134Sthompsa
2498203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2499203134Sthompsa
2500209917Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2501203134Sthompsa		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2502203134Sthompsa		m->m_flags |= M_WEP;
2503203134Sthompsa	}
2504203134Sthompsa
2505209917Sthompsa	if (flags & RT2860_RX_L2PAD) {
2506203134Sthompsa		DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2507203134Sthompsa		len += 2;
2508203134Sthompsa	}
2509203134Sthompsa
2510208019Sthompsa	ni = ieee80211_find_rxnode(ic,
2511208019Sthompsa	    mtod(m, struct ieee80211_frame_min *));
2512208019Sthompsa
2513203134Sthompsa	if (__predict_false(flags & RT2860_RX_MICERR)) {
2514203134Sthompsa		/* report MIC failures to net80211 for TKIP */
2515209917Sthompsa		if (ni != NULL)
2516208019Sthompsa			ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2517203134Sthompsa		m_freem(m);
2518203134Sthompsa		ifp->if_ierrors++;
2519203134Sthompsa		DPRINTF("MIC error. Someone is lying.\n");
2520203134Sthompsa		return;
2521203134Sthompsa	}
2522203134Sthompsa
2523203134Sthompsa	ant = run_maxrssi_chain(sc, rxwi);
2524203134Sthompsa	rssi = rxwi->rssi[ant];
2525203134Sthompsa	nf = run_rssi2dbm(sc, rssi, ant);
2526203134Sthompsa
2527203134Sthompsa	m->m_pkthdr.rcvif = ifp;
2528203134Sthompsa	m->m_pkthdr.len = m->m_len = len;
2529203134Sthompsa
2530203134Sthompsa	if (ni != NULL) {
2531203134Sthompsa		(void)ieee80211_input(ni, m, rssi, nf);
2532203134Sthompsa		ieee80211_free_node(ni);
2533203134Sthompsa	} else {
2534203134Sthompsa		(void)ieee80211_input_all(ic, m, rssi, nf);
2535203134Sthompsa	}
2536203134Sthompsa
2537209917Sthompsa	if (__predict_false(ieee80211_radiotap_active(ic))) {
2538203134Sthompsa		struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2539203134Sthompsa
2540203134Sthompsa		tap->wr_flags = 0;
2541236439Shselasky		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2542236439Shselasky		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2543203134Sthompsa		tap->wr_antsignal = rssi;
2544203134Sthompsa		tap->wr_antenna = ant;
2545203134Sthompsa		tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2546203134Sthompsa		tap->wr_rate = 2;	/* in case it can't be found below */
2547203134Sthompsa		phy = le16toh(rxwi->phy);
2548203134Sthompsa		switch (phy & RT2860_PHY_MODE) {
2549203134Sthompsa		case RT2860_PHY_CCK:
2550203134Sthompsa			switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2551203134Sthompsa			case 0:	tap->wr_rate =   2; break;
2552203134Sthompsa			case 1:	tap->wr_rate =   4; break;
2553203134Sthompsa			case 2:	tap->wr_rate =  11; break;
2554203134Sthompsa			case 3:	tap->wr_rate =  22; break;
2555203134Sthompsa			}
2556203134Sthompsa			if (phy & RT2860_PHY_SHPRE)
2557203134Sthompsa				tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2558203134Sthompsa			break;
2559203134Sthompsa		case RT2860_PHY_OFDM:
2560203134Sthompsa			switch (phy & RT2860_PHY_MCS) {
2561203134Sthompsa			case 0:	tap->wr_rate =  12; break;
2562203134Sthompsa			case 1:	tap->wr_rate =  18; break;
2563203134Sthompsa			case 2:	tap->wr_rate =  24; break;
2564203134Sthompsa			case 3:	tap->wr_rate =  36; break;
2565203134Sthompsa			case 4:	tap->wr_rate =  48; break;
2566203134Sthompsa			case 5:	tap->wr_rate =  72; break;
2567203134Sthompsa			case 6:	tap->wr_rate =  96; break;
2568203134Sthompsa			case 7:	tap->wr_rate = 108; break;
2569203134Sthompsa			}
2570203134Sthompsa			break;
2571203134Sthompsa		}
2572203134Sthompsa	}
2573203134Sthompsa}
2574203134Sthompsa
2575203134Sthompsastatic void
2576203134Sthompsarun_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2577203134Sthompsa{
2578203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2579203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2580203134Sthompsa	struct mbuf *m = NULL;
2581203134Sthompsa	struct mbuf *m0;
2582203134Sthompsa	uint32_t dmalen;
2583203134Sthompsa	int xferlen;
2584203134Sthompsa
2585203134Sthompsa	usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2586203134Sthompsa
2587203134Sthompsa	switch (USB_GET_STATE(xfer)) {
2588203134Sthompsa	case USB_ST_TRANSFERRED:
2589203134Sthompsa
2590203134Sthompsa		DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2591203134Sthompsa
2592233774Shselasky		if (xferlen < (int)(sizeof(uint32_t) +
2593233774Shselasky		    sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) {
2594203134Sthompsa			DPRINTF("xfer too short %d\n", xferlen);
2595203134Sthompsa			goto tr_setup;
2596203134Sthompsa		}
2597203134Sthompsa
2598203134Sthompsa		m = sc->rx_m;
2599203134Sthompsa		sc->rx_m = NULL;
2600203134Sthompsa
2601203134Sthompsa		/* FALLTHROUGH */
2602203134Sthompsa	case USB_ST_SETUP:
2603203134Sthompsatr_setup:
2604203134Sthompsa		if (sc->rx_m == NULL) {
2605243857Sglebius			sc->rx_m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
2606203134Sthompsa			    MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2607203134Sthompsa		}
2608203134Sthompsa		if (sc->rx_m == NULL) {
2609203134Sthompsa			DPRINTF("could not allocate mbuf - idle with stall\n");
2610203134Sthompsa			ifp->if_ierrors++;
2611203134Sthompsa			usbd_xfer_set_stall(xfer);
2612203134Sthompsa			usbd_xfer_set_frames(xfer, 0);
2613203134Sthompsa		} else {
2614203134Sthompsa			/*
2615203134Sthompsa			 * Directly loading a mbuf cluster into DMA to
2616203134Sthompsa			 * save some data copying. This works because
2617203134Sthompsa			 * there is only one cluster.
2618203134Sthompsa			 */
2619203134Sthompsa			usbd_xfer_set_frame_data(xfer, 0,
2620203134Sthompsa			    mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2621203134Sthompsa			usbd_xfer_set_frames(xfer, 1);
2622203134Sthompsa		}
2623203134Sthompsa		usbd_transfer_submit(xfer);
2624203134Sthompsa		break;
2625203134Sthompsa
2626203134Sthompsa	default:	/* Error */
2627203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2628203134Sthompsa			/* try to clear stall first */
2629203134Sthompsa			usbd_xfer_set_stall(xfer);
2630203134Sthompsa
2631203134Sthompsa			if (error == USB_ERR_TIMEOUT)
2632203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2633203134Sthompsa
2634203134Sthompsa			ifp->if_ierrors++;
2635203134Sthompsa
2636203134Sthompsa			goto tr_setup;
2637203134Sthompsa		}
2638209917Sthompsa		if (sc->rx_m != NULL) {
2639203134Sthompsa			m_freem(sc->rx_m);
2640203134Sthompsa			sc->rx_m = NULL;
2641203134Sthompsa		}
2642203134Sthompsa		break;
2643203134Sthompsa	}
2644203134Sthompsa
2645203134Sthompsa	if (m == NULL)
2646203134Sthompsa		return;
2647203134Sthompsa
2648203134Sthompsa	/* inputting all the frames must be last */
2649203134Sthompsa
2650203134Sthompsa	RUN_UNLOCK(sc);
2651203134Sthompsa
2652203134Sthompsa	m->m_pkthdr.len = m->m_len = xferlen;
2653203134Sthompsa
2654203134Sthompsa	/* HW can aggregate multiple 802.11 frames in a single USB xfer */
2655203134Sthompsa	for(;;) {
2656203134Sthompsa		dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2657203134Sthompsa
2658233774Shselasky		if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2659233774Shselasky		    ((dmalen & 3) != 0)) {
2660203134Sthompsa			DPRINTF("bad DMA length %u\n", dmalen);
2661203134Sthompsa			break;
2662203134Sthompsa		}
2663233774Shselasky		if ((dmalen + 8) > (uint32_t)xferlen) {
2664203134Sthompsa			DPRINTF("bad DMA length %u > %d\n",
2665203134Sthompsa			dmalen + 8, xferlen);
2666203134Sthompsa			break;
2667203134Sthompsa		}
2668203134Sthompsa
2669203134Sthompsa		/* If it is the last one or a single frame, we won't copy. */
2670209917Sthompsa		if ((xferlen -= dmalen + 8) <= 8) {
2671203134Sthompsa			/* trim 32-bit DMA-len header */
2672203134Sthompsa			m->m_data += 4;
2673203134Sthompsa			m->m_pkthdr.len = m->m_len -= 4;
2674203134Sthompsa			run_rx_frame(sc, m, dmalen);
2675203134Sthompsa			break;
2676203134Sthompsa		}
2677203134Sthompsa
2678203134Sthompsa		/* copy aggregated frames to another mbuf */
2679243857Sglebius		m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2680203134Sthompsa		if (__predict_false(m0 == NULL)) {
2681203134Sthompsa			DPRINTF("could not allocate mbuf\n");
2682203134Sthompsa			ifp->if_ierrors++;
2683203134Sthompsa			break;
2684203134Sthompsa		}
2685203134Sthompsa		m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2686203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2687203134Sthompsa		m0->m_pkthdr.len = m0->m_len =
2688203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd);
2689203134Sthompsa		run_rx_frame(sc, m0, dmalen);
2690203134Sthompsa
2691203134Sthompsa		/* update data ptr */
2692203134Sthompsa		m->m_data += dmalen + 8;
2693203134Sthompsa		m->m_pkthdr.len = m->m_len -= dmalen + 8;
2694203134Sthompsa	}
2695203134Sthompsa
2696203134Sthompsa	RUN_LOCK(sc);
2697203134Sthompsa}
2698203134Sthompsa
2699203134Sthompsastatic void
2700203134Sthompsarun_tx_free(struct run_endpoint_queue *pq,
2701203134Sthompsa    struct run_tx_data *data, int txerr)
2702203134Sthompsa{
2703203134Sthompsa	if (data->m != NULL) {
2704203134Sthompsa		if (data->m->m_flags & M_TXCB)
2705203134Sthompsa			ieee80211_process_callback(data->ni, data->m,
2706203134Sthompsa			    txerr ? ETIMEDOUT : 0);
2707203134Sthompsa		m_freem(data->m);
2708203134Sthompsa		data->m = NULL;
2709203134Sthompsa
2710209917Sthompsa		if (data->ni == NULL) {
2711203134Sthompsa			DPRINTF("no node\n");
2712203134Sthompsa		} else {
2713203134Sthompsa			ieee80211_free_node(data->ni);
2714203134Sthompsa			data->ni = NULL;
2715203134Sthompsa		}
2716203134Sthompsa	}
2717203134Sthompsa
2718203134Sthompsa	STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2719203134Sthompsa	pq->tx_nfree++;
2720203134Sthompsa}
2721203134Sthompsa
2722203134Sthompsastatic void
2723203134Sthompsarun_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index)
2724203134Sthompsa{
2725203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2726203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2727208019Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2728203134Sthompsa	struct run_tx_data *data;
2729203134Sthompsa	struct ieee80211vap *vap = NULL;
2730203134Sthompsa	struct usb_page_cache *pc;
2731203134Sthompsa	struct run_endpoint_queue *pq = &sc->sc_epq[index];
2732203134Sthompsa	struct mbuf *m;
2733203134Sthompsa	usb_frlength_t size;
2734203134Sthompsa	int actlen;
2735203134Sthompsa	int sumlen;
2736203134Sthompsa
2737203134Sthompsa	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2738203134Sthompsa
2739209917Sthompsa	switch (USB_GET_STATE(xfer)) {
2740203134Sthompsa	case USB_ST_TRANSFERRED:
2741203134Sthompsa		DPRINTFN(11, "transfer complete: %d "
2742203134Sthompsa		    "bytes @ index %d\n", actlen, index);
2743203134Sthompsa
2744203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2745203134Sthompsa
2746203134Sthompsa		run_tx_free(pq, data, 0);
2747203134Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2748203134Sthompsa
2749203134Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2750203134Sthompsa
2751203134Sthompsa		ifp->if_opackets++;
2752203134Sthompsa
2753203134Sthompsa		/* FALLTHROUGH */
2754203134Sthompsa	case USB_ST_SETUP:
2755203134Sthompsatr_setup:
2756203134Sthompsa		data = STAILQ_FIRST(&pq->tx_qh);
2757209917Sthompsa		if (data == NULL)
2758203134Sthompsa			break;
2759203134Sthompsa
2760203134Sthompsa		STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2761203134Sthompsa
2762203134Sthompsa		m = data->m;
2763228508Shselasky		if ((m->m_pkthdr.len +
2764228508Shselasky		    sizeof(data->desc) + 3 + 8) > RUN_MAX_TXSZ) {
2765203134Sthompsa			DPRINTF("data overflow, %u bytes\n",
2766203134Sthompsa			    m->m_pkthdr.len);
2767203134Sthompsa
2768203134Sthompsa			ifp->if_oerrors++;
2769203134Sthompsa
2770203134Sthompsa			run_tx_free(pq, data, 1);
2771203134Sthompsa
2772203134Sthompsa			goto tr_setup;
2773203134Sthompsa		}
2774203134Sthompsa
2775203134Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2776203134Sthompsa		size = sizeof(data->desc);
2777203134Sthompsa		usbd_copy_in(pc, 0, &data->desc, size);
2778203134Sthompsa		usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2779228508Shselasky		size += m->m_pkthdr.len;
2780228508Shselasky		/*
2781228508Shselasky		 * Align end on a 4-byte boundary, pad 8 bytes (CRC +
2782228508Shselasky		 * 4-byte padding), and be sure to zero those trailing
2783228508Shselasky		 * bytes:
2784228508Shselasky		 */
2785228508Shselasky		usbd_frame_zero(pc, size, ((-size) & 3) + 8);
2786228508Shselasky		size += ((-size) & 3) + 8;
2787203134Sthompsa
2788203134Sthompsa		vap = data->ni->ni_vap;
2789203134Sthompsa		if (ieee80211_radiotap_active_vap(vap)) {
2790203134Sthompsa			struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2791208019Sthompsa			struct rt2860_txwi *txwi =
2792208019Sthompsa			    (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2793203134Sthompsa
2794203134Sthompsa			tap->wt_flags = 0;
2795203134Sthompsa			tap->wt_rate = rt2860_rates[data->ridx].rate;
2796236439Shselasky			tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2797236439Shselasky			tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2798203134Sthompsa			tap->wt_hwqueue = index;
2799208019Sthompsa			if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2800203134Sthompsa				tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2801203134Sthompsa
2802203134Sthompsa			ieee80211_radiotap_tx(vap, m);
2803203134Sthompsa		}
2804203134Sthompsa
2805228508Shselasky		DPRINTFN(11, "sending frame len=%u/%u  @ index %d\n",
2806228508Shselasky		    m->m_pkthdr.len, size, index);
2807203134Sthompsa
2808228508Shselasky		usbd_xfer_set_frame_len(xfer, 0, size);
2809203134Sthompsa		usbd_xfer_set_priv(xfer, data);
2810203134Sthompsa
2811203134Sthompsa		usbd_transfer_submit(xfer);
2812203134Sthompsa
2813203134Sthompsa		RUN_UNLOCK(sc);
2814203134Sthompsa		run_start(ifp);
2815203134Sthompsa		RUN_LOCK(sc);
2816203134Sthompsa
2817203134Sthompsa		break;
2818203134Sthompsa
2819203134Sthompsa	default:
2820203134Sthompsa		DPRINTF("USB transfer error, %s\n",
2821203134Sthompsa		    usbd_errstr(error));
2822203134Sthompsa
2823203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2824203134Sthompsa
2825203134Sthompsa		ifp->if_oerrors++;
2826203134Sthompsa
2827203134Sthompsa		if (data != NULL) {
2828208019Sthompsa			if(data->ni != NULL)
2829208019Sthompsa				vap = data->ni->ni_vap;
2830203134Sthompsa			run_tx_free(pq, data, error);
2831203134Sthompsa			usbd_xfer_set_priv(xfer, NULL);
2832203134Sthompsa		}
2833209917Sthompsa		if (vap == NULL)
2834208019Sthompsa			vap = TAILQ_FIRST(&ic->ic_vaps);
2835203134Sthompsa
2836203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2837203134Sthompsa			if (error == USB_ERR_TIMEOUT) {
2838203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2839208019Sthompsa				uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2840208019Sthompsa				DPRINTF("cmdq_store=%d\n", i);
2841208019Sthompsa				sc->cmdq[i].func = run_usb_timeout_cb;
2842208019Sthompsa				sc->cmdq[i].arg0 = vap;
2843208019Sthompsa				ieee80211_runtask(ic, &sc->cmdq_task);
2844203134Sthompsa			}
2845203134Sthompsa
2846203134Sthompsa			/*
2847203134Sthompsa			 * Try to clear stall first, also if other
2848203134Sthompsa			 * errors occur, hence clearing stall
2849203134Sthompsa			 * introduces a 50 ms delay:
2850203134Sthompsa			 */
2851203134Sthompsa			usbd_xfer_set_stall(xfer);
2852203134Sthompsa			goto tr_setup;
2853203134Sthompsa		}
2854203134Sthompsa		break;
2855203134Sthompsa	}
2856203134Sthompsa}
2857203134Sthompsa
2858203134Sthompsastatic void
2859203134Sthompsarun_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2860203134Sthompsa{
2861203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 0);
2862203134Sthompsa}
2863203134Sthompsa
2864203134Sthompsastatic void
2865203134Sthompsarun_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2866203134Sthompsa{
2867203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 1);
2868203134Sthompsa}
2869203134Sthompsa
2870203134Sthompsastatic void
2871203134Sthompsarun_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2872203134Sthompsa{
2873203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 2);
2874203134Sthompsa}
2875203134Sthompsa
2876203134Sthompsastatic void
2877203134Sthompsarun_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2878203134Sthompsa{
2879203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 3);
2880203134Sthompsa}
2881203134Sthompsa
2882203134Sthompsastatic void
2883203134Sthompsarun_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2884203134Sthompsa{
2885203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 4);
2886203134Sthompsa}
2887203134Sthompsa
2888203134Sthompsastatic void
2889203134Sthompsarun_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2890203134Sthompsa{
2891203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 5);
2892203134Sthompsa}
2893203134Sthompsa
2894203134Sthompsastatic void
2895208019Sthompsarun_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2896203134Sthompsa{
2897203134Sthompsa	struct mbuf *m = data->m;
2898203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2899208019Sthompsa	struct ieee80211vap *vap = data->ni->ni_vap;
2900203134Sthompsa	struct ieee80211_frame *wh;
2901203134Sthompsa	struct rt2870_txd *txd;
2902203134Sthompsa	struct rt2860_txwi *txwi;
2903208019Sthompsa	uint16_t xferlen;
2904208019Sthompsa	uint16_t mcs;
2905203134Sthompsa	uint8_t ridx = data->ridx;
2906208019Sthompsa	uint8_t pad;
2907203134Sthompsa
2908203134Sthompsa	/* get MCS code from rate index */
2909208019Sthompsa	mcs = rt2860_rates[ridx].mcs;
2910203134Sthompsa
2911203134Sthompsa	xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2912203134Sthompsa
2913203134Sthompsa	/* roundup to 32-bit alignment */
2914203134Sthompsa	xferlen = (xferlen + 3) & ~3;
2915203134Sthompsa
2916203134Sthompsa	txd = (struct rt2870_txd *)&data->desc;
2917203134Sthompsa	txd->len = htole16(xferlen);
2918203134Sthompsa
2919208019Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2920208019Sthompsa
2921208019Sthompsa	/*
2922208019Sthompsa	 * Ether both are true or both are false, the header
2923208019Sthompsa	 * are nicely aligned to 32-bit. So, no L2 padding.
2924208019Sthompsa	 */
2925208019Sthompsa	if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2926208019Sthompsa		pad = 0;
2927208019Sthompsa	else
2928208019Sthompsa		pad = 2;
2929208019Sthompsa
2930203134Sthompsa	/* setup TX Wireless Information */
2931203134Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
2932203134Sthompsa	txwi->len = htole16(m->m_pkthdr.len - pad);
2933203134Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2934203134Sthompsa		txwi->phy = htole16(RT2860_PHY_CCK);
2935203134Sthompsa		if (ridx != RT2860_RIDX_CCK1 &&
2936203134Sthompsa		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2937203134Sthompsa			mcs |= RT2860_PHY_SHPRE;
2938203134Sthompsa	} else
2939203134Sthompsa		txwi->phy = htole16(RT2860_PHY_OFDM);
2940203134Sthompsa	txwi->phy |= htole16(mcs);
2941203134Sthompsa
2942203134Sthompsa	/* check if RTS/CTS or CTS-to-self protection is required */
2943203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2944203134Sthompsa	    (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2945203134Sthompsa	     ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2946203134Sthompsa	      rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
2947208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_HT;
2948203134Sthompsa	else
2949208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_BACKOFF;
2950209144Sthompsa
2951209917Sthompsa	if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
2952209144Sthompsa		txwi->xflags |= RT2860_TX_NSEQ;
2953203134Sthompsa}
2954203134Sthompsa
2955203134Sthompsa/* This function must be called locked */
2956203134Sthompsastatic int
2957203134Sthompsarun_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2958203134Sthompsa{
2959203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2960208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2961203134Sthompsa	struct ieee80211_frame *wh;
2962208019Sthompsa	struct ieee80211_channel *chan;
2963203134Sthompsa	const struct ieee80211_txparam *tp;
2964208019Sthompsa	struct run_node *rn = (void *)ni;
2965203134Sthompsa	struct run_tx_data *data;
2966208019Sthompsa	struct rt2870_txd *txd;
2967208019Sthompsa	struct rt2860_txwi *txwi;
2968203134Sthompsa	uint16_t qos;
2969203134Sthompsa	uint16_t dur;
2970208019Sthompsa	uint16_t qid;
2971203134Sthompsa	uint8_t type;
2972203134Sthompsa	uint8_t tid;
2973208019Sthompsa	uint8_t ridx;
2974208019Sthompsa	uint8_t ctl_ridx;
2975203134Sthompsa	uint8_t qflags;
2976203134Sthompsa	uint8_t xflags = 0;
2977203134Sthompsa	int hasqos;
2978203134Sthompsa
2979203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2980203134Sthompsa
2981203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2982203134Sthompsa
2983203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2984203134Sthompsa
2985203134Sthompsa	/*
2986203134Sthompsa	 * There are 7 bulk endpoints: 1 for RX
2987203134Sthompsa	 * and 6 for TX (4 EDCAs + HCCA + Prio).
2988203134Sthompsa	 * Update 03-14-2009:  some devices like the Planex GW-US300MiniS
2989203134Sthompsa	 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
2990203134Sthompsa	 */
2991203134Sthompsa	if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
2992203134Sthompsa		uint8_t *frm;
2993203134Sthompsa
2994203134Sthompsa		if(IEEE80211_HAS_ADDR4(wh))
2995203134Sthompsa			frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
2996203134Sthompsa		else
2997203134Sthompsa			frm =((struct ieee80211_qosframe *)wh)->i_qos;
2998203134Sthompsa
2999203134Sthompsa		qos = le16toh(*(const uint16_t *)frm);
3000203134Sthompsa		tid = qos & IEEE80211_QOS_TID;
3001203134Sthompsa		qid = TID_TO_WME_AC(tid);
3002203134Sthompsa	} else {
3003203134Sthompsa		qos = 0;
3004203134Sthompsa		tid = 0;
3005203134Sthompsa		qid = WME_AC_BE;
3006203134Sthompsa	}
3007203134Sthompsa	qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
3008203134Sthompsa
3009203134Sthompsa	DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
3010203134Sthompsa	    qos, qid, tid, qflags);
3011203134Sthompsa
3012208019Sthompsa	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
3013208019Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
3014203134Sthompsa
3015203134Sthompsa	/* pickup a rate index */
3016203134Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
3017203134Sthompsa	    type != IEEE80211_FC0_TYPE_DATA) {
3018203134Sthompsa		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3019203134Sthompsa		    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3020203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3021203134Sthompsa	} else {
3022208019Sthompsa		if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3023208019Sthompsa			ridx = rn->fix_ridx;
3024208019Sthompsa		else
3025208019Sthompsa			ridx = rn->amrr_ridx;
3026203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3027203134Sthompsa	}
3028203134Sthompsa
3029203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3030203134Sthompsa	    (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3031203134Sthompsa	     IEEE80211_QOS_ACKPOLICY_NOACK)) {
3032209144Sthompsa		xflags |= RT2860_TX_ACK;
3033203134Sthompsa		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3034208019Sthompsa			dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3035203134Sthompsa		else
3036208019Sthompsa			dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3037205042Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3038203134Sthompsa	}
3039203134Sthompsa
3040203134Sthompsa	/* reserve slots for mgmt packets, just in case */
3041203134Sthompsa	if (sc->sc_epq[qid].tx_nfree < 3) {
3042203134Sthompsa		DPRINTFN(10, "tx ring %d is full\n", qid);
3043203134Sthompsa		return (-1);
3044203134Sthompsa	}
3045203134Sthompsa
3046203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3047203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3048203134Sthompsa	sc->sc_epq[qid].tx_nfree--;
3049203134Sthompsa
3050208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3051208019Sthompsa	txd->flags = qflags;
3052208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3053208019Sthompsa	txwi->xflags = xflags;
3054245047Shselasky	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3055245047Shselasky		txwi->wcid = 0;
3056245047Shselasky	} else {
3057245047Shselasky		txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
3058245047Shselasky		    1 : RUN_AID2WCID(ni->ni_associd);
3059245047Shselasky	}
3060208019Sthompsa	/* clear leftover garbage bits */
3061208019Sthompsa	txwi->flags = 0;
3062208019Sthompsa	txwi->txop = 0;
3063208019Sthompsa
3064203134Sthompsa	data->m = m;
3065203134Sthompsa	data->ni = ni;
3066203134Sthompsa	data->ridx = ridx;
3067203134Sthompsa
3068208019Sthompsa	run_set_tx_desc(sc, data);
3069203134Sthompsa
3070208019Sthompsa	/*
3071208019Sthompsa	 * The chip keeps track of 2 kind of Tx stats,
3072208019Sthompsa	 *  * TX_STAT_FIFO, for per WCID stats, and
3073208019Sthompsa	 *  * TX_STA_CNT0 for all-TX-in-one stats.
3074208019Sthompsa	 *
3075208019Sthompsa	 * To use FIFO stats, we need to store MCS into the driver-private
3076208019Sthompsa 	 * PacketID field. So that, we can tell whose stats when we read them.
3077208019Sthompsa 	 * We add 1 to the MCS because setting the PacketID field to 0 means
3078208019Sthompsa 	 * that we don't want feedback in TX_STAT_FIFO.
3079208019Sthompsa 	 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3080208019Sthompsa 	 *
3081208019Sthompsa 	 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3082208019Sthompsa 	 */
3083209917Sthompsa	if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3084209917Sthompsa	    vap->iv_opmode == IEEE80211_M_MBSS) {
3085208019Sthompsa		uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3086208019Sthompsa		txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3087208019Sthompsa
3088208019Sthompsa		/*
3089208019Sthompsa		 * Unlike PCI based devices, we don't get any interrupt from
3090208019Sthompsa		 * USB devices, so we simulate FIFO-is-full interrupt here.
3091208019Sthompsa		 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3092208019Sthompsa		 * quickly get fulled. To prevent overflow, increment a counter on
3093208019Sthompsa		 * every FIFO stat request, so we know how many slots are left.
3094208019Sthompsa		 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3095208019Sthompsa		 * are used only in those modes.
3096208019Sthompsa		 * We just drain stats. AMRR gets updated every 1 sec by
3097208019Sthompsa		 * run_ratectl_cb() via callout.
3098208019Sthompsa		 * Call it early. Otherwise overflow.
3099208019Sthompsa		 */
3100209917Sthompsa		if (sc->fifo_cnt++ == 10) {
3101208019Sthompsa			/*
3102208019Sthompsa			 * With multiple vaps or if_bridge, if_start() is called
3103208019Sthompsa			 * with a non-sleepable lock, tcpinp. So, need to defer.
3104208019Sthompsa			 */
3105208019Sthompsa			uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3106208019Sthompsa			DPRINTFN(6, "cmdq_store=%d\n", i);
3107208019Sthompsa			sc->cmdq[i].func = run_drain_fifo;
3108208019Sthompsa			sc->cmdq[i].arg0 = sc;
3109208019Sthompsa			ieee80211_runtask(ic, &sc->cmdq_task);
3110208019Sthompsa		}
3111208019Sthompsa	}
3112208019Sthompsa
3113203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3114203134Sthompsa
3115203134Sthompsa	usbd_transfer_start(sc->sc_xfer[qid]);
3116203134Sthompsa
3117203134Sthompsa	DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3118203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3119203134Sthompsa	    rt2860_rates[ridx].rate, qid);
3120203134Sthompsa
3121203134Sthompsa	return (0);
3122203134Sthompsa}
3123203134Sthompsa
3124203134Sthompsastatic int
3125203134Sthompsarun_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3126203134Sthompsa{
3127203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3128203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3129208019Sthompsa	struct run_node *rn = (void *)ni;
3130203134Sthompsa	struct run_tx_data *data;
3131203134Sthompsa	struct ieee80211_frame *wh;
3132208019Sthompsa	struct rt2870_txd *txd;
3133208019Sthompsa	struct rt2860_txwi *txwi;
3134203134Sthompsa	uint16_t dur;
3135208019Sthompsa	uint8_t ridx = rn->mgt_ridx;
3136203134Sthompsa	uint8_t type;
3137203134Sthompsa	uint8_t xflags = 0;
3138208019Sthompsa	uint8_t wflags = 0;
3139203134Sthompsa
3140203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3141203134Sthompsa
3142203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3143203134Sthompsa
3144203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3145203134Sthompsa
3146208019Sthompsa	/* tell hardware to add timestamp for probe responses */
3147208019Sthompsa	if ((wh->i_fc[0] &
3148208019Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3149208019Sthompsa	    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3150208019Sthompsa		wflags |= RT2860_TX_TS;
3151208019Sthompsa	else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3152203134Sthompsa		xflags |= RT2860_TX_ACK;
3153203134Sthompsa
3154208019Sthompsa		dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3155203134Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3156203134Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3157203134Sthompsa	}
3158203134Sthompsa
3159203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3160203134Sthompsa		/* let caller free mbuf */
3161203134Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3162203134Sthompsa		return (EIO);
3163203134Sthompsa	}
3164203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3165203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3166203134Sthompsa	sc->sc_epq[0].tx_nfree--;
3167203134Sthompsa
3168208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3169208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3170208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3171208019Sthompsa	txwi->wcid = 0xff;
3172208019Sthompsa	txwi->flags = wflags;
3173208019Sthompsa	txwi->xflags = xflags;
3174208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3175208019Sthompsa
3176203134Sthompsa	data->m = m;
3177203134Sthompsa	data->ni = ni;
3178203134Sthompsa	data->ridx = ridx;
3179203134Sthompsa
3180208019Sthompsa	run_set_tx_desc(sc, data);
3181203134Sthompsa
3182203134Sthompsa	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3183203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3184208019Sthompsa	    rt2860_rates[ridx].rate);
3185203134Sthompsa
3186203134Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3187203134Sthompsa
3188203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3189203134Sthompsa
3190203134Sthompsa	return (0);
3191203134Sthompsa}
3192203134Sthompsa
3193203134Sthompsastatic int
3194203134Sthompsarun_sendprot(struct run_softc *sc,
3195203134Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3196203134Sthompsa{
3197203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3198203134Sthompsa	struct ieee80211_frame *wh;
3199203134Sthompsa	struct run_tx_data *data;
3200208019Sthompsa	struct rt2870_txd *txd;
3201208019Sthompsa	struct rt2860_txwi *txwi;
3202203134Sthompsa	struct mbuf *mprot;
3203203134Sthompsa	int ridx;
3204203134Sthompsa	int protrate;
3205203134Sthompsa	int ackrate;
3206203134Sthompsa	int pktlen;
3207203134Sthompsa	int isshort;
3208203134Sthompsa	uint16_t dur;
3209203134Sthompsa	uint8_t type;
3210208019Sthompsa	uint8_t wflags = 0;
3211208019Sthompsa	uint8_t xflags = 0;
3212203134Sthompsa
3213203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3214203134Sthompsa
3215203134Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3216203134Sthompsa	    ("protection %d", prot));
3217203134Sthompsa
3218203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3219203134Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3220203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3221203134Sthompsa
3222203134Sthompsa	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3223203134Sthompsa	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3224203134Sthompsa
3225203134Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3226209189Sjkim	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3227203134Sthompsa	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3228203134Sthompsa	wflags = RT2860_TX_FRAG;
3229203134Sthompsa
3230203134Sthompsa	/* check that there are free slots before allocating the mbuf */
3231203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3232203134Sthompsa		/* let caller free mbuf */
3233203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3234203134Sthompsa		return (ENOBUFS);
3235203134Sthompsa	}
3236203134Sthompsa
3237203134Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
3238203134Sthompsa		/* NB: CTS is the same size as an ACK */
3239203134Sthompsa		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3240208019Sthompsa		xflags |= RT2860_TX_ACK;
3241203134Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3242203134Sthompsa	} else {
3243203134Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3244203134Sthompsa	}
3245203134Sthompsa	if (mprot == NULL) {
3246203134Sthompsa		sc->sc_ifp->if_oerrors++;
3247203134Sthompsa		DPRINTF("could not allocate mbuf\n");
3248203134Sthompsa		return (ENOBUFS);
3249203134Sthompsa	}
3250203134Sthompsa
3251203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3252203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3253203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3254203134Sthompsa
3255208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3256208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3257208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3258208019Sthompsa	txwi->wcid = 0xff;
3259208019Sthompsa	txwi->flags = wflags;
3260208019Sthompsa	txwi->xflags = xflags;
3261208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3262208019Sthompsa
3263203134Sthompsa	data->m = mprot;
3264203134Sthompsa	data->ni = ieee80211_ref_node(ni);
3265203134Sthompsa
3266203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3267203134Sthompsa		if (rt2860_rates[ridx].rate == protrate)
3268203134Sthompsa			break;
3269203134Sthompsa	data->ridx = ridx;
3270203134Sthompsa
3271208019Sthompsa	run_set_tx_desc(sc, data);
3272203134Sthompsa
3273203134Sthompsa        DPRINTFN(1, "sending prot len=%u rate=%u\n",
3274203134Sthompsa            m->m_pkthdr.len, rate);
3275203134Sthompsa
3276203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3277203134Sthompsa
3278203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3279203134Sthompsa
3280203134Sthompsa	return (0);
3281203134Sthompsa}
3282203134Sthompsa
3283203134Sthompsastatic int
3284203134Sthompsarun_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3285203134Sthompsa    const struct ieee80211_bpf_params *params)
3286203134Sthompsa{
3287203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3288203134Sthompsa	struct ieee80211_frame *wh;
3289203134Sthompsa	struct run_tx_data *data;
3290208019Sthompsa	struct rt2870_txd *txd;
3291208019Sthompsa	struct rt2860_txwi *txwi;
3292203134Sthompsa	uint8_t type;
3293208019Sthompsa	uint8_t ridx;
3294208019Sthompsa	uint8_t rate;
3295208019Sthompsa	uint8_t opflags = 0;
3296208019Sthompsa	uint8_t xflags = 0;
3297203134Sthompsa	int error;
3298203134Sthompsa
3299203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3300203134Sthompsa
3301203134Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
3302203134Sthompsa
3303203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3304203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3305203134Sthompsa
3306203134Sthompsa	rate = params->ibp_rate0;
3307203134Sthompsa	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3308203134Sthompsa		/* let caller free mbuf */
3309203134Sthompsa		return (EINVAL);
3310203134Sthompsa	}
3311203134Sthompsa
3312203134Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3313208019Sthompsa		xflags |= RT2860_TX_ACK;
3314203134Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3315203134Sthompsa		error = run_sendprot(sc, m, ni,
3316203134Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
3317203134Sthompsa			IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3318203134Sthompsa		    rate);
3319203134Sthompsa		if (error) {
3320203134Sthompsa			/* let caller free mbuf */
3321209917Sthompsa			return error;
3322203134Sthompsa		}
3323203134Sthompsa		opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3324203134Sthompsa	}
3325203134Sthompsa
3326203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3327203134Sthompsa		/* let caller free mbuf */
3328203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3329203134Sthompsa		DPRINTF("sending raw frame, but tx ring is full\n");
3330203134Sthompsa		return (EIO);
3331203134Sthompsa	}
3332203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3333203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3334203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3335203134Sthompsa
3336208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3337208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3338208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3339208019Sthompsa	txwi->wcid = 0xff;
3340208019Sthompsa	txwi->xflags = xflags;
3341208019Sthompsa	txwi->txop = opflags;
3342208019Sthompsa	txwi->flags = 0;	/* clear leftover garbage bits */
3343208019Sthompsa
3344203134Sthompsa        data->m = m;
3345203134Sthompsa        data->ni = ni;
3346203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3347203134Sthompsa		if (rt2860_rates[ridx].rate == rate)
3348203134Sthompsa			break;
3349203134Sthompsa	data->ridx = ridx;
3350203134Sthompsa
3351208019Sthompsa        run_set_tx_desc(sc, data);
3352203134Sthompsa
3353203134Sthompsa        DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3354203134Sthompsa            m->m_pkthdr.len, rate);
3355203134Sthompsa
3356203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3357203134Sthompsa
3358203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3359203134Sthompsa
3360209917Sthompsa        return (0);
3361203134Sthompsa}
3362203134Sthompsa
3363203134Sthompsastatic int
3364203134Sthompsarun_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3365203134Sthompsa    const struct ieee80211_bpf_params *params)
3366203134Sthompsa{
3367203134Sthompsa	struct ifnet *ifp = ni->ni_ic->ic_ifp;
3368203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3369208019Sthompsa	int error = 0;
3370208019Sthompsa
3371203134Sthompsa	RUN_LOCK(sc);
3372203134Sthompsa
3373203134Sthompsa	/* prevent management frames from being sent if we're not ready */
3374203134Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3375203134Sthompsa		error =  ENETDOWN;
3376208019Sthompsa		goto done;
3377203134Sthompsa	}
3378203134Sthompsa
3379203134Sthompsa	if (params == NULL) {
3380203134Sthompsa		/* tx mgt packet */
3381209917Sthompsa		if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3382203134Sthompsa			ifp->if_oerrors++;
3383203134Sthompsa			DPRINTF("mgt tx failed\n");
3384208019Sthompsa			goto done;
3385203134Sthompsa		}
3386203134Sthompsa	} else {
3387203134Sthompsa		/* tx raw packet with param */
3388209917Sthompsa		if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3389203134Sthompsa			ifp->if_oerrors++;
3390203134Sthompsa			DPRINTF("tx with param failed\n");
3391208019Sthompsa			goto done;
3392203134Sthompsa		}
3393203134Sthompsa	}
3394203134Sthompsa
3395203134Sthompsa	ifp->if_opackets++;
3396203134Sthompsa
3397208019Sthompsadone:
3398203134Sthompsa	RUN_UNLOCK(sc);
3399203134Sthompsa
3400209917Sthompsa	if (error != 0) {
3401208019Sthompsa		if(m != NULL)
3402208019Sthompsa			m_freem(m);
3403208019Sthompsa		ieee80211_free_node(ni);
3404208019Sthompsa	}
3405203134Sthompsa
3406203134Sthompsa	return (error);
3407203134Sthompsa}
3408203134Sthompsa
3409203134Sthompsastatic void
3410203134Sthompsarun_start(struct ifnet *ifp)
3411203134Sthompsa{
3412203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3413203134Sthompsa	struct ieee80211_node *ni;
3414203134Sthompsa	struct mbuf *m;
3415203134Sthompsa
3416203134Sthompsa	RUN_LOCK(sc);
3417203134Sthompsa
3418203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3419203134Sthompsa		RUN_UNLOCK(sc);
3420203134Sthompsa		return;
3421203134Sthompsa	}
3422203134Sthompsa
3423203134Sthompsa	for (;;) {
3424203134Sthompsa		/* send data frames */
3425203134Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3426203134Sthompsa		if (m == NULL)
3427203134Sthompsa			break;
3428203134Sthompsa
3429203134Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3430203134Sthompsa		if (run_tx(sc, m, ni) != 0) {
3431203134Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
3432203134Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3433203134Sthompsa			break;
3434203134Sthompsa		}
3435203134Sthompsa	}
3436203134Sthompsa
3437203134Sthompsa	RUN_UNLOCK(sc);
3438203134Sthompsa}
3439203134Sthompsa
3440203134Sthompsastatic int
3441203134Sthompsarun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3442203134Sthompsa{
3443203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3444203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3445203134Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
3446208019Sthompsa	int startall = 0;
3447246614Shselasky	int error;
3448203134Sthompsa
3449246614Shselasky	RUN_LOCK(sc);
3450246614Shselasky	error = sc->sc_detached ? ENXIO : 0;
3451246614Shselasky	RUN_UNLOCK(sc);
3452246614Shselasky	if (error)
3453246614Shselasky		return (error);
3454246614Shselasky
3455203134Sthompsa	switch (cmd) {
3456203134Sthompsa	case SIOCSIFFLAGS:
3457203134Sthompsa		RUN_LOCK(sc);
3458203134Sthompsa		if (ifp->if_flags & IFF_UP) {
3459203134Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3460208019Sthompsa				startall = 1;
3461203134Sthompsa				run_init_locked(sc);
3462203134Sthompsa			} else
3463203134Sthompsa				run_update_promisc_locked(ifp);
3464203134Sthompsa		} else {
3465209917Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3466209917Sthompsa			    (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3467208019Sthompsa					run_stop(sc);
3468208019Sthompsa			}
3469203134Sthompsa		}
3470203134Sthompsa		RUN_UNLOCK(sc);
3471209917Sthompsa		if (startall)
3472208019Sthompsa			ieee80211_start_all(ic);
3473203134Sthompsa		break;
3474203134Sthompsa	case SIOCGIFMEDIA:
3475203134Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3476203134Sthompsa		break;
3477203134Sthompsa	case SIOCGIFADDR:
3478203134Sthompsa		error = ether_ioctl(ifp, cmd, data);
3479203134Sthompsa		break;
3480203134Sthompsa	default:
3481203134Sthompsa		error = EINVAL;
3482203134Sthompsa		break;
3483203134Sthompsa	}
3484203134Sthompsa
3485203134Sthompsa	return (error);
3486203134Sthompsa}
3487203134Sthompsa
3488203134Sthompsastatic void
3489205042Sthompsarun_set_agc(struct run_softc *sc, uint8_t agc)
3490205042Sthompsa{
3491205042Sthompsa	uint8_t bbp;
3492205042Sthompsa
3493205042Sthompsa	if (sc->mac_ver == 0x3572) {
3494205042Sthompsa		run_bbp_read(sc, 27, &bbp);
3495205042Sthompsa		bbp &= ~(0x3 << 5);
3496205042Sthompsa		run_bbp_write(sc, 27, bbp | 0 << 5);	/* select Rx0 */
3497205042Sthompsa		run_bbp_write(sc, 66, agc);
3498205042Sthompsa		run_bbp_write(sc, 27, bbp | 1 << 5);	/* select Rx1 */
3499205042Sthompsa		run_bbp_write(sc, 66, agc);
3500205042Sthompsa	} else
3501205042Sthompsa		run_bbp_write(sc, 66, agc);
3502205042Sthompsa}
3503205042Sthompsa
3504205042Sthompsastatic void
3505203134Sthompsarun_select_chan_group(struct run_softc *sc, int group)
3506203134Sthompsa{
3507203134Sthompsa	uint32_t tmp;
3508205042Sthompsa	uint8_t agc;
3509203134Sthompsa
3510203134Sthompsa	run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3511203134Sthompsa	run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3512203134Sthompsa	run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3513203134Sthompsa	run_bbp_write(sc, 86, 0x00);
3514203134Sthompsa
3515203134Sthompsa	if (group == 0) {
3516203134Sthompsa		if (sc->ext_2ghz_lna) {
3517203134Sthompsa			run_bbp_write(sc, 82, 0x62);
3518203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3519203134Sthompsa		} else {
3520203134Sthompsa			run_bbp_write(sc, 82, 0x84);
3521203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3522203134Sthompsa		}
3523203134Sthompsa	} else {
3524205042Sthompsa		if (sc->mac_ver == 0x3572)
3525205042Sthompsa			run_bbp_write(sc, 82, 0x94);
3526205042Sthompsa		else
3527203134Sthompsa			run_bbp_write(sc, 82, 0xf2);
3528205042Sthompsa		if (sc->ext_5ghz_lna)
3529203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3530205042Sthompsa		else
3531203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3532203134Sthompsa	}
3533203134Sthompsa
3534203134Sthompsa	run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3535203134Sthompsa	tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3536203134Sthompsa	tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3537203134Sthompsa	run_write(sc, RT2860_TX_BAND_CFG, tmp);
3538203134Sthompsa
3539203134Sthompsa	/* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3540208019Sthompsa	tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3541208019Sthompsa	if (sc->nrxchains > 1)
3542208019Sthompsa		tmp |= RT2860_LNA_PE1_EN;
3543203134Sthompsa	if (group == 0) {	/* 2GHz */
3544208019Sthompsa		tmp |= RT2860_PA_PE_G0_EN;
3545203134Sthompsa		if (sc->ntxchains > 1)
3546203134Sthompsa			tmp |= RT2860_PA_PE_G1_EN;
3547203134Sthompsa	} else {		/* 5GHz */
3548208019Sthompsa		tmp |= RT2860_PA_PE_A0_EN;
3549203134Sthompsa		if (sc->ntxchains > 1)
3550203134Sthompsa			tmp |= RT2860_PA_PE_A1_EN;
3551203134Sthompsa	}
3552205042Sthompsa	if (sc->mac_ver == 0x3572) {
3553205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x00);
3554205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3555205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x80);
3556205042Sthompsa	} else
3557205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3558203134Sthompsa
3559203134Sthompsa	/* set initial AGC value */
3560205042Sthompsa	if (group == 0) {	/* 2GHz band */
3561205042Sthompsa		if (sc->mac_ver >= 0x3070)
3562205042Sthompsa			agc = 0x1c + sc->lna[0] * 2;
3563205042Sthompsa		else
3564205042Sthompsa			agc = 0x2e + sc->lna[0];
3565205042Sthompsa	} else {		/* 5GHz band */
3566205042Sthompsa		if (sc->mac_ver == 0x3572)
3567205042Sthompsa			agc = 0x22 + (sc->lna[group] * 5) / 3;
3568205042Sthompsa		else
3569205042Sthompsa			agc = 0x32 + (sc->lna[group] * 5) / 3;
3570205042Sthompsa	}
3571205042Sthompsa	run_set_agc(sc, agc);
3572203134Sthompsa}
3573203134Sthompsa
3574203134Sthompsastatic void
3575203134Sthompsarun_rt2870_set_chan(struct run_softc *sc, uint32_t chan)
3576203134Sthompsa{
3577203134Sthompsa	const struct rfprog *rfprog = rt2860_rf2850;
3578203134Sthompsa	uint32_t r2, r3, r4;
3579203134Sthompsa	int8_t txpow1, txpow2;
3580203134Sthompsa	int i;
3581203134Sthompsa
3582203134Sthompsa	/* find the settings for this channel (we know it exists) */
3583203134Sthompsa	for (i = 0; rfprog[i].chan != chan; i++);
3584203134Sthompsa
3585203134Sthompsa	r2 = rfprog[i].r2;
3586203134Sthompsa	if (sc->ntxchains == 1)
3587203134Sthompsa		r2 |= 1 << 12;		/* 1T: disable Tx chain 2 */
3588203134Sthompsa	if (sc->nrxchains == 1)
3589203134Sthompsa		r2 |= 1 << 15 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3590203134Sthompsa	else if (sc->nrxchains == 2)
3591203134Sthompsa		r2 |= 1 << 4;		/* 2R: disable Rx chain 3 */
3592203134Sthompsa
3593203134Sthompsa	/* use Tx power values from EEPROM */
3594203134Sthompsa	txpow1 = sc->txpow1[i];
3595203134Sthompsa	txpow2 = sc->txpow2[i];
3596203134Sthompsa	if (chan > 14) {
3597203134Sthompsa		if (txpow1 >= 0)
3598208019Sthompsa			txpow1 = txpow1 << 1 | 1;
3599203134Sthompsa		else
3600208019Sthompsa			txpow1 = (7 + txpow1) << 1;
3601203134Sthompsa		if (txpow2 >= 0)
3602208019Sthompsa			txpow2 = txpow2 << 1 | 1;
3603203134Sthompsa		else
3604208019Sthompsa			txpow2 = (7 + txpow2) << 1;
3605203134Sthompsa	}
3606203134Sthompsa	r3 = rfprog[i].r3 | txpow1 << 7;
3607203134Sthompsa	r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3608203134Sthompsa
3609203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3610203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3611203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3612203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3613203134Sthompsa
3614203134Sthompsa	run_delay(sc, 10);
3615203134Sthompsa
3616203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3617203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3618203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3619203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3620203134Sthompsa
3621203134Sthompsa	run_delay(sc, 10);
3622203134Sthompsa
3623203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3624203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3625203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3626203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3627203134Sthompsa}
3628203134Sthompsa
3629203134Sthompsastatic void
3630203134Sthompsarun_rt3070_set_chan(struct run_softc *sc, uint32_t chan)
3631203134Sthompsa{
3632203134Sthompsa	int8_t txpow1, txpow2;
3633203134Sthompsa	uint8_t rf;
3634205042Sthompsa	int i;
3635203134Sthompsa
3636203134Sthompsa	/* RT3070 is 2GHz only */
3637203134Sthompsa	KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3638203134Sthompsa
3639205042Sthompsa	/* find the settings for this channel (we know it exists) */
3640205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3641205042Sthompsa
3642203134Sthompsa	/* use Tx power values from EEPROM */
3643205042Sthompsa	txpow1 = sc->txpow1[i];
3644205042Sthompsa	txpow2 = sc->txpow2[i];
3645203134Sthompsa
3646205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3647205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3648203134Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3649205042Sthompsa	rf = (rf & ~0x03) | rt3070_freqs[i].r;
3650203134Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3651203134Sthompsa
3652203134Sthompsa	/* set Tx0 power */
3653203134Sthompsa	run_rt3070_rf_read(sc, 12, &rf);
3654203134Sthompsa	rf = (rf & ~0x1f) | txpow1;
3655203134Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3656203134Sthompsa
3657203134Sthompsa	/* set Tx1 power */
3658203134Sthompsa	run_rt3070_rf_read(sc, 13, &rf);
3659203134Sthompsa	rf = (rf & ~0x1f) | txpow2;
3660203134Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3661203134Sthompsa
3662203134Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3663203134Sthompsa	rf &= ~0xfc;
3664203134Sthompsa	if (sc->ntxchains == 1)
3665203134Sthompsa		rf |= 1 << 7 | 1 << 5;	/* 1T: disable Tx chains 2 & 3 */
3666203134Sthompsa	else if (sc->ntxchains == 2)
3667203134Sthompsa		rf |= 1 << 7;		/* 2T: disable Tx chain 3 */
3668203134Sthompsa	if (sc->nrxchains == 1)
3669203134Sthompsa		rf |= 1 << 6 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3670203134Sthompsa	else if (sc->nrxchains == 2)
3671203134Sthompsa		rf |= 1 << 6;		/* 2R: disable Rx chain 3 */
3672203134Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3673203134Sthompsa
3674203134Sthompsa	/* set RF offset */
3675203134Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3676203134Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3677203134Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3678203134Sthompsa
3679203134Sthompsa	/* program RF filter */
3680205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf);	/* Tx */
3681205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3682205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);
3683205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);	/* Rx */
3684205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3685205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);
3686203134Sthompsa
3687203134Sthompsa	/* enable RF tuning */
3688203134Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3689203134Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3690203134Sthompsa}
3691203134Sthompsa
3692203134Sthompsastatic void
3693205042Sthompsarun_rt3572_set_chan(struct run_softc *sc, u_int chan)
3694205042Sthompsa{
3695205042Sthompsa	int8_t txpow1, txpow2;
3696205042Sthompsa	uint32_t tmp;
3697205042Sthompsa	uint8_t rf;
3698205042Sthompsa	int i;
3699205042Sthompsa
3700205042Sthompsa	/* find the settings for this channel (we know it exists) */
3701205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3702205042Sthompsa
3703205042Sthompsa	/* use Tx power values from EEPROM */
3704205042Sthompsa	txpow1 = sc->txpow1[i];
3705205042Sthompsa	txpow2 = sc->txpow2[i];
3706205042Sthompsa
3707205042Sthompsa	if (chan <= 14) {
3708205042Sthompsa		run_bbp_write(sc, 25, sc->bbp25);
3709205042Sthompsa		run_bbp_write(sc, 26, sc->bbp26);
3710205042Sthompsa	} else {
3711205042Sthompsa		/* enable IQ phase correction */
3712205042Sthompsa		run_bbp_write(sc, 25, 0x09);
3713205042Sthompsa		run_bbp_write(sc, 26, 0xff);
3714205042Sthompsa	}
3715205042Sthompsa
3716205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3717205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3718205042Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3719205042Sthompsa	rf  = (rf & ~0x0f) | rt3070_freqs[i].r;
3720205042Sthompsa	rf |= (chan <= 14) ? 0x08 : 0x04;
3721205042Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3722205042Sthompsa
3723205042Sthompsa	/* set PLL mode */
3724205042Sthompsa	run_rt3070_rf_read(sc, 5, &rf);
3725205042Sthompsa	rf &= ~(0x08 | 0x04);
3726205042Sthompsa	rf |= (chan <= 14) ? 0x04 : 0x08;
3727205042Sthompsa	run_rt3070_rf_write(sc, 5, rf);
3728205042Sthompsa
3729205042Sthompsa	/* set Tx power for chain 0 */
3730205042Sthompsa	if (chan <= 14)
3731205042Sthompsa		rf = 0x60 | txpow1;
3732205042Sthompsa	else
3733205042Sthompsa		rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3734205042Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3735205042Sthompsa
3736205042Sthompsa	/* set Tx power for chain 1 */
3737205042Sthompsa	if (chan <= 14)
3738205042Sthompsa		rf = 0x60 | txpow2;
3739205042Sthompsa	else
3740205042Sthompsa		rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3741205042Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3742205042Sthompsa
3743205042Sthompsa	/* set Tx/Rx streams */
3744205042Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3745205042Sthompsa	rf &= ~0xfc;
3746205042Sthompsa	if (sc->ntxchains == 1)
3747205042Sthompsa		rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3748205042Sthompsa	else if (sc->ntxchains == 2)
3749205042Sthompsa		rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3750205042Sthompsa	if (sc->nrxchains == 1)
3751205042Sthompsa		rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3752205042Sthompsa	else if (sc->nrxchains == 2)
3753205042Sthompsa		rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3754205042Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3755205042Sthompsa
3756205042Sthompsa	/* set RF offset */
3757205042Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3758205042Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3759205042Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3760205042Sthompsa
3761205042Sthompsa	/* program RF filter */
3762205042Sthompsa	rf = sc->rf24_20mhz;
3763205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);	/* Tx */
3764205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);	/* Rx */
3765205042Sthompsa
3766205042Sthompsa	/* enable RF tuning */
3767205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3768205042Sthompsa	rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3769205042Sthompsa	run_rt3070_rf_write(sc, 7, rf);
3770205042Sthompsa
3771205042Sthompsa	/* TSSI */
3772205042Sthompsa	rf = (chan <= 14) ? 0xc3 : 0xc0;
3773205042Sthompsa	run_rt3070_rf_write(sc, 9, rf);
3774205042Sthompsa
3775205042Sthompsa	/* set loop filter 1 */
3776205042Sthompsa	run_rt3070_rf_write(sc, 10, 0xf1);
3777205042Sthompsa	/* set loop filter 2 */
3778205042Sthompsa	run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3779205042Sthompsa
3780205042Sthompsa	/* set tx_mx2_ic */
3781205042Sthompsa	run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3782205042Sthompsa	/* set tx_mx1_ic */
3783205042Sthompsa	if (chan <= 14)
3784205042Sthompsa		rf = 0x48 | sc->txmixgain_2ghz;
3785205042Sthompsa	else
3786205042Sthompsa		rf = 0x78 | sc->txmixgain_5ghz;
3787205042Sthompsa	run_rt3070_rf_write(sc, 16, rf);
3788205042Sthompsa
3789205042Sthompsa	/* set tx_lo1 */
3790205042Sthompsa	run_rt3070_rf_write(sc, 17, 0x23);
3791205042Sthompsa	/* set tx_lo2 */
3792205042Sthompsa	if (chan <= 14)
3793205042Sthompsa		rf = 0x93;
3794205042Sthompsa	else if (chan <= 64)
3795205042Sthompsa		rf = 0xb7;
3796205042Sthompsa	else if (chan <= 128)
3797205042Sthompsa		rf = 0x74;
3798205042Sthompsa	else
3799205042Sthompsa		rf = 0x72;
3800205042Sthompsa	run_rt3070_rf_write(sc, 19, rf);
3801205042Sthompsa
3802205042Sthompsa	/* set rx_lo1 */
3803205042Sthompsa	if (chan <= 14)
3804205042Sthompsa		rf = 0xb3;
3805205042Sthompsa	else if (chan <= 64)
3806205042Sthompsa		rf = 0xf6;
3807205042Sthompsa	else if (chan <= 128)
3808205042Sthompsa		rf = 0xf4;
3809205042Sthompsa	else
3810205042Sthompsa		rf = 0xf3;
3811205042Sthompsa	run_rt3070_rf_write(sc, 20, rf);
3812205042Sthompsa
3813205042Sthompsa	/* set pfd_delay */
3814205042Sthompsa	if (chan <= 14)
3815205042Sthompsa		rf = 0x15;
3816205042Sthompsa	else if (chan <= 64)
3817205042Sthompsa		rf = 0x3d;
3818205042Sthompsa	else
3819205042Sthompsa		rf = 0x01;
3820205042Sthompsa	run_rt3070_rf_write(sc, 25, rf);
3821205042Sthompsa
3822205042Sthompsa	/* set rx_lo2 */
3823205042Sthompsa	run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3824205042Sthompsa	/* set ldo_rf_vc */
3825205042Sthompsa	run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3826205042Sthompsa	/* set drv_cc */
3827205042Sthompsa	run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3828205042Sthompsa
3829205042Sthompsa	run_read(sc, RT2860_GPIO_CTRL, &tmp);
3830205042Sthompsa	tmp &= ~0x8080;
3831205042Sthompsa	if (chan <= 14)
3832205042Sthompsa		tmp |= 0x80;
3833205042Sthompsa	run_write(sc, RT2860_GPIO_CTRL, tmp);
3834205042Sthompsa
3835205042Sthompsa	/* enable RF tuning */
3836205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3837205042Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3838205042Sthompsa
3839205042Sthompsa	run_delay(sc, 2);
3840205042Sthompsa}
3841205042Sthompsa
3842205042Sthompsastatic void
3843203134Sthompsarun_set_rx_antenna(struct run_softc *sc, int aux)
3844203134Sthompsa{
3845203134Sthompsa	uint32_t tmp;
3846203134Sthompsa
3847203134Sthompsa	if (aux) {
3848205042Sthompsa		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
3849203134Sthompsa		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3850203134Sthompsa		run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
3851203134Sthompsa	} else {
3852205042Sthompsa		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
3853203134Sthompsa		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3854203134Sthompsa		run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
3855203134Sthompsa	}
3856203134Sthompsa}
3857203134Sthompsa
3858203134Sthompsastatic int
3859203134Sthompsarun_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
3860203134Sthompsa{
3861203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3862203134Sthompsa	uint32_t chan, group;
3863203134Sthompsa
3864203134Sthompsa	chan = ieee80211_chan2ieee(ic, c);
3865203134Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
3866209917Sthompsa		return (EINVAL);
3867203134Sthompsa
3868205042Sthompsa	if (sc->mac_ver == 0x3572)
3869205042Sthompsa		run_rt3572_set_chan(sc, chan);
3870205042Sthompsa	else if (sc->mac_ver >= 0x3070)
3871203134Sthompsa		run_rt3070_set_chan(sc, chan);
3872203134Sthompsa	else
3873203134Sthompsa		run_rt2870_set_chan(sc, chan);
3874203134Sthompsa
3875203134Sthompsa	/* determine channel group */
3876203134Sthompsa	if (chan <= 14)
3877203134Sthompsa		group = 0;
3878203134Sthompsa	else if (chan <= 64)
3879203134Sthompsa		group = 1;
3880203134Sthompsa	else if (chan <= 128)
3881203134Sthompsa		group = 2;
3882203134Sthompsa	else
3883203134Sthompsa		group = 3;
3884203134Sthompsa
3885203134Sthompsa	/* XXX necessary only when group has changed! */
3886203134Sthompsa	run_select_chan_group(sc, group);
3887203134Sthompsa
3888203134Sthompsa	run_delay(sc, 10);
3889203134Sthompsa
3890209917Sthompsa	return (0);
3891203134Sthompsa}
3892203134Sthompsa
3893203134Sthompsastatic void
3894203134Sthompsarun_set_channel(struct ieee80211com *ic)
3895203134Sthompsa{
3896203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3897203134Sthompsa
3898203134Sthompsa	RUN_LOCK(sc);
3899203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
3900203134Sthompsa	RUN_UNLOCK(sc);
3901203134Sthompsa
3902203134Sthompsa	return;
3903203134Sthompsa}
3904203134Sthompsa
3905203134Sthompsastatic void
3906203134Sthompsarun_scan_start(struct ieee80211com *ic)
3907203134Sthompsa{
3908203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3909203134Sthompsa	uint32_t tmp;
3910203134Sthompsa
3911203134Sthompsa	RUN_LOCK(sc);
3912203134Sthompsa
3913203134Sthompsa	/* abort TSF synchronization */
3914203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
3915203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG,
3916203134Sthompsa	    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
3917203134Sthompsa	    RT2860_TBTT_TIMER_EN));
3918203134Sthompsa	run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
3919203134Sthompsa
3920203134Sthompsa	RUN_UNLOCK(sc);
3921203134Sthompsa
3922203134Sthompsa	return;
3923203134Sthompsa}
3924203134Sthompsa
3925203134Sthompsastatic void
3926203134Sthompsarun_scan_end(struct ieee80211com *ic)
3927203134Sthompsa{
3928203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3929203134Sthompsa
3930203134Sthompsa	RUN_LOCK(sc);
3931203134Sthompsa
3932203134Sthompsa	run_enable_tsf_sync(sc);
3933203134Sthompsa	/* XXX keep local copy */
3934203134Sthompsa	run_set_bssid(sc, sc->sc_bssid);
3935203134Sthompsa
3936203134Sthompsa	RUN_UNLOCK(sc);
3937203134Sthompsa
3938203134Sthompsa	return;
3939203134Sthompsa}
3940203134Sthompsa
3941208019Sthompsa/*
3942208019Sthompsa * Could be called from ieee80211_node_timeout()
3943208019Sthompsa * (non-sleepable thread)
3944208019Sthompsa */
3945208019Sthompsastatic void
3946208019Sthompsarun_update_beacon(struct ieee80211vap *vap, int item)
3947203134Sthompsa{
3948208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
3949208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3950218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
3951218492Sbschmidt	int mcast = 0;
3952208019Sthompsa	uint32_t i;
3953208019Sthompsa
3954218492Sbschmidt	KASSERT(vap != NULL, ("no beacon"));
3955218492Sbschmidt
3956218492Sbschmidt	switch (item) {
3957218492Sbschmidt	case IEEE80211_BEACON_ERP:
3958218492Sbschmidt		run_updateslot(ic->ic_ifp);
3959218492Sbschmidt		break;
3960218492Sbschmidt	case IEEE80211_BEACON_HTINFO:
3961218492Sbschmidt		run_updateprot(ic);
3962218492Sbschmidt		break;
3963218492Sbschmidt	case IEEE80211_BEACON_TIM:
3964218492Sbschmidt		mcast = 1;	/*TODO*/
3965218492Sbschmidt		break;
3966218492Sbschmidt	default:
3967218492Sbschmidt		break;
3968218492Sbschmidt	}
3969218492Sbschmidt
3970218492Sbschmidt	setbit(rvp->bo.bo_flags, item);
3971218492Sbschmidt	ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
3972218492Sbschmidt
3973208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
3974208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
3975208019Sthompsa	sc->cmdq[i].func = run_update_beacon_cb;
3976208019Sthompsa	sc->cmdq[i].arg0 = vap;
3977208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
3978208019Sthompsa
3979208019Sthompsa	return;
3980203134Sthompsa}
3981203134Sthompsa
3982203134Sthompsastatic void
3983208019Sthompsarun_update_beacon_cb(void *arg)
3984203134Sthompsa{
3985208019Sthompsa	struct ieee80211vap *vap = arg;
3986218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
3987203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
3988203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3989203134Sthompsa	struct rt2860_txwi txwi;
3990203134Sthompsa	struct mbuf *m;
3991208019Sthompsa	uint8_t ridx;
3992203134Sthompsa
3993209917Sthompsa	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
3994208019Sthompsa		return;
3995236439Shselasky	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
3996236439Shselasky		return;
3997208019Sthompsa
3998218492Sbschmidt	/*
3999218492Sbschmidt	 * No need to call ieee80211_beacon_update(), run_update_beacon()
4000218492Sbschmidt	 * is taking care of apropriate calls.
4001218492Sbschmidt	 */
4002218492Sbschmidt	if (rvp->beacon_mbuf == NULL) {
4003218492Sbschmidt		rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
4004218492Sbschmidt		    &rvp->bo);
4005218492Sbschmidt		if (rvp->beacon_mbuf == NULL)
4006218492Sbschmidt			return;
4007218492Sbschmidt	}
4008218492Sbschmidt	m = rvp->beacon_mbuf;
4009203134Sthompsa
4010203134Sthompsa	memset(&txwi, 0, sizeof txwi);
4011203134Sthompsa	txwi.wcid = 0xff;
4012203134Sthompsa	txwi.len = htole16(m->m_pkthdr.len);
4013203134Sthompsa	/* send beacons at the lowest available rate */
4014208019Sthompsa	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4015208019Sthompsa	    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
4016208019Sthompsa	txwi.phy = htole16(rt2860_rates[ridx].mcs);
4017208019Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
4018203134Sthompsa	        txwi.phy |= htole16(RT2860_PHY_OFDM);
4019203134Sthompsa	txwi.txop = RT2860_TX_TXOP_HT;
4020203134Sthompsa	txwi.flags = RT2860_TX_TS;
4021209144Sthompsa	txwi.xflags = RT2860_TX_NSEQ;
4022203134Sthompsa
4023218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
4024208019Sthompsa	    (uint8_t *)&txwi, sizeof txwi);
4025218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
4026203134Sthompsa	    mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);	/* roundup len */
4027203134Sthompsa
4028203134Sthompsa	return;
4029203134Sthompsa}
4030203134Sthompsa
4031203134Sthompsastatic void
4032203134Sthompsarun_updateprot(struct ieee80211com *ic)
4033203134Sthompsa{
4034203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4035218492Sbschmidt	uint32_t i;
4036218492Sbschmidt
4037218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4038218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4039218492Sbschmidt	sc->cmdq[i].func = run_updateprot_cb;
4040218492Sbschmidt	sc->cmdq[i].arg0 = ic;
4041218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4042218492Sbschmidt}
4043218492Sbschmidt
4044218492Sbschmidtstatic void
4045218492Sbschmidtrun_updateprot_cb(void *arg)
4046218492Sbschmidt{
4047218492Sbschmidt	struct ieee80211com *ic = arg;
4048218492Sbschmidt	struct run_softc *sc = ic->ic_ifp->if_softc;
4049203134Sthompsa	uint32_t tmp;
4050203134Sthompsa
4051203134Sthompsa	tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4052203134Sthompsa	/* setup protection frame rate (MCS code) */
4053203134Sthompsa	tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4054203134Sthompsa	    rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4055203134Sthompsa	    rt2860_rates[RT2860_RIDX_CCK11].mcs;
4056203134Sthompsa
4057203134Sthompsa	/* CCK frames don't require protection */
4058203134Sthompsa	run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4059203134Sthompsa	if (ic->ic_flags & IEEE80211_F_USEPROT) {
4060203134Sthompsa		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4061203134Sthompsa			tmp |= RT2860_PROT_CTRL_RTS_CTS;
4062203134Sthompsa		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4063203134Sthompsa			tmp |= RT2860_PROT_CTRL_CTS;
4064203134Sthompsa	}
4065203134Sthompsa	run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4066203134Sthompsa}
4067203134Sthompsa
4068203134Sthompsastatic void
4069208019Sthompsarun_usb_timeout_cb(void *arg)
4070203134Sthompsa{
4071208019Sthompsa	struct ieee80211vap *vap = arg;
4072208019Sthompsa	struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4073203134Sthompsa
4074208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4075203134Sthompsa
4076203134Sthompsa	if(vap->iv_state == IEEE80211_S_RUN &&
4077203134Sthompsa	    vap->iv_opmode != IEEE80211_M_STA)
4078203134Sthompsa		run_reset_livelock(sc);
4079209917Sthompsa	else if (vap->iv_state == IEEE80211_S_SCAN) {
4080203134Sthompsa		DPRINTF("timeout caused by scan\n");
4081203134Sthompsa		/* cancel bgscan */
4082203134Sthompsa		ieee80211_cancel_scan(vap);
4083203134Sthompsa	} else
4084203134Sthompsa		DPRINTF("timeout by unknown cause\n");
4085203134Sthompsa}
4086203134Sthompsa
4087203134Sthompsastatic void
4088203134Sthompsarun_reset_livelock(struct run_softc *sc)
4089203134Sthompsa{
4090203134Sthompsa	uint32_t tmp;
4091203134Sthompsa
4092208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4093208019Sthompsa
4094203134Sthompsa	/*
4095203134Sthompsa	 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4096203134Sthompsa	 * can run into a livelock and start sending CTS-to-self frames like
4097203134Sthompsa	 * crazy if protection is enabled.  Reset MAC/BBP for a while
4098203134Sthompsa	 */
4099203134Sthompsa	run_read(sc, RT2860_DEBUG, &tmp);
4100208019Sthompsa	DPRINTFN(3, "debug reg %08x\n", tmp);
4101209917Sthompsa	if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4102203134Sthompsa		DPRINTF("CTS-to-self livelock detected\n");
4103203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4104203134Sthompsa		run_delay(sc, 1);
4105203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL,
4106203134Sthompsa		    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4107203134Sthompsa	}
4108203134Sthompsa}
4109203134Sthompsa
4110203134Sthompsastatic void
4111203134Sthompsarun_update_promisc_locked(struct ifnet *ifp)
4112203134Sthompsa{
4113203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4114203134Sthompsa        uint32_t tmp;
4115203134Sthompsa
4116203134Sthompsa	run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4117203134Sthompsa
4118203134Sthompsa	tmp |= RT2860_DROP_UC_NOME;
4119203134Sthompsa        if (ifp->if_flags & IFF_PROMISC)
4120203134Sthompsa		tmp &= ~RT2860_DROP_UC_NOME;
4121203134Sthompsa
4122203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4123203134Sthompsa
4124203134Sthompsa        DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4125203134Sthompsa            "entering" : "leaving");
4126203134Sthompsa}
4127203134Sthompsa
4128203134Sthompsastatic void
4129203134Sthompsarun_update_promisc(struct ifnet *ifp)
4130203134Sthompsa{
4131203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4132203134Sthompsa
4133203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4134203134Sthompsa		return;
4135203134Sthompsa
4136203134Sthompsa	RUN_LOCK(sc);
4137203134Sthompsa	run_update_promisc_locked(ifp);
4138203134Sthompsa	RUN_UNLOCK(sc);
4139203134Sthompsa}
4140203134Sthompsa
4141203134Sthompsastatic void
4142203134Sthompsarun_enable_tsf_sync(struct run_softc *sc)
4143203134Sthompsa{
4144208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4145203134Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4146203134Sthompsa	uint32_t tmp;
4147203134Sthompsa
4148208019Sthompsa	DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode);
4149208019Sthompsa
4150203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4151203134Sthompsa	tmp &= ~0x1fffff;
4152203134Sthompsa	tmp |= vap->iv_bss->ni_intval * 16;
4153203134Sthompsa	tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4154203134Sthompsa
4155208019Sthompsa	if (ic->ic_opmode == IEEE80211_M_STA) {
4156203134Sthompsa		/*
4157203134Sthompsa		 * Local TSF is always updated with remote TSF on beacon
4158203134Sthompsa		 * reception.
4159203134Sthompsa		 */
4160203134Sthompsa		tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4161208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4162203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4163203134Sthompsa	        /*
4164203134Sthompsa	         * Local TSF is updated with remote TSF on beacon reception
4165203134Sthompsa	         * only if the remote TSF is greater than local TSF.
4166203134Sthompsa	         */
4167203134Sthompsa	        tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4168208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4169208019Sthompsa		    ic->ic_opmode == IEEE80211_M_MBSS) {
4170203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4171203134Sthompsa	        /* SYNC with nobody */
4172203134Sthompsa	        tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4173208019Sthompsa	} else {
4174203134Sthompsa		DPRINTF("Enabling TSF failed. undefined opmode\n");
4175208019Sthompsa		return;
4176208019Sthompsa	}
4177203134Sthompsa
4178203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4179203134Sthompsa}
4180203134Sthompsa
4181203134Sthompsastatic void
4182203134Sthompsarun_enable_mrr(struct run_softc *sc)
4183203134Sthompsa{
4184203134Sthompsa#define CCK(mcs)	(mcs)
4185203134Sthompsa#define OFDM(mcs)	(1 << 3 | (mcs))
4186203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG0,
4187203134Sthompsa	    OFDM(6) << 28 |	/* 54->48 */
4188203134Sthompsa	    OFDM(5) << 24 |	/* 48->36 */
4189203134Sthompsa	    OFDM(4) << 20 |	/* 36->24 */
4190203134Sthompsa	    OFDM(3) << 16 |	/* 24->18 */
4191203134Sthompsa	    OFDM(2) << 12 |	/* 18->12 */
4192203134Sthompsa	    OFDM(1) <<  8 |	/* 12-> 9 */
4193203134Sthompsa	    OFDM(0) <<  4 |	/*  9-> 6 */
4194203134Sthompsa	    OFDM(0));		/*  6-> 6 */
4195203134Sthompsa
4196203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG1,
4197203134Sthompsa	    CCK(2) << 12 |	/* 11->5.5 */
4198203134Sthompsa	    CCK(1) <<  8 |	/* 5.5-> 2 */
4199203134Sthompsa	    CCK(0) <<  4 |	/*   2-> 1 */
4200203134Sthompsa	    CCK(0));		/*   1-> 1 */
4201203134Sthompsa#undef OFDM
4202203134Sthompsa#undef CCK
4203203134Sthompsa}
4204203134Sthompsa
4205203134Sthompsastatic void
4206203134Sthompsarun_set_txpreamble(struct run_softc *sc)
4207203134Sthompsa{
4208203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4209203134Sthompsa	uint32_t tmp;
4210203134Sthompsa
4211203134Sthompsa	run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4212203134Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4213203134Sthompsa		tmp |= RT2860_CCK_SHORT_EN;
4214203134Sthompsa	else
4215203134Sthompsa		tmp &= ~RT2860_CCK_SHORT_EN;
4216203134Sthompsa	run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4217203134Sthompsa}
4218203134Sthompsa
4219203134Sthompsastatic void
4220203134Sthompsarun_set_basicrates(struct run_softc *sc)
4221203134Sthompsa{
4222203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4223203134Sthompsa
4224203134Sthompsa	/* set basic rates mask */
4225203134Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
4226203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4227203134Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
4228203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4229203134Sthompsa	else	/* 11g */
4230203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4231203134Sthompsa}
4232203134Sthompsa
4233203134Sthompsastatic void
4234203134Sthompsarun_set_leds(struct run_softc *sc, uint16_t which)
4235203134Sthompsa{
4236203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4237203134Sthompsa	    which | (sc->leds & 0x7f));
4238203134Sthompsa}
4239203134Sthompsa
4240203134Sthompsastatic void
4241203134Sthompsarun_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4242203134Sthompsa{
4243203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW0,
4244203134Sthompsa	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4245203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW1,
4246203134Sthompsa	    bssid[4] | bssid[5] << 8);
4247203134Sthompsa}
4248203134Sthompsa
4249203134Sthompsastatic void
4250203134Sthompsarun_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4251203134Sthompsa{
4252203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW0,
4253203134Sthompsa	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4254203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW1,
4255203134Sthompsa	    addr[4] | addr[5] << 8 | 0xff << 16);
4256203134Sthompsa}
4257203134Sthompsa
4258203134Sthompsastatic void
4259203134Sthompsarun_updateslot(struct ifnet *ifp)
4260203134Sthompsa{
4261203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4262203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4263218492Sbschmidt	uint32_t i;
4264218492Sbschmidt
4265218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4266218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4267218492Sbschmidt	sc->cmdq[i].func = run_updateslot_cb;
4268218492Sbschmidt	sc->cmdq[i].arg0 = ifp;
4269218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4270218492Sbschmidt
4271218492Sbschmidt	return;
4272218492Sbschmidt}
4273218492Sbschmidt
4274218492Sbschmidt/* ARGSUSED */
4275218492Sbschmidtstatic void
4276218492Sbschmidtrun_updateslot_cb(void *arg)
4277218492Sbschmidt{
4278218492Sbschmidt	struct ifnet *ifp = arg;
4279218492Sbschmidt	struct run_softc *sc = ifp->if_softc;
4280218492Sbschmidt	struct ieee80211com *ic = ifp->if_l2com;
4281203134Sthompsa	uint32_t tmp;
4282203134Sthompsa
4283203134Sthompsa	run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4284203134Sthompsa	tmp &= ~0xff;
4285203134Sthompsa	tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4286203134Sthompsa	run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4287203134Sthompsa}
4288203134Sthompsa
4289208019Sthompsastatic void
4290208019Sthompsarun_update_mcast(struct ifnet *ifp)
4291208019Sthompsa{
4292208019Sthompsa	/* h/w filter supports getting everything or nothing */
4293208019Sthompsa	ifp->if_flags |= IFF_ALLMULTI;
4294208019Sthompsa}
4295208019Sthompsa
4296203134Sthompsastatic int8_t
4297203134Sthompsarun_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4298203134Sthompsa{
4299203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4300203134Sthompsa	struct ieee80211_channel *c = ic->ic_curchan;
4301203134Sthompsa	int delta;
4302203134Sthompsa
4303203134Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
4304203134Sthompsa		uint32_t chan = ieee80211_chan2ieee(ic, c);
4305203134Sthompsa		delta = sc->rssi_5ghz[rxchain];
4306203134Sthompsa
4307203134Sthompsa		/* determine channel group */
4308203134Sthompsa		if (chan <= 64)
4309203134Sthompsa			delta -= sc->lna[1];
4310203134Sthompsa		else if (chan <= 128)
4311203134Sthompsa			delta -= sc->lna[2];
4312203134Sthompsa		else
4313203134Sthompsa			delta -= sc->lna[3];
4314203134Sthompsa	} else
4315203134Sthompsa		delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4316203134Sthompsa
4317209917Sthompsa	return (-12 - delta - rssi);
4318203134Sthompsa}
4319203134Sthompsa
4320203134Sthompsastatic int
4321203134Sthompsarun_bbp_init(struct run_softc *sc)
4322203134Sthompsa{
4323203134Sthompsa	int i, error, ntries;
4324203134Sthompsa	uint8_t bbp0;
4325203134Sthompsa
4326203134Sthompsa	/* wait for BBP to wake up */
4327203134Sthompsa	for (ntries = 0; ntries < 20; ntries++) {
4328203134Sthompsa		if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4329203134Sthompsa			return error;
4330203134Sthompsa		if (bbp0 != 0 && bbp0 != 0xff)
4331203134Sthompsa			break;
4332203134Sthompsa	}
4333203134Sthompsa	if (ntries == 20)
4334209917Sthompsa		return (ETIMEDOUT);
4335203134Sthompsa
4336203134Sthompsa	/* initialize BBP registers to default values */
4337233774Shselasky	for (i = 0; i < N(rt2860_def_bbp); i++) {
4338203134Sthompsa		run_bbp_write(sc, rt2860_def_bbp[i].reg,
4339203134Sthompsa		    rt2860_def_bbp[i].val);
4340203134Sthompsa	}
4341203134Sthompsa
4342203134Sthompsa	/* fix BBP84 for RT2860E */
4343205042Sthompsa	if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4344205042Sthompsa		run_bbp_write(sc, 84, 0x19);
4345203134Sthompsa
4346205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4347203134Sthompsa		run_bbp_write(sc, 79, 0x13);
4348203134Sthompsa		run_bbp_write(sc, 80, 0x05);
4349203134Sthompsa		run_bbp_write(sc, 81, 0x33);
4350205042Sthompsa	} else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4351203134Sthompsa		run_bbp_write(sc, 69, 0x16);
4352203134Sthompsa		run_bbp_write(sc, 73, 0x12);
4353203134Sthompsa	}
4354209917Sthompsa	return (0);
4355203134Sthompsa}
4356203134Sthompsa
4357203134Sthompsastatic int
4358203134Sthompsarun_rt3070_rf_init(struct run_softc *sc)
4359203134Sthompsa{
4360203134Sthompsa	uint32_t tmp;
4361205042Sthompsa	uint8_t rf, target, bbp4;
4362203134Sthompsa	int i;
4363203134Sthompsa
4364203134Sthompsa	run_rt3070_rf_read(sc, 30, &rf);
4365203134Sthompsa	/* toggle RF R30 bit 7 */
4366203134Sthompsa	run_rt3070_rf_write(sc, 30, rf | 0x80);
4367203134Sthompsa	run_delay(sc, 10);
4368203134Sthompsa	run_rt3070_rf_write(sc, 30, rf & ~0x80);
4369203134Sthompsa
4370203134Sthompsa	/* initialize RF registers to default value */
4371205042Sthompsa	if (sc->mac_ver == 0x3572) {
4372233774Shselasky		for (i = 0; i < N(rt3572_def_rf); i++) {
4373205042Sthompsa			run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4374205042Sthompsa			    rt3572_def_rf[i].val);
4375205042Sthompsa		}
4376205042Sthompsa	} else {
4377233774Shselasky		for (i = 0; i < N(rt3070_def_rf); i++) {
4378205042Sthompsa			run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4379205042Sthompsa			    rt3070_def_rf[i].val);
4380205042Sthompsa		}
4381203134Sthompsa	}
4382205042Sthompsa
4383205042Sthompsa	if (sc->mac_ver == 0x3070) {
4384203134Sthompsa		/* change voltage from 1.2V to 1.35V for RT3070 */
4385203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4386203134Sthompsa		tmp = (tmp & ~0x0f000000) | 0x0d000000;
4387203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4388203134Sthompsa
4389205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4390203134Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4391203134Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4392203134Sthompsa		run_rt3070_rf_write(sc, 31, 0x14);
4393203134Sthompsa
4394203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4395203134Sthompsa		tmp &= ~0x1f000000;
4396205042Sthompsa		if (sc->mac_rev < 0x0211)
4397205042Sthompsa			tmp |= 0x0d000000;	/* 1.3V */
4398203134Sthompsa		else
4399205042Sthompsa			tmp |= 0x01000000;	/* 1.2V */
4400203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4401203134Sthompsa
4402203134Sthompsa		/* patch LNA_PE_G1 */
4403203134Sthompsa		run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4404203134Sthompsa		run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4405208019Sthompsa
4406209917Sthompsa	} else if (sc->mac_ver == 0x3572) {
4407205042Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4408205042Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4409205042Sthompsa
4410208019Sthompsa		/* increase voltage from 1.2V to 1.35V */
4411208019Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4412208019Sthompsa		tmp = (tmp & ~0x1f000000) | 0x0d000000;
4413208019Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4414203134Sthompsa
4415209917Sthompsa		if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4416203134Sthompsa			run_delay(sc, 1);	/* wait for 1msec */
4417205042Sthompsa			/* decrease voltage back to 1.2V */
4418203134Sthompsa			tmp = (tmp & ~0x1f000000) | 0x01000000;
4419203134Sthompsa			run_write(sc, RT3070_LDO_CFG0, tmp);
4420203134Sthompsa		}
4421203134Sthompsa	}
4422203134Sthompsa
4423203134Sthompsa	/* select 20MHz bandwidth */
4424203134Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4425203134Sthompsa	run_rt3070_rf_write(sc, 31, rf & ~0x20);
4426203134Sthompsa
4427203134Sthompsa	/* calibrate filter for 20MHz bandwidth */
4428203134Sthompsa	sc->rf24_20mhz = 0x1f;	/* default value */
4429205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4430205042Sthompsa	run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4431203134Sthompsa
4432203134Sthompsa	/* select 40MHz bandwidth */
4433203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4434203134Sthompsa	run_bbp_write(sc, 4, (bbp4 & ~0x08) | 0x10);
4435205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4436205042Sthompsa	run_rt3070_rf_write(sc, 31, rf | 0x20);
4437203134Sthompsa
4438203134Sthompsa	/* calibrate filter for 40MHz bandwidth */
4439203134Sthompsa	sc->rf24_40mhz = 0x2f;	/* default value */
4440205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4441205042Sthompsa	run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4442203134Sthompsa
4443203134Sthompsa	/* go back to 20MHz bandwidth */
4444203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4445203134Sthompsa	run_bbp_write(sc, 4, bbp4 & ~0x18);
4446203134Sthompsa
4447205042Sthompsa	if (sc->mac_ver == 0x3572) {
4448205042Sthompsa		/* save default BBP registers 25 and 26 values */
4449205042Sthompsa		run_bbp_read(sc, 25, &sc->bbp25);
4450205042Sthompsa		run_bbp_read(sc, 26, &sc->bbp26);
4451205042Sthompsa	} else if (sc->mac_rev < 0x0211)
4452203134Sthompsa		run_rt3070_rf_write(sc, 27, 0x03);
4453203134Sthompsa
4454203134Sthompsa	run_read(sc, RT3070_OPT_14, &tmp);
4455203134Sthompsa	run_write(sc, RT3070_OPT_14, tmp | 1);
4456203134Sthompsa
4457205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4458205042Sthompsa		run_rt3070_rf_read(sc, 17, &rf);
4459205042Sthompsa		rf &= ~RT3070_TX_LO1;
4460205042Sthompsa		if ((sc->mac_ver == 0x3070 ||
4461205042Sthompsa		     (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4462205042Sthompsa		    !sc->ext_2ghz_lna)
4463205042Sthompsa			rf |= 0x20;	/* fix for long range Rx issue */
4464205042Sthompsa		if (sc->txmixgain_2ghz >= 1)
4465205042Sthompsa			rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4466205042Sthompsa		run_rt3070_rf_write(sc, 17, rf);
4467205042Sthompsa	}
4468205042Sthompsa
4469205042Sthompsa	if (sc->mac_rev == 0x3071) {
4470203134Sthompsa		run_rt3070_rf_read(sc, 1, &rf);
4471203134Sthompsa		rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4472203134Sthompsa		rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4473203134Sthompsa		run_rt3070_rf_write(sc, 1, rf);
4474203134Sthompsa
4475203134Sthompsa		run_rt3070_rf_read(sc, 15, &rf);
4476203134Sthompsa		run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4477203134Sthompsa
4478203134Sthompsa		run_rt3070_rf_read(sc, 20, &rf);
4479203134Sthompsa		run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4480203134Sthompsa
4481203134Sthompsa		run_rt3070_rf_read(sc, 21, &rf);
4482203134Sthompsa		run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4483205042Sthompsa	}
4484203134Sthompsa
4485205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4486205042Sthompsa		/* fix Tx to Rx IQ glitch by raising RF voltage */
4487203134Sthompsa		run_rt3070_rf_read(sc, 27, &rf);
4488203134Sthompsa		rf &= ~0x77;
4489205042Sthompsa		if (sc->mac_rev < 0x0211)
4490203134Sthompsa			rf |= 0x03;
4491203134Sthompsa		run_rt3070_rf_write(sc, 27, rf);
4492203134Sthompsa	}
4493209917Sthompsa	return (0);
4494203134Sthompsa}
4495203134Sthompsa
4496203134Sthompsastatic int
4497203134Sthompsarun_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4498203134Sthompsa    uint8_t *val)
4499203134Sthompsa{
4500203134Sthompsa	uint8_t rf22, rf24;
4501203134Sthompsa	uint8_t bbp55_pb, bbp55_sb, delta;
4502203134Sthompsa	int ntries;
4503203134Sthompsa
4504203134Sthompsa	/* program filter */
4505205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf24);
4506205042Sthompsa	rf24 = (rf24 & 0xc0) | init;	/* initial filter value */
4507203134Sthompsa	run_rt3070_rf_write(sc, 24, rf24);
4508203134Sthompsa
4509203134Sthompsa	/* enable baseband loopback mode */
4510203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4511203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4512203134Sthompsa
4513203134Sthompsa	/* set power and frequency of passband test tone */
4514203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4515203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4516203134Sthompsa		/* transmit test tone */
4517203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4518203134Sthompsa		run_delay(sc, 10);
4519203134Sthompsa		/* read received power */
4520203134Sthompsa		run_bbp_read(sc, 55, &bbp55_pb);
4521203134Sthompsa		if (bbp55_pb != 0)
4522203134Sthompsa			break;
4523203134Sthompsa	}
4524203134Sthompsa	if (ntries == 100)
4525203134Sthompsa		return ETIMEDOUT;
4526203134Sthompsa
4527203134Sthompsa	/* set power and frequency of stopband test tone */
4528203134Sthompsa	run_bbp_write(sc, 24, 0x06);
4529203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4530203134Sthompsa		/* transmit test tone */
4531203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4532203134Sthompsa		run_delay(sc, 10);
4533203134Sthompsa		/* read received power */
4534203134Sthompsa		run_bbp_read(sc, 55, &bbp55_sb);
4535203134Sthompsa
4536203134Sthompsa		delta = bbp55_pb - bbp55_sb;
4537203134Sthompsa		if (delta > target)
4538203134Sthompsa			break;
4539203134Sthompsa
4540203134Sthompsa		/* reprogram filter */
4541203134Sthompsa		rf24++;
4542203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4543203134Sthompsa	}
4544203134Sthompsa	if (ntries < 100) {
4545203134Sthompsa		if (rf24 != init)
4546203134Sthompsa			rf24--;	/* backtrack */
4547203134Sthompsa		*val = rf24;
4548203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4549203134Sthompsa	}
4550203134Sthompsa
4551203134Sthompsa	/* restore initial state */
4552203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4553203134Sthompsa
4554203134Sthompsa	/* disable baseband loopback mode */
4555203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4556203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4557203134Sthompsa
4558209917Sthompsa	return (0);
4559203134Sthompsa}
4560203134Sthompsa
4561205042Sthompsastatic void
4562205042Sthompsarun_rt3070_rf_setup(struct run_softc *sc)
4563205042Sthompsa{
4564205042Sthompsa	uint8_t bbp, rf;
4565205042Sthompsa	int i;
4566205042Sthompsa
4567205042Sthompsa	if (sc->mac_ver == 0x3572) {
4568205042Sthompsa		/* enable DC filter */
4569205042Sthompsa		if (sc->mac_rev >= 0x0201)
4570205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4571205042Sthompsa
4572205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4573205042Sthompsa		if (sc->ntxchains == 1)
4574205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4575205042Sthompsa		if (sc->nrxchains == 1)
4576205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4577205042Sthompsa		run_bbp_write(sc, 138, bbp);
4578205042Sthompsa
4579205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4580205042Sthompsa			/* improve power consumption */
4581205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4582205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4583205042Sthompsa		}
4584205042Sthompsa
4585205042Sthompsa		run_rt3070_rf_read(sc, 16, &rf);
4586205042Sthompsa		rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4587205042Sthompsa		run_rt3070_rf_write(sc, 16, rf);
4588205042Sthompsa
4589205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4590205042Sthompsa		/* enable DC filter */
4591205042Sthompsa		if (sc->mac_rev >= 0x0201)
4592205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4593205042Sthompsa
4594205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4595205042Sthompsa		if (sc->ntxchains == 1)
4596205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4597205042Sthompsa		if (sc->nrxchains == 1)
4598205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4599205042Sthompsa		run_bbp_write(sc, 138, bbp);
4600205042Sthompsa
4601205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4602205042Sthompsa			/* improve power consumption */
4603205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4604205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4605205042Sthompsa		}
4606205042Sthompsa
4607205042Sthompsa		run_write(sc, RT2860_TX_SW_CFG1, 0);
4608205042Sthompsa		if (sc->mac_rev < 0x0211) {
4609205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2,
4610205042Sthompsa			    sc->patch_dac ? 0x2c : 0x0f);
4611205042Sthompsa		} else
4612205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4613205042Sthompsa
4614205042Sthompsa	} else if (sc->mac_ver == 0x3070) {
4615205042Sthompsa		if (sc->mac_rev >= 0x0201) {
4616205042Sthompsa			/* enable DC filter */
4617205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4618205042Sthompsa
4619205042Sthompsa			/* improve power consumption */
4620205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4621205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4622205042Sthompsa		}
4623205042Sthompsa
4624205042Sthompsa		if (sc->mac_rev < 0x0211) {
4625205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG1, 0);
4626205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4627205042Sthompsa		} else
4628205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4629205042Sthompsa	}
4630205042Sthompsa
4631205042Sthompsa	/* initialize RF registers from ROM for >=RT3071*/
4632205042Sthompsa	if (sc->mac_ver >= 0x3071) {
4633205042Sthompsa		for (i = 0; i < 10; i++) {
4634205042Sthompsa			if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4635205042Sthompsa				continue;
4636205042Sthompsa			run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4637205042Sthompsa		}
4638205042Sthompsa	}
4639205042Sthompsa}
4640205042Sthompsa
4641203134Sthompsastatic int
4642203134Sthompsarun_txrx_enable(struct run_softc *sc)
4643203134Sthompsa{
4644203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4645203134Sthompsa	uint32_t tmp;
4646203134Sthompsa	int error, ntries;
4647203134Sthompsa
4648203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4649203134Sthompsa	for (ntries = 0; ntries < 200; ntries++) {
4650203134Sthompsa		if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4651203134Sthompsa			return error;
4652203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4653203134Sthompsa			break;
4654203134Sthompsa		run_delay(sc, 50);
4655203134Sthompsa	}
4656203134Sthompsa	if (ntries == 200)
4657203134Sthompsa		return ETIMEDOUT;
4658203134Sthompsa
4659203134Sthompsa	run_delay(sc, 50);
4660203134Sthompsa
4661203134Sthompsa	tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4662203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4663203134Sthompsa
4664203134Sthompsa	/* enable Rx bulk aggregation (set timeout and limit) */
4665203134Sthompsa	tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
4666203134Sthompsa	    RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
4667203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, tmp);
4668203134Sthompsa
4669203134Sthompsa	/* set Rx filter */
4670203134Sthompsa	tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
4671203134Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
4672203134Sthompsa		tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
4673203134Sthompsa		    RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
4674203134Sthompsa		    RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
4675203134Sthompsa		    RT2860_DROP_CFACK | RT2860_DROP_CFEND;
4676203134Sthompsa		if (ic->ic_opmode == IEEE80211_M_STA)
4677203134Sthompsa			tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
4678203134Sthompsa	}
4679203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4680203134Sthompsa
4681203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
4682203134Sthompsa	    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4683203134Sthompsa
4684209917Sthompsa	return (0);
4685203134Sthompsa}
4686203134Sthompsa
4687203134Sthompsastatic void
4688203134Sthompsarun_init_locked(struct run_softc *sc)
4689203134Sthompsa{
4690203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4691203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4692203134Sthompsa	uint32_t tmp;
4693203134Sthompsa	uint8_t bbp1, bbp3;
4694203134Sthompsa	int i;
4695203134Sthompsa	int ridx;
4696203134Sthompsa	int ntries;
4697203134Sthompsa
4698209917Sthompsa	if (ic->ic_nrunning > 1)
4699208019Sthompsa		return;
4700208019Sthompsa
4701203134Sthompsa	run_stop(sc);
4702203134Sthompsa
4703233283Sbschmidt	if (run_load_microcode(sc) != 0) {
4704233283Sbschmidt		device_printf(sc->sc_dev, "could not load 8051 microcode\n");
4705233283Sbschmidt		goto fail;
4706233283Sbschmidt	}
4707233283Sbschmidt
4708203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4709203134Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
4710203134Sthompsa			goto fail;
4711203134Sthompsa		if (tmp != 0 && tmp != 0xffffffff)
4712203134Sthompsa			break;
4713203134Sthompsa		run_delay(sc, 10);
4714203134Sthompsa	}
4715203134Sthompsa	if (ntries == 100)
4716203134Sthompsa		goto fail;
4717203134Sthompsa
4718203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
4719203134Sthompsa		run_setup_tx_list(sc, &sc->sc_epq[i]);
4720203134Sthompsa
4721203134Sthompsa	run_set_macaddr(sc, IF_LLADDR(ifp));
4722203134Sthompsa
4723203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4724203134Sthompsa		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
4725203134Sthompsa			goto fail;
4726203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4727203134Sthompsa			break;
4728203134Sthompsa		run_delay(sc, 10);
4729203134Sthompsa	}
4730203134Sthompsa	if (ntries == 100) {
4731203138Sthompsa		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
4732203134Sthompsa		goto fail;
4733203134Sthompsa	}
4734203134Sthompsa	tmp &= 0xff0;
4735203134Sthompsa	tmp |= RT2860_TX_WB_DDONE;
4736203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4737203134Sthompsa
4738203134Sthompsa	/* turn off PME_OEN to solve high-current issue */
4739203134Sthompsa	run_read(sc, RT2860_SYS_CTRL, &tmp);
4740203134Sthompsa	run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
4741203134Sthompsa
4742203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
4743203134Sthompsa	    RT2860_BBP_HRST | RT2860_MAC_SRST);
4744203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
4745203134Sthompsa
4746203134Sthompsa	if (run_reset(sc) != 0) {
4747203138Sthompsa		device_printf(sc->sc_dev, "could not reset chipset\n");
4748203134Sthompsa		goto fail;
4749203134Sthompsa	}
4750203134Sthompsa
4751203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4752203134Sthompsa
4753203134Sthompsa	/* init Tx power for all Tx rates (from EEPROM) */
4754203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
4755203134Sthompsa		if (sc->txpow20mhz[ridx] == 0xffffffff)
4756203134Sthompsa			continue;
4757203134Sthompsa		run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
4758203134Sthompsa	}
4759203134Sthompsa
4760233774Shselasky	for (i = 0; i < N(rt2870_def_mac); i++)
4761203134Sthompsa		run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
4762203134Sthompsa	run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
4763203134Sthompsa	run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
4764203134Sthompsa	run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
4765203134Sthompsa
4766205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4767203134Sthompsa		/* set delay of PA_PE assertion to 1us (unit of 0.25us) */
4768203134Sthompsa		run_write(sc, RT2860_TX_SW_CFG0,
4769203134Sthompsa		    4 << RT2860_DLY_PAPE_EN_SHIFT);
4770203134Sthompsa	}
4771203134Sthompsa
4772203134Sthompsa	/* wait while MAC is busy */
4773203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4774203134Sthompsa		if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
4775203134Sthompsa			goto fail;
4776203134Sthompsa		if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
4777203134Sthompsa			break;
4778203134Sthompsa		run_delay(sc, 10);
4779203134Sthompsa	}
4780203134Sthompsa	if (ntries == 100)
4781203134Sthompsa		goto fail;
4782203134Sthompsa
4783203134Sthompsa	/* clear Host to MCU mailbox */
4784203134Sthompsa	run_write(sc, RT2860_H2M_BBPAGENT, 0);
4785203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
4786203134Sthompsa	run_delay(sc, 10);
4787203134Sthompsa
4788203134Sthompsa	if (run_bbp_init(sc) != 0) {
4789203138Sthompsa		device_printf(sc->sc_dev, "could not initialize BBP\n");
4790203134Sthompsa		goto fail;
4791203134Sthompsa	}
4792203134Sthompsa
4793203134Sthompsa	/* abort TSF synchronization */
4794203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4795203134Sthompsa	tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4796203134Sthompsa	    RT2860_TBTT_TIMER_EN);
4797203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4798203134Sthompsa
4799203134Sthompsa	/* clear RX WCID search table */
4800203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
4801203134Sthompsa	/* clear WCID attribute table */
4802203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
4803203134Sthompsa
4804209144Sthompsa	/* hostapd sets a key before init. So, don't clear it. */
4805209917Sthompsa	if (sc->cmdq_key_set != RUN_CMDQ_GO) {
4806209144Sthompsa		/* clear shared key table */
4807209144Sthompsa		run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
4808209144Sthompsa		/* clear shared key mode */
4809209144Sthompsa		run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
4810209144Sthompsa	}
4811209144Sthompsa
4812203134Sthompsa	run_read(sc, RT2860_US_CYC_CNT, &tmp);
4813203134Sthompsa	tmp = (tmp & ~0xff) | 0x1e;
4814203134Sthompsa	run_write(sc, RT2860_US_CYC_CNT, tmp);
4815203134Sthompsa
4816205042Sthompsa	if (sc->mac_rev != 0x0101)
4817203134Sthompsa		run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
4818203134Sthompsa
4819203134Sthompsa	run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
4820203134Sthompsa	run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
4821203134Sthompsa
4822203134Sthompsa	/* write vendor-specific BBP values (from EEPROM) */
4823208019Sthompsa	for (i = 0; i < 10; i++) {
4824203134Sthompsa		if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
4825203134Sthompsa			continue;
4826203134Sthompsa		run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
4827203134Sthompsa	}
4828203134Sthompsa
4829203134Sthompsa	/* select Main antenna for 1T1R devices */
4830203134Sthompsa	if (sc->rf_rev == RT3070_RF_3020)
4831203134Sthompsa		run_set_rx_antenna(sc, 0);
4832203134Sthompsa
4833203134Sthompsa	/* send LEDs operating mode to microcontroller */
4834203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
4835203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
4836203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
4837203134Sthompsa
4838205042Sthompsa	if (sc->mac_ver >= 0x3070)
4839205042Sthompsa		run_rt3070_rf_init(sc);
4840205042Sthompsa
4841203134Sthompsa	/* disable non-existing Rx chains */
4842203134Sthompsa	run_bbp_read(sc, 3, &bbp3);
4843203134Sthompsa	bbp3 &= ~(1 << 3 | 1 << 4);
4844203134Sthompsa	if (sc->nrxchains == 2)
4845203134Sthompsa		bbp3 |= 1 << 3;
4846203134Sthompsa	else if (sc->nrxchains == 3)
4847203134Sthompsa		bbp3 |= 1 << 4;
4848203134Sthompsa	run_bbp_write(sc, 3, bbp3);
4849203134Sthompsa
4850203134Sthompsa	/* disable non-existing Tx chains */
4851203134Sthompsa	run_bbp_read(sc, 1, &bbp1);
4852203134Sthompsa	if (sc->ntxchains == 1)
4853203134Sthompsa		bbp1 &= ~(1 << 3 | 1 << 4);
4854203134Sthompsa	run_bbp_write(sc, 1, bbp1);
4855203134Sthompsa
4856205042Sthompsa	if (sc->mac_ver >= 0x3070)
4857205042Sthompsa		run_rt3070_rf_setup(sc);
4858203134Sthompsa
4859203134Sthompsa	/* select default channel */
4860203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
4861203134Sthompsa
4862203134Sthompsa	/* setup initial protection mode */
4863218492Sbschmidt	run_updateprot_cb(ic);
4864203134Sthompsa
4865203134Sthompsa	/* turn radio LED on */
4866203134Sthompsa	run_set_leds(sc, RT2860_LED_RADIO);
4867203134Sthompsa
4868203134Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4869203134Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
4870208019Sthompsa	sc->cmdq_run = RUN_CMDQ_GO;
4871203134Sthompsa
4872209917Sthompsa	for (i = 0; i != RUN_N_XFER; i++)
4873203134Sthompsa		usbd_xfer_set_stall(sc->sc_xfer[i]);
4874203134Sthompsa
4875203134Sthompsa	usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
4876203134Sthompsa
4877203134Sthompsa	if (run_txrx_enable(sc) != 0)
4878203134Sthompsa		goto fail;
4879203134Sthompsa
4880203134Sthompsa	return;
4881203134Sthompsa
4882203134Sthompsafail:
4883203134Sthompsa	run_stop(sc);
4884203134Sthompsa}
4885203134Sthompsa
4886203134Sthompsastatic void
4887203134Sthompsarun_init(void *arg)
4888203134Sthompsa{
4889203134Sthompsa	struct run_softc *sc = arg;
4890203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4891203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4892203134Sthompsa
4893203134Sthompsa	RUN_LOCK(sc);
4894203134Sthompsa	run_init_locked(sc);
4895203134Sthompsa	RUN_UNLOCK(sc);
4896203134Sthompsa
4897203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4898208019Sthompsa		ieee80211_start_all(ic);
4899203134Sthompsa}
4900203134Sthompsa
4901203134Sthompsastatic void
4902203134Sthompsarun_stop(void *arg)
4903203134Sthompsa{
4904203134Sthompsa	struct run_softc *sc = (struct run_softc *)arg;
4905203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4906203134Sthompsa	uint32_t tmp;
4907203134Sthompsa	int i;
4908203134Sthompsa	int ntries;
4909203134Sthompsa
4910203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4911203134Sthompsa
4912203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4913203134Sthompsa		run_set_leds(sc, 0);	/* turn all LEDs off */
4914203134Sthompsa
4915203134Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4916203134Sthompsa
4917208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
4918209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set;
4919208019Sthompsa
4920203134Sthompsa	RUN_UNLOCK(sc);
4921203134Sthompsa
4922203134Sthompsa	for(i = 0; i < RUN_N_XFER; i++)
4923203134Sthompsa		usbd_transfer_drain(sc->sc_xfer[i]);
4924203134Sthompsa
4925203134Sthompsa	RUN_LOCK(sc);
4926203134Sthompsa
4927209917Sthompsa	if (sc->rx_m != NULL) {
4928203134Sthompsa		m_free(sc->rx_m);
4929203134Sthompsa		sc->rx_m = NULL;
4930203134Sthompsa	}
4931203134Sthompsa
4932203134Sthompsa	/* disable Tx/Rx */
4933203134Sthompsa	run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
4934203134Sthompsa	tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4935203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
4936203134Sthompsa
4937203134Sthompsa	/* wait for pending Tx to complete */
4938203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4939209917Sthompsa		if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
4940203134Sthompsa			DPRINTF("Cannot read Tx queue count\n");
4941203134Sthompsa			break;
4942203134Sthompsa		}
4943209917Sthompsa		if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
4944203134Sthompsa			DPRINTF("All Tx cleared\n");
4945203134Sthompsa			break;
4946203134Sthompsa		}
4947203134Sthompsa		run_delay(sc, 10);
4948203134Sthompsa	}
4949209917Sthompsa	if (ntries >= 100)
4950203134Sthompsa		DPRINTF("There are still pending Tx\n");
4951203134Sthompsa	run_delay(sc, 10);
4952203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
4953203134Sthompsa
4954203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
4955203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4956203134Sthompsa
4957203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
4958203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
4959203134Sthompsa
4960203134Sthompsa	return;
4961203134Sthompsa}
4962203134Sthompsa
4963203134Sthompsastatic void
4964203134Sthompsarun_delay(struct run_softc *sc, unsigned int ms)
4965203134Sthompsa{
4966203134Sthompsa	usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
4967203134Sthompsa	    &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
4968203134Sthompsa}
4969203134Sthompsa
4970203134Sthompsastatic device_method_t run_methods[] = {
4971203134Sthompsa	/* Device interface */
4972203134Sthompsa	DEVMETHOD(device_probe,		run_match),
4973203134Sthompsa	DEVMETHOD(device_attach,	run_attach),
4974203134Sthompsa	DEVMETHOD(device_detach,	run_detach),
4975246614Shselasky	DEVMETHOD_END
4976203134Sthompsa};
4977203134Sthompsa
4978203134Sthompsastatic driver_t run_driver = {
4979233774Shselasky	.name = "run",
4980233774Shselasky	.methods = run_methods,
4981233774Shselasky	.size = sizeof(struct run_softc)
4982203134Sthompsa};
4983203134Sthompsa
4984203134Sthompsastatic devclass_t run_devclass;
4985203134Sthompsa
4986203134SthompsaDRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0);
4987212122SthompsaMODULE_DEPEND(run, wlan, 1, 1, 1);
4988212122SthompsaMODULE_DEPEND(run, usb, 1, 1, 1);
4989212122SthompsaMODULE_DEPEND(run, firmware, 1, 1, 1);
4990212122SthompsaMODULE_VERSION(run, 1);
4991