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$");
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),
150257044Shselasky    RUN_DEV(BELKIN,		F6D4050V2),
151209918Sthompsa    RUN_DEV(BELKIN,		RT2870_1),
152209918Sthompsa    RUN_DEV(BELKIN,		RT2870_2),
153226534Shselasky    RUN_DEV(CISCOLINKSYS,	AE1000),
154209918Sthompsa    RUN_DEV(CISCOLINKSYS2,	RT3070),
155209918Sthompsa    RUN_DEV(CISCOLINKSYS3,	RT3070),
156209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_1),
157209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_2),
158209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_3),
159209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_4),
160209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_5),
161209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_6),
162209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_7),
163209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_8),
164209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT3070_1),
165209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT3070_2),
166209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	VIGORN61),
167209918Sthompsa    RUN_DEV(COREGA,		CGWLUSB300GNM),
168209918Sthompsa    RUN_DEV(COREGA,		RT2870_1),
169209918Sthompsa    RUN_DEV(COREGA,		RT2870_2),
170209918Sthompsa    RUN_DEV(COREGA,		RT2870_3),
171209918Sthompsa    RUN_DEV(COREGA,		RT3070),
172209918Sthompsa    RUN_DEV(CYBERTAN,		RT2870),
173209918Sthompsa    RUN_DEV(DLINK,		RT2870),
174209918Sthompsa    RUN_DEV(DLINK,		RT3072),
175255238Sbr    RUN_DEV(DLINK,		DWA127),
176209918Sthompsa    RUN_DEV(DLINK2,		DWA130),
177209918Sthompsa    RUN_DEV(DLINK2,		RT2870_1),
178209918Sthompsa    RUN_DEV(DLINK2,		RT2870_2),
179209918Sthompsa    RUN_DEV(DLINK2,		RT3070_1),
180209918Sthompsa    RUN_DEV(DLINK2,		RT3070_2),
181209918Sthompsa    RUN_DEV(DLINK2,		RT3070_3),
182209918Sthompsa    RUN_DEV(DLINK2,		RT3070_4),
183209918Sthompsa    RUN_DEV(DLINK2,		RT3070_5),
184209918Sthompsa    RUN_DEV(DLINK2,		RT3072),
185209918Sthompsa    RUN_DEV(DLINK2,		RT3072_1),
186209918Sthompsa    RUN_DEV(EDIMAX,		EW7717),
187209918Sthompsa    RUN_DEV(EDIMAX,		EW7718),
188209918Sthompsa    RUN_DEV(EDIMAX,		RT2870_1),
189209918Sthompsa    RUN_DEV(ENCORE,		RT3070_1),
190209918Sthompsa    RUN_DEV(ENCORE,		RT3070_2),
191209918Sthompsa    RUN_DEV(ENCORE,		RT3070_3),
192209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB31N),
193209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB32L),
194209918Sthompsa    RUN_DEV(GIGABYTE,		RT2870_1),
195209918Sthompsa    RUN_DEV(GIGASET,		RT3070_1),
196209918Sthompsa    RUN_DEV(GIGASET,		RT3070_2),
197209918Sthompsa    RUN_DEV(GUILLEMOT,		HWNU300),
198209918Sthompsa    RUN_DEV(HAWKING,		HWUN2),
199209918Sthompsa    RUN_DEV(HAWKING,		RT2870_1),
200209918Sthompsa    RUN_DEV(HAWKING,		RT2870_2),
201209918Sthompsa    RUN_DEV(HAWKING,		RT3070),
202209918Sthompsa    RUN_DEV(IODATA,		RT3072_1),
203209918Sthompsa    RUN_DEV(IODATA,		RT3072_2),
204209918Sthompsa    RUN_DEV(IODATA,		RT3072_3),
205209918Sthompsa    RUN_DEV(IODATA,		RT3072_4),
206209918Sthompsa    RUN_DEV(LINKSYS4,		RT3070),
207209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB100),
208209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB54GCV3),
209209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600N),
210209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600NV2),
211209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_1),
212209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_2),
213209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_3),
214230333Shselasky    RUN_DEV(LOGITEC,		LANW300NU2),
215238274Shrs    RUN_DEV(LOGITEC,		LANW150NU2),
216248458Shselasky    RUN_DEV(LOGITEC,		LANW300NU2S),
217209918Sthompsa    RUN_DEV(MELCO,		RT2870_1),
218209918Sthompsa    RUN_DEV(MELCO,		RT2870_2),
219209918Sthompsa    RUN_DEV(MELCO,		WLIUCAG300N),
220209918Sthompsa    RUN_DEV(MELCO,		WLIUCG300N),
221219257Sdaichi    RUN_DEV(MELCO,		WLIUCG301N),
222209918Sthompsa    RUN_DEV(MELCO,		WLIUCGN),
223227781Shselasky    RUN_DEV(MELCO,		WLIUCGNM),
224238274Shrs    RUN_DEV(MELCO,		WLIUCGNM2),
225209918Sthompsa    RUN_DEV(MOTOROLA4,		RT2770),
226209918Sthompsa    RUN_DEV(MOTOROLA4,		RT3070),
227209918Sthompsa    RUN_DEV(MSI,		RT3070_1),
228209918Sthompsa    RUN_DEV(MSI,		RT3070_2),
229209918Sthompsa    RUN_DEV(MSI,		RT3070_3),
230209918Sthompsa    RUN_DEV(MSI,		RT3070_4),
231209918Sthompsa    RUN_DEV(MSI,		RT3070_5),
232209918Sthompsa    RUN_DEV(MSI,		RT3070_6),
233209918Sthompsa    RUN_DEV(MSI,		RT3070_7),
234209918Sthompsa    RUN_DEV(MSI,		RT3070_8),
235209918Sthompsa    RUN_DEV(MSI,		RT3070_9),
236209918Sthompsa    RUN_DEV(MSI,		RT3070_10),
237209918Sthompsa    RUN_DEV(MSI,		RT3070_11),
238209918Sthompsa    RUN_DEV(OVISLINK,		RT3072),
239209918Sthompsa    RUN_DEV(PARA,		RT3070),
240209918Sthompsa    RUN_DEV(PEGATRON,		RT2870),
241209918Sthompsa    RUN_DEV(PEGATRON,		RT3070),
242209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_2),
243209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_3),
244209918Sthompsa    RUN_DEV(PHILIPS,		RT2870),
245209918Sthompsa    RUN_DEV(PLANEX2,		GWUS300MINIS),
246209918Sthompsa    RUN_DEV(PLANEX2,		GWUSMICRON),
247209918Sthompsa    RUN_DEV(PLANEX2,		RT2870),
248209918Sthompsa    RUN_DEV(PLANEX2,		RT3070),
249209918Sthompsa    RUN_DEV(QCOM,		RT2870),
250209918Sthompsa    RUN_DEV(QUANTA,		RT3070),
251209918Sthompsa    RUN_DEV(RALINK,		RT2070),
252209918Sthompsa    RUN_DEV(RALINK,		RT2770),
253209918Sthompsa    RUN_DEV(RALINK,		RT2870),
254209918Sthompsa    RUN_DEV(RALINK,		RT3070),
255209918Sthompsa    RUN_DEV(RALINK,		RT3071),
256209918Sthompsa    RUN_DEV(RALINK,		RT3072),
257209918Sthompsa    RUN_DEV(RALINK,		RT3370),
258209918Sthompsa    RUN_DEV(RALINK,		RT3572),
259209918Sthompsa    RUN_DEV(RALINK,		RT8070),
260226534Shselasky    RUN_DEV(SAMSUNG,		WIS09ABGN),
261209918Sthompsa    RUN_DEV(SAMSUNG2,		RT2870_1),
262209918Sthompsa    RUN_DEV(SENAO,		RT2870_1),
263209918Sthompsa    RUN_DEV(SENAO,		RT2870_2),
264209918Sthompsa    RUN_DEV(SENAO,		RT2870_3),
265209918Sthompsa    RUN_DEV(SENAO,		RT2870_4),
266209918Sthompsa    RUN_DEV(SENAO,		RT3070),
267209918Sthompsa    RUN_DEV(SENAO,		RT3071),
268209918Sthompsa    RUN_DEV(SENAO,		RT3072_1),
269209918Sthompsa    RUN_DEV(SENAO,		RT3072_2),
270209918Sthompsa    RUN_DEV(SENAO,		RT3072_3),
271209918Sthompsa    RUN_DEV(SENAO,		RT3072_4),
272209918Sthompsa    RUN_DEV(SENAO,		RT3072_5),
273209918Sthompsa    RUN_DEV(SITECOMEU,		RT2770),
274209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_1),
275209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_2),
276209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_3),
277209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_4),
278209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070),
279209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_2),
280209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_3),
281209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_4),
282209918Sthompsa    RUN_DEV(SITECOMEU,		RT3071),
283209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_1),
284209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_2),
285209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_3),
286209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_4),
287209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_5),
288209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_6),
289209918Sthompsa    RUN_DEV(SITECOMEU,		WL608),
290209918Sthompsa    RUN_DEV(SPARKLAN,		RT2870_1),
291209918Sthompsa    RUN_DEV(SPARKLAN,		RT3070),
292209918Sthompsa    RUN_DEV(SWEEX2,		LW153),
293209918Sthompsa    RUN_DEV(SWEEX2,		LW303),
294209918Sthompsa    RUN_DEV(SWEEX2,		LW313),
295209918Sthompsa    RUN_DEV(TOSHIBA,		RT3070),
296209918Sthompsa    RUN_DEV(UMEDIA,		RT2870_1),
297209918Sthompsa    RUN_DEV(ZCOM,		RT2870_1),
298209918Sthompsa    RUN_DEV(ZCOM,		RT2870_2),
299209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_1),
300209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_2),
301209918Sthompsa    RUN_DEV(ZINWELL,		RT3070),
302209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_1),
303209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_2),
304209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_1),
305209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_2),
306209918Sthompsa#undef RUN_DEV
307203134Sthompsa};
308203134Sthompsa
309203134Sthompsastatic device_probe_t	run_match;
310203134Sthompsastatic device_attach_t	run_attach;
311203134Sthompsastatic device_detach_t	run_detach;
312203134Sthompsa
313203134Sthompsastatic usb_callback_t	run_bulk_rx_callback;
314203134Sthompsastatic usb_callback_t	run_bulk_tx_callback0;
315203134Sthompsastatic usb_callback_t	run_bulk_tx_callback1;
316203134Sthompsastatic usb_callback_t	run_bulk_tx_callback2;
317203134Sthompsastatic usb_callback_t	run_bulk_tx_callback3;
318203134Sthompsastatic usb_callback_t	run_bulk_tx_callback4;
319203134Sthompsastatic usb_callback_t	run_bulk_tx_callback5;
320203134Sthompsa
321203134Sthompsastatic void	run_bulk_tx_callbackN(struct usb_xfer *xfer,
322203134Sthompsa		    usb_error_t error, unsigned int index);
323203134Sthompsastatic struct ieee80211vap *run_vap_create(struct ieee80211com *,
324228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
325228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
326228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
327203134Sthompsastatic void	run_vap_delete(struct ieee80211vap *);
328208019Sthompsastatic void	run_cmdq_cb(void *, int);
329203134Sthompsastatic void	run_setup_tx_list(struct run_softc *,
330203134Sthompsa		    struct run_endpoint_queue *);
331203134Sthompsastatic void	run_unsetup_tx_list(struct run_softc *,
332203134Sthompsa		    struct run_endpoint_queue *);
333203134Sthompsastatic int	run_load_microcode(struct run_softc *);
334203134Sthompsastatic int	run_reset(struct run_softc *);
335203134Sthompsastatic usb_error_t run_do_request(struct run_softc *,
336203134Sthompsa		    struct usb_device_request *, void *);
337203134Sthompsastatic int	run_read(struct run_softc *, uint16_t, uint32_t *);
338203134Sthompsastatic int	run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
339203134Sthompsastatic int	run_write_2(struct run_softc *, uint16_t, uint16_t);
340203134Sthompsastatic int	run_write(struct run_softc *, uint16_t, uint32_t);
341203134Sthompsastatic int	run_write_region_1(struct run_softc *, uint16_t,
342203134Sthompsa		    const uint8_t *, int);
343203134Sthompsastatic int	run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
344203134Sthompsastatic int	run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
345203134Sthompsastatic int	run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
346203134Sthompsastatic int	run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t);
347203134Sthompsastatic int	run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
348203134Sthompsastatic int	run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
349203134Sthompsastatic int	run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
350203134Sthompsastatic int	run_bbp_write(struct run_softc *, uint8_t, uint8_t);
351203134Sthompsastatic int	run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
352203134Sthompsastatic const char *run_get_rf(int);
353203134Sthompsastatic int	run_read_eeprom(struct run_softc *);
354203134Sthompsastatic struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
355203134Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
356203134Sthompsastatic int	run_media_change(struct ifnet *);
357203134Sthompsastatic int	run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
358203134Sthompsastatic int	run_wme_update(struct ieee80211com *);
359208019Sthompsastatic void	run_wme_update_cb(void *);
360203134Sthompsastatic void	run_key_update_begin(struct ieee80211vap *);
361203134Sthompsastatic void	run_key_update_end(struct ieee80211vap *);
362208019Sthompsastatic void	run_key_set_cb(void *);
363208019Sthompsastatic int	run_key_set(struct ieee80211vap *, struct ieee80211_key *,
364203134Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
365208019Sthompsastatic void	run_key_delete_cb(void *);
366208019Sthompsastatic int	run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
367206358Srpaulostatic void	run_ratectl_to(void *);
368206358Srpaulostatic void	run_ratectl_cb(void *, int);
369208019Sthompsastatic void	run_drain_fifo(void *);
370203134Sthompsastatic void	run_iter_func(void *, struct ieee80211_node *);
371208019Sthompsastatic void	run_newassoc_cb(void *);
372203134Sthompsastatic void	run_newassoc(struct ieee80211_node *, int);
373203134Sthompsastatic void	run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
374203134Sthompsastatic void	run_tx_free(struct run_endpoint_queue *pq,
375203134Sthompsa		    struct run_tx_data *, int);
376208019Sthompsastatic void	run_set_tx_desc(struct run_softc *, struct run_tx_data *);
377203134Sthompsastatic int	run_tx(struct run_softc *, struct mbuf *,
378203134Sthompsa		    struct ieee80211_node *);
379203134Sthompsastatic int	run_tx_mgt(struct run_softc *, struct mbuf *,
380203134Sthompsa		    struct ieee80211_node *);
381203134Sthompsastatic int	run_sendprot(struct run_softc *, const struct mbuf *,
382203134Sthompsa		    struct ieee80211_node *, int, int);
383203134Sthompsastatic int	run_tx_param(struct run_softc *, struct mbuf *,
384203134Sthompsa		    struct ieee80211_node *,
385203134Sthompsa		    const struct ieee80211_bpf_params *);
386203134Sthompsastatic int	run_raw_xmit(struct ieee80211_node *, struct mbuf *,
387203134Sthompsa		    const struct ieee80211_bpf_params *);
388203134Sthompsastatic void	run_start(struct ifnet *);
389203134Sthompsastatic int	run_ioctl(struct ifnet *, u_long, caddr_t);
390205042Sthompsastatic void	run_set_agc(struct run_softc *, uint8_t);
391203134Sthompsastatic void	run_select_chan_group(struct run_softc *, int);
392203134Sthompsastatic void	run_set_rx_antenna(struct run_softc *, int);
393203134Sthompsastatic void	run_rt2870_set_chan(struct run_softc *, u_int);
394203134Sthompsastatic void	run_rt3070_set_chan(struct run_softc *, u_int);
395205042Sthompsastatic void	run_rt3572_set_chan(struct run_softc *, u_int);
396203134Sthompsastatic int	run_set_chan(struct run_softc *, struct ieee80211_channel *);
397203134Sthompsastatic void	run_set_channel(struct ieee80211com *);
398203134Sthompsastatic void	run_scan_start(struct ieee80211com *);
399203134Sthompsastatic void	run_scan_end(struct ieee80211com *);
400203134Sthompsastatic void	run_update_beacon(struct ieee80211vap *, int);
401208019Sthompsastatic void	run_update_beacon_cb(void *);
402203134Sthompsastatic void	run_updateprot(struct ieee80211com *);
403218492Sbschmidtstatic void	run_updateprot_cb(void *);
404208019Sthompsastatic void	run_usb_timeout_cb(void *);
405203134Sthompsastatic void	run_reset_livelock(struct run_softc *);
406203134Sthompsastatic void	run_enable_tsf_sync(struct run_softc *);
407203134Sthompsastatic void	run_enable_mrr(struct run_softc *);
408203134Sthompsastatic void	run_set_txpreamble(struct run_softc *);
409203134Sthompsastatic void	run_set_basicrates(struct run_softc *);
410203134Sthompsastatic void	run_set_leds(struct run_softc *, uint16_t);
411203134Sthompsastatic void	run_set_bssid(struct run_softc *, const uint8_t *);
412203134Sthompsastatic void	run_set_macaddr(struct run_softc *, const uint8_t *);
413203134Sthompsastatic void	run_updateslot(struct ifnet *);
414218492Sbschmidtstatic void	run_updateslot_cb(void *);
415208019Sthompsastatic void	run_update_mcast(struct ifnet *);
416203134Sthompsastatic int8_t	run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
417203134Sthompsastatic void	run_update_promisc_locked(struct ifnet *);
418203134Sthompsastatic void	run_update_promisc(struct ifnet *);
419203134Sthompsastatic int	run_bbp_init(struct run_softc *);
420203134Sthompsastatic int	run_rt3070_rf_init(struct run_softc *);
421203134Sthompsastatic int	run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
422203134Sthompsa		    uint8_t *);
423205042Sthompsastatic void	run_rt3070_rf_setup(struct run_softc *);
424203134Sthompsastatic int	run_txrx_enable(struct run_softc *);
425203134Sthompsastatic void	run_init(void *);
426203134Sthompsastatic void	run_init_locked(struct run_softc *);
427203134Sthompsastatic void	run_stop(void *);
428203134Sthompsastatic void	run_delay(struct run_softc *, unsigned int);
429203134Sthompsa
430203134Sthompsastatic const struct {
431208019Sthompsa	uint16_t	reg;
432203134Sthompsa	uint32_t	val;
433203134Sthompsa} rt2870_def_mac[] = {
434203134Sthompsa	RT2870_DEF_MAC
435203134Sthompsa};
436203134Sthompsa
437203134Sthompsastatic const struct {
438203134Sthompsa	uint8_t	reg;
439203134Sthompsa	uint8_t	val;
440203134Sthompsa} rt2860_def_bbp[] = {
441203134Sthompsa	RT2860_DEF_BBP
442203134Sthompsa};
443203134Sthompsa
444203134Sthompsastatic const struct rfprog {
445203134Sthompsa	uint8_t		chan;
446203134Sthompsa	uint32_t	r1, r2, r3, r4;
447203134Sthompsa} rt2860_rf2850[] = {
448203134Sthompsa	RT2860_RF2850
449203134Sthompsa};
450203134Sthompsa
451203134Sthompsastruct {
452203134Sthompsa	uint8_t	n, r, k;
453205042Sthompsa} rt3070_freqs[] = {
454205042Sthompsa	RT3070_RF3052
455203134Sthompsa};
456203134Sthompsa
457203134Sthompsastatic const struct {
458203134Sthompsa	uint8_t	reg;
459203134Sthompsa	uint8_t	val;
460203134Sthompsa} rt3070_def_rf[] = {
461203134Sthompsa	RT3070_DEF_RF
462205042Sthompsa},rt3572_def_rf[] = {
463205042Sthompsa	RT3572_DEF_RF
464203134Sthompsa};
465203134Sthompsa
466203134Sthompsastatic const struct usb_config run_config[RUN_N_XFER] = {
467203134Sthompsa    [RUN_BULK_TX_BE] = {
468203134Sthompsa	.type = UE_BULK,
469203134Sthompsa	.endpoint = UE_ADDR_ANY,
470203134Sthompsa	.ep_index = 0,
471203134Sthompsa	.direction = UE_DIR_OUT,
472203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
473203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
474203134Sthompsa	.callback = run_bulk_tx_callback0,
475203134Sthompsa	.timeout = 5000,	/* ms */
476203134Sthompsa    },
477203134Sthompsa    [RUN_BULK_TX_BK] = {
478203134Sthompsa	.type = UE_BULK,
479203134Sthompsa	.endpoint = UE_ADDR_ANY,
480203134Sthompsa	.direction = UE_DIR_OUT,
481203134Sthompsa	.ep_index = 1,
482203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
483203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
484203134Sthompsa	.callback = run_bulk_tx_callback1,
485203134Sthompsa	.timeout = 5000,	/* ms */
486203134Sthompsa    },
487203134Sthompsa    [RUN_BULK_TX_VI] = {
488203134Sthompsa	.type = UE_BULK,
489203134Sthompsa	.endpoint = UE_ADDR_ANY,
490203134Sthompsa	.direction = UE_DIR_OUT,
491203134Sthompsa	.ep_index = 2,
492203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
493203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
494203134Sthompsa	.callback = run_bulk_tx_callback2,
495203134Sthompsa	.timeout = 5000,	/* ms */
496203134Sthompsa    },
497203134Sthompsa    [RUN_BULK_TX_VO] = {
498203134Sthompsa	.type = UE_BULK,
499203134Sthompsa	.endpoint = UE_ADDR_ANY,
500203134Sthompsa	.direction = UE_DIR_OUT,
501203134Sthompsa	.ep_index = 3,
502203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
503203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
504203134Sthompsa	.callback = run_bulk_tx_callback3,
505203134Sthompsa	.timeout = 5000,	/* ms */
506203134Sthompsa    },
507203134Sthompsa    [RUN_BULK_TX_HCCA] = {
508203134Sthompsa	.type = UE_BULK,
509203134Sthompsa	.endpoint = UE_ADDR_ANY,
510203134Sthompsa	.direction = UE_DIR_OUT,
511203134Sthompsa	.ep_index = 4,
512203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
513203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
514203134Sthompsa	.callback = run_bulk_tx_callback4,
515203134Sthompsa	.timeout = 5000,	/* ms */
516203134Sthompsa    },
517203134Sthompsa    [RUN_BULK_TX_PRIO] = {
518203134Sthompsa	.type = UE_BULK,
519203134Sthompsa	.endpoint = UE_ADDR_ANY,
520203134Sthompsa	.direction = UE_DIR_OUT,
521203134Sthompsa	.ep_index = 5,
522203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
523203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
524203134Sthompsa	.callback = run_bulk_tx_callback5,
525203134Sthompsa	.timeout = 5000,	/* ms */
526203134Sthompsa    },
527203134Sthompsa    [RUN_BULK_RX] = {
528203134Sthompsa	.type = UE_BULK,
529203134Sthompsa	.endpoint = UE_ADDR_ANY,
530203134Sthompsa	.direction = UE_DIR_IN,
531203134Sthompsa	.bufsize = RUN_MAX_RXSZ,
532203134Sthompsa	.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
533203134Sthompsa	.callback = run_bulk_rx_callback,
534203134Sthompsa    }
535203134Sthompsa};
536203134Sthompsa
537220235Skevlostatic int
538203134Sthompsarun_match(device_t self)
539203134Sthompsa{
540203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
541203134Sthompsa
542203134Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
543203134Sthompsa		return (ENXIO);
544203134Sthompsa	if (uaa->info.bConfigIndex != 0)
545203134Sthompsa		return (ENXIO);
546203134Sthompsa	if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
547203134Sthompsa		return (ENXIO);
548203134Sthompsa
549203134Sthompsa	return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
550203134Sthompsa}
551203134Sthompsa
552203134Sthompsastatic int
553203134Sthompsarun_attach(device_t self)
554203134Sthompsa{
555203134Sthompsa	struct run_softc *sc = device_get_softc(self);
556203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
557203134Sthompsa	struct ieee80211com *ic;
558203134Sthompsa	struct ifnet *ifp;
559205042Sthompsa	uint32_t ver;
560203134Sthompsa	int i, ntries, error;
561203134Sthompsa	uint8_t iface_index, bands;
562203134Sthompsa
563203134Sthompsa	device_set_usb_desc(self);
564203134Sthompsa	sc->sc_udev = uaa->device;
565203134Sthompsa	sc->sc_dev = self;
566203134Sthompsa
567203134Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
568203134Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
569203134Sthompsa
570203134Sthompsa	iface_index = RT2860_IFACE_INDEX;
571208019Sthompsa
572203134Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
573203134Sthompsa	    sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
574203134Sthompsa	if (error) {
575205042Sthompsa		device_printf(self, "could not allocate USB transfers, "
576203134Sthompsa		    "err=%s\n", usbd_errstr(error));
577203134Sthompsa		goto detach;
578203134Sthompsa	}
579203134Sthompsa
580203134Sthompsa	RUN_LOCK(sc);
581203134Sthompsa
582203134Sthompsa	/* wait for the chip to settle */
583203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
584209917Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
585203134Sthompsa			RUN_UNLOCK(sc);
586203134Sthompsa			goto detach;
587203134Sthompsa		}
588205042Sthompsa		if (ver != 0 && ver != 0xffffffff)
589203134Sthompsa			break;
590203134Sthompsa		run_delay(sc, 10);
591203134Sthompsa	}
592203134Sthompsa	if (ntries == 100) {
593203138Sthompsa		device_printf(sc->sc_dev,
594203138Sthompsa		    "timeout waiting for NIC to initialize\n");
595203134Sthompsa		RUN_UNLOCK(sc);
596203134Sthompsa		goto detach;
597203134Sthompsa	}
598205042Sthompsa	sc->mac_ver = ver >> 16;
599205042Sthompsa	sc->mac_rev = ver & 0xffff;
600203134Sthompsa
601203134Sthompsa	/* retrieve RF rev. no and various other things from EEPROM */
602203134Sthompsa	run_read_eeprom(sc);
603203134Sthompsa
604203138Sthompsa	device_printf(sc->sc_dev,
605203138Sthompsa	    "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
606205042Sthompsa	    sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
607203138Sthompsa	    sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
608203134Sthompsa
609203134Sthompsa	RUN_UNLOCK(sc);
610203134Sthompsa
611203134Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
612220235Skevlo	if (ifp == NULL) {
613203138Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
614203134Sthompsa		goto detach;
615203134Sthompsa	}
616203134Sthompsa	ic = ifp->if_l2com;
617203134Sthompsa
618203134Sthompsa	ifp->if_softc = sc;
619203134Sthompsa	if_initname(ifp, "run", device_get_unit(sc->sc_dev));
620203134Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
621203134Sthompsa	ifp->if_init = run_init;
622203134Sthompsa	ifp->if_ioctl = run_ioctl;
623203134Sthompsa	ifp->if_start = run_start;
624207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
625207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
626203134Sthompsa	IFQ_SET_READY(&ifp->if_snd);
627203134Sthompsa
628203134Sthompsa	ic->ic_ifp = ifp;
629203134Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
630203134Sthompsa	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
631208019Sthompsa
632203134Sthompsa	/* set device capabilities */
633203134Sthompsa	ic->ic_caps =
634203134Sthompsa	    IEEE80211_C_STA |		/* station mode supported */
635203134Sthompsa	    IEEE80211_C_MONITOR |	/* monitor mode supported */
636203134Sthompsa	    IEEE80211_C_IBSS |
637203134Sthompsa	    IEEE80211_C_HOSTAP |
638208019Sthompsa	    IEEE80211_C_WDS |		/* 4-address traffic works */
639208019Sthompsa	    IEEE80211_C_MBSS |
640203134Sthompsa	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
641203134Sthompsa	    IEEE80211_C_SHSLOT |	/* short slot time supported */
642203134Sthompsa	    IEEE80211_C_WME |		/* WME */
643214894Sbschmidt	    IEEE80211_C_WPA;		/* WPA1|WPA2(RSN) */
644203134Sthompsa
645203134Sthompsa	ic->ic_cryptocaps =
646203134Sthompsa	    IEEE80211_CRYPTO_WEP |
647203134Sthompsa	    IEEE80211_CRYPTO_AES_CCM |
648203134Sthompsa	    IEEE80211_CRYPTO_TKIPMIC |
649203134Sthompsa	    IEEE80211_CRYPTO_TKIP;
650203134Sthompsa
651203134Sthompsa	ic->ic_flags |= IEEE80211_F_DATAPAD;
652203134Sthompsa	ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
653203134Sthompsa
654203134Sthompsa	bands = 0;
655203134Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
656203134Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
657203134Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
658203134Sthompsa
659203134Sthompsa	/*
660203134Sthompsa	 * Do this by own because h/w supports
661203134Sthompsa	 * more channels than ieee80211_init_channels()
662203134Sthompsa	 */
663205042Sthompsa	if (sc->rf_rev == RT2860_RF_2750 ||
664205042Sthompsa	    sc->rf_rev == RT2860_RF_2850 ||
665205042Sthompsa	    sc->rf_rev == RT3070_RF_3052) {
666203134Sthompsa		/* set supported .11a rates */
667233774Shselasky		for (i = 14; i < N(rt2860_rf2850); i++) {
668203134Sthompsa			uint8_t chan = rt2860_rf2850[i].chan;
669203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_freq =
670203134Sthompsa			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A);
671203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_ieee = chan;
672203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A;
673203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_extieee = 0;
674203134Sthompsa			ic->ic_nchans++;
675203134Sthompsa		}
676203134Sthompsa	}
677203134Sthompsa
678203134Sthompsa	ieee80211_ifattach(ic, sc->sc_bssid);
679203134Sthompsa
680203134Sthompsa	ic->ic_scan_start = run_scan_start;
681203134Sthompsa	ic->ic_scan_end = run_scan_end;
682203134Sthompsa	ic->ic_set_channel = run_set_channel;
683203134Sthompsa	ic->ic_node_alloc = run_node_alloc;
684203134Sthompsa	ic->ic_newassoc = run_newassoc;
685218492Sbschmidt	ic->ic_updateslot = run_updateslot;
686208019Sthompsa	ic->ic_update_mcast = run_update_mcast;
687203134Sthompsa	ic->ic_wme.wme_update = run_wme_update;
688203134Sthompsa	ic->ic_raw_xmit = run_raw_xmit;
689203134Sthompsa	ic->ic_update_promisc = run_update_promisc;
690203134Sthompsa
691203134Sthompsa	ic->ic_vap_create = run_vap_create;
692203134Sthompsa	ic->ic_vap_delete = run_vap_delete;
693203134Sthompsa
694203134Sthompsa	ieee80211_radiotap_attach(ic,
695203134Sthompsa	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
696203134Sthompsa		RUN_TX_RADIOTAP_PRESENT,
697203134Sthompsa	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
698203134Sthompsa		RUN_RX_RADIOTAP_PRESENT);
699203134Sthompsa
700208019Sthompsa	TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
701208019Sthompsa	TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
702208019Sthompsa	callout_init((struct callout *)&sc->ratectl_ch, 1);
703208019Sthompsa
704203134Sthompsa	if (bootverbose)
705203134Sthompsa		ieee80211_announce(ic);
706203134Sthompsa
707209917Sthompsa	return (0);
708203134Sthompsa
709203134Sthompsadetach:
710203134Sthompsa	run_detach(self);
711209917Sthompsa	return (ENXIO);
712203134Sthompsa}
713203134Sthompsa
714203134Sthompsastatic int
715203134Sthompsarun_detach(device_t self)
716203134Sthompsa{
717203134Sthompsa	struct run_softc *sc = device_get_softc(self);
718203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
719203134Sthompsa	struct ieee80211com *ic;
720203134Sthompsa	int i;
721203134Sthompsa
722246614Shselasky	RUN_LOCK(sc);
723246614Shselasky	sc->sc_detached = 1;
724246614Shselasky	RUN_UNLOCK(sc);
725246614Shselasky
726203134Sthompsa	/* stop all USB transfers */
727203134Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
728203134Sthompsa
729203134Sthompsa	RUN_LOCK(sc);
730209144Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
731209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
732209144Sthompsa
733203134Sthompsa	/* free TX list, if any */
734203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
735203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
736203134Sthompsa	RUN_UNLOCK(sc);
737203134Sthompsa
738203134Sthompsa	if (ifp) {
739203134Sthompsa		ic = ifp->if_l2com;
740208019Sthompsa		/* drain tasks */
741208019Sthompsa		usb_callout_drain(&sc->ratectl_ch);
742208019Sthompsa		ieee80211_draintask(ic, &sc->cmdq_task);
743208019Sthompsa		ieee80211_draintask(ic, &sc->ratectl_task);
744203134Sthompsa		ieee80211_ifdetach(ic);
745203134Sthompsa		if_free(ifp);
746203134Sthompsa	}
747203134Sthompsa
748203134Sthompsa	mtx_destroy(&sc->sc_mtx);
749203134Sthompsa
750203134Sthompsa	return (0);
751203134Sthompsa}
752203134Sthompsa
753203134Sthompsastatic struct ieee80211vap *
754228621Sbschmidtrun_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
755228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
756203134Sthompsa    const uint8_t bssid[IEEE80211_ADDR_LEN],
757203134Sthompsa    const uint8_t mac[IEEE80211_ADDR_LEN])
758203134Sthompsa{
759208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
760208019Sthompsa	struct run_softc *sc = ifp->if_softc;
761203134Sthompsa	struct run_vap *rvp;
762203134Sthompsa	struct ieee80211vap *vap;
763208019Sthompsa	int i;
764203134Sthompsa
765209917Sthompsa	if (sc->rvp_cnt >= RUN_VAP_MAX) {
766208019Sthompsa		if_printf(ifp, "number of VAPs maxed out\n");
767209917Sthompsa		return (NULL);
768208019Sthompsa	}
769208019Sthompsa
770208019Sthompsa	switch (opmode) {
771208019Sthompsa	case IEEE80211_M_STA:
772208019Sthompsa		/* enable s/w bmiss handling for sta mode */
773208019Sthompsa		flags |= IEEE80211_CLONE_NOBEACONS;
774208019Sthompsa		/* fall though */
775208019Sthompsa	case IEEE80211_M_IBSS:
776208019Sthompsa	case IEEE80211_M_MONITOR:
777208019Sthompsa	case IEEE80211_M_HOSTAP:
778208019Sthompsa	case IEEE80211_M_MBSS:
779208019Sthompsa		/* other than WDS vaps, only one at a time */
780208019Sthompsa		if (!TAILQ_EMPTY(&ic->ic_vaps))
781209917Sthompsa			return (NULL);
782208019Sthompsa		break;
783208019Sthompsa	case IEEE80211_M_WDS:
784208019Sthompsa		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
785208019Sthompsa			if(vap->iv_opmode != IEEE80211_M_HOSTAP)
786208019Sthompsa				continue;
787208019Sthompsa			/* WDS vap's always share the local mac address. */
788208019Sthompsa			flags &= ~IEEE80211_CLONE_BSSID;
789208019Sthompsa			break;
790208019Sthompsa		}
791209917Sthompsa		if (vap == NULL) {
792208019Sthompsa			if_printf(ifp, "wds only supported in ap mode\n");
793209917Sthompsa			return (NULL);
794208019Sthompsa		}
795208019Sthompsa		break;
796208019Sthompsa	default:
797208019Sthompsa		if_printf(ifp, "unknown opmode %d\n", opmode);
798209917Sthompsa		return (NULL);
799208019Sthompsa	}
800208019Sthompsa
801208019Sthompsa	rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
802203134Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
803203134Sthompsa	if (rvp == NULL)
804209917Sthompsa		return (NULL);
805203134Sthompsa	vap = &rvp->vap;
806208019Sthompsa	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
807203134Sthompsa
808203134Sthompsa	vap->iv_key_update_begin = run_key_update_begin;
809203134Sthompsa	vap->iv_key_update_end = run_key_update_end;
810203134Sthompsa	vap->iv_update_beacon = run_update_beacon;
811208019Sthompsa	vap->iv_max_aid = RT2870_WCID_MAX;
812208019Sthompsa	/*
813208019Sthompsa	 * To delete the right key from h/w, we need wcid.
814208019Sthompsa	 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
815208019Sthompsa	 * and matching wcid will be written into there. So, cast
816208019Sthompsa	 * some spells to remove 'const' from ieee80211_key{}
817208019Sthompsa	 */
818208019Sthompsa	vap->iv_key_delete = (void *)run_key_delete;
819208019Sthompsa	vap->iv_key_set = (void *)run_key_set;
820203134Sthompsa
821203134Sthompsa	/* override state transition machine */
822203134Sthompsa	rvp->newstate = vap->iv_newstate;
823203134Sthompsa	vap->iv_newstate = run_newstate;
824203134Sthompsa
825206358Srpaulo	ieee80211_ratectl_init(vap);
826206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
827203134Sthompsa
828203134Sthompsa	/* complete setup */
829203134Sthompsa	ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
830208019Sthompsa
831208019Sthompsa	/* make sure id is always unique */
832209917Sthompsa	for (i = 0; i < RUN_VAP_MAX; i++) {
833208019Sthompsa		if((sc->rvp_bmap & 1 << i) == 0){
834208019Sthompsa			sc->rvp_bmap |= 1 << i;
835208019Sthompsa			rvp->rvp_id = i;
836208019Sthompsa			break;
837208019Sthompsa		}
838208019Sthompsa	}
839209917Sthompsa	if (sc->rvp_cnt++ == 0)
840208019Sthompsa		ic->ic_opmode = opmode;
841208019Sthompsa
842209917Sthompsa	if (opmode == IEEE80211_M_HOSTAP)
843209144Sthompsa		sc->cmdq_run = RUN_CMDQ_GO;
844209144Sthompsa
845208019Sthompsa	DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
846208019Sthompsa	    rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
847208019Sthompsa
848209917Sthompsa	return (vap);
849203134Sthompsa}
850203134Sthompsa
851203134Sthompsastatic void
852203134Sthompsarun_vap_delete(struct ieee80211vap *vap)
853203134Sthompsa{
854203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
855203134Sthompsa	struct ifnet *ifp;
856203134Sthompsa	struct ieee80211com *ic;
857203134Sthompsa	struct run_softc *sc;
858208019Sthompsa	uint8_t rvp_id;
859203134Sthompsa
860209917Sthompsa	if (vap == NULL)
861203134Sthompsa		return;
862203134Sthompsa
863203134Sthompsa	ic = vap->iv_ic;
864203134Sthompsa	ifp = ic->ic_ifp;
865203134Sthompsa
866203134Sthompsa	sc = ifp->if_softc;
867203134Sthompsa
868205042Sthompsa	RUN_LOCK(sc);
869208019Sthompsa
870218492Sbschmidt	m_freem(rvp->beacon_mbuf);
871218492Sbschmidt	rvp->beacon_mbuf = NULL;
872218492Sbschmidt
873208019Sthompsa	rvp_id = rvp->rvp_id;
874208019Sthompsa	sc->ratectl_run &= ~(1 << rvp_id);
875208019Sthompsa	sc->rvp_bmap &= ~(1 << rvp_id);
876208019Sthompsa	run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
877208019Sthompsa	run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
878208019Sthompsa	--sc->rvp_cnt;
879208019Sthompsa
880208019Sthompsa	DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
881208019Sthompsa	    vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
882208019Sthompsa
883205042Sthompsa	RUN_UNLOCK(sc);
884203134Sthompsa
885206358Srpaulo	ieee80211_ratectl_deinit(vap);
886203134Sthompsa	ieee80211_vap_detach(vap);
887203134Sthompsa	free(rvp, M_80211_VAP);
888203134Sthompsa}
889203134Sthompsa
890208019Sthompsa/*
891208019Sthompsa * There are numbers of functions need to be called in context thread.
892208019Sthompsa * Rather than creating taskqueue event for each of those functions,
893208019Sthompsa * here is all-for-one taskqueue callback function. This function
894208019Sthompsa * gurantees deferred functions are executed in the same order they
895208019Sthompsa * were enqueued.
896208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[].
897208019Sthompsa */
898203134Sthompsastatic void
899208019Sthompsarun_cmdq_cb(void *arg, int pending)
900208019Sthompsa{
901208019Sthompsa	struct run_softc *sc = arg;
902208019Sthompsa	uint8_t i;
903208019Sthompsa
904208019Sthompsa	/* call cmdq[].func locked */
905208019Sthompsa	RUN_LOCK(sc);
906209917Sthompsa	for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
907209917Sthompsa	    i = sc->cmdq_exec, pending--) {
908208019Sthompsa		DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
909209917Sthompsa		if (sc->cmdq_run == RUN_CMDQ_GO) {
910208019Sthompsa			/*
911208019Sthompsa			 * If arg0 is NULL, callback func needs more
912208019Sthompsa			 * than one arg. So, pass ptr to cmdq struct.
913208019Sthompsa			 */
914209917Sthompsa			if (sc->cmdq[i].arg0)
915208019Sthompsa				sc->cmdq[i].func(sc->cmdq[i].arg0);
916208019Sthompsa			else
917208019Sthompsa				sc->cmdq[i].func(&sc->cmdq[i]);
918208019Sthompsa		}
919208019Sthompsa		sc->cmdq[i].arg0 = NULL;
920208019Sthompsa		sc->cmdq[i].func = NULL;
921208019Sthompsa		sc->cmdq_exec++;
922208019Sthompsa		sc->cmdq_exec &= RUN_CMDQ_MASQ;
923208019Sthompsa	}
924208019Sthompsa	RUN_UNLOCK(sc);
925208019Sthompsa}
926208019Sthompsa
927208019Sthompsastatic void
928203134Sthompsarun_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
929203134Sthompsa{
930203134Sthompsa	struct run_tx_data *data;
931203134Sthompsa
932203134Sthompsa	memset(pq, 0, sizeof(*pq));
933203134Sthompsa
934203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
935203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
936203134Sthompsa
937203134Sthompsa	for (data = &pq->tx_data[0];
938203134Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
939203134Sthompsa		data->sc = sc;
940203134Sthompsa		STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
941203134Sthompsa	}
942203134Sthompsa	pq->tx_nfree = RUN_TX_RING_COUNT;
943203134Sthompsa}
944203134Sthompsa
945203134Sthompsastatic void
946203134Sthompsarun_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
947203134Sthompsa{
948203134Sthompsa	struct run_tx_data *data;
949203134Sthompsa
950203134Sthompsa	/* make sure any subsequent use of the queues will fail */
951203134Sthompsa	pq->tx_nfree = 0;
952203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
953203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
954203134Sthompsa
955203134Sthompsa	/* free up all node references and mbufs */
956203134Sthompsa	for (data = &pq->tx_data[0];
957209917Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
958203134Sthompsa		if (data->m != NULL) {
959203134Sthompsa			m_freem(data->m);
960203134Sthompsa			data->m = NULL;
961203134Sthompsa		}
962203134Sthompsa		if (data->ni != NULL) {
963203134Sthompsa			ieee80211_free_node(data->ni);
964203134Sthompsa			data->ni = NULL;
965203134Sthompsa		}
966203134Sthompsa	}
967203134Sthompsa}
968203134Sthompsa
969220235Skevlostatic int
970203134Sthompsarun_load_microcode(struct run_softc *sc)
971203134Sthompsa{
972203134Sthompsa	usb_device_request_t req;
973203137Sthompsa	const struct firmware *fw;
974203134Sthompsa	const u_char *base;
975203134Sthompsa	uint32_t tmp;
976203134Sthompsa	int ntries, error;
977203134Sthompsa	const uint64_t *temp;
978203134Sthompsa	uint64_t bytes;
979203134Sthompsa
980205042Sthompsa	RUN_UNLOCK(sc);
981203137Sthompsa	fw = firmware_get("runfw");
982205042Sthompsa	RUN_LOCK(sc);
983209917Sthompsa	if (fw == NULL) {
984203138Sthompsa		device_printf(sc->sc_dev,
985203138Sthompsa		    "failed loadfirmware of file %s\n", "runfw");
986203134Sthompsa		return ENOENT;
987203134Sthompsa	}
988203134Sthompsa
989203137Sthompsa	if (fw->datasize != 8192) {
990203138Sthompsa		device_printf(sc->sc_dev,
991203138Sthompsa		    "invalid firmware size (should be 8KB)\n");
992203137Sthompsa		error = EINVAL;
993203137Sthompsa		goto fail;
994203134Sthompsa	}
995203134Sthompsa
996203134Sthompsa	/*
997203134Sthompsa	 * RT3071/RT3072 use a different firmware
998203134Sthompsa	 * run-rt2870 (8KB) contains both,
999203134Sthompsa	 * first half (4KB) is for rt2870,
1000203134Sthompsa	 * last half is for rt3071.
1001203134Sthompsa	 */
1002203137Sthompsa	base = fw->data;
1003205042Sthompsa	if ((sc->mac_ver) != 0x2860 &&
1004205042Sthompsa	    (sc->mac_ver) != 0x2872 &&
1005209917Sthompsa	    (sc->mac_ver) != 0x3070) {
1006203134Sthompsa		base += 4096;
1007205042Sthompsa	}
1008203134Sthompsa
1009203134Sthompsa	/* cheap sanity check */
1010203137Sthompsa	temp = fw->data;
1011203134Sthompsa	bytes = *temp;
1012209917Sthompsa	if (bytes != be64toh(0xffffff0210280210)) {
1013203138Sthompsa		device_printf(sc->sc_dev, "firmware checksum failed\n");
1014203137Sthompsa		error = EINVAL;
1015203137Sthompsa		goto fail;
1016203137Sthompsa	}
1017203134Sthompsa
1018203134Sthompsa	run_read(sc, RT2860_ASIC_VER_ID, &tmp);
1019203134Sthompsa	/* write microcode image */
1020203134Sthompsa	run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1021203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1022203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1023203134Sthompsa
1024203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1025203134Sthompsa	req.bRequest = RT2870_RESET;
1026203134Sthompsa	USETW(req.wValue, 8);
1027203134Sthompsa	USETW(req.wIndex, 0);
1028203134Sthompsa	USETW(req.wLength, 0);
1029220235Skevlo	if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL))
1030220235Skevlo	    != 0) {
1031203138Sthompsa		device_printf(sc->sc_dev, "firmware reset failed\n");
1032203137Sthompsa		goto fail;
1033203137Sthompsa	}
1034203134Sthompsa
1035203134Sthompsa	run_delay(sc, 10);
1036203134Sthompsa
1037203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
1038205042Sthompsa	if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1039203137Sthompsa		goto fail;
1040203134Sthompsa
1041203134Sthompsa	/* wait until microcontroller is ready */
1042203134Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
1043203137Sthompsa		if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1044203137Sthompsa			goto fail;
1045203137Sthompsa		}
1046203134Sthompsa		if (tmp & RT2860_MCU_READY)
1047203134Sthompsa			break;
1048203134Sthompsa		run_delay(sc, 10);
1049203134Sthompsa	}
1050203134Sthompsa	if (ntries == 1000) {
1051203138Sthompsa		device_printf(sc->sc_dev,
1052203138Sthompsa		    "timeout waiting for MCU to initialize\n");
1053203137Sthompsa		error = ETIMEDOUT;
1054203137Sthompsa		goto fail;
1055203134Sthompsa	}
1056233283Sbschmidt	device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n",
1057233283Sbschmidt	    (base == fw->data) ? "RT2870" : "RT3071",
1058233283Sbschmidt	    *(base + 4092), *(base + 4093));
1059203134Sthompsa
1060203137Sthompsafail:
1061203137Sthompsa	firmware_put(fw, FIRMWARE_UNLOAD);
1062203137Sthompsa	return (error);
1063203134Sthompsa}
1064203134Sthompsa
1065203134Sthompsaint
1066203134Sthompsarun_reset(struct run_softc *sc)
1067203134Sthompsa{
1068203134Sthompsa	usb_device_request_t req;
1069203134Sthompsa
1070203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1071203134Sthompsa	req.bRequest = RT2870_RESET;
1072203134Sthompsa	USETW(req.wValue, 1);
1073203134Sthompsa	USETW(req.wIndex, 0);
1074203134Sthompsa	USETW(req.wLength, 0);
1075209917Sthompsa	return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1076203134Sthompsa}
1077203134Sthompsa
1078203134Sthompsastatic usb_error_t
1079203134Sthompsarun_do_request(struct run_softc *sc,
1080203134Sthompsa    struct usb_device_request *req, void *data)
1081203134Sthompsa{
1082203134Sthompsa	usb_error_t err;
1083203134Sthompsa	int ntries = 10;
1084203134Sthompsa
1085203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1086203134Sthompsa
1087203134Sthompsa	while (ntries--) {
1088203134Sthompsa		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1089203134Sthompsa		    req, data, 0, NULL, 250 /* ms */);
1090203134Sthompsa		if (err == 0)
1091203134Sthompsa			break;
1092203134Sthompsa		DPRINTFN(1, "Control request failed, %s (retrying)\n",
1093203134Sthompsa		    usbd_errstr(err));
1094203134Sthompsa		run_delay(sc, 10);
1095203134Sthompsa	}
1096203134Sthompsa	return (err);
1097203134Sthompsa}
1098203134Sthompsa
1099203134Sthompsastatic int
1100203134Sthompsarun_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1101203134Sthompsa{
1102203134Sthompsa	uint32_t tmp;
1103203134Sthompsa	int error;
1104203134Sthompsa
1105203134Sthompsa	error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1106203134Sthompsa	if (error == 0)
1107203134Sthompsa		*val = le32toh(tmp);
1108203134Sthompsa	else
1109203134Sthompsa		*val = 0xffffffff;
1110209917Sthompsa	return (error);
1111203134Sthompsa}
1112203134Sthompsa
1113203134Sthompsastatic int
1114203134Sthompsarun_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1115203134Sthompsa{
1116203134Sthompsa	usb_device_request_t req;
1117203134Sthompsa
1118203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1119203134Sthompsa	req.bRequest = RT2870_READ_REGION_1;
1120203134Sthompsa	USETW(req.wValue, 0);
1121203134Sthompsa	USETW(req.wIndex, reg);
1122203134Sthompsa	USETW(req.wLength, len);
1123203134Sthompsa
1124209917Sthompsa	return (run_do_request(sc, &req, buf));
1125203134Sthompsa}
1126203134Sthompsa
1127203134Sthompsastatic int
1128203134Sthompsarun_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1129203134Sthompsa{
1130203134Sthompsa	usb_device_request_t req;
1131203134Sthompsa
1132203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1133203134Sthompsa	req.bRequest = RT2870_WRITE_2;
1134203134Sthompsa	USETW(req.wValue, val);
1135203134Sthompsa	USETW(req.wIndex, reg);
1136203134Sthompsa	USETW(req.wLength, 0);
1137203134Sthompsa
1138209917Sthompsa	return (run_do_request(sc, &req, NULL));
1139203134Sthompsa}
1140203134Sthompsa
1141203134Sthompsastatic int
1142203134Sthompsarun_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1143203134Sthompsa{
1144203134Sthompsa	int error;
1145203134Sthompsa
1146203134Sthompsa	if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1147203134Sthompsa		error = run_write_2(sc, reg + 2, val >> 16);
1148209917Sthompsa	return (error);
1149203134Sthompsa}
1150203134Sthompsa
1151203134Sthompsastatic int
1152203134Sthompsarun_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1153203134Sthompsa    int len)
1154203134Sthompsa{
1155203134Sthompsa#if 1
1156203134Sthompsa	int i, error = 0;
1157203134Sthompsa	/*
1158203134Sthompsa	 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1159203134Sthompsa	 * We thus issue multiple WRITE_2 commands instead.
1160203134Sthompsa	 */
1161203134Sthompsa	KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1162203134Sthompsa	for (i = 0; i < len && error == 0; i += 2)
1163203134Sthompsa		error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1164209917Sthompsa	return (error);
1165203134Sthompsa#else
1166203134Sthompsa	usb_device_request_t req;
1167203134Sthompsa
1168203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1169203134Sthompsa	req.bRequest = RT2870_WRITE_REGION_1;
1170203134Sthompsa	USETW(req.wValue, 0);
1171203134Sthompsa	USETW(req.wIndex, reg);
1172203134Sthompsa	USETW(req.wLength, len);
1173209917Sthompsa	return (run_do_request(sc, &req, buf));
1174203134Sthompsa#endif
1175203134Sthompsa}
1176203134Sthompsa
1177203134Sthompsastatic int
1178203134Sthompsarun_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1179203134Sthompsa{
1180203134Sthompsa	int i, error = 0;
1181203134Sthompsa
1182203134Sthompsa	KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1183203134Sthompsa	for (i = 0; i < len && error == 0; i += 4)
1184203134Sthompsa		error = run_write(sc, reg + i, val);
1185209917Sthompsa	return (error);
1186203134Sthompsa}
1187203134Sthompsa
1188203134Sthompsa/* Read 16-bit from eFUSE ROM (RT3070 only.) */
1189203134Sthompsastatic int
1190203134Sthompsarun_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1191203134Sthompsa{
1192203134Sthompsa	uint32_t tmp;
1193203134Sthompsa	uint16_t reg;
1194203134Sthompsa	int error, ntries;
1195203134Sthompsa
1196203134Sthompsa	if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1197209917Sthompsa		return (error);
1198203134Sthompsa
1199203134Sthompsa	addr *= 2;
1200203134Sthompsa	/*-
1201203134Sthompsa	 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1202203134Sthompsa	 * DATA0: F E D C
1203203134Sthompsa	 * DATA1: B A 9 8
1204203134Sthompsa	 * DATA2: 7 6 5 4
1205203134Sthompsa	 * DATA3: 3 2 1 0
1206203134Sthompsa	 */
1207203134Sthompsa	tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1208203134Sthompsa	tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1209203134Sthompsa	run_write(sc, RT3070_EFUSE_CTRL, tmp);
1210203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1211203134Sthompsa		if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1212209917Sthompsa			return (error);
1213203134Sthompsa		if (!(tmp & RT3070_EFSROM_KICK))
1214203134Sthompsa			break;
1215203134Sthompsa		run_delay(sc, 2);
1216203134Sthompsa	}
1217203134Sthompsa	if (ntries == 100)
1218209917Sthompsa		return (ETIMEDOUT);
1219203134Sthompsa
1220203134Sthompsa	if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1221203134Sthompsa		*val = 0xffff;	/* address not found */
1222209917Sthompsa		return (0);
1223203134Sthompsa	}
1224203134Sthompsa	/* determine to which 32-bit register our 16-bit word belongs */
1225203134Sthompsa	reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1226203134Sthompsa	if ((error = run_read(sc, reg, &tmp)) != 0)
1227209917Sthompsa		return (error);
1228203134Sthompsa
1229203134Sthompsa	*val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1230209917Sthompsa	return (0);
1231203134Sthompsa}
1232203134Sthompsa
1233203134Sthompsastatic int
1234203134Sthompsarun_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1235203134Sthompsa{
1236203134Sthompsa	usb_device_request_t req;
1237203134Sthompsa	uint16_t tmp;
1238203134Sthompsa	int error;
1239203134Sthompsa
1240203134Sthompsa	addr *= 2;
1241203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1242203134Sthompsa	req.bRequest = RT2870_EEPROM_READ;
1243203134Sthompsa	USETW(req.wValue, 0);
1244203134Sthompsa	USETW(req.wIndex, addr);
1245203134Sthompsa	USETW(req.wLength, sizeof tmp);
1246203134Sthompsa
1247203134Sthompsa	error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1248203134Sthompsa	if (error == 0)
1249203134Sthompsa		*val = le16toh(tmp);
1250203134Sthompsa	else
1251203134Sthompsa		*val = 0xffff;
1252209917Sthompsa	return (error);
1253203134Sthompsa}
1254203134Sthompsa
1255203134Sthompsastatic __inline int
1256203134Sthompsarun_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1257203134Sthompsa{
1258203134Sthompsa	/* either eFUSE ROM or EEPROM */
1259203134Sthompsa	return sc->sc_srom_read(sc, addr, val);
1260203134Sthompsa}
1261203134Sthompsa
1262203134Sthompsastatic int
1263203134Sthompsarun_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val)
1264203134Sthompsa{
1265203134Sthompsa	uint32_t tmp;
1266203134Sthompsa	int error, ntries;
1267203134Sthompsa
1268203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1269203134Sthompsa		if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1270209917Sthompsa			return (error);
1271203134Sthompsa		if (!(tmp & RT2860_RF_REG_CTRL))
1272203134Sthompsa			break;
1273203134Sthompsa	}
1274203134Sthompsa	if (ntries == 10)
1275209917Sthompsa		return (ETIMEDOUT);
1276203134Sthompsa
1277203134Sthompsa	/* RF registers are 24-bit on the RT2860 */
1278203134Sthompsa	tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1279203134Sthompsa	    (val & 0x3fffff) << 2 | (reg & 3);
1280209917Sthompsa	return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1281203134Sthompsa}
1282203134Sthompsa
1283203134Sthompsastatic int
1284203134Sthompsarun_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1285203134Sthompsa{
1286203134Sthompsa	uint32_t tmp;
1287203134Sthompsa	int error, ntries;
1288203134Sthompsa
1289203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1290203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1291209917Sthompsa			return (error);
1292203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1293203134Sthompsa			break;
1294203134Sthompsa	}
1295203134Sthompsa	if (ntries == 100)
1296209917Sthompsa		return (ETIMEDOUT);
1297203134Sthompsa
1298203134Sthompsa	tmp = RT3070_RF_KICK | reg << 8;
1299203134Sthompsa	if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1300209917Sthompsa		return (error);
1301203134Sthompsa
1302203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1303203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1304209917Sthompsa			return (error);
1305203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1306203134Sthompsa			break;
1307203134Sthompsa	}
1308203134Sthompsa	if (ntries == 100)
1309209917Sthompsa		return (ETIMEDOUT);
1310203134Sthompsa
1311203134Sthompsa	*val = tmp & 0xff;
1312209917Sthompsa	return (0);
1313203134Sthompsa}
1314203134Sthompsa
1315203134Sthompsastatic int
1316203134Sthompsarun_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1317203134Sthompsa{
1318203134Sthompsa	uint32_t tmp;
1319203134Sthompsa	int error, ntries;
1320203134Sthompsa
1321203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1322203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1323209917Sthompsa			return (error);
1324203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1325203134Sthompsa			break;
1326203134Sthompsa	}
1327203134Sthompsa	if (ntries == 10)
1328209917Sthompsa		return (ETIMEDOUT);
1329203134Sthompsa
1330203134Sthompsa	tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1331209917Sthompsa	return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1332203134Sthompsa}
1333203134Sthompsa
1334203134Sthompsastatic int
1335203134Sthompsarun_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1336203134Sthompsa{
1337203134Sthompsa	uint32_t tmp;
1338203134Sthompsa	int ntries, error;
1339203134Sthompsa
1340203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1341203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1342209917Sthompsa			return (error);
1343203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1344203134Sthompsa			break;
1345203134Sthompsa	}
1346203134Sthompsa	if (ntries == 10)
1347209917Sthompsa		return (ETIMEDOUT);
1348203134Sthompsa
1349203134Sthompsa	tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1350203134Sthompsa	if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1351209917Sthompsa		return (error);
1352203134Sthompsa
1353203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1354203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1355209917Sthompsa			return (error);
1356203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1357203134Sthompsa			break;
1358203134Sthompsa	}
1359203134Sthompsa	if (ntries == 10)
1360209917Sthompsa		return (ETIMEDOUT);
1361203134Sthompsa
1362203134Sthompsa	*val = tmp & 0xff;
1363209917Sthompsa	return (0);
1364203134Sthompsa}
1365203134Sthompsa
1366203134Sthompsastatic int
1367203134Sthompsarun_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1368203134Sthompsa{
1369203134Sthompsa	uint32_t tmp;
1370203134Sthompsa	int ntries, error;
1371203134Sthompsa
1372203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1373203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1374209917Sthompsa			return (error);
1375203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1376203134Sthompsa			break;
1377203134Sthompsa	}
1378203134Sthompsa	if (ntries == 10)
1379209917Sthompsa		return (ETIMEDOUT);
1380203134Sthompsa
1381203134Sthompsa	tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1382209917Sthompsa	return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1383203134Sthompsa}
1384203134Sthompsa
1385203134Sthompsa/*
1386203134Sthompsa * Send a command to the 8051 microcontroller unit.
1387203134Sthompsa */
1388203134Sthompsastatic int
1389203134Sthompsarun_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1390203134Sthompsa{
1391203134Sthompsa	uint32_t tmp;
1392203134Sthompsa	int error, ntries;
1393203134Sthompsa
1394203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1395203134Sthompsa		if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1396203134Sthompsa			return error;
1397203134Sthompsa		if (!(tmp & RT2860_H2M_BUSY))
1398203134Sthompsa			break;
1399203134Sthompsa	}
1400203134Sthompsa	if (ntries == 100)
1401203134Sthompsa		return ETIMEDOUT;
1402203134Sthompsa
1403203134Sthompsa	tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1404203134Sthompsa	if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1405203134Sthompsa		error = run_write(sc, RT2860_HOST_CMD, cmd);
1406209917Sthompsa	return (error);
1407203134Sthompsa}
1408203134Sthompsa
1409203134Sthompsa/*
1410203134Sthompsa * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1411203134Sthompsa * Used to adjust per-rate Tx power registers.
1412203134Sthompsa */
1413203134Sthompsastatic __inline uint32_t
1414203134Sthompsab4inc(uint32_t b32, int8_t delta)
1415203134Sthompsa{
1416203134Sthompsa	int8_t i, b4;
1417203134Sthompsa
1418203134Sthompsa	for (i = 0; i < 8; i++) {
1419203134Sthompsa		b4 = b32 & 0xf;
1420203134Sthompsa		b4 += delta;
1421203134Sthompsa		if (b4 < 0)
1422203134Sthompsa			b4 = 0;
1423203134Sthompsa		else if (b4 > 0xf)
1424203134Sthompsa			b4 = 0xf;
1425203134Sthompsa		b32 = b32 >> 4 | b4 << 28;
1426203134Sthompsa	}
1427209917Sthompsa	return (b32);
1428203134Sthompsa}
1429203134Sthompsa
1430203134Sthompsastatic const char *
1431203134Sthompsarun_get_rf(int rev)
1432203134Sthompsa{
1433203134Sthompsa	switch (rev) {
1434203134Sthompsa	case RT2860_RF_2820:	return "RT2820";
1435203134Sthompsa	case RT2860_RF_2850:	return "RT2850";
1436203134Sthompsa	case RT2860_RF_2720:	return "RT2720";
1437203134Sthompsa	case RT2860_RF_2750:	return "RT2750";
1438203134Sthompsa	case RT3070_RF_3020:	return "RT3020";
1439203134Sthompsa	case RT3070_RF_2020:	return "RT2020";
1440203134Sthompsa	case RT3070_RF_3021:	return "RT3021";
1441203134Sthompsa	case RT3070_RF_3022:	return "RT3022";
1442203134Sthompsa	case RT3070_RF_3052:	return "RT3052";
1443203134Sthompsa	}
1444209917Sthompsa	return ("unknown");
1445203134Sthompsa}
1446203134Sthompsa
1447203134Sthompsaint
1448203134Sthompsarun_read_eeprom(struct run_softc *sc)
1449203134Sthompsa{
1450203134Sthompsa	int8_t delta_2ghz, delta_5ghz;
1451203134Sthompsa	uint32_t tmp;
1452203134Sthompsa	uint16_t val;
1453203134Sthompsa	int ridx, ant, i;
1454203134Sthompsa
1455203134Sthompsa	/* check whether the ROM is eFUSE ROM or EEPROM */
1456203134Sthompsa	sc->sc_srom_read = run_eeprom_read_2;
1457205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1458203134Sthompsa		run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1459203134Sthompsa		DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1460203134Sthompsa		if (tmp & RT3070_SEL_EFUSE)
1461203134Sthompsa			sc->sc_srom_read = run_efuse_read_2;
1462203134Sthompsa	}
1463203134Sthompsa
1464203134Sthompsa	/* read ROM version */
1465203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1466203134Sthompsa	DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1467203134Sthompsa
1468203134Sthompsa	/* read MAC address */
1469203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1470203134Sthompsa	sc->sc_bssid[0] = val & 0xff;
1471203134Sthompsa	sc->sc_bssid[1] = val >> 8;
1472203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1473203134Sthompsa	sc->sc_bssid[2] = val & 0xff;
1474203134Sthompsa	sc->sc_bssid[3] = val >> 8;
1475203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1476203134Sthompsa	sc->sc_bssid[4] = val & 0xff;
1477203134Sthompsa	sc->sc_bssid[5] = val >> 8;
1478203134Sthompsa
1479205042Sthompsa	/* read vender BBP settings */
1480205042Sthompsa	for (i = 0; i < 10; i++) {
1481203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1482203134Sthompsa		sc->bbp[i].val = val & 0xff;
1483203134Sthompsa		sc->bbp[i].reg = val >> 8;
1484203134Sthompsa		DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val);
1485203134Sthompsa	}
1486205042Sthompsa	if (sc->mac_ver >= 0x3071) {
1487205042Sthompsa		/* read vendor RF settings */
1488205042Sthompsa		for (i = 0; i < 10; i++) {
1489205042Sthompsa			run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val);
1490205042Sthompsa			sc->rf[i].val = val & 0xff;
1491205042Sthompsa			sc->rf[i].reg = val >> 8;
1492205042Sthompsa			DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1493205042Sthompsa			    sc->rf[i].val);
1494205042Sthompsa		}
1495205042Sthompsa	}
1496203134Sthompsa
1497203134Sthompsa	/* read RF frequency offset from EEPROM */
1498203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1499203134Sthompsa	sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1500203134Sthompsa	DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1501203134Sthompsa
1502205042Sthompsa	if (val >> 8 != 0xff) {
1503203134Sthompsa		/* read LEDs operating mode */
1504205042Sthompsa		sc->leds = val >> 8;
1505203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1506203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1507203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1508203134Sthompsa	} else {
1509203134Sthompsa		/* broken EEPROM, use default settings */
1510203134Sthompsa		sc->leds = 0x01;
1511203134Sthompsa		sc->led[0] = 0x5555;
1512203134Sthompsa		sc->led[1] = 0x2221;
1513203134Sthompsa		sc->led[2] = 0x5627;	/* differs from RT2860 */
1514203134Sthompsa	}
1515203134Sthompsa	DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1516203134Sthompsa	    sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1517203134Sthompsa
1518203134Sthompsa	/* read RF information */
1519203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1520203134Sthompsa	if (val == 0xffff) {
1521203134Sthompsa		DPRINTF("invalid EEPROM antenna info, using default\n");
1522205042Sthompsa		if (sc->mac_ver == 0x3572) {
1523205042Sthompsa			/* default to RF3052 2T2R */
1524205042Sthompsa			sc->rf_rev = RT3070_RF_3052;
1525205042Sthompsa			sc->ntxchains = 2;
1526205042Sthompsa			sc->nrxchains = 2;
1527205042Sthompsa		} else if (sc->mac_ver >= 0x3070) {
1528203134Sthompsa			/* default to RF3020 1T1R */
1529203134Sthompsa			sc->rf_rev = RT3070_RF_3020;
1530203134Sthompsa			sc->ntxchains = 1;
1531203134Sthompsa			sc->nrxchains = 1;
1532203134Sthompsa		} else {
1533203134Sthompsa			/* default to RF2820 1T2R */
1534203134Sthompsa			sc->rf_rev = RT2860_RF_2820;
1535203134Sthompsa			sc->ntxchains = 1;
1536203134Sthompsa			sc->nrxchains = 2;
1537203134Sthompsa		}
1538203134Sthompsa	} else {
1539203134Sthompsa		sc->rf_rev = (val >> 8) & 0xf;
1540203134Sthompsa		sc->ntxchains = (val >> 4) & 0xf;
1541203134Sthompsa		sc->nrxchains = val & 0xf;
1542203134Sthompsa	}
1543203134Sthompsa	DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n",
1544203134Sthompsa	    sc->rf_rev, sc->ntxchains, sc->nrxchains);
1545203134Sthompsa
1546208019Sthompsa	/* check if RF supports automatic Tx access gain control */
1547203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1548203134Sthompsa	DPRINTF("EEPROM CFG 0x%04x\n", val);
1549205042Sthompsa	/* check if driver should patch the DAC issue */
1550205042Sthompsa	if ((val >> 8) != 0xff)
1551205042Sthompsa		sc->patch_dac = (val >> 15) & 1;
1552203134Sthompsa	if ((val & 0xff) != 0xff) {
1553203134Sthompsa		sc->ext_5ghz_lna = (val >> 3) & 1;
1554203134Sthompsa		sc->ext_2ghz_lna = (val >> 2) & 1;
1555205042Sthompsa		/* check if RF supports automatic Tx access gain control */
1556203134Sthompsa		sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1557205042Sthompsa		/* check if we have a hardware radio switch */
1558205042Sthompsa		sc->rfswitch = val & 1;
1559203134Sthompsa	}
1560203134Sthompsa
1561203134Sthompsa	/* read power settings for 2GHz channels */
1562203134Sthompsa	for (i = 0; i < 14; i += 2) {
1563203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1564203134Sthompsa		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1565203134Sthompsa		sc->txpow1[i + 1] = (int8_t)(val >> 8);
1566203134Sthompsa
1567203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1568203134Sthompsa		sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1569203134Sthompsa		sc->txpow2[i + 1] = (int8_t)(val >> 8);
1570203134Sthompsa	}
1571203134Sthompsa	/* fix broken Tx power entries */
1572203134Sthompsa	for (i = 0; i < 14; i++) {
1573203134Sthompsa		if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1574203134Sthompsa			sc->txpow1[i] = 5;
1575203134Sthompsa		if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1576203134Sthompsa			sc->txpow2[i] = 5;
1577203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1578203134Sthompsa		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1579203134Sthompsa	}
1580203134Sthompsa	/* read power settings for 5GHz channels */
1581205042Sthompsa	for (i = 0; i < 40; i += 2) {
1582203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1583203134Sthompsa		sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1584203134Sthompsa		sc->txpow1[i + 15] = (int8_t)(val >> 8);
1585203134Sthompsa
1586203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1587203134Sthompsa		sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1588203134Sthompsa		sc->txpow2[i + 15] = (int8_t)(val >> 8);
1589203134Sthompsa	}
1590203134Sthompsa	/* fix broken Tx power entries */
1591205042Sthompsa	for (i = 0; i < 40; i++) {
1592203134Sthompsa		if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1593203134Sthompsa			sc->txpow1[14 + i] = 5;
1594203134Sthompsa		if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1595203134Sthompsa			sc->txpow2[14 + i] = 5;
1596203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1597203134Sthompsa		    rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1598203134Sthompsa		    sc->txpow2[14 + i]);
1599203134Sthompsa	}
1600203134Sthompsa
1601203134Sthompsa	/* read Tx power compensation for each Tx rate */
1602203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1603203134Sthompsa	delta_2ghz = delta_5ghz = 0;
1604203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1605203134Sthompsa		delta_2ghz = val & 0xf;
1606203134Sthompsa		if (!(val & 0x40))	/* negative number */
1607203134Sthompsa			delta_2ghz = -delta_2ghz;
1608203134Sthompsa	}
1609203134Sthompsa	val >>= 8;
1610203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1611203134Sthompsa		delta_5ghz = val & 0xf;
1612203134Sthompsa		if (!(val & 0x40))	/* negative number */
1613203134Sthompsa			delta_5ghz = -delta_5ghz;
1614203134Sthompsa	}
1615203134Sthompsa	DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1616203134Sthompsa	    delta_2ghz, delta_5ghz);
1617203134Sthompsa
1618203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
1619203134Sthompsa		uint32_t reg;
1620203134Sthompsa
1621208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1622208019Sthompsa		reg = val;
1623208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1624208019Sthompsa		reg |= (uint32_t)val << 16;
1625203134Sthompsa
1626203134Sthompsa		sc->txpow20mhz[ridx] = reg;
1627203134Sthompsa		sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1628203134Sthompsa		sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1629203134Sthompsa
1630203134Sthompsa		DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1631203134Sthompsa		    "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1632203134Sthompsa		    sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1633203134Sthompsa	}
1634203134Sthompsa
1635203134Sthompsa	/* read RSSI offsets and LNA gains from EEPROM */
1636203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1637203134Sthompsa	sc->rssi_2ghz[0] = val & 0xff;	/* Ant A */
1638203134Sthompsa	sc->rssi_2ghz[1] = val >> 8;	/* Ant B */
1639203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1640205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1641205042Sthompsa		/*
1642205042Sthompsa		 * On RT3070 chips (limited to 2 Rx chains), this ROM
1643205042Sthompsa		 * field contains the Tx mixer gain for the 2GHz band.
1644205042Sthompsa		 */
1645205042Sthompsa		if ((val & 0xff) != 0xff)
1646205042Sthompsa			sc->txmixgain_2ghz = val & 0x7;
1647205042Sthompsa		DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1648205042Sthompsa	} else
1649205042Sthompsa		sc->rssi_2ghz[2] = val & 0xff;	/* Ant C */
1650203134Sthompsa	sc->lna[2] = val >> 8;		/* channel group 2 */
1651203134Sthompsa
1652203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1653203134Sthompsa	sc->rssi_5ghz[0] = val & 0xff;	/* Ant A */
1654203134Sthompsa	sc->rssi_5ghz[1] = val >> 8;	/* Ant B */
1655203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1656205042Sthompsa	if (sc->mac_ver == 0x3572) {
1657205042Sthompsa		/*
1658205042Sthompsa		 * On RT3572 chips (limited to 2 Rx chains), this ROM
1659205042Sthompsa		 * field contains the Tx mixer gain for the 5GHz band.
1660205042Sthompsa		 */
1661205042Sthompsa		if ((val & 0xff) != 0xff)
1662205042Sthompsa			sc->txmixgain_5ghz = val & 0x7;
1663205042Sthompsa		DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1664205042Sthompsa	} else
1665205042Sthompsa		sc->rssi_5ghz[2] = val & 0xff;	/* Ant C */
1666203134Sthompsa	sc->lna[3] = val >> 8;		/* channel group 3 */
1667203134Sthompsa
1668203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1669203134Sthompsa	sc->lna[0] = val & 0xff;	/* channel group 0 */
1670203134Sthompsa	sc->lna[1] = val >> 8;		/* channel group 1 */
1671203134Sthompsa
1672203134Sthompsa	/* fix broken 5GHz LNA entries */
1673203134Sthompsa	if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1674203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 2);
1675203134Sthompsa		sc->lna[2] = sc->lna[1];
1676203134Sthompsa	}
1677203134Sthompsa	if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1678203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 3);
1679203134Sthompsa		sc->lna[3] = sc->lna[1];
1680203134Sthompsa	}
1681203134Sthompsa
1682203134Sthompsa	/* fix broken RSSI offset entries */
1683203134Sthompsa	for (ant = 0; ant < 3; ant++) {
1684203134Sthompsa		if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1685203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1686203134Sthompsa			    ant + 1, sc->rssi_2ghz[ant]);
1687203134Sthompsa			sc->rssi_2ghz[ant] = 0;
1688203134Sthompsa		}
1689203134Sthompsa		if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1690203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1691203134Sthompsa			    ant + 1, sc->rssi_5ghz[ant]);
1692203134Sthompsa			sc->rssi_5ghz[ant] = 0;
1693203134Sthompsa		}
1694203134Sthompsa	}
1695209917Sthompsa	return (0);
1696203134Sthompsa}
1697203134Sthompsa
1698218676Shselaskystatic struct ieee80211_node *
1699203134Sthompsarun_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1700203134Sthompsa{
1701203134Sthompsa	return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1702203134Sthompsa}
1703203134Sthompsa
1704203134Sthompsastatic int
1705203134Sthompsarun_media_change(struct ifnet *ifp)
1706203134Sthompsa{
1707208019Sthompsa	struct ieee80211vap *vap = ifp->if_softc;
1708208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1709203134Sthompsa	const struct ieee80211_txparam *tp;
1710208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1711203134Sthompsa	uint8_t rate, ridx;
1712203134Sthompsa	int error;
1713203134Sthompsa
1714203134Sthompsa	RUN_LOCK(sc);
1715203134Sthompsa
1716203134Sthompsa	error = ieee80211_media_change(ifp);
1717209917Sthompsa	if (error != ENETRESET) {
1718203134Sthompsa		RUN_UNLOCK(sc);
1719209917Sthompsa		return (error);
1720208019Sthompsa	}
1721203134Sthompsa
1722203134Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1723203134Sthompsa	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1724212127Sthompsa		struct ieee80211_node *ni;
1725212127Sthompsa		struct run_node	*rn;
1726212127Sthompsa
1727203134Sthompsa		rate = ic->ic_sup_rates[ic->ic_curmode].
1728203134Sthompsa		    rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1729203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1730203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
1731203134Sthompsa				break;
1732212127Sthompsa		ni = ieee80211_ref_node(vap->iv_bss);
1733212127Sthompsa		rn = (struct run_node *)ni;
1734208019Sthompsa		rn->fix_ridx = ridx;
1735208019Sthompsa		DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1736212127Sthompsa		ieee80211_free_node(ni);
1737203134Sthompsa	}
1738203134Sthompsa
1739208019Sthompsa#if 0
1740203134Sthompsa	if ((ifp->if_flags & IFF_UP) &&
1741203134Sthompsa	    (ifp->if_drv_flags &  IFF_DRV_RUNNING)){
1742203134Sthompsa		run_init_locked(sc);
1743203134Sthompsa	}
1744208019Sthompsa#endif
1745203134Sthompsa
1746203134Sthompsa	RUN_UNLOCK(sc);
1747203134Sthompsa
1748209917Sthompsa	return (0);
1749203134Sthompsa}
1750203134Sthompsa
1751203134Sthompsastatic int
1752203134Sthompsarun_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1753203134Sthompsa{
1754203134Sthompsa	const struct ieee80211_txparam *tp;
1755203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1756203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1757203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
1758203134Sthompsa	enum ieee80211_state ostate;
1759208019Sthompsa	uint32_t sta[3];
1760203134Sthompsa	uint32_t tmp;
1761208019Sthompsa	uint8_t ratectl;
1762208019Sthompsa	uint8_t restart_ratectl = 0;
1763208019Sthompsa	uint8_t bid = 1 << rvp->rvp_id;
1764203134Sthompsa
1765203134Sthompsa	ostate = vap->iv_state;
1766203134Sthompsa	DPRINTF("%s -> %s\n",
1767203134Sthompsa		ieee80211_state_name[ostate],
1768203134Sthompsa		ieee80211_state_name[nstate]);
1769203134Sthompsa
1770203134Sthompsa	IEEE80211_UNLOCK(ic);
1771203134Sthompsa	RUN_LOCK(sc);
1772203134Sthompsa
1773208019Sthompsa	ratectl = sc->ratectl_run; /* remember current state */
1774208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
1775208019Sthompsa	usb_callout_stop(&sc->ratectl_ch);
1776203134Sthompsa
1777203134Sthompsa	if (ostate == IEEE80211_S_RUN) {
1778203134Sthompsa		/* turn link LED off */
1779203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO);
1780203134Sthompsa	}
1781203134Sthompsa
1782203134Sthompsa	switch (nstate) {
1783203134Sthompsa	case IEEE80211_S_INIT:
1784208019Sthompsa		restart_ratectl = 1;
1785208019Sthompsa
1786208019Sthompsa		if (ostate != IEEE80211_S_RUN)
1787208019Sthompsa			break;
1788208019Sthompsa
1789208019Sthompsa		ratectl &= ~bid;
1790208019Sthompsa		sc->runbmap &= ~bid;
1791208019Sthompsa
1792208019Sthompsa		/* abort TSF synchronization if there is no vap running */
1793209917Sthompsa		if (--sc->running == 0) {
1794203134Sthompsa			run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1795203134Sthompsa			run_write(sc, RT2860_BCN_TIME_CFG,
1796203134Sthompsa			    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1797203134Sthompsa			    RT2860_TBTT_TIMER_EN));
1798203134Sthompsa		}
1799203134Sthompsa		break;
1800203134Sthompsa
1801203134Sthompsa	case IEEE80211_S_RUN:
1802209917Sthompsa		if (!(sc->runbmap & bid)) {
1803208019Sthompsa			if(sc->running++)
1804208019Sthompsa				restart_ratectl = 1;
1805208019Sthompsa			sc->runbmap |= bid;
1806208019Sthompsa		}
1807203134Sthompsa
1808218492Sbschmidt		m_freem(rvp->beacon_mbuf);
1809218492Sbschmidt		rvp->beacon_mbuf = NULL;
1810218492Sbschmidt
1811209917Sthompsa		switch (vap->iv_opmode) {
1812208019Sthompsa		case IEEE80211_M_HOSTAP:
1813208019Sthompsa		case IEEE80211_M_MBSS:
1814208019Sthompsa			sc->ap_running |= bid;
1815208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1816208019Sthompsa			run_update_beacon_cb(vap);
1817208019Sthompsa			break;
1818208019Sthompsa		case IEEE80211_M_IBSS:
1819208019Sthompsa			sc->adhoc_running |= bid;
1820209917Sthompsa			if (!sc->ap_running)
1821208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1822208019Sthompsa			run_update_beacon_cb(vap);
1823208019Sthompsa			break;
1824208019Sthompsa		case IEEE80211_M_STA:
1825208019Sthompsa			sc->sta_running |= bid;
1826209917Sthompsa			if (!sc->ap_running && !sc->adhoc_running)
1827208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1828208019Sthompsa
1829208019Sthompsa			/* read statistic counters (clear on read) */
1830208019Sthompsa			run_read_region_1(sc, RT2860_TX_STA_CNT0,
1831208019Sthompsa			    (uint8_t *)sta, sizeof sta);
1832208019Sthompsa
1833208019Sthompsa			break;
1834208019Sthompsa		default:
1835208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1836208019Sthompsa			break;
1837208019Sthompsa		}
1838208019Sthompsa
1839203134Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1840212127Sthompsa			struct ieee80211_node *ni;
1841212127Sthompsa
1842236439Shselasky			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
1843236439Shselasky				RUN_UNLOCK(sc);
1844236439Shselasky				IEEE80211_LOCK(ic);
1845236439Shselasky				return (-1);
1846236439Shselasky			}
1847203134Sthompsa			run_updateslot(ic->ic_ifp);
1848203134Sthompsa			run_enable_mrr(sc);
1849203134Sthompsa			run_set_txpreamble(sc);
1850203134Sthompsa			run_set_basicrates(sc);
1851212127Sthompsa			ni = ieee80211_ref_node(vap->iv_bss);
1852203134Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1853203134Sthompsa			run_set_bssid(sc, ni->ni_bssid);
1854212127Sthompsa			ieee80211_free_node(ni);
1855208019Sthompsa			run_enable_tsf_sync(sc);
1856203134Sthompsa
1857208019Sthompsa			/* enable automatic rate adaptation */
1858208019Sthompsa			tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1859208019Sthompsa			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1860208019Sthompsa				ratectl |= bid;
1861203134Sthompsa		}
1862203134Sthompsa
1863203134Sthompsa		/* turn link LED on */
1864203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO |
1865208019Sthompsa		    (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1866203134Sthompsa		     RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1867203134Sthompsa
1868203134Sthompsa		break;
1869203134Sthompsa	default:
1870203134Sthompsa		DPRINTFN(6, "undefined case\n");
1871203134Sthompsa		break;
1872203134Sthompsa	}
1873203134Sthompsa
1874208019Sthompsa	/* restart amrr for running VAPs */
1875209917Sthompsa	if ((sc->ratectl_run = ratectl) && restart_ratectl)
1876208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1877208019Sthompsa
1878203134Sthompsa	RUN_UNLOCK(sc);
1879203134Sthompsa	IEEE80211_LOCK(ic);
1880203134Sthompsa
1881203134Sthompsa	return(rvp->newstate(vap, nstate, arg));
1882203134Sthompsa}
1883203134Sthompsa
1884203134Sthompsa/* ARGSUSED */
1885203134Sthompsastatic void
1886208019Sthompsarun_wme_update_cb(void *arg)
1887203134Sthompsa{
1888203134Sthompsa	struct ieee80211com *ic = arg;
1889203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1890203134Sthompsa	struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1891203134Sthompsa	int aci, error = 0;
1892203134Sthompsa
1893208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1894203134Sthompsa
1895203134Sthompsa	/* update MAC TX configuration registers */
1896203134Sthompsa	for (aci = 0; aci < WME_NUM_AC; aci++) {
1897203134Sthompsa		error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1898203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmax << 16 |
1899203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmin << 12 |
1900203134Sthompsa		    wmesp->wme_params[aci].wmep_aifsn  <<  8 |
1901203134Sthompsa		    wmesp->wme_params[aci].wmep_txopLimit);
1902209917Sthompsa		if (error) goto err;
1903203134Sthompsa	}
1904203134Sthompsa
1905203134Sthompsa	/* update SCH/DMA registers too */
1906203134Sthompsa	error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1907203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_aifsn  << 12 |
1908203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_aifsn  <<  8 |
1909203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_aifsn  <<  4 |
1910203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1911209917Sthompsa	if (error) goto err;
1912203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1913203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1914203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmin <<  8 |
1915203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmin <<  4 |
1916203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1917209917Sthompsa	if (error) goto err;
1918203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1919203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1920203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmax <<  8 |
1921203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmax <<  4 |
1922203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1923209917Sthompsa	if (error) goto err;
1924203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1925203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1926203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1927209917Sthompsa	if (error) goto err;
1928203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1929203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1930203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1931203134Sthompsa
1932203134Sthompsaerr:
1933209917Sthompsa	if (error)
1934203134Sthompsa		DPRINTF("WME update failed\n");
1935203134Sthompsa
1936203134Sthompsa	return;
1937203134Sthompsa}
1938203134Sthompsa
1939208019Sthompsastatic int
1940208019Sthompsarun_wme_update(struct ieee80211com *ic)
1941208019Sthompsa{
1942208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1943208019Sthompsa
1944208019Sthompsa	/* sometime called wothout lock */
1945209917Sthompsa	if (mtx_owned(&ic->ic_comlock.mtx)) {
1946208019Sthompsa		uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1947208019Sthompsa		DPRINTF("cmdq_store=%d\n", i);
1948208019Sthompsa		sc->cmdq[i].func = run_wme_update_cb;
1949208019Sthompsa		sc->cmdq[i].arg0 = ic;
1950208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
1951209918Sthompsa		return (0);
1952208019Sthompsa	}
1953208019Sthompsa
1954208019Sthompsa	RUN_LOCK(sc);
1955208019Sthompsa	run_wme_update_cb(ic);
1956208019Sthompsa	RUN_UNLOCK(sc);
1957208019Sthompsa
1958208019Sthompsa	/* return whatever, upper layer desn't care anyway */
1959208019Sthompsa	return (0);
1960208019Sthompsa}
1961208019Sthompsa
1962203134Sthompsastatic void
1963203134Sthompsarun_key_update_begin(struct ieee80211vap *vap)
1964203134Sthompsa{
1965203134Sthompsa	/*
1966208019Sthompsa	 * To avoid out-of-order events, both run_key_set() and
1967208019Sthompsa	 * _delete() are deferred and handled by run_cmdq_cb().
1968208019Sthompsa	 * So, there is nothing we need to do here.
1969203134Sthompsa	 */
1970203134Sthompsa}
1971203134Sthompsa
1972203134Sthompsastatic void
1973203134Sthompsarun_key_update_end(struct ieee80211vap *vap)
1974203134Sthompsa{
1975203134Sthompsa	/* null */
1976203134Sthompsa}
1977203134Sthompsa
1978208019Sthompsastatic void
1979208019Sthompsarun_key_set_cb(void *arg)
1980203134Sthompsa{
1981208019Sthompsa	struct run_cmdq *cmdq = arg;
1982208019Sthompsa	struct ieee80211vap *vap = cmdq->arg1;
1983208019Sthompsa	struct ieee80211_key *k = cmdq->k;
1984203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1985208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1986203134Sthompsa	struct ieee80211_node *ni;
1987203134Sthompsa	uint32_t attr;
1988203134Sthompsa	uint16_t base, associd;
1989209144Sthompsa	uint8_t mode, wcid, iv[8];
1990203134Sthompsa
1991208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1992203134Sthompsa
1993209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1994208019Sthompsa		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
1995209144Sthompsa	else
1996203134Sthompsa		ni = vap->iv_bss;
1997208019Sthompsa	associd = (ni != NULL) ? ni->ni_associd : 0;
1998203134Sthompsa
1999203134Sthompsa	/* map net80211 cipher to RT2860 security mode */
2000203134Sthompsa	switch (k->wk_cipher->ic_cipher) {
2001203134Sthompsa	case IEEE80211_CIPHER_WEP:
2002203134Sthompsa		if(k->wk_keylen < 8)
2003203134Sthompsa			mode = RT2860_MODE_WEP40;
2004203134Sthompsa		else
2005203134Sthompsa			mode = RT2860_MODE_WEP104;
2006203134Sthompsa		break;
2007203134Sthompsa	case IEEE80211_CIPHER_TKIP:
2008203134Sthompsa		mode = RT2860_MODE_TKIP;
2009203134Sthompsa		break;
2010203134Sthompsa	case IEEE80211_CIPHER_AES_CCM:
2011203134Sthompsa		mode = RT2860_MODE_AES_CCMP;
2012203134Sthompsa		break;
2013203134Sthompsa	default:
2014203134Sthompsa		DPRINTF("undefined case\n");
2015208019Sthompsa		return;
2016203134Sthompsa	}
2017203134Sthompsa
2018208019Sthompsa	DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2019203134Sthompsa	    associd, k->wk_keyix, mode,
2020208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2021208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2022208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2023203134Sthompsa
2024203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2025203134Sthompsa		wcid = 0;	/* NB: update WCID0 for group keys */
2026208019Sthompsa		base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2027203134Sthompsa	} else {
2028245047Shselasky		wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2029245047Shselasky		    1 : RUN_AID2WCID(associd);
2030203134Sthompsa		base = RT2860_PKEY(wcid);
2031203134Sthompsa	}
2032203134Sthompsa
2033203134Sthompsa	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2034203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, 16))
2035208019Sthompsa			return;
2036209144Sthompsa		if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8))	/* wk_txmic */
2037208019Sthompsa			return;
2038209144Sthompsa		if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8))	/* wk_rxmic */
2039208019Sthompsa			return;
2040203134Sthompsa	} else {
2041203134Sthompsa		/* roundup len to 16-bit: XXX fix write_region_1() instead */
2042203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2043208019Sthompsa			return;
2044203134Sthompsa	}
2045203134Sthompsa
2046203134Sthompsa	if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2047203134Sthompsa	    (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2048203134Sthompsa		/* set initial packet number in IV+EIV */
2049209917Sthompsa		if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2050203134Sthompsa			memset(iv, 0, sizeof iv);
2051208019Sthompsa			iv[3] = vap->iv_def_txkey << 6;
2052203134Sthompsa		} else {
2053203134Sthompsa			if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2054203134Sthompsa				iv[0] = k->wk_keytsc >> 8;
2055203134Sthompsa				iv[1] = (iv[0] | 0x20) & 0x7f;
2056203134Sthompsa				iv[2] = k->wk_keytsc;
2057203134Sthompsa			} else /* CCMP */ {
2058203134Sthompsa				iv[0] = k->wk_keytsc;
2059203134Sthompsa				iv[1] = k->wk_keytsc >> 8;
2060203134Sthompsa				iv[2] = 0;
2061203134Sthompsa			}
2062203134Sthompsa			iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2063203134Sthompsa			iv[4] = k->wk_keytsc >> 16;
2064203134Sthompsa			iv[5] = k->wk_keytsc >> 24;
2065203134Sthompsa			iv[6] = k->wk_keytsc >> 32;
2066203134Sthompsa			iv[7] = k->wk_keytsc >> 40;
2067203134Sthompsa		}
2068209917Sthompsa		if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2069208019Sthompsa			return;
2070203134Sthompsa	}
2071203134Sthompsa
2072203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2073203134Sthompsa		/* install group key */
2074209917Sthompsa		if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2075208019Sthompsa			return;
2076203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2077203134Sthompsa		attr |= mode << (k->wk_keyix * 4);
2078209917Sthompsa		if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2079208019Sthompsa			return;
2080203134Sthompsa	} else {
2081203134Sthompsa		/* install pairwise key */
2082209917Sthompsa		if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2083208019Sthompsa			return;
2084203134Sthompsa		attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2085209917Sthompsa		if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2086208019Sthompsa			return;
2087203134Sthompsa	}
2088203134Sthompsa
2089203134Sthompsa	/* TODO create a pass-thru key entry? */
2090203134Sthompsa
2091208019Sthompsa	/* need wcid to delete the right key later */
2092208019Sthompsa	k->wk_pad = wcid;
2093203134Sthompsa}
2094203134Sthompsa
2095203134Sthompsa/*
2096208019Sthompsa * Don't have to be deferred, but in order to keep order of
2097208019Sthompsa * execution, i.e. with run_key_delete(), defer this and let
2098208019Sthompsa * run_cmdq_cb() maintain the order.
2099208019Sthompsa *
2100203134Sthompsa * return 0 on error
2101203134Sthompsa */
2102203134Sthompsastatic int
2103208019Sthompsarun_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2104208019Sthompsa		const uint8_t mac[IEEE80211_ADDR_LEN])
2105203134Sthompsa{
2106203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2107203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2108208019Sthompsa	uint32_t i;
2109208019Sthompsa
2110208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2111208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2112208019Sthompsa	sc->cmdq[i].func = run_key_set_cb;
2113208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2114208019Sthompsa	sc->cmdq[i].arg1 = vap;
2115208019Sthompsa	sc->cmdq[i].k = k;
2116208019Sthompsa	IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2117208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2118208019Sthompsa
2119209144Sthompsa	/*
2120209144Sthompsa	 * To make sure key will be set when hostapd
2121209144Sthompsa	 * calls iv_key_set() before if_init().
2122209144Sthompsa	 */
2123209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2124209144Sthompsa		RUN_LOCK(sc);
2125209144Sthompsa		sc->cmdq_key_set = RUN_CMDQ_GO;
2126209144Sthompsa		RUN_UNLOCK(sc);
2127209144Sthompsa	}
2128209144Sthompsa
2129209917Sthompsa	return (1);
2130208019Sthompsa}
2131208019Sthompsa
2132208019Sthompsa/*
2133208019Sthompsa * If wlan is destroyed without being brought down i.e. without
2134208019Sthompsa * wlan down or wpa_cli terminate, this function is called after
2135208019Sthompsa * vap is gone. Don't refer it.
2136208019Sthompsa */
2137208019Sthompsastatic void
2138208019Sthompsarun_key_delete_cb(void *arg)
2139208019Sthompsa{
2140208019Sthompsa	struct run_cmdq *cmdq = arg;
2141208019Sthompsa	struct run_softc *sc = cmdq->arg1;
2142208019Sthompsa	struct ieee80211_key *k = &cmdq->key;
2143203134Sthompsa	uint32_t attr;
2144203134Sthompsa	uint8_t wcid;
2145203134Sthompsa
2146208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2147203134Sthompsa
2148203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2149203134Sthompsa		/* remove group key */
2150208019Sthompsa		DPRINTF("removing group key\n");
2151208019Sthompsa		run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2152203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2153208019Sthompsa		run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2154203134Sthompsa	} else {
2155203134Sthompsa		/* remove pairwise key */
2156208019Sthompsa		DPRINTF("removing key for wcid %x\n", k->wk_pad);
2157208019Sthompsa		/* matching wcid was written to wk_pad in run_key_set() */
2158208019Sthompsa		wcid = k->wk_pad;
2159208019Sthompsa		run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2160203134Sthompsa		attr &= ~0xf;
2161208019Sthompsa		run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2162208019Sthompsa		run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2163203134Sthompsa	}
2164203134Sthompsa
2165208019Sthompsa	k->wk_pad = 0;
2166203134Sthompsa}
2167203134Sthompsa
2168208019Sthompsa/*
2169208019Sthompsa * return 0 on error
2170208019Sthompsa */
2171208019Sthompsastatic int
2172208019Sthompsarun_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2173203134Sthompsa{
2174208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2175208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2176208019Sthompsa	struct ieee80211_key *k0;
2177208019Sthompsa	uint32_t i;
2178203134Sthompsa
2179208019Sthompsa	/*
2180208019Sthompsa	 * When called back, key might be gone. So, make a copy
2181208019Sthompsa	 * of some values need to delete keys before deferring.
2182208019Sthompsa	 * But, because of LOR with node lock, cannot use lock here.
2183208019Sthompsa	 * So, use atomic instead.
2184208019Sthompsa	 */
2185208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2186208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2187208019Sthompsa	sc->cmdq[i].func = run_key_delete_cb;
2188208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2189208019Sthompsa	sc->cmdq[i].arg1 = sc;
2190208019Sthompsa	k0 = &sc->cmdq[i].key;
2191208019Sthompsa	k0->wk_flags = k->wk_flags;
2192208019Sthompsa	k0->wk_keyix = k->wk_keyix;
2193208019Sthompsa	/* matching wcid was written to wk_pad in run_key_set() */
2194208019Sthompsa	k0->wk_pad = k->wk_pad;
2195208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2196208019Sthompsa	return (1);	/* return fake success */
2197203134Sthompsa
2198203134Sthompsa}
2199203134Sthompsa
2200203134Sthompsastatic void
2201206358Srpaulorun_ratectl_to(void *arg)
2202203134Sthompsa{
2203208019Sthompsa	struct run_softc *sc = arg;
2204203134Sthompsa
2205203134Sthompsa	/* do it in a process context, so it can go sleep */
2206208019Sthompsa	ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2207203134Sthompsa	/* next timeout will be rescheduled in the callback task */
2208203134Sthompsa}
2209203134Sthompsa
2210203134Sthompsa/* ARGSUSED */
2211203134Sthompsastatic void
2212206358Srpaulorun_ratectl_cb(void *arg, int pending)
2213203134Sthompsa{
2214208019Sthompsa	struct run_softc *sc = arg;
2215208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2216208019Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2217203134Sthompsa
2218209917Sthompsa	if (vap == NULL)
2219208019Sthompsa		return;
2220208019Sthompsa
2221209917Sthompsa	if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2222208019Sthompsa		run_iter_func(sc, vap->iv_bss);
2223203134Sthompsa	else {
2224203134Sthompsa		/*
2225203134Sthompsa		 * run_reset_livelock() doesn't do anything with AMRR,
2226203134Sthompsa		 * but Ralink wants us to call it every 1 sec. So, we
2227203134Sthompsa		 * piggyback here rather than creating another callout.
2228203134Sthompsa		 * Livelock may occur only in HOSTAP or IBSS mode
2229203134Sthompsa		 * (when h/w is sending beacons).
2230203134Sthompsa		 */
2231203134Sthompsa		RUN_LOCK(sc);
2232203134Sthompsa		run_reset_livelock(sc);
2233208019Sthompsa		/* just in case, there are some stats to drain */
2234208019Sthompsa		run_drain_fifo(sc);
2235203134Sthompsa		RUN_UNLOCK(sc);
2236208019Sthompsa		ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2237203134Sthompsa	}
2238203134Sthompsa
2239208019Sthompsa	if(sc->ratectl_run != RUN_RATECTL_OFF)
2240208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2241203134Sthompsa}
2242203134Sthompsa
2243203134Sthompsastatic void
2244208019Sthompsarun_drain_fifo(void *arg)
2245203134Sthompsa{
2246208019Sthompsa	struct run_softc *sc = arg;
2247208019Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2248208019Sthompsa	uint32_t stat;
2249218676Shselasky	uint16_t (*wstat)[3];
2250203134Sthompsa	uint8_t wcid, mcs, pid;
2251218676Shselasky	int8_t retry;
2252203134Sthompsa
2253208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2254203134Sthompsa
2255208019Sthompsa	for (;;) {
2256203134Sthompsa		/* drain Tx status FIFO (maxsize = 16) */
2257203134Sthompsa		run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2258208019Sthompsa		DPRINTFN(4, "tx stat 0x%08x\n", stat);
2259209917Sthompsa		if (!(stat & RT2860_TXQ_VLD))
2260208019Sthompsa			break;
2261203134Sthompsa
2262208019Sthompsa		wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2263203134Sthompsa
2264208019Sthompsa		/* if no ACK was requested, no feedback is available */
2265208019Sthompsa		if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2266208019Sthompsa		    wcid == 0)
2267208019Sthompsa			continue;
2268203134Sthompsa
2269218676Shselasky		/*
2270218676Shselasky		 * Even though each stat is Tx-complete-status like format,
2271218676Shselasky		 * the device can poll stats. Because there is no guarantee
2272218676Shselasky		 * that the referring node is still around when read the stats.
2273218676Shselasky		 * So that, if we use ieee80211_ratectl_tx_update(), we will
2274218676Shselasky		 * have hard time not to refer already freed node.
2275218676Shselasky		 *
2276218676Shselasky		 * To eliminate such page faults, we poll stats in softc.
2277218676Shselasky		 * Then, update the rates later with ieee80211_ratectl_tx_update().
2278218676Shselasky		 */
2279218676Shselasky		wstat = &(sc->wcid_stats[wcid]);
2280218676Shselasky		(*wstat)[RUN_TXCNT]++;
2281218676Shselasky		if (stat & RT2860_TXQ_OK)
2282218676Shselasky			(*wstat)[RUN_SUCCESS]++;
2283218676Shselasky		else
2284208019Sthompsa			ifp->if_oerrors++;
2285218676Shselasky		/*
2286218676Shselasky		 * Check if there were retries, ie if the Tx success rate is
2287218676Shselasky		 * different from the requested rate. Note that it works only
2288218676Shselasky		 * because we do not allow rate fallback from OFDM to CCK.
2289218676Shselasky		 */
2290218676Shselasky		mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2291218676Shselasky		pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2292218676Shselasky		if ((retry = pid -1 - mcs) > 0) {
2293218676Shselasky			(*wstat)[RUN_TXCNT] += retry;
2294218676Shselasky			(*wstat)[RUN_RETRY] += retry;
2295203134Sthompsa		}
2296208019Sthompsa	}
2297208019Sthompsa	DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2298208019Sthompsa
2299208019Sthompsa	sc->fifo_cnt = 0;
2300208019Sthompsa}
2301208019Sthompsa
2302208019Sthompsastatic void
2303208019Sthompsarun_iter_func(void *arg, struct ieee80211_node *ni)
2304208019Sthompsa{
2305208019Sthompsa	struct run_softc *sc = arg;
2306208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2307208019Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2308208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2309208019Sthompsa	struct run_node *rn = (void *)ni;
2310218676Shselasky	union run_stats sta[2];
2311218676Shselasky	uint16_t (*wstat)[3];
2312218676Shselasky	int txcnt, success, retrycnt, error;
2313208019Sthompsa
2314218676Shselasky	RUN_LOCK(sc);
2315218676Shselasky
2316209917Sthompsa	if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2317209917Sthompsa	    vap->iv_opmode == IEEE80211_M_STA)) {
2318203134Sthompsa		/* read statistic counters (clear on read) and update AMRR state */
2319203134Sthompsa		error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2320203134Sthompsa		    sizeof sta);
2321203134Sthompsa		if (error != 0)
2322218676Shselasky			goto fail;
2323203134Sthompsa
2324203134Sthompsa		/* count failed TX as errors */
2325218676Shselasky		ifp->if_oerrors += le16toh(sta[0].error.fail);
2326203134Sthompsa
2327218676Shselasky		retrycnt = le16toh(sta[1].tx.retry);
2328218676Shselasky		success = le16toh(sta[1].tx.success);
2329218676Shselasky		txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2330203134Sthompsa
2331218676Shselasky		DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2332218676Shselasky			retrycnt, success, le16toh(sta[0].error.fail));
2333218676Shselasky	} else {
2334218676Shselasky		wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2335203134Sthompsa
2336218676Shselasky		if (wstat == &(sc->wcid_stats[0]) ||
2337218676Shselasky		    wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2338218676Shselasky			goto fail;
2339208019Sthompsa
2340218676Shselasky		txcnt = (*wstat)[RUN_TXCNT];
2341218676Shselasky		success = (*wstat)[RUN_SUCCESS];
2342218676Shselasky		retrycnt = (*wstat)[RUN_RETRY];
2343218676Shselasky		DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2344218676Shselasky		    retrycnt, txcnt, success);
2345208019Sthompsa
2346218676Shselasky		memset(wstat, 0, sizeof(*wstat));
2347203134Sthompsa	}
2348203134Sthompsa
2349218676Shselasky	ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2350208019Sthompsa	rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2351218676Shselasky
2352218676Shselaskyfail:
2353218676Shselasky	RUN_UNLOCK(sc);
2354218676Shselasky
2355208019Sthompsa	DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2356208019Sthompsa}
2357203134Sthompsa
2358208019Sthompsastatic void
2359208019Sthompsarun_newassoc_cb(void *arg)
2360208019Sthompsa{
2361208019Sthompsa	struct run_cmdq *cmdq = arg;
2362208019Sthompsa	struct ieee80211_node *ni = cmdq->arg1;
2363208019Sthompsa	struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2364208019Sthompsa	uint8_t wcid = cmdq->wcid;
2365203134Sthompsa
2366208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2367208019Sthompsa
2368208019Sthompsa	run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2369208019Sthompsa	    ni->ni_macaddr, IEEE80211_ADDR_LEN);
2370218676Shselasky
2371218676Shselasky	memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2372203134Sthompsa}
2373203134Sthompsa
2374203134Sthompsastatic void
2375203134Sthompsarun_newassoc(struct ieee80211_node *ni, int isnew)
2376203134Sthompsa{
2377203134Sthompsa	struct run_node *rn = (void *)ni;
2378203134Sthompsa	struct ieee80211_rateset *rs = &ni->ni_rates;
2379208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2380208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2381208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2382203134Sthompsa	uint8_t rate;
2383208019Sthompsa	uint8_t ridx;
2384245047Shselasky	uint8_t wcid;
2385208019Sthompsa	int i, j;
2386203134Sthompsa
2387245047Shselasky	wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2388245047Shselasky	    1 : RUN_AID2WCID(ni->ni_associd);
2389245047Shselasky
2390209917Sthompsa	if (wcid > RT2870_WCID_MAX) {
2391208019Sthompsa		device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2392208019Sthompsa		return;
2393208019Sthompsa	}
2394203134Sthompsa
2395208019Sthompsa	/* only interested in true associations */
2396209917Sthompsa	if (isnew && ni->ni_associd != 0) {
2397208019Sthompsa
2398208019Sthompsa		/*
2399208019Sthompsa		 * This function could is called though timeout function.
2400208019Sthompsa		 * Need to defer.
2401208019Sthompsa		 */
2402208019Sthompsa		uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2403208019Sthompsa		DPRINTF("cmdq_store=%d\n", cnt);
2404208019Sthompsa		sc->cmdq[cnt].func = run_newassoc_cb;
2405208019Sthompsa		sc->cmdq[cnt].arg0 = NULL;
2406208019Sthompsa		sc->cmdq[cnt].arg1 = ni;
2407208019Sthompsa		sc->cmdq[cnt].wcid = wcid;
2408208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
2409208019Sthompsa	}
2410208019Sthompsa
2411208019Sthompsa	DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2412208019Sthompsa	    isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2413208019Sthompsa
2414203134Sthompsa	for (i = 0; i < rs->rs_nrates; i++) {
2415203134Sthompsa		rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2416203134Sthompsa		/* convert 802.11 rate to hardware rate index */
2417203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2418203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
2419203134Sthompsa				break;
2420203134Sthompsa		rn->ridx[i] = ridx;
2421203134Sthompsa		/* determine rate of control response frames */
2422203134Sthompsa		for (j = i; j >= 0; j--) {
2423203134Sthompsa			if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2424203134Sthompsa			    rt2860_rates[rn->ridx[i]].phy ==
2425203134Sthompsa			    rt2860_rates[rn->ridx[j]].phy)
2426203134Sthompsa				break;
2427203134Sthompsa		}
2428203134Sthompsa		if (j >= 0) {
2429203134Sthompsa			rn->ctl_ridx[i] = rn->ridx[j];
2430203134Sthompsa		} else {
2431203134Sthompsa			/* no basic rate found, use mandatory one */
2432203134Sthompsa			rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2433203134Sthompsa		}
2434203134Sthompsa		DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2435203134Sthompsa		    rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2436203134Sthompsa	}
2437208019Sthompsa	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2438208019Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2439208019Sthompsa		if (rt2860_rates[ridx].rate == rate)
2440208019Sthompsa			break;
2441208019Sthompsa	rn->mgt_ridx = ridx;
2442208019Sthompsa	DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2443208019Sthompsa
2444208019Sthompsa	usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2445203134Sthompsa}
2446203134Sthompsa
2447203134Sthompsa/*
2448203134Sthompsa * Return the Rx chain with the highest RSSI for a given frame.
2449203134Sthompsa */
2450203134Sthompsastatic __inline uint8_t
2451203134Sthompsarun_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2452203134Sthompsa{
2453203134Sthompsa	uint8_t rxchain = 0;
2454203134Sthompsa
2455203134Sthompsa	if (sc->nrxchains > 1) {
2456203134Sthompsa		if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2457203134Sthompsa			rxchain = 1;
2458203134Sthompsa		if (sc->nrxchains > 2)
2459203134Sthompsa			if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2460203134Sthompsa				rxchain = 2;
2461203134Sthompsa	}
2462209917Sthompsa	return (rxchain);
2463203134Sthompsa}
2464203134Sthompsa
2465203134Sthompsastatic void
2466203134Sthompsarun_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2467203134Sthompsa{
2468203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2469203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2470203134Sthompsa	struct ieee80211_frame *wh;
2471203134Sthompsa	struct ieee80211_node *ni;
2472203134Sthompsa	struct rt2870_rxd *rxd;
2473203134Sthompsa	struct rt2860_rxwi *rxwi;
2474203134Sthompsa	uint32_t flags;
2475203134Sthompsa	uint16_t len, phy;
2476203134Sthompsa	uint8_t ant, rssi;
2477203134Sthompsa	int8_t nf;
2478203134Sthompsa
2479203134Sthompsa	rxwi = mtod(m, struct rt2860_rxwi *);
2480203134Sthompsa	len = le16toh(rxwi->len) & 0xfff;
2481203134Sthompsa	if (__predict_false(len > dmalen)) {
2482203134Sthompsa		m_freem(m);
2483203134Sthompsa		ifp->if_ierrors++;
2484203134Sthompsa		DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2485203134Sthompsa		return;
2486203134Sthompsa	}
2487203134Sthompsa	/* Rx descriptor is located at the end */
2488203134Sthompsa	rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2489203134Sthompsa	flags = le32toh(rxd->flags);
2490203134Sthompsa
2491203134Sthompsa	if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2492203134Sthompsa		m_freem(m);
2493203134Sthompsa		ifp->if_ierrors++;
2494203134Sthompsa		DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2495203134Sthompsa		return;
2496203134Sthompsa	}
2497203134Sthompsa
2498203134Sthompsa	m->m_data += sizeof(struct rt2860_rxwi);
2499203134Sthompsa	m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2500203134Sthompsa
2501203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2502203134Sthompsa
2503209917Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2504203134Sthompsa		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2505203134Sthompsa		m->m_flags |= M_WEP;
2506203134Sthompsa	}
2507203134Sthompsa
2508209917Sthompsa	if (flags & RT2860_RX_L2PAD) {
2509203134Sthompsa		DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2510203134Sthompsa		len += 2;
2511203134Sthompsa	}
2512203134Sthompsa
2513208019Sthompsa	ni = ieee80211_find_rxnode(ic,
2514208019Sthompsa	    mtod(m, struct ieee80211_frame_min *));
2515208019Sthompsa
2516203134Sthompsa	if (__predict_false(flags & RT2860_RX_MICERR)) {
2517203134Sthompsa		/* report MIC failures to net80211 for TKIP */
2518209917Sthompsa		if (ni != NULL)
2519208019Sthompsa			ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2520203134Sthompsa		m_freem(m);
2521203134Sthompsa		ifp->if_ierrors++;
2522203134Sthompsa		DPRINTF("MIC error. Someone is lying.\n");
2523203134Sthompsa		return;
2524203134Sthompsa	}
2525203134Sthompsa
2526203134Sthompsa	ant = run_maxrssi_chain(sc, rxwi);
2527203134Sthompsa	rssi = rxwi->rssi[ant];
2528203134Sthompsa	nf = run_rssi2dbm(sc, rssi, ant);
2529203134Sthompsa
2530203134Sthompsa	m->m_pkthdr.rcvif = ifp;
2531203134Sthompsa	m->m_pkthdr.len = m->m_len = len;
2532203134Sthompsa
2533203134Sthompsa	if (ni != NULL) {
2534203134Sthompsa		(void)ieee80211_input(ni, m, rssi, nf);
2535203134Sthompsa		ieee80211_free_node(ni);
2536203134Sthompsa	} else {
2537203134Sthompsa		(void)ieee80211_input_all(ic, m, rssi, nf);
2538203134Sthompsa	}
2539203134Sthompsa
2540209917Sthompsa	if (__predict_false(ieee80211_radiotap_active(ic))) {
2541203134Sthompsa		struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2542203134Sthompsa
2543203134Sthompsa		tap->wr_flags = 0;
2544236439Shselasky		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2545236439Shselasky		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2546203134Sthompsa		tap->wr_antsignal = rssi;
2547203134Sthompsa		tap->wr_antenna = ant;
2548203134Sthompsa		tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2549203134Sthompsa		tap->wr_rate = 2;	/* in case it can't be found below */
2550203134Sthompsa		phy = le16toh(rxwi->phy);
2551203134Sthompsa		switch (phy & RT2860_PHY_MODE) {
2552203134Sthompsa		case RT2860_PHY_CCK:
2553203134Sthompsa			switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2554203134Sthompsa			case 0:	tap->wr_rate =   2; break;
2555203134Sthompsa			case 1:	tap->wr_rate =   4; break;
2556203134Sthompsa			case 2:	tap->wr_rate =  11; break;
2557203134Sthompsa			case 3:	tap->wr_rate =  22; break;
2558203134Sthompsa			}
2559203134Sthompsa			if (phy & RT2860_PHY_SHPRE)
2560203134Sthompsa				tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2561203134Sthompsa			break;
2562203134Sthompsa		case RT2860_PHY_OFDM:
2563203134Sthompsa			switch (phy & RT2860_PHY_MCS) {
2564203134Sthompsa			case 0:	tap->wr_rate =  12; break;
2565203134Sthompsa			case 1:	tap->wr_rate =  18; break;
2566203134Sthompsa			case 2:	tap->wr_rate =  24; break;
2567203134Sthompsa			case 3:	tap->wr_rate =  36; break;
2568203134Sthompsa			case 4:	tap->wr_rate =  48; break;
2569203134Sthompsa			case 5:	tap->wr_rate =  72; break;
2570203134Sthompsa			case 6:	tap->wr_rate =  96; break;
2571203134Sthompsa			case 7:	tap->wr_rate = 108; break;
2572203134Sthompsa			}
2573203134Sthompsa			break;
2574203134Sthompsa		}
2575203134Sthompsa	}
2576203134Sthompsa}
2577203134Sthompsa
2578203134Sthompsastatic void
2579203134Sthompsarun_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2580203134Sthompsa{
2581203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2582203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2583203134Sthompsa	struct mbuf *m = NULL;
2584203134Sthompsa	struct mbuf *m0;
2585203134Sthompsa	uint32_t dmalen;
2586203134Sthompsa	int xferlen;
2587203134Sthompsa
2588203134Sthompsa	usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2589203134Sthompsa
2590203134Sthompsa	switch (USB_GET_STATE(xfer)) {
2591203134Sthompsa	case USB_ST_TRANSFERRED:
2592203134Sthompsa
2593203134Sthompsa		DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2594203134Sthompsa
2595233774Shselasky		if (xferlen < (int)(sizeof(uint32_t) +
2596233774Shselasky		    sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) {
2597203134Sthompsa			DPRINTF("xfer too short %d\n", xferlen);
2598203134Sthompsa			goto tr_setup;
2599203134Sthompsa		}
2600203134Sthompsa
2601203134Sthompsa		m = sc->rx_m;
2602203134Sthompsa		sc->rx_m = NULL;
2603203134Sthompsa
2604203134Sthompsa		/* FALLTHROUGH */
2605203134Sthompsa	case USB_ST_SETUP:
2606203134Sthompsatr_setup:
2607203134Sthompsa		if (sc->rx_m == NULL) {
2608243857Sglebius			sc->rx_m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
2609203134Sthompsa			    MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2610203134Sthompsa		}
2611203134Sthompsa		if (sc->rx_m == NULL) {
2612203134Sthompsa			DPRINTF("could not allocate mbuf - idle with stall\n");
2613203134Sthompsa			ifp->if_ierrors++;
2614203134Sthompsa			usbd_xfer_set_stall(xfer);
2615203134Sthompsa			usbd_xfer_set_frames(xfer, 0);
2616203134Sthompsa		} else {
2617203134Sthompsa			/*
2618203134Sthompsa			 * Directly loading a mbuf cluster into DMA to
2619203134Sthompsa			 * save some data copying. This works because
2620203134Sthompsa			 * there is only one cluster.
2621203134Sthompsa			 */
2622203134Sthompsa			usbd_xfer_set_frame_data(xfer, 0,
2623203134Sthompsa			    mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2624203134Sthompsa			usbd_xfer_set_frames(xfer, 1);
2625203134Sthompsa		}
2626203134Sthompsa		usbd_transfer_submit(xfer);
2627203134Sthompsa		break;
2628203134Sthompsa
2629203134Sthompsa	default:	/* Error */
2630203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2631203134Sthompsa			/* try to clear stall first */
2632203134Sthompsa			usbd_xfer_set_stall(xfer);
2633203134Sthompsa
2634203134Sthompsa			if (error == USB_ERR_TIMEOUT)
2635203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2636203134Sthompsa
2637203134Sthompsa			ifp->if_ierrors++;
2638203134Sthompsa
2639203134Sthompsa			goto tr_setup;
2640203134Sthompsa		}
2641209917Sthompsa		if (sc->rx_m != NULL) {
2642203134Sthompsa			m_freem(sc->rx_m);
2643203134Sthompsa			sc->rx_m = NULL;
2644203134Sthompsa		}
2645203134Sthompsa		break;
2646203134Sthompsa	}
2647203134Sthompsa
2648203134Sthompsa	if (m == NULL)
2649203134Sthompsa		return;
2650203134Sthompsa
2651203134Sthompsa	/* inputting all the frames must be last */
2652203134Sthompsa
2653203134Sthompsa	RUN_UNLOCK(sc);
2654203134Sthompsa
2655203134Sthompsa	m->m_pkthdr.len = m->m_len = xferlen;
2656203134Sthompsa
2657203134Sthompsa	/* HW can aggregate multiple 802.11 frames in a single USB xfer */
2658203134Sthompsa	for(;;) {
2659203134Sthompsa		dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2660203134Sthompsa
2661233774Shselasky		if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2662233774Shselasky		    ((dmalen & 3) != 0)) {
2663203134Sthompsa			DPRINTF("bad DMA length %u\n", dmalen);
2664203134Sthompsa			break;
2665203134Sthompsa		}
2666233774Shselasky		if ((dmalen + 8) > (uint32_t)xferlen) {
2667203134Sthompsa			DPRINTF("bad DMA length %u > %d\n",
2668203134Sthompsa			dmalen + 8, xferlen);
2669203134Sthompsa			break;
2670203134Sthompsa		}
2671203134Sthompsa
2672203134Sthompsa		/* If it is the last one or a single frame, we won't copy. */
2673209917Sthompsa		if ((xferlen -= dmalen + 8) <= 8) {
2674203134Sthompsa			/* trim 32-bit DMA-len header */
2675203134Sthompsa			m->m_data += 4;
2676203134Sthompsa			m->m_pkthdr.len = m->m_len -= 4;
2677203134Sthompsa			run_rx_frame(sc, m, dmalen);
2678203134Sthompsa			break;
2679203134Sthompsa		}
2680203134Sthompsa
2681203134Sthompsa		/* copy aggregated frames to another mbuf */
2682243857Sglebius		m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2683203134Sthompsa		if (__predict_false(m0 == NULL)) {
2684203134Sthompsa			DPRINTF("could not allocate mbuf\n");
2685203134Sthompsa			ifp->if_ierrors++;
2686203134Sthompsa			break;
2687203134Sthompsa		}
2688203134Sthompsa		m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2689203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2690203134Sthompsa		m0->m_pkthdr.len = m0->m_len =
2691203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd);
2692203134Sthompsa		run_rx_frame(sc, m0, dmalen);
2693203134Sthompsa
2694203134Sthompsa		/* update data ptr */
2695203134Sthompsa		m->m_data += dmalen + 8;
2696203134Sthompsa		m->m_pkthdr.len = m->m_len -= dmalen + 8;
2697203134Sthompsa	}
2698203134Sthompsa
2699203134Sthompsa	RUN_LOCK(sc);
2700203134Sthompsa}
2701203134Sthompsa
2702203134Sthompsastatic void
2703203134Sthompsarun_tx_free(struct run_endpoint_queue *pq,
2704203134Sthompsa    struct run_tx_data *data, int txerr)
2705203134Sthompsa{
2706203134Sthompsa	if (data->m != NULL) {
2707203134Sthompsa		if (data->m->m_flags & M_TXCB)
2708203134Sthompsa			ieee80211_process_callback(data->ni, data->m,
2709203134Sthompsa			    txerr ? ETIMEDOUT : 0);
2710203134Sthompsa		m_freem(data->m);
2711203134Sthompsa		data->m = NULL;
2712203134Sthompsa
2713209917Sthompsa		if (data->ni == NULL) {
2714203134Sthompsa			DPRINTF("no node\n");
2715203134Sthompsa		} else {
2716203134Sthompsa			ieee80211_free_node(data->ni);
2717203134Sthompsa			data->ni = NULL;
2718203134Sthompsa		}
2719203134Sthompsa	}
2720203134Sthompsa
2721203134Sthompsa	STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2722203134Sthompsa	pq->tx_nfree++;
2723203134Sthompsa}
2724203134Sthompsa
2725203134Sthompsastatic void
2726203134Sthompsarun_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index)
2727203134Sthompsa{
2728203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2729203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2730208019Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2731203134Sthompsa	struct run_tx_data *data;
2732203134Sthompsa	struct ieee80211vap *vap = NULL;
2733203134Sthompsa	struct usb_page_cache *pc;
2734203134Sthompsa	struct run_endpoint_queue *pq = &sc->sc_epq[index];
2735203134Sthompsa	struct mbuf *m;
2736203134Sthompsa	usb_frlength_t size;
2737203134Sthompsa	int actlen;
2738203134Sthompsa	int sumlen;
2739203134Sthompsa
2740203134Sthompsa	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2741203134Sthompsa
2742209917Sthompsa	switch (USB_GET_STATE(xfer)) {
2743203134Sthompsa	case USB_ST_TRANSFERRED:
2744203134Sthompsa		DPRINTFN(11, "transfer complete: %d "
2745203134Sthompsa		    "bytes @ index %d\n", actlen, index);
2746203134Sthompsa
2747203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2748203134Sthompsa
2749203134Sthompsa		run_tx_free(pq, data, 0);
2750203134Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2751203134Sthompsa
2752203134Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2753203134Sthompsa
2754203134Sthompsa		ifp->if_opackets++;
2755203134Sthompsa
2756203134Sthompsa		/* FALLTHROUGH */
2757203134Sthompsa	case USB_ST_SETUP:
2758203134Sthompsatr_setup:
2759203134Sthompsa		data = STAILQ_FIRST(&pq->tx_qh);
2760209917Sthompsa		if (data == NULL)
2761203134Sthompsa			break;
2762203134Sthompsa
2763203134Sthompsa		STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2764203134Sthompsa
2765203134Sthompsa		m = data->m;
2766228508Shselasky		if ((m->m_pkthdr.len +
2767228508Shselasky		    sizeof(data->desc) + 3 + 8) > RUN_MAX_TXSZ) {
2768203134Sthompsa			DPRINTF("data overflow, %u bytes\n",
2769203134Sthompsa			    m->m_pkthdr.len);
2770203134Sthompsa
2771203134Sthompsa			ifp->if_oerrors++;
2772203134Sthompsa
2773203134Sthompsa			run_tx_free(pq, data, 1);
2774203134Sthompsa
2775203134Sthompsa			goto tr_setup;
2776203134Sthompsa		}
2777203134Sthompsa
2778203134Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2779203134Sthompsa		size = sizeof(data->desc);
2780203134Sthompsa		usbd_copy_in(pc, 0, &data->desc, size);
2781203134Sthompsa		usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2782228508Shselasky		size += m->m_pkthdr.len;
2783228508Shselasky		/*
2784228508Shselasky		 * Align end on a 4-byte boundary, pad 8 bytes (CRC +
2785228508Shselasky		 * 4-byte padding), and be sure to zero those trailing
2786228508Shselasky		 * bytes:
2787228508Shselasky		 */
2788228508Shselasky		usbd_frame_zero(pc, size, ((-size) & 3) + 8);
2789228508Shselasky		size += ((-size) & 3) + 8;
2790203134Sthompsa
2791203134Sthompsa		vap = data->ni->ni_vap;
2792203134Sthompsa		if (ieee80211_radiotap_active_vap(vap)) {
2793203134Sthompsa			struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2794208019Sthompsa			struct rt2860_txwi *txwi =
2795208019Sthompsa			    (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2796203134Sthompsa
2797203134Sthompsa			tap->wt_flags = 0;
2798203134Sthompsa			tap->wt_rate = rt2860_rates[data->ridx].rate;
2799236439Shselasky			tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2800236439Shselasky			tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2801203134Sthompsa			tap->wt_hwqueue = index;
2802208019Sthompsa			if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2803203134Sthompsa				tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2804203134Sthompsa
2805203134Sthompsa			ieee80211_radiotap_tx(vap, m);
2806203134Sthompsa		}
2807203134Sthompsa
2808228508Shselasky		DPRINTFN(11, "sending frame len=%u/%u  @ index %d\n",
2809228508Shselasky		    m->m_pkthdr.len, size, index);
2810203134Sthompsa
2811228508Shselasky		usbd_xfer_set_frame_len(xfer, 0, size);
2812203134Sthompsa		usbd_xfer_set_priv(xfer, data);
2813203134Sthompsa
2814203134Sthompsa		usbd_transfer_submit(xfer);
2815203134Sthompsa
2816203134Sthompsa		RUN_UNLOCK(sc);
2817203134Sthompsa		run_start(ifp);
2818203134Sthompsa		RUN_LOCK(sc);
2819203134Sthompsa
2820203134Sthompsa		break;
2821203134Sthompsa
2822203134Sthompsa	default:
2823203134Sthompsa		DPRINTF("USB transfer error, %s\n",
2824203134Sthompsa		    usbd_errstr(error));
2825203134Sthompsa
2826203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2827203134Sthompsa
2828203134Sthompsa		ifp->if_oerrors++;
2829203134Sthompsa
2830203134Sthompsa		if (data != NULL) {
2831208019Sthompsa			if(data->ni != NULL)
2832208019Sthompsa				vap = data->ni->ni_vap;
2833203134Sthompsa			run_tx_free(pq, data, error);
2834203134Sthompsa			usbd_xfer_set_priv(xfer, NULL);
2835203134Sthompsa		}
2836209917Sthompsa		if (vap == NULL)
2837208019Sthompsa			vap = TAILQ_FIRST(&ic->ic_vaps);
2838203134Sthompsa
2839203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2840203134Sthompsa			if (error == USB_ERR_TIMEOUT) {
2841203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2842208019Sthompsa				uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2843208019Sthompsa				DPRINTF("cmdq_store=%d\n", i);
2844208019Sthompsa				sc->cmdq[i].func = run_usb_timeout_cb;
2845208019Sthompsa				sc->cmdq[i].arg0 = vap;
2846208019Sthompsa				ieee80211_runtask(ic, &sc->cmdq_task);
2847203134Sthompsa			}
2848203134Sthompsa
2849203134Sthompsa			/*
2850203134Sthompsa			 * Try to clear stall first, also if other
2851203134Sthompsa			 * errors occur, hence clearing stall
2852203134Sthompsa			 * introduces a 50 ms delay:
2853203134Sthompsa			 */
2854203134Sthompsa			usbd_xfer_set_stall(xfer);
2855203134Sthompsa			goto tr_setup;
2856203134Sthompsa		}
2857203134Sthompsa		break;
2858203134Sthompsa	}
2859203134Sthompsa}
2860203134Sthompsa
2861203134Sthompsastatic void
2862203134Sthompsarun_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2863203134Sthompsa{
2864203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 0);
2865203134Sthompsa}
2866203134Sthompsa
2867203134Sthompsastatic void
2868203134Sthompsarun_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2869203134Sthompsa{
2870203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 1);
2871203134Sthompsa}
2872203134Sthompsa
2873203134Sthompsastatic void
2874203134Sthompsarun_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2875203134Sthompsa{
2876203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 2);
2877203134Sthompsa}
2878203134Sthompsa
2879203134Sthompsastatic void
2880203134Sthompsarun_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2881203134Sthompsa{
2882203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 3);
2883203134Sthompsa}
2884203134Sthompsa
2885203134Sthompsastatic void
2886203134Sthompsarun_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2887203134Sthompsa{
2888203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 4);
2889203134Sthompsa}
2890203134Sthompsa
2891203134Sthompsastatic void
2892203134Sthompsarun_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2893203134Sthompsa{
2894203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 5);
2895203134Sthompsa}
2896203134Sthompsa
2897203134Sthompsastatic void
2898208019Sthompsarun_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2899203134Sthompsa{
2900203134Sthompsa	struct mbuf *m = data->m;
2901203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2902208019Sthompsa	struct ieee80211vap *vap = data->ni->ni_vap;
2903203134Sthompsa	struct ieee80211_frame *wh;
2904203134Sthompsa	struct rt2870_txd *txd;
2905203134Sthompsa	struct rt2860_txwi *txwi;
2906208019Sthompsa	uint16_t xferlen;
2907208019Sthompsa	uint16_t mcs;
2908203134Sthompsa	uint8_t ridx = data->ridx;
2909208019Sthompsa	uint8_t pad;
2910203134Sthompsa
2911203134Sthompsa	/* get MCS code from rate index */
2912208019Sthompsa	mcs = rt2860_rates[ridx].mcs;
2913203134Sthompsa
2914203134Sthompsa	xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2915203134Sthompsa
2916203134Sthompsa	/* roundup to 32-bit alignment */
2917203134Sthompsa	xferlen = (xferlen + 3) & ~3;
2918203134Sthompsa
2919203134Sthompsa	txd = (struct rt2870_txd *)&data->desc;
2920203134Sthompsa	txd->len = htole16(xferlen);
2921203134Sthompsa
2922208019Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2923208019Sthompsa
2924208019Sthompsa	/*
2925208019Sthompsa	 * Ether both are true or both are false, the header
2926208019Sthompsa	 * are nicely aligned to 32-bit. So, no L2 padding.
2927208019Sthompsa	 */
2928208019Sthompsa	if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2929208019Sthompsa		pad = 0;
2930208019Sthompsa	else
2931208019Sthompsa		pad = 2;
2932208019Sthompsa
2933203134Sthompsa	/* setup TX Wireless Information */
2934203134Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
2935203134Sthompsa	txwi->len = htole16(m->m_pkthdr.len - pad);
2936203134Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2937203134Sthompsa		txwi->phy = htole16(RT2860_PHY_CCK);
2938203134Sthompsa		if (ridx != RT2860_RIDX_CCK1 &&
2939203134Sthompsa		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2940203134Sthompsa			mcs |= RT2860_PHY_SHPRE;
2941203134Sthompsa	} else
2942203134Sthompsa		txwi->phy = htole16(RT2860_PHY_OFDM);
2943203134Sthompsa	txwi->phy |= htole16(mcs);
2944203134Sthompsa
2945203134Sthompsa	/* check if RTS/CTS or CTS-to-self protection is required */
2946203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2947203134Sthompsa	    (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2948203134Sthompsa	     ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2949203134Sthompsa	      rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
2950208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_HT;
2951203134Sthompsa	else
2952208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_BACKOFF;
2953209144Sthompsa
2954209917Sthompsa	if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
2955209144Sthompsa		txwi->xflags |= RT2860_TX_NSEQ;
2956203134Sthompsa}
2957203134Sthompsa
2958203134Sthompsa/* This function must be called locked */
2959203134Sthompsastatic int
2960203134Sthompsarun_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2961203134Sthompsa{
2962203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2963208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2964203134Sthompsa	struct ieee80211_frame *wh;
2965208019Sthompsa	struct ieee80211_channel *chan;
2966203134Sthompsa	const struct ieee80211_txparam *tp;
2967208019Sthompsa	struct run_node *rn = (void *)ni;
2968203134Sthompsa	struct run_tx_data *data;
2969208019Sthompsa	struct rt2870_txd *txd;
2970208019Sthompsa	struct rt2860_txwi *txwi;
2971203134Sthompsa	uint16_t qos;
2972203134Sthompsa	uint16_t dur;
2973208019Sthompsa	uint16_t qid;
2974203134Sthompsa	uint8_t type;
2975203134Sthompsa	uint8_t tid;
2976208019Sthompsa	uint8_t ridx;
2977208019Sthompsa	uint8_t ctl_ridx;
2978203134Sthompsa	uint8_t qflags;
2979203134Sthompsa	uint8_t xflags = 0;
2980203134Sthompsa	int hasqos;
2981203134Sthompsa
2982203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2983203134Sthompsa
2984203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2985203134Sthompsa
2986203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2987203134Sthompsa
2988203134Sthompsa	/*
2989203134Sthompsa	 * There are 7 bulk endpoints: 1 for RX
2990203134Sthompsa	 * and 6 for TX (4 EDCAs + HCCA + Prio).
2991203134Sthompsa	 * Update 03-14-2009:  some devices like the Planex GW-US300MiniS
2992203134Sthompsa	 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
2993203134Sthompsa	 */
2994203134Sthompsa	if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
2995203134Sthompsa		uint8_t *frm;
2996203134Sthompsa
2997203134Sthompsa		if(IEEE80211_HAS_ADDR4(wh))
2998203134Sthompsa			frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
2999203134Sthompsa		else
3000203134Sthompsa			frm =((struct ieee80211_qosframe *)wh)->i_qos;
3001203134Sthompsa
3002203134Sthompsa		qos = le16toh(*(const uint16_t *)frm);
3003203134Sthompsa		tid = qos & IEEE80211_QOS_TID;
3004203134Sthompsa		qid = TID_TO_WME_AC(tid);
3005203134Sthompsa	} else {
3006203134Sthompsa		qos = 0;
3007203134Sthompsa		tid = 0;
3008203134Sthompsa		qid = WME_AC_BE;
3009203134Sthompsa	}
3010203134Sthompsa	qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
3011203134Sthompsa
3012203134Sthompsa	DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
3013203134Sthompsa	    qos, qid, tid, qflags);
3014203134Sthompsa
3015208019Sthompsa	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
3016208019Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
3017203134Sthompsa
3018203134Sthompsa	/* pickup a rate index */
3019203134Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
3020203134Sthompsa	    type != IEEE80211_FC0_TYPE_DATA) {
3021203134Sthompsa		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3022203134Sthompsa		    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3023203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3024203134Sthompsa	} else {
3025208019Sthompsa		if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3026208019Sthompsa			ridx = rn->fix_ridx;
3027208019Sthompsa		else
3028208019Sthompsa			ridx = rn->amrr_ridx;
3029203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3030203134Sthompsa	}
3031203134Sthompsa
3032203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3033203134Sthompsa	    (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3034203134Sthompsa	     IEEE80211_QOS_ACKPOLICY_NOACK)) {
3035209144Sthompsa		xflags |= RT2860_TX_ACK;
3036203134Sthompsa		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3037208019Sthompsa			dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3038203134Sthompsa		else
3039208019Sthompsa			dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3040205042Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3041203134Sthompsa	}
3042203134Sthompsa
3043203134Sthompsa	/* reserve slots for mgmt packets, just in case */
3044203134Sthompsa	if (sc->sc_epq[qid].tx_nfree < 3) {
3045203134Sthompsa		DPRINTFN(10, "tx ring %d is full\n", qid);
3046203134Sthompsa		return (-1);
3047203134Sthompsa	}
3048203134Sthompsa
3049203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3050203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3051203134Sthompsa	sc->sc_epq[qid].tx_nfree--;
3052203134Sthompsa
3053208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3054208019Sthompsa	txd->flags = qflags;
3055208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3056208019Sthompsa	txwi->xflags = xflags;
3057245047Shselasky	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3058245047Shselasky		txwi->wcid = 0;
3059245047Shselasky	} else {
3060245047Shselasky		txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
3061245047Shselasky		    1 : RUN_AID2WCID(ni->ni_associd);
3062245047Shselasky	}
3063208019Sthompsa	/* clear leftover garbage bits */
3064208019Sthompsa	txwi->flags = 0;
3065208019Sthompsa	txwi->txop = 0;
3066208019Sthompsa
3067203134Sthompsa	data->m = m;
3068203134Sthompsa	data->ni = ni;
3069203134Sthompsa	data->ridx = ridx;
3070203134Sthompsa
3071208019Sthompsa	run_set_tx_desc(sc, data);
3072203134Sthompsa
3073208019Sthompsa	/*
3074208019Sthompsa	 * The chip keeps track of 2 kind of Tx stats,
3075208019Sthompsa	 *  * TX_STAT_FIFO, for per WCID stats, and
3076208019Sthompsa	 *  * TX_STA_CNT0 for all-TX-in-one stats.
3077208019Sthompsa	 *
3078208019Sthompsa	 * To use FIFO stats, we need to store MCS into the driver-private
3079208019Sthompsa 	 * PacketID field. So that, we can tell whose stats when we read them.
3080208019Sthompsa 	 * We add 1 to the MCS because setting the PacketID field to 0 means
3081208019Sthompsa 	 * that we don't want feedback in TX_STAT_FIFO.
3082208019Sthompsa 	 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3083208019Sthompsa 	 *
3084208019Sthompsa 	 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3085208019Sthompsa 	 */
3086209917Sthompsa	if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3087209917Sthompsa	    vap->iv_opmode == IEEE80211_M_MBSS) {
3088208019Sthompsa		uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3089208019Sthompsa		txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3090208019Sthompsa
3091208019Sthompsa		/*
3092208019Sthompsa		 * Unlike PCI based devices, we don't get any interrupt from
3093208019Sthompsa		 * USB devices, so we simulate FIFO-is-full interrupt here.
3094208019Sthompsa		 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3095208019Sthompsa		 * quickly get fulled. To prevent overflow, increment a counter on
3096208019Sthompsa		 * every FIFO stat request, so we know how many slots are left.
3097208019Sthompsa		 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3098208019Sthompsa		 * are used only in those modes.
3099208019Sthompsa		 * We just drain stats. AMRR gets updated every 1 sec by
3100208019Sthompsa		 * run_ratectl_cb() via callout.
3101208019Sthompsa		 * Call it early. Otherwise overflow.
3102208019Sthompsa		 */
3103209917Sthompsa		if (sc->fifo_cnt++ == 10) {
3104208019Sthompsa			/*
3105208019Sthompsa			 * With multiple vaps or if_bridge, if_start() is called
3106208019Sthompsa			 * with a non-sleepable lock, tcpinp. So, need to defer.
3107208019Sthompsa			 */
3108208019Sthompsa			uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3109208019Sthompsa			DPRINTFN(6, "cmdq_store=%d\n", i);
3110208019Sthompsa			sc->cmdq[i].func = run_drain_fifo;
3111208019Sthompsa			sc->cmdq[i].arg0 = sc;
3112208019Sthompsa			ieee80211_runtask(ic, &sc->cmdq_task);
3113208019Sthompsa		}
3114208019Sthompsa	}
3115208019Sthompsa
3116203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3117203134Sthompsa
3118203134Sthompsa	usbd_transfer_start(sc->sc_xfer[qid]);
3119203134Sthompsa
3120203134Sthompsa	DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3121203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3122203134Sthompsa	    rt2860_rates[ridx].rate, qid);
3123203134Sthompsa
3124203134Sthompsa	return (0);
3125203134Sthompsa}
3126203134Sthompsa
3127203134Sthompsastatic int
3128203134Sthompsarun_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3129203134Sthompsa{
3130203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3131203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3132208019Sthompsa	struct run_node *rn = (void *)ni;
3133203134Sthompsa	struct run_tx_data *data;
3134203134Sthompsa	struct ieee80211_frame *wh;
3135208019Sthompsa	struct rt2870_txd *txd;
3136208019Sthompsa	struct rt2860_txwi *txwi;
3137203134Sthompsa	uint16_t dur;
3138208019Sthompsa	uint8_t ridx = rn->mgt_ridx;
3139203134Sthompsa	uint8_t type;
3140203134Sthompsa	uint8_t xflags = 0;
3141208019Sthompsa	uint8_t wflags = 0;
3142203134Sthompsa
3143203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3144203134Sthompsa
3145203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3146203134Sthompsa
3147203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3148203134Sthompsa
3149208019Sthompsa	/* tell hardware to add timestamp for probe responses */
3150208019Sthompsa	if ((wh->i_fc[0] &
3151208019Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3152208019Sthompsa	    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3153208019Sthompsa		wflags |= RT2860_TX_TS;
3154208019Sthompsa	else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3155203134Sthompsa		xflags |= RT2860_TX_ACK;
3156203134Sthompsa
3157208019Sthompsa		dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3158203134Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3159203134Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3160203134Sthompsa	}
3161203134Sthompsa
3162203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3163203134Sthompsa		/* let caller free mbuf */
3164203134Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3165203134Sthompsa		return (EIO);
3166203134Sthompsa	}
3167203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3168203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3169203134Sthompsa	sc->sc_epq[0].tx_nfree--;
3170203134Sthompsa
3171208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3172208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3173208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3174208019Sthompsa	txwi->wcid = 0xff;
3175208019Sthompsa	txwi->flags = wflags;
3176208019Sthompsa	txwi->xflags = xflags;
3177208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3178208019Sthompsa
3179203134Sthompsa	data->m = m;
3180203134Sthompsa	data->ni = ni;
3181203134Sthompsa	data->ridx = ridx;
3182203134Sthompsa
3183208019Sthompsa	run_set_tx_desc(sc, data);
3184203134Sthompsa
3185203134Sthompsa	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3186203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3187208019Sthompsa	    rt2860_rates[ridx].rate);
3188203134Sthompsa
3189203134Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3190203134Sthompsa
3191203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3192203134Sthompsa
3193203134Sthompsa	return (0);
3194203134Sthompsa}
3195203134Sthompsa
3196203134Sthompsastatic int
3197203134Sthompsarun_sendprot(struct run_softc *sc,
3198203134Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3199203134Sthompsa{
3200203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3201203134Sthompsa	struct ieee80211_frame *wh;
3202203134Sthompsa	struct run_tx_data *data;
3203208019Sthompsa	struct rt2870_txd *txd;
3204208019Sthompsa	struct rt2860_txwi *txwi;
3205203134Sthompsa	struct mbuf *mprot;
3206203134Sthompsa	int ridx;
3207203134Sthompsa	int protrate;
3208203134Sthompsa	int ackrate;
3209203134Sthompsa	int pktlen;
3210203134Sthompsa	int isshort;
3211203134Sthompsa	uint16_t dur;
3212203134Sthompsa	uint8_t type;
3213208019Sthompsa	uint8_t wflags = 0;
3214208019Sthompsa	uint8_t xflags = 0;
3215203134Sthompsa
3216203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3217203134Sthompsa
3218203134Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3219203134Sthompsa	    ("protection %d", prot));
3220203134Sthompsa
3221203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3222203134Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3223203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3224203134Sthompsa
3225203134Sthompsa	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3226203134Sthompsa	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3227203134Sthompsa
3228203134Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3229209189Sjkim	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3230203134Sthompsa	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3231203134Sthompsa	wflags = RT2860_TX_FRAG;
3232203134Sthompsa
3233203134Sthompsa	/* check that there are free slots before allocating the mbuf */
3234203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3235203134Sthompsa		/* let caller free mbuf */
3236203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3237203134Sthompsa		return (ENOBUFS);
3238203134Sthompsa	}
3239203134Sthompsa
3240203134Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
3241203134Sthompsa		/* NB: CTS is the same size as an ACK */
3242203134Sthompsa		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3243208019Sthompsa		xflags |= RT2860_TX_ACK;
3244203134Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3245203134Sthompsa	} else {
3246203134Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3247203134Sthompsa	}
3248203134Sthompsa	if (mprot == NULL) {
3249203134Sthompsa		sc->sc_ifp->if_oerrors++;
3250203134Sthompsa		DPRINTF("could not allocate mbuf\n");
3251203134Sthompsa		return (ENOBUFS);
3252203134Sthompsa	}
3253203134Sthompsa
3254203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3255203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3256203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3257203134Sthompsa
3258208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3259208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3260208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3261208019Sthompsa	txwi->wcid = 0xff;
3262208019Sthompsa	txwi->flags = wflags;
3263208019Sthompsa	txwi->xflags = xflags;
3264208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3265208019Sthompsa
3266203134Sthompsa	data->m = mprot;
3267203134Sthompsa	data->ni = ieee80211_ref_node(ni);
3268203134Sthompsa
3269203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3270203134Sthompsa		if (rt2860_rates[ridx].rate == protrate)
3271203134Sthompsa			break;
3272203134Sthompsa	data->ridx = ridx;
3273203134Sthompsa
3274208019Sthompsa	run_set_tx_desc(sc, data);
3275203134Sthompsa
3276203134Sthompsa        DPRINTFN(1, "sending prot len=%u rate=%u\n",
3277203134Sthompsa            m->m_pkthdr.len, rate);
3278203134Sthompsa
3279203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3280203134Sthompsa
3281203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3282203134Sthompsa
3283203134Sthompsa	return (0);
3284203134Sthompsa}
3285203134Sthompsa
3286203134Sthompsastatic int
3287203134Sthompsarun_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3288203134Sthompsa    const struct ieee80211_bpf_params *params)
3289203134Sthompsa{
3290203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3291203134Sthompsa	struct ieee80211_frame *wh;
3292203134Sthompsa	struct run_tx_data *data;
3293208019Sthompsa	struct rt2870_txd *txd;
3294208019Sthompsa	struct rt2860_txwi *txwi;
3295203134Sthompsa	uint8_t type;
3296208019Sthompsa	uint8_t ridx;
3297208019Sthompsa	uint8_t rate;
3298208019Sthompsa	uint8_t opflags = 0;
3299208019Sthompsa	uint8_t xflags = 0;
3300203134Sthompsa	int error;
3301203134Sthompsa
3302203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3303203134Sthompsa
3304203134Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
3305203134Sthompsa
3306203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3307203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3308203134Sthompsa
3309203134Sthompsa	rate = params->ibp_rate0;
3310203134Sthompsa	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3311203134Sthompsa		/* let caller free mbuf */
3312203134Sthompsa		return (EINVAL);
3313203134Sthompsa	}
3314203134Sthompsa
3315203134Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3316208019Sthompsa		xflags |= RT2860_TX_ACK;
3317203134Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3318203134Sthompsa		error = run_sendprot(sc, m, ni,
3319203134Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
3320203134Sthompsa			IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3321203134Sthompsa		    rate);
3322203134Sthompsa		if (error) {
3323203134Sthompsa			/* let caller free mbuf */
3324209917Sthompsa			return error;
3325203134Sthompsa		}
3326203134Sthompsa		opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3327203134Sthompsa	}
3328203134Sthompsa
3329203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3330203134Sthompsa		/* let caller free mbuf */
3331203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3332203134Sthompsa		DPRINTF("sending raw frame, but tx ring is full\n");
3333203134Sthompsa		return (EIO);
3334203134Sthompsa	}
3335203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3336203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3337203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3338203134Sthompsa
3339208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3340208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3341208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3342208019Sthompsa	txwi->wcid = 0xff;
3343208019Sthompsa	txwi->xflags = xflags;
3344208019Sthompsa	txwi->txop = opflags;
3345208019Sthompsa	txwi->flags = 0;	/* clear leftover garbage bits */
3346208019Sthompsa
3347203134Sthompsa        data->m = m;
3348203134Sthompsa        data->ni = ni;
3349203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3350203134Sthompsa		if (rt2860_rates[ridx].rate == rate)
3351203134Sthompsa			break;
3352203134Sthompsa	data->ridx = ridx;
3353203134Sthompsa
3354208019Sthompsa        run_set_tx_desc(sc, data);
3355203134Sthompsa
3356203134Sthompsa        DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3357203134Sthompsa            m->m_pkthdr.len, rate);
3358203134Sthompsa
3359203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3360203134Sthompsa
3361203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3362203134Sthompsa
3363209917Sthompsa        return (0);
3364203134Sthompsa}
3365203134Sthompsa
3366203134Sthompsastatic int
3367203134Sthompsarun_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3368203134Sthompsa    const struct ieee80211_bpf_params *params)
3369203134Sthompsa{
3370203134Sthompsa	struct ifnet *ifp = ni->ni_ic->ic_ifp;
3371203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3372208019Sthompsa	int error = 0;
3373208019Sthompsa
3374203134Sthompsa	RUN_LOCK(sc);
3375203134Sthompsa
3376203134Sthompsa	/* prevent management frames from being sent if we're not ready */
3377203134Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3378203134Sthompsa		error =  ENETDOWN;
3379208019Sthompsa		goto done;
3380203134Sthompsa	}
3381203134Sthompsa
3382203134Sthompsa	if (params == NULL) {
3383203134Sthompsa		/* tx mgt packet */
3384209917Sthompsa		if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3385203134Sthompsa			ifp->if_oerrors++;
3386203134Sthompsa			DPRINTF("mgt tx failed\n");
3387208019Sthompsa			goto done;
3388203134Sthompsa		}
3389203134Sthompsa	} else {
3390203134Sthompsa		/* tx raw packet with param */
3391209917Sthompsa		if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3392203134Sthompsa			ifp->if_oerrors++;
3393203134Sthompsa			DPRINTF("tx with param failed\n");
3394208019Sthompsa			goto done;
3395203134Sthompsa		}
3396203134Sthompsa	}
3397203134Sthompsa
3398203134Sthompsa	ifp->if_opackets++;
3399203134Sthompsa
3400208019Sthompsadone:
3401203134Sthompsa	RUN_UNLOCK(sc);
3402203134Sthompsa
3403209917Sthompsa	if (error != 0) {
3404208019Sthompsa		if(m != NULL)
3405208019Sthompsa			m_freem(m);
3406208019Sthompsa		ieee80211_free_node(ni);
3407208019Sthompsa	}
3408203134Sthompsa
3409203134Sthompsa	return (error);
3410203134Sthompsa}
3411203134Sthompsa
3412203134Sthompsastatic void
3413203134Sthompsarun_start(struct ifnet *ifp)
3414203134Sthompsa{
3415203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3416203134Sthompsa	struct ieee80211_node *ni;
3417203134Sthompsa	struct mbuf *m;
3418203134Sthompsa
3419203134Sthompsa	RUN_LOCK(sc);
3420203134Sthompsa
3421203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3422203134Sthompsa		RUN_UNLOCK(sc);
3423203134Sthompsa		return;
3424203134Sthompsa	}
3425203134Sthompsa
3426203134Sthompsa	for (;;) {
3427203134Sthompsa		/* send data frames */
3428203134Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3429203134Sthompsa		if (m == NULL)
3430203134Sthompsa			break;
3431203134Sthompsa
3432203134Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3433203134Sthompsa		if (run_tx(sc, m, ni) != 0) {
3434203134Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
3435203134Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3436203134Sthompsa			break;
3437203134Sthompsa		}
3438203134Sthompsa	}
3439203134Sthompsa
3440203134Sthompsa	RUN_UNLOCK(sc);
3441203134Sthompsa}
3442203134Sthompsa
3443203134Sthompsastatic int
3444203134Sthompsarun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3445203134Sthompsa{
3446203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3447203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3448203134Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
3449208019Sthompsa	int startall = 0;
3450246614Shselasky	int error;
3451203134Sthompsa
3452246614Shselasky	RUN_LOCK(sc);
3453246614Shselasky	error = sc->sc_detached ? ENXIO : 0;
3454246614Shselasky	RUN_UNLOCK(sc);
3455246614Shselasky	if (error)
3456246614Shselasky		return (error);
3457246614Shselasky
3458203134Sthompsa	switch (cmd) {
3459203134Sthompsa	case SIOCSIFFLAGS:
3460203134Sthompsa		RUN_LOCK(sc);
3461203134Sthompsa		if (ifp->if_flags & IFF_UP) {
3462203134Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3463208019Sthompsa				startall = 1;
3464203134Sthompsa				run_init_locked(sc);
3465203134Sthompsa			} else
3466203134Sthompsa				run_update_promisc_locked(ifp);
3467203134Sthompsa		} else {
3468209917Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3469209917Sthompsa			    (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3470208019Sthompsa					run_stop(sc);
3471208019Sthompsa			}
3472203134Sthompsa		}
3473203134Sthompsa		RUN_UNLOCK(sc);
3474209917Sthompsa		if (startall)
3475208019Sthompsa			ieee80211_start_all(ic);
3476203134Sthompsa		break;
3477203134Sthompsa	case SIOCGIFMEDIA:
3478203134Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3479203134Sthompsa		break;
3480203134Sthompsa	case SIOCGIFADDR:
3481203134Sthompsa		error = ether_ioctl(ifp, cmd, data);
3482203134Sthompsa		break;
3483203134Sthompsa	default:
3484203134Sthompsa		error = EINVAL;
3485203134Sthompsa		break;
3486203134Sthompsa	}
3487203134Sthompsa
3488203134Sthompsa	return (error);
3489203134Sthompsa}
3490203134Sthompsa
3491203134Sthompsastatic void
3492205042Sthompsarun_set_agc(struct run_softc *sc, uint8_t agc)
3493205042Sthompsa{
3494205042Sthompsa	uint8_t bbp;
3495205042Sthompsa
3496205042Sthompsa	if (sc->mac_ver == 0x3572) {
3497205042Sthompsa		run_bbp_read(sc, 27, &bbp);
3498205042Sthompsa		bbp &= ~(0x3 << 5);
3499205042Sthompsa		run_bbp_write(sc, 27, bbp | 0 << 5);	/* select Rx0 */
3500205042Sthompsa		run_bbp_write(sc, 66, agc);
3501205042Sthompsa		run_bbp_write(sc, 27, bbp | 1 << 5);	/* select Rx1 */
3502205042Sthompsa		run_bbp_write(sc, 66, agc);
3503205042Sthompsa	} else
3504205042Sthompsa		run_bbp_write(sc, 66, agc);
3505205042Sthompsa}
3506205042Sthompsa
3507205042Sthompsastatic void
3508203134Sthompsarun_select_chan_group(struct run_softc *sc, int group)
3509203134Sthompsa{
3510203134Sthompsa	uint32_t tmp;
3511205042Sthompsa	uint8_t agc;
3512203134Sthompsa
3513203134Sthompsa	run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3514203134Sthompsa	run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3515203134Sthompsa	run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3516203134Sthompsa	run_bbp_write(sc, 86, 0x00);
3517203134Sthompsa
3518203134Sthompsa	if (group == 0) {
3519203134Sthompsa		if (sc->ext_2ghz_lna) {
3520203134Sthompsa			run_bbp_write(sc, 82, 0x62);
3521203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3522203134Sthompsa		} else {
3523203134Sthompsa			run_bbp_write(sc, 82, 0x84);
3524203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3525203134Sthompsa		}
3526203134Sthompsa	} else {
3527205042Sthompsa		if (sc->mac_ver == 0x3572)
3528205042Sthompsa			run_bbp_write(sc, 82, 0x94);
3529205042Sthompsa		else
3530203134Sthompsa			run_bbp_write(sc, 82, 0xf2);
3531205042Sthompsa		if (sc->ext_5ghz_lna)
3532203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3533205042Sthompsa		else
3534203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3535203134Sthompsa	}
3536203134Sthompsa
3537203134Sthompsa	run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3538203134Sthompsa	tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3539203134Sthompsa	tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3540203134Sthompsa	run_write(sc, RT2860_TX_BAND_CFG, tmp);
3541203134Sthompsa
3542203134Sthompsa	/* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3543208019Sthompsa	tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3544208019Sthompsa	if (sc->nrxchains > 1)
3545208019Sthompsa		tmp |= RT2860_LNA_PE1_EN;
3546203134Sthompsa	if (group == 0) {	/* 2GHz */
3547208019Sthompsa		tmp |= RT2860_PA_PE_G0_EN;
3548203134Sthompsa		if (sc->ntxchains > 1)
3549203134Sthompsa			tmp |= RT2860_PA_PE_G1_EN;
3550203134Sthompsa	} else {		/* 5GHz */
3551208019Sthompsa		tmp |= RT2860_PA_PE_A0_EN;
3552203134Sthompsa		if (sc->ntxchains > 1)
3553203134Sthompsa			tmp |= RT2860_PA_PE_A1_EN;
3554203134Sthompsa	}
3555205042Sthompsa	if (sc->mac_ver == 0x3572) {
3556205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x00);
3557205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3558205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x80);
3559205042Sthompsa	} else
3560205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3561203134Sthompsa
3562203134Sthompsa	/* set initial AGC value */
3563205042Sthompsa	if (group == 0) {	/* 2GHz band */
3564205042Sthompsa		if (sc->mac_ver >= 0x3070)
3565205042Sthompsa			agc = 0x1c + sc->lna[0] * 2;
3566205042Sthompsa		else
3567205042Sthompsa			agc = 0x2e + sc->lna[0];
3568205042Sthompsa	} else {		/* 5GHz band */
3569205042Sthompsa		if (sc->mac_ver == 0x3572)
3570205042Sthompsa			agc = 0x22 + (sc->lna[group] * 5) / 3;
3571205042Sthompsa		else
3572205042Sthompsa			agc = 0x32 + (sc->lna[group] * 5) / 3;
3573205042Sthompsa	}
3574205042Sthompsa	run_set_agc(sc, agc);
3575203134Sthompsa}
3576203134Sthompsa
3577203134Sthompsastatic void
3578203134Sthompsarun_rt2870_set_chan(struct run_softc *sc, uint32_t chan)
3579203134Sthompsa{
3580203134Sthompsa	const struct rfprog *rfprog = rt2860_rf2850;
3581203134Sthompsa	uint32_t r2, r3, r4;
3582203134Sthompsa	int8_t txpow1, txpow2;
3583203134Sthompsa	int i;
3584203134Sthompsa
3585203134Sthompsa	/* find the settings for this channel (we know it exists) */
3586203134Sthompsa	for (i = 0; rfprog[i].chan != chan; i++);
3587203134Sthompsa
3588203134Sthompsa	r2 = rfprog[i].r2;
3589203134Sthompsa	if (sc->ntxchains == 1)
3590203134Sthompsa		r2 |= 1 << 12;		/* 1T: disable Tx chain 2 */
3591203134Sthompsa	if (sc->nrxchains == 1)
3592203134Sthompsa		r2 |= 1 << 15 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3593203134Sthompsa	else if (sc->nrxchains == 2)
3594203134Sthompsa		r2 |= 1 << 4;		/* 2R: disable Rx chain 3 */
3595203134Sthompsa
3596203134Sthompsa	/* use Tx power values from EEPROM */
3597203134Sthompsa	txpow1 = sc->txpow1[i];
3598203134Sthompsa	txpow2 = sc->txpow2[i];
3599203134Sthompsa	if (chan > 14) {
3600203134Sthompsa		if (txpow1 >= 0)
3601208019Sthompsa			txpow1 = txpow1 << 1 | 1;
3602203134Sthompsa		else
3603208019Sthompsa			txpow1 = (7 + txpow1) << 1;
3604203134Sthompsa		if (txpow2 >= 0)
3605208019Sthompsa			txpow2 = txpow2 << 1 | 1;
3606203134Sthompsa		else
3607208019Sthompsa			txpow2 = (7 + txpow2) << 1;
3608203134Sthompsa	}
3609203134Sthompsa	r3 = rfprog[i].r3 | txpow1 << 7;
3610203134Sthompsa	r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3611203134Sthompsa
3612203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3613203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3614203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3615203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3616203134Sthompsa
3617203134Sthompsa	run_delay(sc, 10);
3618203134Sthompsa
3619203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3620203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3621203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3622203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3623203134Sthompsa
3624203134Sthompsa	run_delay(sc, 10);
3625203134Sthompsa
3626203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3627203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3628203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3629203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3630203134Sthompsa}
3631203134Sthompsa
3632203134Sthompsastatic void
3633203134Sthompsarun_rt3070_set_chan(struct run_softc *sc, uint32_t chan)
3634203134Sthompsa{
3635203134Sthompsa	int8_t txpow1, txpow2;
3636203134Sthompsa	uint8_t rf;
3637205042Sthompsa	int i;
3638203134Sthompsa
3639203134Sthompsa	/* RT3070 is 2GHz only */
3640203134Sthompsa	KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3641203134Sthompsa
3642205042Sthompsa	/* find the settings for this channel (we know it exists) */
3643205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3644205042Sthompsa
3645203134Sthompsa	/* use Tx power values from EEPROM */
3646205042Sthompsa	txpow1 = sc->txpow1[i];
3647205042Sthompsa	txpow2 = sc->txpow2[i];
3648203134Sthompsa
3649205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3650205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3651203134Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3652205042Sthompsa	rf = (rf & ~0x03) | rt3070_freqs[i].r;
3653203134Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3654203134Sthompsa
3655203134Sthompsa	/* set Tx0 power */
3656203134Sthompsa	run_rt3070_rf_read(sc, 12, &rf);
3657203134Sthompsa	rf = (rf & ~0x1f) | txpow1;
3658203134Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3659203134Sthompsa
3660203134Sthompsa	/* set Tx1 power */
3661203134Sthompsa	run_rt3070_rf_read(sc, 13, &rf);
3662203134Sthompsa	rf = (rf & ~0x1f) | txpow2;
3663203134Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3664203134Sthompsa
3665203134Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3666203134Sthompsa	rf &= ~0xfc;
3667203134Sthompsa	if (sc->ntxchains == 1)
3668203134Sthompsa		rf |= 1 << 7 | 1 << 5;	/* 1T: disable Tx chains 2 & 3 */
3669203134Sthompsa	else if (sc->ntxchains == 2)
3670203134Sthompsa		rf |= 1 << 7;		/* 2T: disable Tx chain 3 */
3671203134Sthompsa	if (sc->nrxchains == 1)
3672203134Sthompsa		rf |= 1 << 6 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3673203134Sthompsa	else if (sc->nrxchains == 2)
3674203134Sthompsa		rf |= 1 << 6;		/* 2R: disable Rx chain 3 */
3675203134Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3676203134Sthompsa
3677203134Sthompsa	/* set RF offset */
3678203134Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3679203134Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3680203134Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3681203134Sthompsa
3682203134Sthompsa	/* program RF filter */
3683205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf);	/* Tx */
3684205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3685205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);
3686205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);	/* Rx */
3687205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3688205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);
3689203134Sthompsa
3690203134Sthompsa	/* enable RF tuning */
3691203134Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3692203134Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3693203134Sthompsa}
3694203134Sthompsa
3695203134Sthompsastatic void
3696205042Sthompsarun_rt3572_set_chan(struct run_softc *sc, u_int chan)
3697205042Sthompsa{
3698205042Sthompsa	int8_t txpow1, txpow2;
3699205042Sthompsa	uint32_t tmp;
3700205042Sthompsa	uint8_t rf;
3701205042Sthompsa	int i;
3702205042Sthompsa
3703205042Sthompsa	/* find the settings for this channel (we know it exists) */
3704205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3705205042Sthompsa
3706205042Sthompsa	/* use Tx power values from EEPROM */
3707205042Sthompsa	txpow1 = sc->txpow1[i];
3708205042Sthompsa	txpow2 = sc->txpow2[i];
3709205042Sthompsa
3710205042Sthompsa	if (chan <= 14) {
3711205042Sthompsa		run_bbp_write(sc, 25, sc->bbp25);
3712205042Sthompsa		run_bbp_write(sc, 26, sc->bbp26);
3713205042Sthompsa	} else {
3714205042Sthompsa		/* enable IQ phase correction */
3715205042Sthompsa		run_bbp_write(sc, 25, 0x09);
3716205042Sthompsa		run_bbp_write(sc, 26, 0xff);
3717205042Sthompsa	}
3718205042Sthompsa
3719205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3720205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3721205042Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3722205042Sthompsa	rf  = (rf & ~0x0f) | rt3070_freqs[i].r;
3723205042Sthompsa	rf |= (chan <= 14) ? 0x08 : 0x04;
3724205042Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3725205042Sthompsa
3726205042Sthompsa	/* set PLL mode */
3727205042Sthompsa	run_rt3070_rf_read(sc, 5, &rf);
3728205042Sthompsa	rf &= ~(0x08 | 0x04);
3729205042Sthompsa	rf |= (chan <= 14) ? 0x04 : 0x08;
3730205042Sthompsa	run_rt3070_rf_write(sc, 5, rf);
3731205042Sthompsa
3732205042Sthompsa	/* set Tx power for chain 0 */
3733205042Sthompsa	if (chan <= 14)
3734205042Sthompsa		rf = 0x60 | txpow1;
3735205042Sthompsa	else
3736205042Sthompsa		rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3737205042Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3738205042Sthompsa
3739205042Sthompsa	/* set Tx power for chain 1 */
3740205042Sthompsa	if (chan <= 14)
3741205042Sthompsa		rf = 0x60 | txpow2;
3742205042Sthompsa	else
3743205042Sthompsa		rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3744205042Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3745205042Sthompsa
3746205042Sthompsa	/* set Tx/Rx streams */
3747205042Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3748205042Sthompsa	rf &= ~0xfc;
3749205042Sthompsa	if (sc->ntxchains == 1)
3750205042Sthompsa		rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3751205042Sthompsa	else if (sc->ntxchains == 2)
3752205042Sthompsa		rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3753205042Sthompsa	if (sc->nrxchains == 1)
3754205042Sthompsa		rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3755205042Sthompsa	else if (sc->nrxchains == 2)
3756205042Sthompsa		rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3757205042Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3758205042Sthompsa
3759205042Sthompsa	/* set RF offset */
3760205042Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3761205042Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3762205042Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3763205042Sthompsa
3764205042Sthompsa	/* program RF filter */
3765205042Sthompsa	rf = sc->rf24_20mhz;
3766205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);	/* Tx */
3767205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);	/* Rx */
3768205042Sthompsa
3769205042Sthompsa	/* enable RF tuning */
3770205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3771205042Sthompsa	rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3772205042Sthompsa	run_rt3070_rf_write(sc, 7, rf);
3773205042Sthompsa
3774205042Sthompsa	/* TSSI */
3775205042Sthompsa	rf = (chan <= 14) ? 0xc3 : 0xc0;
3776205042Sthompsa	run_rt3070_rf_write(sc, 9, rf);
3777205042Sthompsa
3778205042Sthompsa	/* set loop filter 1 */
3779205042Sthompsa	run_rt3070_rf_write(sc, 10, 0xf1);
3780205042Sthompsa	/* set loop filter 2 */
3781205042Sthompsa	run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3782205042Sthompsa
3783205042Sthompsa	/* set tx_mx2_ic */
3784205042Sthompsa	run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3785205042Sthompsa	/* set tx_mx1_ic */
3786205042Sthompsa	if (chan <= 14)
3787205042Sthompsa		rf = 0x48 | sc->txmixgain_2ghz;
3788205042Sthompsa	else
3789205042Sthompsa		rf = 0x78 | sc->txmixgain_5ghz;
3790205042Sthompsa	run_rt3070_rf_write(sc, 16, rf);
3791205042Sthompsa
3792205042Sthompsa	/* set tx_lo1 */
3793205042Sthompsa	run_rt3070_rf_write(sc, 17, 0x23);
3794205042Sthompsa	/* set tx_lo2 */
3795205042Sthompsa	if (chan <= 14)
3796205042Sthompsa		rf = 0x93;
3797205042Sthompsa	else if (chan <= 64)
3798205042Sthompsa		rf = 0xb7;
3799205042Sthompsa	else if (chan <= 128)
3800205042Sthompsa		rf = 0x74;
3801205042Sthompsa	else
3802205042Sthompsa		rf = 0x72;
3803205042Sthompsa	run_rt3070_rf_write(sc, 19, rf);
3804205042Sthompsa
3805205042Sthompsa	/* set rx_lo1 */
3806205042Sthompsa	if (chan <= 14)
3807205042Sthompsa		rf = 0xb3;
3808205042Sthompsa	else if (chan <= 64)
3809205042Sthompsa		rf = 0xf6;
3810205042Sthompsa	else if (chan <= 128)
3811205042Sthompsa		rf = 0xf4;
3812205042Sthompsa	else
3813205042Sthompsa		rf = 0xf3;
3814205042Sthompsa	run_rt3070_rf_write(sc, 20, rf);
3815205042Sthompsa
3816205042Sthompsa	/* set pfd_delay */
3817205042Sthompsa	if (chan <= 14)
3818205042Sthompsa		rf = 0x15;
3819205042Sthompsa	else if (chan <= 64)
3820205042Sthompsa		rf = 0x3d;
3821205042Sthompsa	else
3822205042Sthompsa		rf = 0x01;
3823205042Sthompsa	run_rt3070_rf_write(sc, 25, rf);
3824205042Sthompsa
3825205042Sthompsa	/* set rx_lo2 */
3826205042Sthompsa	run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3827205042Sthompsa	/* set ldo_rf_vc */
3828205042Sthompsa	run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3829205042Sthompsa	/* set drv_cc */
3830205042Sthompsa	run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3831205042Sthompsa
3832205042Sthompsa	run_read(sc, RT2860_GPIO_CTRL, &tmp);
3833205042Sthompsa	tmp &= ~0x8080;
3834205042Sthompsa	if (chan <= 14)
3835205042Sthompsa		tmp |= 0x80;
3836205042Sthompsa	run_write(sc, RT2860_GPIO_CTRL, tmp);
3837205042Sthompsa
3838205042Sthompsa	/* enable RF tuning */
3839205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3840205042Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3841205042Sthompsa
3842205042Sthompsa	run_delay(sc, 2);
3843205042Sthompsa}
3844205042Sthompsa
3845205042Sthompsastatic void
3846203134Sthompsarun_set_rx_antenna(struct run_softc *sc, int aux)
3847203134Sthompsa{
3848203134Sthompsa	uint32_t tmp;
3849203134Sthompsa
3850203134Sthompsa	if (aux) {
3851205042Sthompsa		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
3852203134Sthompsa		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3853203134Sthompsa		run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
3854203134Sthompsa	} else {
3855205042Sthompsa		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
3856203134Sthompsa		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3857203134Sthompsa		run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
3858203134Sthompsa	}
3859203134Sthompsa}
3860203134Sthompsa
3861203134Sthompsastatic int
3862203134Sthompsarun_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
3863203134Sthompsa{
3864203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3865203134Sthompsa	uint32_t chan, group;
3866203134Sthompsa
3867203134Sthompsa	chan = ieee80211_chan2ieee(ic, c);
3868203134Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
3869209917Sthompsa		return (EINVAL);
3870203134Sthompsa
3871205042Sthompsa	if (sc->mac_ver == 0x3572)
3872205042Sthompsa		run_rt3572_set_chan(sc, chan);
3873205042Sthompsa	else if (sc->mac_ver >= 0x3070)
3874203134Sthompsa		run_rt3070_set_chan(sc, chan);
3875203134Sthompsa	else
3876203134Sthompsa		run_rt2870_set_chan(sc, chan);
3877203134Sthompsa
3878203134Sthompsa	/* determine channel group */
3879203134Sthompsa	if (chan <= 14)
3880203134Sthompsa		group = 0;
3881203134Sthompsa	else if (chan <= 64)
3882203134Sthompsa		group = 1;
3883203134Sthompsa	else if (chan <= 128)
3884203134Sthompsa		group = 2;
3885203134Sthompsa	else
3886203134Sthompsa		group = 3;
3887203134Sthompsa
3888203134Sthompsa	/* XXX necessary only when group has changed! */
3889203134Sthompsa	run_select_chan_group(sc, group);
3890203134Sthompsa
3891203134Sthompsa	run_delay(sc, 10);
3892203134Sthompsa
3893209917Sthompsa	return (0);
3894203134Sthompsa}
3895203134Sthompsa
3896203134Sthompsastatic void
3897203134Sthompsarun_set_channel(struct ieee80211com *ic)
3898203134Sthompsa{
3899203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3900203134Sthompsa
3901203134Sthompsa	RUN_LOCK(sc);
3902203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
3903203134Sthompsa	RUN_UNLOCK(sc);
3904203134Sthompsa
3905203134Sthompsa	return;
3906203134Sthompsa}
3907203134Sthompsa
3908203134Sthompsastatic void
3909203134Sthompsarun_scan_start(struct ieee80211com *ic)
3910203134Sthompsa{
3911203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3912203134Sthompsa	uint32_t tmp;
3913203134Sthompsa
3914203134Sthompsa	RUN_LOCK(sc);
3915203134Sthompsa
3916203134Sthompsa	/* abort TSF synchronization */
3917203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
3918203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG,
3919203134Sthompsa	    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
3920203134Sthompsa	    RT2860_TBTT_TIMER_EN));
3921203134Sthompsa	run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
3922203134Sthompsa
3923203134Sthompsa	RUN_UNLOCK(sc);
3924203134Sthompsa
3925203134Sthompsa	return;
3926203134Sthompsa}
3927203134Sthompsa
3928203134Sthompsastatic void
3929203134Sthompsarun_scan_end(struct ieee80211com *ic)
3930203134Sthompsa{
3931203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3932203134Sthompsa
3933203134Sthompsa	RUN_LOCK(sc);
3934203134Sthompsa
3935203134Sthompsa	run_enable_tsf_sync(sc);
3936203134Sthompsa	/* XXX keep local copy */
3937203134Sthompsa	run_set_bssid(sc, sc->sc_bssid);
3938203134Sthompsa
3939203134Sthompsa	RUN_UNLOCK(sc);
3940203134Sthompsa
3941203134Sthompsa	return;
3942203134Sthompsa}
3943203134Sthompsa
3944208019Sthompsa/*
3945208019Sthompsa * Could be called from ieee80211_node_timeout()
3946208019Sthompsa * (non-sleepable thread)
3947208019Sthompsa */
3948208019Sthompsastatic void
3949208019Sthompsarun_update_beacon(struct ieee80211vap *vap, int item)
3950203134Sthompsa{
3951208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
3952208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3953218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
3954218492Sbschmidt	int mcast = 0;
3955208019Sthompsa	uint32_t i;
3956208019Sthompsa
3957218492Sbschmidt	KASSERT(vap != NULL, ("no beacon"));
3958218492Sbschmidt
3959218492Sbschmidt	switch (item) {
3960218492Sbschmidt	case IEEE80211_BEACON_ERP:
3961218492Sbschmidt		run_updateslot(ic->ic_ifp);
3962218492Sbschmidt		break;
3963218492Sbschmidt	case IEEE80211_BEACON_HTINFO:
3964218492Sbschmidt		run_updateprot(ic);
3965218492Sbschmidt		break;
3966218492Sbschmidt	case IEEE80211_BEACON_TIM:
3967218492Sbschmidt		mcast = 1;	/*TODO*/
3968218492Sbschmidt		break;
3969218492Sbschmidt	default:
3970218492Sbschmidt		break;
3971218492Sbschmidt	}
3972218492Sbschmidt
3973218492Sbschmidt	setbit(rvp->bo.bo_flags, item);
3974218492Sbschmidt	ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
3975218492Sbschmidt
3976208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
3977208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
3978208019Sthompsa	sc->cmdq[i].func = run_update_beacon_cb;
3979208019Sthompsa	sc->cmdq[i].arg0 = vap;
3980208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
3981208019Sthompsa
3982208019Sthompsa	return;
3983203134Sthompsa}
3984203134Sthompsa
3985203134Sthompsastatic void
3986208019Sthompsarun_update_beacon_cb(void *arg)
3987203134Sthompsa{
3988208019Sthompsa	struct ieee80211vap *vap = arg;
3989218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
3990203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
3991203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3992203134Sthompsa	struct rt2860_txwi txwi;
3993203134Sthompsa	struct mbuf *m;
3994208019Sthompsa	uint8_t ridx;
3995203134Sthompsa
3996209917Sthompsa	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
3997208019Sthompsa		return;
3998236439Shselasky	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
3999236439Shselasky		return;
4000208019Sthompsa
4001218492Sbschmidt	/*
4002218492Sbschmidt	 * No need to call ieee80211_beacon_update(), run_update_beacon()
4003218492Sbschmidt	 * is taking care of apropriate calls.
4004218492Sbschmidt	 */
4005218492Sbschmidt	if (rvp->beacon_mbuf == NULL) {
4006218492Sbschmidt		rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
4007218492Sbschmidt		    &rvp->bo);
4008218492Sbschmidt		if (rvp->beacon_mbuf == NULL)
4009218492Sbschmidt			return;
4010218492Sbschmidt	}
4011218492Sbschmidt	m = rvp->beacon_mbuf;
4012203134Sthompsa
4013203134Sthompsa	memset(&txwi, 0, sizeof txwi);
4014203134Sthompsa	txwi.wcid = 0xff;
4015203134Sthompsa	txwi.len = htole16(m->m_pkthdr.len);
4016203134Sthompsa	/* send beacons at the lowest available rate */
4017208019Sthompsa	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4018208019Sthompsa	    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
4019208019Sthompsa	txwi.phy = htole16(rt2860_rates[ridx].mcs);
4020208019Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
4021203134Sthompsa	        txwi.phy |= htole16(RT2860_PHY_OFDM);
4022203134Sthompsa	txwi.txop = RT2860_TX_TXOP_HT;
4023203134Sthompsa	txwi.flags = RT2860_TX_TS;
4024209144Sthompsa	txwi.xflags = RT2860_TX_NSEQ;
4025203134Sthompsa
4026218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
4027208019Sthompsa	    (uint8_t *)&txwi, sizeof txwi);
4028218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
4029203134Sthompsa	    mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);	/* roundup len */
4030203134Sthompsa
4031203134Sthompsa	return;
4032203134Sthompsa}
4033203134Sthompsa
4034203134Sthompsastatic void
4035203134Sthompsarun_updateprot(struct ieee80211com *ic)
4036203134Sthompsa{
4037203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4038218492Sbschmidt	uint32_t i;
4039218492Sbschmidt
4040218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4041218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4042218492Sbschmidt	sc->cmdq[i].func = run_updateprot_cb;
4043218492Sbschmidt	sc->cmdq[i].arg0 = ic;
4044218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4045218492Sbschmidt}
4046218492Sbschmidt
4047218492Sbschmidtstatic void
4048218492Sbschmidtrun_updateprot_cb(void *arg)
4049218492Sbschmidt{
4050218492Sbschmidt	struct ieee80211com *ic = arg;
4051218492Sbschmidt	struct run_softc *sc = ic->ic_ifp->if_softc;
4052203134Sthompsa	uint32_t tmp;
4053203134Sthompsa
4054203134Sthompsa	tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4055203134Sthompsa	/* setup protection frame rate (MCS code) */
4056203134Sthompsa	tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4057203134Sthompsa	    rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4058203134Sthompsa	    rt2860_rates[RT2860_RIDX_CCK11].mcs;
4059203134Sthompsa
4060203134Sthompsa	/* CCK frames don't require protection */
4061203134Sthompsa	run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4062203134Sthompsa	if (ic->ic_flags & IEEE80211_F_USEPROT) {
4063203134Sthompsa		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4064203134Sthompsa			tmp |= RT2860_PROT_CTRL_RTS_CTS;
4065203134Sthompsa		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4066203134Sthompsa			tmp |= RT2860_PROT_CTRL_CTS;
4067203134Sthompsa	}
4068203134Sthompsa	run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4069203134Sthompsa}
4070203134Sthompsa
4071203134Sthompsastatic void
4072208019Sthompsarun_usb_timeout_cb(void *arg)
4073203134Sthompsa{
4074208019Sthompsa	struct ieee80211vap *vap = arg;
4075208019Sthompsa	struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4076203134Sthompsa
4077208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4078203134Sthompsa
4079203134Sthompsa	if(vap->iv_state == IEEE80211_S_RUN &&
4080203134Sthompsa	    vap->iv_opmode != IEEE80211_M_STA)
4081203134Sthompsa		run_reset_livelock(sc);
4082209917Sthompsa	else if (vap->iv_state == IEEE80211_S_SCAN) {
4083203134Sthompsa		DPRINTF("timeout caused by scan\n");
4084203134Sthompsa		/* cancel bgscan */
4085203134Sthompsa		ieee80211_cancel_scan(vap);
4086203134Sthompsa	} else
4087203134Sthompsa		DPRINTF("timeout by unknown cause\n");
4088203134Sthompsa}
4089203134Sthompsa
4090203134Sthompsastatic void
4091203134Sthompsarun_reset_livelock(struct run_softc *sc)
4092203134Sthompsa{
4093203134Sthompsa	uint32_t tmp;
4094203134Sthompsa
4095208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4096208019Sthompsa
4097203134Sthompsa	/*
4098203134Sthompsa	 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4099203134Sthompsa	 * can run into a livelock and start sending CTS-to-self frames like
4100203134Sthompsa	 * crazy if protection is enabled.  Reset MAC/BBP for a while
4101203134Sthompsa	 */
4102203134Sthompsa	run_read(sc, RT2860_DEBUG, &tmp);
4103208019Sthompsa	DPRINTFN(3, "debug reg %08x\n", tmp);
4104209917Sthompsa	if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4105203134Sthompsa		DPRINTF("CTS-to-self livelock detected\n");
4106203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4107203134Sthompsa		run_delay(sc, 1);
4108203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL,
4109203134Sthompsa		    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4110203134Sthompsa	}
4111203134Sthompsa}
4112203134Sthompsa
4113203134Sthompsastatic void
4114203134Sthompsarun_update_promisc_locked(struct ifnet *ifp)
4115203134Sthompsa{
4116203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4117203134Sthompsa        uint32_t tmp;
4118203134Sthompsa
4119203134Sthompsa	run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4120203134Sthompsa
4121203134Sthompsa	tmp |= RT2860_DROP_UC_NOME;
4122203134Sthompsa        if (ifp->if_flags & IFF_PROMISC)
4123203134Sthompsa		tmp &= ~RT2860_DROP_UC_NOME;
4124203134Sthompsa
4125203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4126203134Sthompsa
4127203134Sthompsa        DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4128203134Sthompsa            "entering" : "leaving");
4129203134Sthompsa}
4130203134Sthompsa
4131203134Sthompsastatic void
4132203134Sthompsarun_update_promisc(struct ifnet *ifp)
4133203134Sthompsa{
4134203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4135203134Sthompsa
4136203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4137203134Sthompsa		return;
4138203134Sthompsa
4139203134Sthompsa	RUN_LOCK(sc);
4140203134Sthompsa	run_update_promisc_locked(ifp);
4141203134Sthompsa	RUN_UNLOCK(sc);
4142203134Sthompsa}
4143203134Sthompsa
4144203134Sthompsastatic void
4145203134Sthompsarun_enable_tsf_sync(struct run_softc *sc)
4146203134Sthompsa{
4147208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4148203134Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4149203134Sthompsa	uint32_t tmp;
4150203134Sthompsa
4151208019Sthompsa	DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode);
4152208019Sthompsa
4153203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4154203134Sthompsa	tmp &= ~0x1fffff;
4155203134Sthompsa	tmp |= vap->iv_bss->ni_intval * 16;
4156203134Sthompsa	tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4157203134Sthompsa
4158208019Sthompsa	if (ic->ic_opmode == IEEE80211_M_STA) {
4159203134Sthompsa		/*
4160203134Sthompsa		 * Local TSF is always updated with remote TSF on beacon
4161203134Sthompsa		 * reception.
4162203134Sthompsa		 */
4163203134Sthompsa		tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4164208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4165203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4166203134Sthompsa	        /*
4167203134Sthompsa	         * Local TSF is updated with remote TSF on beacon reception
4168203134Sthompsa	         * only if the remote TSF is greater than local TSF.
4169203134Sthompsa	         */
4170203134Sthompsa	        tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4171208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4172208019Sthompsa		    ic->ic_opmode == IEEE80211_M_MBSS) {
4173203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4174203134Sthompsa	        /* SYNC with nobody */
4175203134Sthompsa	        tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4176208019Sthompsa	} else {
4177203134Sthompsa		DPRINTF("Enabling TSF failed. undefined opmode\n");
4178208019Sthompsa		return;
4179208019Sthompsa	}
4180203134Sthompsa
4181203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4182203134Sthompsa}
4183203134Sthompsa
4184203134Sthompsastatic void
4185203134Sthompsarun_enable_mrr(struct run_softc *sc)
4186203134Sthompsa{
4187203134Sthompsa#define CCK(mcs)	(mcs)
4188203134Sthompsa#define OFDM(mcs)	(1 << 3 | (mcs))
4189203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG0,
4190203134Sthompsa	    OFDM(6) << 28 |	/* 54->48 */
4191203134Sthompsa	    OFDM(5) << 24 |	/* 48->36 */
4192203134Sthompsa	    OFDM(4) << 20 |	/* 36->24 */
4193203134Sthompsa	    OFDM(3) << 16 |	/* 24->18 */
4194203134Sthompsa	    OFDM(2) << 12 |	/* 18->12 */
4195203134Sthompsa	    OFDM(1) <<  8 |	/* 12-> 9 */
4196203134Sthompsa	    OFDM(0) <<  4 |	/*  9-> 6 */
4197203134Sthompsa	    OFDM(0));		/*  6-> 6 */
4198203134Sthompsa
4199203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG1,
4200203134Sthompsa	    CCK(2) << 12 |	/* 11->5.5 */
4201203134Sthompsa	    CCK(1) <<  8 |	/* 5.5-> 2 */
4202203134Sthompsa	    CCK(0) <<  4 |	/*   2-> 1 */
4203203134Sthompsa	    CCK(0));		/*   1-> 1 */
4204203134Sthompsa#undef OFDM
4205203134Sthompsa#undef CCK
4206203134Sthompsa}
4207203134Sthompsa
4208203134Sthompsastatic void
4209203134Sthompsarun_set_txpreamble(struct run_softc *sc)
4210203134Sthompsa{
4211203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4212203134Sthompsa	uint32_t tmp;
4213203134Sthompsa
4214203134Sthompsa	run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4215203134Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4216203134Sthompsa		tmp |= RT2860_CCK_SHORT_EN;
4217203134Sthompsa	else
4218203134Sthompsa		tmp &= ~RT2860_CCK_SHORT_EN;
4219203134Sthompsa	run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4220203134Sthompsa}
4221203134Sthompsa
4222203134Sthompsastatic void
4223203134Sthompsarun_set_basicrates(struct run_softc *sc)
4224203134Sthompsa{
4225203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4226203134Sthompsa
4227203134Sthompsa	/* set basic rates mask */
4228203134Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
4229203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4230203134Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
4231203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4232203134Sthompsa	else	/* 11g */
4233203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4234203134Sthompsa}
4235203134Sthompsa
4236203134Sthompsastatic void
4237203134Sthompsarun_set_leds(struct run_softc *sc, uint16_t which)
4238203134Sthompsa{
4239203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4240203134Sthompsa	    which | (sc->leds & 0x7f));
4241203134Sthompsa}
4242203134Sthompsa
4243203134Sthompsastatic void
4244203134Sthompsarun_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4245203134Sthompsa{
4246203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW0,
4247203134Sthompsa	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4248203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW1,
4249203134Sthompsa	    bssid[4] | bssid[5] << 8);
4250203134Sthompsa}
4251203134Sthompsa
4252203134Sthompsastatic void
4253203134Sthompsarun_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4254203134Sthompsa{
4255203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW0,
4256203134Sthompsa	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4257203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW1,
4258203134Sthompsa	    addr[4] | addr[5] << 8 | 0xff << 16);
4259203134Sthompsa}
4260203134Sthompsa
4261203134Sthompsastatic void
4262203134Sthompsarun_updateslot(struct ifnet *ifp)
4263203134Sthompsa{
4264203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4265203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4266218492Sbschmidt	uint32_t i;
4267218492Sbschmidt
4268218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4269218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4270218492Sbschmidt	sc->cmdq[i].func = run_updateslot_cb;
4271218492Sbschmidt	sc->cmdq[i].arg0 = ifp;
4272218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4273218492Sbschmidt
4274218492Sbschmidt	return;
4275218492Sbschmidt}
4276218492Sbschmidt
4277218492Sbschmidt/* ARGSUSED */
4278218492Sbschmidtstatic void
4279218492Sbschmidtrun_updateslot_cb(void *arg)
4280218492Sbschmidt{
4281218492Sbschmidt	struct ifnet *ifp = arg;
4282218492Sbschmidt	struct run_softc *sc = ifp->if_softc;
4283218492Sbschmidt	struct ieee80211com *ic = ifp->if_l2com;
4284203134Sthompsa	uint32_t tmp;
4285203134Sthompsa
4286203134Sthompsa	run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4287203134Sthompsa	tmp &= ~0xff;
4288203134Sthompsa	tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4289203134Sthompsa	run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4290203134Sthompsa}
4291203134Sthompsa
4292208019Sthompsastatic void
4293208019Sthompsarun_update_mcast(struct ifnet *ifp)
4294208019Sthompsa{
4295208019Sthompsa	/* h/w filter supports getting everything or nothing */
4296208019Sthompsa	ifp->if_flags |= IFF_ALLMULTI;
4297208019Sthompsa}
4298208019Sthompsa
4299203134Sthompsastatic int8_t
4300203134Sthompsarun_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4301203134Sthompsa{
4302203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4303203134Sthompsa	struct ieee80211_channel *c = ic->ic_curchan;
4304203134Sthompsa	int delta;
4305203134Sthompsa
4306203134Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
4307203134Sthompsa		uint32_t chan = ieee80211_chan2ieee(ic, c);
4308203134Sthompsa		delta = sc->rssi_5ghz[rxchain];
4309203134Sthompsa
4310203134Sthompsa		/* determine channel group */
4311203134Sthompsa		if (chan <= 64)
4312203134Sthompsa			delta -= sc->lna[1];
4313203134Sthompsa		else if (chan <= 128)
4314203134Sthompsa			delta -= sc->lna[2];
4315203134Sthompsa		else
4316203134Sthompsa			delta -= sc->lna[3];
4317203134Sthompsa	} else
4318203134Sthompsa		delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4319203134Sthompsa
4320209917Sthompsa	return (-12 - delta - rssi);
4321203134Sthompsa}
4322203134Sthompsa
4323203134Sthompsastatic int
4324203134Sthompsarun_bbp_init(struct run_softc *sc)
4325203134Sthompsa{
4326203134Sthompsa	int i, error, ntries;
4327203134Sthompsa	uint8_t bbp0;
4328203134Sthompsa
4329203134Sthompsa	/* wait for BBP to wake up */
4330203134Sthompsa	for (ntries = 0; ntries < 20; ntries++) {
4331203134Sthompsa		if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4332203134Sthompsa			return error;
4333203134Sthompsa		if (bbp0 != 0 && bbp0 != 0xff)
4334203134Sthompsa			break;
4335203134Sthompsa	}
4336203134Sthompsa	if (ntries == 20)
4337209917Sthompsa		return (ETIMEDOUT);
4338203134Sthompsa
4339203134Sthompsa	/* initialize BBP registers to default values */
4340233774Shselasky	for (i = 0; i < N(rt2860_def_bbp); i++) {
4341203134Sthompsa		run_bbp_write(sc, rt2860_def_bbp[i].reg,
4342203134Sthompsa		    rt2860_def_bbp[i].val);
4343203134Sthompsa	}
4344203134Sthompsa
4345203134Sthompsa	/* fix BBP84 for RT2860E */
4346205042Sthompsa	if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4347205042Sthompsa		run_bbp_write(sc, 84, 0x19);
4348203134Sthompsa
4349205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4350203134Sthompsa		run_bbp_write(sc, 79, 0x13);
4351203134Sthompsa		run_bbp_write(sc, 80, 0x05);
4352203134Sthompsa		run_bbp_write(sc, 81, 0x33);
4353205042Sthompsa	} else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4354203134Sthompsa		run_bbp_write(sc, 69, 0x16);
4355203134Sthompsa		run_bbp_write(sc, 73, 0x12);
4356203134Sthompsa	}
4357209917Sthompsa	return (0);
4358203134Sthompsa}
4359203134Sthompsa
4360203134Sthompsastatic int
4361203134Sthompsarun_rt3070_rf_init(struct run_softc *sc)
4362203134Sthompsa{
4363203134Sthompsa	uint32_t tmp;
4364205042Sthompsa	uint8_t rf, target, bbp4;
4365203134Sthompsa	int i;
4366203134Sthompsa
4367203134Sthompsa	run_rt3070_rf_read(sc, 30, &rf);
4368203134Sthompsa	/* toggle RF R30 bit 7 */
4369203134Sthompsa	run_rt3070_rf_write(sc, 30, rf | 0x80);
4370203134Sthompsa	run_delay(sc, 10);
4371203134Sthompsa	run_rt3070_rf_write(sc, 30, rf & ~0x80);
4372203134Sthompsa
4373203134Sthompsa	/* initialize RF registers to default value */
4374205042Sthompsa	if (sc->mac_ver == 0x3572) {
4375233774Shselasky		for (i = 0; i < N(rt3572_def_rf); i++) {
4376205042Sthompsa			run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4377205042Sthompsa			    rt3572_def_rf[i].val);
4378205042Sthompsa		}
4379205042Sthompsa	} else {
4380233774Shselasky		for (i = 0; i < N(rt3070_def_rf); i++) {
4381205042Sthompsa			run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4382205042Sthompsa			    rt3070_def_rf[i].val);
4383205042Sthompsa		}
4384203134Sthompsa	}
4385205042Sthompsa
4386205042Sthompsa	if (sc->mac_ver == 0x3070) {
4387203134Sthompsa		/* change voltage from 1.2V to 1.35V for RT3070 */
4388203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4389203134Sthompsa		tmp = (tmp & ~0x0f000000) | 0x0d000000;
4390203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4391203134Sthompsa
4392205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4393203134Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4394203134Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4395203134Sthompsa		run_rt3070_rf_write(sc, 31, 0x14);
4396203134Sthompsa
4397203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4398203134Sthompsa		tmp &= ~0x1f000000;
4399205042Sthompsa		if (sc->mac_rev < 0x0211)
4400205042Sthompsa			tmp |= 0x0d000000;	/* 1.3V */
4401203134Sthompsa		else
4402205042Sthompsa			tmp |= 0x01000000;	/* 1.2V */
4403203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4404203134Sthompsa
4405203134Sthompsa		/* patch LNA_PE_G1 */
4406203134Sthompsa		run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4407203134Sthompsa		run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4408208019Sthompsa
4409209917Sthompsa	} else if (sc->mac_ver == 0x3572) {
4410205042Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4411205042Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4412205042Sthompsa
4413208019Sthompsa		/* increase voltage from 1.2V to 1.35V */
4414208019Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4415208019Sthompsa		tmp = (tmp & ~0x1f000000) | 0x0d000000;
4416208019Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4417203134Sthompsa
4418209917Sthompsa		if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4419203134Sthompsa			run_delay(sc, 1);	/* wait for 1msec */
4420205042Sthompsa			/* decrease voltage back to 1.2V */
4421203134Sthompsa			tmp = (tmp & ~0x1f000000) | 0x01000000;
4422203134Sthompsa			run_write(sc, RT3070_LDO_CFG0, tmp);
4423203134Sthompsa		}
4424203134Sthompsa	}
4425203134Sthompsa
4426203134Sthompsa	/* select 20MHz bandwidth */
4427203134Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4428203134Sthompsa	run_rt3070_rf_write(sc, 31, rf & ~0x20);
4429203134Sthompsa
4430203134Sthompsa	/* calibrate filter for 20MHz bandwidth */
4431203134Sthompsa	sc->rf24_20mhz = 0x1f;	/* default value */
4432205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4433205042Sthompsa	run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4434203134Sthompsa
4435203134Sthompsa	/* select 40MHz bandwidth */
4436203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4437203134Sthompsa	run_bbp_write(sc, 4, (bbp4 & ~0x08) | 0x10);
4438205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4439205042Sthompsa	run_rt3070_rf_write(sc, 31, rf | 0x20);
4440203134Sthompsa
4441203134Sthompsa	/* calibrate filter for 40MHz bandwidth */
4442203134Sthompsa	sc->rf24_40mhz = 0x2f;	/* default value */
4443205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4444205042Sthompsa	run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4445203134Sthompsa
4446203134Sthompsa	/* go back to 20MHz bandwidth */
4447203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4448203134Sthompsa	run_bbp_write(sc, 4, bbp4 & ~0x18);
4449203134Sthompsa
4450205042Sthompsa	if (sc->mac_ver == 0x3572) {
4451205042Sthompsa		/* save default BBP registers 25 and 26 values */
4452205042Sthompsa		run_bbp_read(sc, 25, &sc->bbp25);
4453205042Sthompsa		run_bbp_read(sc, 26, &sc->bbp26);
4454205042Sthompsa	} else if (sc->mac_rev < 0x0211)
4455203134Sthompsa		run_rt3070_rf_write(sc, 27, 0x03);
4456203134Sthompsa
4457203134Sthompsa	run_read(sc, RT3070_OPT_14, &tmp);
4458203134Sthompsa	run_write(sc, RT3070_OPT_14, tmp | 1);
4459203134Sthompsa
4460205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4461205042Sthompsa		run_rt3070_rf_read(sc, 17, &rf);
4462205042Sthompsa		rf &= ~RT3070_TX_LO1;
4463205042Sthompsa		if ((sc->mac_ver == 0x3070 ||
4464205042Sthompsa		     (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4465205042Sthompsa		    !sc->ext_2ghz_lna)
4466205042Sthompsa			rf |= 0x20;	/* fix for long range Rx issue */
4467205042Sthompsa		if (sc->txmixgain_2ghz >= 1)
4468205042Sthompsa			rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4469205042Sthompsa		run_rt3070_rf_write(sc, 17, rf);
4470205042Sthompsa	}
4471205042Sthompsa
4472205042Sthompsa	if (sc->mac_rev == 0x3071) {
4473203134Sthompsa		run_rt3070_rf_read(sc, 1, &rf);
4474203134Sthompsa		rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4475203134Sthompsa		rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4476203134Sthompsa		run_rt3070_rf_write(sc, 1, rf);
4477203134Sthompsa
4478203134Sthompsa		run_rt3070_rf_read(sc, 15, &rf);
4479203134Sthompsa		run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4480203134Sthompsa
4481203134Sthompsa		run_rt3070_rf_read(sc, 20, &rf);
4482203134Sthompsa		run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4483203134Sthompsa
4484203134Sthompsa		run_rt3070_rf_read(sc, 21, &rf);
4485203134Sthompsa		run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4486205042Sthompsa	}
4487203134Sthompsa
4488205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4489205042Sthompsa		/* fix Tx to Rx IQ glitch by raising RF voltage */
4490203134Sthompsa		run_rt3070_rf_read(sc, 27, &rf);
4491203134Sthompsa		rf &= ~0x77;
4492205042Sthompsa		if (sc->mac_rev < 0x0211)
4493203134Sthompsa			rf |= 0x03;
4494203134Sthompsa		run_rt3070_rf_write(sc, 27, rf);
4495203134Sthompsa	}
4496209917Sthompsa	return (0);
4497203134Sthompsa}
4498203134Sthompsa
4499203134Sthompsastatic int
4500203134Sthompsarun_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4501203134Sthompsa    uint8_t *val)
4502203134Sthompsa{
4503203134Sthompsa	uint8_t rf22, rf24;
4504203134Sthompsa	uint8_t bbp55_pb, bbp55_sb, delta;
4505203134Sthompsa	int ntries;
4506203134Sthompsa
4507203134Sthompsa	/* program filter */
4508205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf24);
4509205042Sthompsa	rf24 = (rf24 & 0xc0) | init;	/* initial filter value */
4510203134Sthompsa	run_rt3070_rf_write(sc, 24, rf24);
4511203134Sthompsa
4512203134Sthompsa	/* enable baseband loopback mode */
4513203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4514203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4515203134Sthompsa
4516203134Sthompsa	/* set power and frequency of passband test tone */
4517203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4518203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4519203134Sthompsa		/* transmit test tone */
4520203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4521203134Sthompsa		run_delay(sc, 10);
4522203134Sthompsa		/* read received power */
4523203134Sthompsa		run_bbp_read(sc, 55, &bbp55_pb);
4524203134Sthompsa		if (bbp55_pb != 0)
4525203134Sthompsa			break;
4526203134Sthompsa	}
4527203134Sthompsa	if (ntries == 100)
4528203134Sthompsa		return ETIMEDOUT;
4529203134Sthompsa
4530203134Sthompsa	/* set power and frequency of stopband test tone */
4531203134Sthompsa	run_bbp_write(sc, 24, 0x06);
4532203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4533203134Sthompsa		/* transmit test tone */
4534203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4535203134Sthompsa		run_delay(sc, 10);
4536203134Sthompsa		/* read received power */
4537203134Sthompsa		run_bbp_read(sc, 55, &bbp55_sb);
4538203134Sthompsa
4539203134Sthompsa		delta = bbp55_pb - bbp55_sb;
4540203134Sthompsa		if (delta > target)
4541203134Sthompsa			break;
4542203134Sthompsa
4543203134Sthompsa		/* reprogram filter */
4544203134Sthompsa		rf24++;
4545203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4546203134Sthompsa	}
4547203134Sthompsa	if (ntries < 100) {
4548203134Sthompsa		if (rf24 != init)
4549203134Sthompsa			rf24--;	/* backtrack */
4550203134Sthompsa		*val = rf24;
4551203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4552203134Sthompsa	}
4553203134Sthompsa
4554203134Sthompsa	/* restore initial state */
4555203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4556203134Sthompsa
4557203134Sthompsa	/* disable baseband loopback mode */
4558203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4559203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4560203134Sthompsa
4561209917Sthompsa	return (0);
4562203134Sthompsa}
4563203134Sthompsa
4564205042Sthompsastatic void
4565205042Sthompsarun_rt3070_rf_setup(struct run_softc *sc)
4566205042Sthompsa{
4567205042Sthompsa	uint8_t bbp, rf;
4568205042Sthompsa	int i;
4569205042Sthompsa
4570205042Sthompsa	if (sc->mac_ver == 0x3572) {
4571205042Sthompsa		/* enable DC filter */
4572205042Sthompsa		if (sc->mac_rev >= 0x0201)
4573205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4574205042Sthompsa
4575205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4576205042Sthompsa		if (sc->ntxchains == 1)
4577205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4578205042Sthompsa		if (sc->nrxchains == 1)
4579205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4580205042Sthompsa		run_bbp_write(sc, 138, bbp);
4581205042Sthompsa
4582205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4583205042Sthompsa			/* improve power consumption */
4584205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4585205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4586205042Sthompsa		}
4587205042Sthompsa
4588205042Sthompsa		run_rt3070_rf_read(sc, 16, &rf);
4589205042Sthompsa		rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4590205042Sthompsa		run_rt3070_rf_write(sc, 16, rf);
4591205042Sthompsa
4592205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4593205042Sthompsa		/* enable DC filter */
4594205042Sthompsa		if (sc->mac_rev >= 0x0201)
4595205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4596205042Sthompsa
4597205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4598205042Sthompsa		if (sc->ntxchains == 1)
4599205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4600205042Sthompsa		if (sc->nrxchains == 1)
4601205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4602205042Sthompsa		run_bbp_write(sc, 138, bbp);
4603205042Sthompsa
4604205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4605205042Sthompsa			/* improve power consumption */
4606205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4607205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4608205042Sthompsa		}
4609205042Sthompsa
4610205042Sthompsa		run_write(sc, RT2860_TX_SW_CFG1, 0);
4611205042Sthompsa		if (sc->mac_rev < 0x0211) {
4612205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2,
4613205042Sthompsa			    sc->patch_dac ? 0x2c : 0x0f);
4614205042Sthompsa		} else
4615205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4616205042Sthompsa
4617205042Sthompsa	} else if (sc->mac_ver == 0x3070) {
4618205042Sthompsa		if (sc->mac_rev >= 0x0201) {
4619205042Sthompsa			/* enable DC filter */
4620205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4621205042Sthompsa
4622205042Sthompsa			/* improve power consumption */
4623205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4624205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4625205042Sthompsa		}
4626205042Sthompsa
4627205042Sthompsa		if (sc->mac_rev < 0x0211) {
4628205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG1, 0);
4629205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4630205042Sthompsa		} else
4631205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4632205042Sthompsa	}
4633205042Sthompsa
4634205042Sthompsa	/* initialize RF registers from ROM for >=RT3071*/
4635205042Sthompsa	if (sc->mac_ver >= 0x3071) {
4636205042Sthompsa		for (i = 0; i < 10; i++) {
4637205042Sthompsa			if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4638205042Sthompsa				continue;
4639205042Sthompsa			run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4640205042Sthompsa		}
4641205042Sthompsa	}
4642205042Sthompsa}
4643205042Sthompsa
4644203134Sthompsastatic int
4645203134Sthompsarun_txrx_enable(struct run_softc *sc)
4646203134Sthompsa{
4647203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4648203134Sthompsa	uint32_t tmp;
4649203134Sthompsa	int error, ntries;
4650203134Sthompsa
4651203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4652203134Sthompsa	for (ntries = 0; ntries < 200; ntries++) {
4653203134Sthompsa		if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4654203134Sthompsa			return error;
4655203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4656203134Sthompsa			break;
4657203134Sthompsa		run_delay(sc, 50);
4658203134Sthompsa	}
4659203134Sthompsa	if (ntries == 200)
4660203134Sthompsa		return ETIMEDOUT;
4661203134Sthompsa
4662203134Sthompsa	run_delay(sc, 50);
4663203134Sthompsa
4664203134Sthompsa	tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4665203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4666203134Sthompsa
4667203134Sthompsa	/* enable Rx bulk aggregation (set timeout and limit) */
4668203134Sthompsa	tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
4669203134Sthompsa	    RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
4670203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, tmp);
4671203134Sthompsa
4672203134Sthompsa	/* set Rx filter */
4673203134Sthompsa	tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
4674203134Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
4675203134Sthompsa		tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
4676203134Sthompsa		    RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
4677203134Sthompsa		    RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
4678203134Sthompsa		    RT2860_DROP_CFACK | RT2860_DROP_CFEND;
4679203134Sthompsa		if (ic->ic_opmode == IEEE80211_M_STA)
4680203134Sthompsa			tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
4681203134Sthompsa	}
4682203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4683203134Sthompsa
4684203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
4685203134Sthompsa	    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4686203134Sthompsa
4687209917Sthompsa	return (0);
4688203134Sthompsa}
4689203134Sthompsa
4690203134Sthompsastatic void
4691203134Sthompsarun_init_locked(struct run_softc *sc)
4692203134Sthompsa{
4693203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4694203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4695203134Sthompsa	uint32_t tmp;
4696203134Sthompsa	uint8_t bbp1, bbp3;
4697203134Sthompsa	int i;
4698203134Sthompsa	int ridx;
4699203134Sthompsa	int ntries;
4700203134Sthompsa
4701209917Sthompsa	if (ic->ic_nrunning > 1)
4702208019Sthompsa		return;
4703208019Sthompsa
4704203134Sthompsa	run_stop(sc);
4705203134Sthompsa
4706233283Sbschmidt	if (run_load_microcode(sc) != 0) {
4707233283Sbschmidt		device_printf(sc->sc_dev, "could not load 8051 microcode\n");
4708233283Sbschmidt		goto fail;
4709233283Sbschmidt	}
4710233283Sbschmidt
4711203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4712203134Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
4713203134Sthompsa			goto fail;
4714203134Sthompsa		if (tmp != 0 && tmp != 0xffffffff)
4715203134Sthompsa			break;
4716203134Sthompsa		run_delay(sc, 10);
4717203134Sthompsa	}
4718203134Sthompsa	if (ntries == 100)
4719203134Sthompsa		goto fail;
4720203134Sthompsa
4721203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
4722203134Sthompsa		run_setup_tx_list(sc, &sc->sc_epq[i]);
4723203134Sthompsa
4724203134Sthompsa	run_set_macaddr(sc, IF_LLADDR(ifp));
4725203134Sthompsa
4726203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4727203134Sthompsa		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
4728203134Sthompsa			goto fail;
4729203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4730203134Sthompsa			break;
4731203134Sthompsa		run_delay(sc, 10);
4732203134Sthompsa	}
4733203134Sthompsa	if (ntries == 100) {
4734203138Sthompsa		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
4735203134Sthompsa		goto fail;
4736203134Sthompsa	}
4737203134Sthompsa	tmp &= 0xff0;
4738203134Sthompsa	tmp |= RT2860_TX_WB_DDONE;
4739203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4740203134Sthompsa
4741203134Sthompsa	/* turn off PME_OEN to solve high-current issue */
4742203134Sthompsa	run_read(sc, RT2860_SYS_CTRL, &tmp);
4743203134Sthompsa	run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
4744203134Sthompsa
4745203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
4746203134Sthompsa	    RT2860_BBP_HRST | RT2860_MAC_SRST);
4747203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
4748203134Sthompsa
4749203134Sthompsa	if (run_reset(sc) != 0) {
4750203138Sthompsa		device_printf(sc->sc_dev, "could not reset chipset\n");
4751203134Sthompsa		goto fail;
4752203134Sthompsa	}
4753203134Sthompsa
4754203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4755203134Sthompsa
4756203134Sthompsa	/* init Tx power for all Tx rates (from EEPROM) */
4757203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
4758203134Sthompsa		if (sc->txpow20mhz[ridx] == 0xffffffff)
4759203134Sthompsa			continue;
4760203134Sthompsa		run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
4761203134Sthompsa	}
4762203134Sthompsa
4763233774Shselasky	for (i = 0; i < N(rt2870_def_mac); i++)
4764203134Sthompsa		run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
4765203134Sthompsa	run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
4766203134Sthompsa	run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
4767203134Sthompsa	run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
4768203134Sthompsa
4769205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4770203134Sthompsa		/* set delay of PA_PE assertion to 1us (unit of 0.25us) */
4771203134Sthompsa		run_write(sc, RT2860_TX_SW_CFG0,
4772203134Sthompsa		    4 << RT2860_DLY_PAPE_EN_SHIFT);
4773203134Sthompsa	}
4774203134Sthompsa
4775203134Sthompsa	/* wait while MAC is busy */
4776203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4777203134Sthompsa		if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
4778203134Sthompsa			goto fail;
4779203134Sthompsa		if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
4780203134Sthompsa			break;
4781203134Sthompsa		run_delay(sc, 10);
4782203134Sthompsa	}
4783203134Sthompsa	if (ntries == 100)
4784203134Sthompsa		goto fail;
4785203134Sthompsa
4786203134Sthompsa	/* clear Host to MCU mailbox */
4787203134Sthompsa	run_write(sc, RT2860_H2M_BBPAGENT, 0);
4788203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
4789203134Sthompsa	run_delay(sc, 10);
4790203134Sthompsa
4791203134Sthompsa	if (run_bbp_init(sc) != 0) {
4792203138Sthompsa		device_printf(sc->sc_dev, "could not initialize BBP\n");
4793203134Sthompsa		goto fail;
4794203134Sthompsa	}
4795203134Sthompsa
4796203134Sthompsa	/* abort TSF synchronization */
4797203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4798203134Sthompsa	tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4799203134Sthompsa	    RT2860_TBTT_TIMER_EN);
4800203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4801203134Sthompsa
4802203134Sthompsa	/* clear RX WCID search table */
4803203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
4804203134Sthompsa	/* clear WCID attribute table */
4805203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
4806203134Sthompsa
4807209144Sthompsa	/* hostapd sets a key before init. So, don't clear it. */
4808209917Sthompsa	if (sc->cmdq_key_set != RUN_CMDQ_GO) {
4809209144Sthompsa		/* clear shared key table */
4810209144Sthompsa		run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
4811209144Sthompsa		/* clear shared key mode */
4812209144Sthompsa		run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
4813209144Sthompsa	}
4814209144Sthompsa
4815203134Sthompsa	run_read(sc, RT2860_US_CYC_CNT, &tmp);
4816203134Sthompsa	tmp = (tmp & ~0xff) | 0x1e;
4817203134Sthompsa	run_write(sc, RT2860_US_CYC_CNT, tmp);
4818203134Sthompsa
4819205042Sthompsa	if (sc->mac_rev != 0x0101)
4820203134Sthompsa		run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
4821203134Sthompsa
4822203134Sthompsa	run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
4823203134Sthompsa	run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
4824203134Sthompsa
4825203134Sthompsa	/* write vendor-specific BBP values (from EEPROM) */
4826208019Sthompsa	for (i = 0; i < 10; i++) {
4827203134Sthompsa		if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
4828203134Sthompsa			continue;
4829203134Sthompsa		run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
4830203134Sthompsa	}
4831203134Sthompsa
4832203134Sthompsa	/* select Main antenna for 1T1R devices */
4833203134Sthompsa	if (sc->rf_rev == RT3070_RF_3020)
4834203134Sthompsa		run_set_rx_antenna(sc, 0);
4835203134Sthompsa
4836203134Sthompsa	/* send LEDs operating mode to microcontroller */
4837203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
4838203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
4839203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
4840203134Sthompsa
4841205042Sthompsa	if (sc->mac_ver >= 0x3070)
4842205042Sthompsa		run_rt3070_rf_init(sc);
4843205042Sthompsa
4844203134Sthompsa	/* disable non-existing Rx chains */
4845203134Sthompsa	run_bbp_read(sc, 3, &bbp3);
4846203134Sthompsa	bbp3 &= ~(1 << 3 | 1 << 4);
4847203134Sthompsa	if (sc->nrxchains == 2)
4848203134Sthompsa		bbp3 |= 1 << 3;
4849203134Sthompsa	else if (sc->nrxchains == 3)
4850203134Sthompsa		bbp3 |= 1 << 4;
4851203134Sthompsa	run_bbp_write(sc, 3, bbp3);
4852203134Sthompsa
4853203134Sthompsa	/* disable non-existing Tx chains */
4854203134Sthompsa	run_bbp_read(sc, 1, &bbp1);
4855203134Sthompsa	if (sc->ntxchains == 1)
4856203134Sthompsa		bbp1 &= ~(1 << 3 | 1 << 4);
4857203134Sthompsa	run_bbp_write(sc, 1, bbp1);
4858203134Sthompsa
4859205042Sthompsa	if (sc->mac_ver >= 0x3070)
4860205042Sthompsa		run_rt3070_rf_setup(sc);
4861203134Sthompsa
4862203134Sthompsa	/* select default channel */
4863203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
4864203134Sthompsa
4865203134Sthompsa	/* setup initial protection mode */
4866218492Sbschmidt	run_updateprot_cb(ic);
4867203134Sthompsa
4868203134Sthompsa	/* turn radio LED on */
4869203134Sthompsa	run_set_leds(sc, RT2860_LED_RADIO);
4870203134Sthompsa
4871203134Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4872203134Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
4873208019Sthompsa	sc->cmdq_run = RUN_CMDQ_GO;
4874203134Sthompsa
4875209917Sthompsa	for (i = 0; i != RUN_N_XFER; i++)
4876203134Sthompsa		usbd_xfer_set_stall(sc->sc_xfer[i]);
4877203134Sthompsa
4878203134Sthompsa	usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
4879203134Sthompsa
4880203134Sthompsa	if (run_txrx_enable(sc) != 0)
4881203134Sthompsa		goto fail;
4882203134Sthompsa
4883203134Sthompsa	return;
4884203134Sthompsa
4885203134Sthompsafail:
4886203134Sthompsa	run_stop(sc);
4887203134Sthompsa}
4888203134Sthompsa
4889203134Sthompsastatic void
4890203134Sthompsarun_init(void *arg)
4891203134Sthompsa{
4892203134Sthompsa	struct run_softc *sc = arg;
4893203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4894203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4895203134Sthompsa
4896203134Sthompsa	RUN_LOCK(sc);
4897203134Sthompsa	run_init_locked(sc);
4898203134Sthompsa	RUN_UNLOCK(sc);
4899203134Sthompsa
4900203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4901208019Sthompsa		ieee80211_start_all(ic);
4902203134Sthompsa}
4903203134Sthompsa
4904203134Sthompsastatic void
4905203134Sthompsarun_stop(void *arg)
4906203134Sthompsa{
4907203134Sthompsa	struct run_softc *sc = (struct run_softc *)arg;
4908203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4909203134Sthompsa	uint32_t tmp;
4910203134Sthompsa	int i;
4911203134Sthompsa	int ntries;
4912203134Sthompsa
4913203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4914203134Sthompsa
4915203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4916203134Sthompsa		run_set_leds(sc, 0);	/* turn all LEDs off */
4917203134Sthompsa
4918203134Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4919203134Sthompsa
4920208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
4921209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set;
4922208019Sthompsa
4923203134Sthompsa	RUN_UNLOCK(sc);
4924203134Sthompsa
4925203134Sthompsa	for(i = 0; i < RUN_N_XFER; i++)
4926203134Sthompsa		usbd_transfer_drain(sc->sc_xfer[i]);
4927203134Sthompsa
4928203134Sthompsa	RUN_LOCK(sc);
4929203134Sthompsa
4930209917Sthompsa	if (sc->rx_m != NULL) {
4931203134Sthompsa		m_free(sc->rx_m);
4932203134Sthompsa		sc->rx_m = NULL;
4933203134Sthompsa	}
4934203134Sthompsa
4935203134Sthompsa	/* disable Tx/Rx */
4936203134Sthompsa	run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
4937203134Sthompsa	tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4938203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
4939203134Sthompsa
4940203134Sthompsa	/* wait for pending Tx to complete */
4941203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4942209917Sthompsa		if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
4943203134Sthompsa			DPRINTF("Cannot read Tx queue count\n");
4944203134Sthompsa			break;
4945203134Sthompsa		}
4946209917Sthompsa		if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
4947203134Sthompsa			DPRINTF("All Tx cleared\n");
4948203134Sthompsa			break;
4949203134Sthompsa		}
4950203134Sthompsa		run_delay(sc, 10);
4951203134Sthompsa	}
4952209917Sthompsa	if (ntries >= 100)
4953203134Sthompsa		DPRINTF("There are still pending Tx\n");
4954203134Sthompsa	run_delay(sc, 10);
4955203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
4956203134Sthompsa
4957203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
4958203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4959203134Sthompsa
4960203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
4961203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
4962203134Sthompsa
4963203134Sthompsa	return;
4964203134Sthompsa}
4965203134Sthompsa
4966203134Sthompsastatic void
4967203134Sthompsarun_delay(struct run_softc *sc, unsigned int ms)
4968203134Sthompsa{
4969203134Sthompsa	usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
4970203134Sthompsa	    &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
4971203134Sthompsa}
4972203134Sthompsa
4973203134Sthompsastatic device_method_t run_methods[] = {
4974203134Sthompsa	/* Device interface */
4975203134Sthompsa	DEVMETHOD(device_probe,		run_match),
4976203134Sthompsa	DEVMETHOD(device_attach,	run_attach),
4977203134Sthompsa	DEVMETHOD(device_detach,	run_detach),
4978246614Shselasky	DEVMETHOD_END
4979203134Sthompsa};
4980203134Sthompsa
4981203134Sthompsastatic driver_t run_driver = {
4982233774Shselasky	.name = "run",
4983233774Shselasky	.methods = run_methods,
4984233774Shselasky	.size = sizeof(struct run_softc)
4985203134Sthompsa};
4986203134Sthompsa
4987203134Sthompsastatic devclass_t run_devclass;
4988203134Sthompsa
4989203134SthompsaDRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0);
4990212122SthompsaMODULE_DEPEND(run, wlan, 1, 1, 1);
4991212122SthompsaMODULE_DEPEND(run, usb, 1, 1, 1);
4992212122SthompsaMODULE_DEPEND(run, firmware, 1, 1, 1);
4993212122SthompsaMODULE_VERSION(run, 1);
4994