if_run.c revision 219257
1203134Sthompsa/*-
2205042Sthompsa * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr>
3205042Sthompsa * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca>
4205042Sthompsa * USB Consulting, Hans Petter Selasky <hselasky@freebsd.org>
5203134Sthompsa *
6203134Sthompsa * Permission to use, copy, modify, and distribute this software for any
7203134Sthompsa * purpose with or without fee is hereby granted, provided that the above
8203134Sthompsa * copyright notice and this permission notice appear in all copies.
9203134Sthompsa *
10203134Sthompsa * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11203134Sthompsa * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12203134Sthompsa * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13203134Sthompsa * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14203134Sthompsa * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15203134Sthompsa * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16203134Sthompsa * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17203134Sthompsa */
18203134Sthompsa
19203134Sthompsa#include <sys/cdefs.h>
20203134Sthompsa__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_run.c 219257 2011-03-04 07:01:45Z daichi $");
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
74208019Sthompsa#include "if_runreg.h"
75203134Sthompsa#include "if_runvar.h"
76203134Sthompsa
77203134Sthompsa#define nitems(_a)      (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;
85203134SthompsaSYSCTL_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
99203134Sthompsastatic const struct usb_device_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),
139209918Sthompsa    RUN_DEV(ASUS2,		USBN11),
140209918Sthompsa    RUN_DEV(AZUREWAVE,		RT2870_1),
141209918Sthompsa    RUN_DEV(AZUREWAVE,		RT2870_2),
142209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_1),
143209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_2),
144209918Sthompsa    RUN_DEV(AZUREWAVE,		RT3070_3),
145209918Sthompsa    RUN_DEV(BELKIN,		F5D8053V3),
146209918Sthompsa    RUN_DEV(BELKIN,		F5D8055),
147209918Sthompsa    RUN_DEV(BELKIN,		F6D4050V1),
148209918Sthompsa    RUN_DEV(BELKIN,		RT2870_1),
149209918Sthompsa    RUN_DEV(BELKIN,		RT2870_2),
150209918Sthompsa    RUN_DEV(CISCOLINKSYS2,	RT3070),
151209918Sthompsa    RUN_DEV(CISCOLINKSYS3,	RT3070),
152209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_1),
153209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_2),
154209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_3),
155209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_4),
156209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_5),
157209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_6),
158209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_7),
159209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT2870_8),
160209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT3070_1),
161209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	RT3070_2),
162209918Sthompsa    RUN_DEV(CONCEPTRONIC2,	VIGORN61),
163209918Sthompsa    RUN_DEV(COREGA,		CGWLUSB300GNM),
164209918Sthompsa    RUN_DEV(COREGA,		RT2870_1),
165209918Sthompsa    RUN_DEV(COREGA,		RT2870_2),
166209918Sthompsa    RUN_DEV(COREGA,		RT2870_3),
167209918Sthompsa    RUN_DEV(COREGA,		RT3070),
168209918Sthompsa    RUN_DEV(CYBERTAN,		RT2870),
169209918Sthompsa    RUN_DEV(DLINK,		RT2870),
170209918Sthompsa    RUN_DEV(DLINK,		RT3072),
171209918Sthompsa    RUN_DEV(DLINK2,		DWA130),
172209918Sthompsa    RUN_DEV(DLINK2,		RT2870_1),
173209918Sthompsa    RUN_DEV(DLINK2,		RT2870_2),
174209918Sthompsa    RUN_DEV(DLINK2,		RT3070_1),
175209918Sthompsa    RUN_DEV(DLINK2,		RT3070_2),
176209918Sthompsa    RUN_DEV(DLINK2,		RT3070_3),
177209918Sthompsa    RUN_DEV(DLINK2,		RT3070_4),
178209918Sthompsa    RUN_DEV(DLINK2,		RT3070_5),
179209918Sthompsa    RUN_DEV(DLINK2,		RT3072),
180209918Sthompsa    RUN_DEV(DLINK2,		RT3072_1),
181209918Sthompsa    RUN_DEV(EDIMAX,		EW7717),
182209918Sthompsa    RUN_DEV(EDIMAX,		EW7718),
183209918Sthompsa    RUN_DEV(EDIMAX,		RT2870_1),
184209918Sthompsa    RUN_DEV(ENCORE,		RT3070_1),
185209918Sthompsa    RUN_DEV(ENCORE,		RT3070_2),
186209918Sthompsa    RUN_DEV(ENCORE,		RT3070_3),
187209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB31N),
188209918Sthompsa    RUN_DEV(GIGABYTE,		GNWB32L),
189209918Sthompsa    RUN_DEV(GIGABYTE,		RT2870_1),
190209918Sthompsa    RUN_DEV(GIGASET,		RT3070_1),
191209918Sthompsa    RUN_DEV(GIGASET,		RT3070_2),
192209918Sthompsa    RUN_DEV(GUILLEMOT,		HWNU300),
193209918Sthompsa    RUN_DEV(HAWKING,		HWUN2),
194209918Sthompsa    RUN_DEV(HAWKING,		RT2870_1),
195209918Sthompsa    RUN_DEV(HAWKING,		RT2870_2),
196209918Sthompsa    RUN_DEV(HAWKING,		RT3070),
197209918Sthompsa    RUN_DEV(IODATA,		RT3072_1),
198209918Sthompsa    RUN_DEV(IODATA,		RT3072_2),
199209918Sthompsa    RUN_DEV(IODATA,		RT3072_3),
200209918Sthompsa    RUN_DEV(IODATA,		RT3072_4),
201209918Sthompsa    RUN_DEV(LINKSYS4,		RT3070),
202209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB100),
203209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB54GCV3),
204209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600N),
205209918Sthompsa    RUN_DEV(LINKSYS4,		WUSB600NV2),
206209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_1),
207209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_2),
208209918Sthompsa    RUN_DEV(LOGITEC,		RT2870_3),
209209918Sthompsa    RUN_DEV(MELCO,		RT2870_1),
210209918Sthompsa    RUN_DEV(MELCO,		RT2870_2),
211209918Sthompsa    RUN_DEV(MELCO,		WLIUCAG300N),
212209918Sthompsa    RUN_DEV(MELCO,		WLIUCG300N),
213219257Sdaichi    RUN_DEV(MELCO,		WLIUCG301N),
214209918Sthompsa    RUN_DEV(MELCO,		WLIUCGN),
215209918Sthompsa    RUN_DEV(MOTOROLA4,		RT2770),
216209918Sthompsa    RUN_DEV(MOTOROLA4,		RT3070),
217209918Sthompsa    RUN_DEV(MSI,		RT3070_1),
218209918Sthompsa    RUN_DEV(MSI,		RT3070_2),
219209918Sthompsa    RUN_DEV(MSI,		RT3070_3),
220209918Sthompsa    RUN_DEV(MSI,		RT3070_4),
221209918Sthompsa    RUN_DEV(MSI,		RT3070_5),
222209918Sthompsa    RUN_DEV(MSI,		RT3070_6),
223209918Sthompsa    RUN_DEV(MSI,		RT3070_7),
224209918Sthompsa    RUN_DEV(MSI,		RT3070_8),
225209918Sthompsa    RUN_DEV(MSI,		RT3070_9),
226209918Sthompsa    RUN_DEV(MSI,		RT3070_10),
227209918Sthompsa    RUN_DEV(MSI,		RT3070_11),
228209918Sthompsa    RUN_DEV(OVISLINK,		RT3072),
229209918Sthompsa    RUN_DEV(PARA,		RT3070),
230209918Sthompsa    RUN_DEV(PEGATRON,		RT2870),
231209918Sthompsa    RUN_DEV(PEGATRON,		RT3070),
232209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_2),
233209918Sthompsa    RUN_DEV(PEGATRON,		RT3070_3),
234209918Sthompsa    RUN_DEV(PHILIPS,		RT2870),
235209918Sthompsa    RUN_DEV(PLANEX2,		GWUS300MINIS),
236209918Sthompsa    RUN_DEV(PLANEX2,		GWUSMICRON),
237209918Sthompsa    RUN_DEV(PLANEX2,		RT2870),
238209918Sthompsa    RUN_DEV(PLANEX2,		RT3070),
239209918Sthompsa    RUN_DEV(QCOM,		RT2870),
240209918Sthompsa    RUN_DEV(QUANTA,		RT3070),
241209918Sthompsa    RUN_DEV(RALINK,		RT2070),
242209918Sthompsa    RUN_DEV(RALINK,		RT2770),
243209918Sthompsa    RUN_DEV(RALINK,		RT2870),
244209918Sthompsa    RUN_DEV(RALINK,		RT3070),
245209918Sthompsa    RUN_DEV(RALINK,		RT3071),
246209918Sthompsa    RUN_DEV(RALINK,		RT3072),
247209918Sthompsa    RUN_DEV(RALINK,		RT3370),
248209918Sthompsa    RUN_DEV(RALINK,		RT3572),
249209918Sthompsa    RUN_DEV(RALINK,		RT8070),
250209918Sthompsa    RUN_DEV(SAMSUNG2,		RT2870_1),
251209918Sthompsa    RUN_DEV(SENAO,		RT2870_1),
252209918Sthompsa    RUN_DEV(SENAO,		RT2870_2),
253209918Sthompsa    RUN_DEV(SENAO,		RT2870_3),
254209918Sthompsa    RUN_DEV(SENAO,		RT2870_4),
255209918Sthompsa    RUN_DEV(SENAO,		RT3070),
256209918Sthompsa    RUN_DEV(SENAO,		RT3071),
257209918Sthompsa    RUN_DEV(SENAO,		RT3072_1),
258209918Sthompsa    RUN_DEV(SENAO,		RT3072_2),
259209918Sthompsa    RUN_DEV(SENAO,		RT3072_3),
260209918Sthompsa    RUN_DEV(SENAO,		RT3072_4),
261209918Sthompsa    RUN_DEV(SENAO,		RT3072_5),
262209918Sthompsa    RUN_DEV(SITECOMEU,		RT2770),
263209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_1),
264209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_2),
265209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_3),
266209918Sthompsa    RUN_DEV(SITECOMEU,		RT2870_4),
267209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070),
268209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_2),
269209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_3),
270209918Sthompsa    RUN_DEV(SITECOMEU,		RT3070_4),
271209918Sthompsa    RUN_DEV(SITECOMEU,		RT3071),
272209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_1),
273209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_2),
274209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_3),
275209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_4),
276209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_5),
277209918Sthompsa    RUN_DEV(SITECOMEU,		RT3072_6),
278209918Sthompsa    RUN_DEV(SITECOMEU,		WL608),
279209918Sthompsa    RUN_DEV(SPARKLAN,		RT2870_1),
280209918Sthompsa    RUN_DEV(SPARKLAN,		RT3070),
281209918Sthompsa    RUN_DEV(SWEEX2,		LW153),
282209918Sthompsa    RUN_DEV(SWEEX2,		LW303),
283209918Sthompsa    RUN_DEV(SWEEX2,		LW313),
284209918Sthompsa    RUN_DEV(TOSHIBA,		RT3070),
285209918Sthompsa    RUN_DEV(UMEDIA,		RT2870_1),
286209918Sthompsa    RUN_DEV(ZCOM,		RT2870_1),
287209918Sthompsa    RUN_DEV(ZCOM,		RT2870_2),
288209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_1),
289209918Sthompsa    RUN_DEV(ZINWELL,		RT2870_2),
290209918Sthompsa    RUN_DEV(ZINWELL,		RT3070),
291209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_1),
292209918Sthompsa    RUN_DEV(ZINWELL,		RT3072_2),
293209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_1),
294209918Sthompsa    RUN_DEV(ZYXEL,		RT2870_2),
295209918Sthompsa#undef RUN_DEV
296203134Sthompsa};
297203134Sthompsa
298203134Sthompsastatic device_probe_t	run_match;
299203134Sthompsastatic device_attach_t	run_attach;
300203134Sthompsastatic device_detach_t	run_detach;
301203134Sthompsa
302203134Sthompsastatic usb_callback_t	run_bulk_rx_callback;
303203134Sthompsastatic usb_callback_t	run_bulk_tx_callback0;
304203134Sthompsastatic usb_callback_t	run_bulk_tx_callback1;
305203134Sthompsastatic usb_callback_t	run_bulk_tx_callback2;
306203134Sthompsastatic usb_callback_t	run_bulk_tx_callback3;
307203134Sthompsastatic usb_callback_t	run_bulk_tx_callback4;
308203134Sthompsastatic usb_callback_t	run_bulk_tx_callback5;
309203134Sthompsa
310203134Sthompsastatic void	run_bulk_tx_callbackN(struct usb_xfer *xfer,
311203134Sthompsa		    usb_error_t error, unsigned int index);
312203134Sthompsastatic struct ieee80211vap *run_vap_create(struct ieee80211com *,
313203134Sthompsa		    const char name[IFNAMSIZ], int unit, int opmode, int flags,
314203134Sthompsa		    const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t
315203134Sthompsa		    mac[IEEE80211_ADDR_LEN]);
316203134Sthompsastatic void	run_vap_delete(struct ieee80211vap *);
317208019Sthompsastatic void	run_cmdq_cb(void *, int);
318203134Sthompsastatic void	run_setup_tx_list(struct run_softc *,
319203134Sthompsa		    struct run_endpoint_queue *);
320203134Sthompsastatic void	run_unsetup_tx_list(struct run_softc *,
321203134Sthompsa		    struct run_endpoint_queue *);
322203134Sthompsastatic int	run_load_microcode(struct run_softc *);
323203134Sthompsastatic int	run_reset(struct run_softc *);
324203134Sthompsastatic usb_error_t run_do_request(struct run_softc *,
325203134Sthompsa		    struct usb_device_request *, void *);
326203134Sthompsastatic int	run_read(struct run_softc *, uint16_t, uint32_t *);
327203134Sthompsastatic int	run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
328203134Sthompsastatic int	run_write_2(struct run_softc *, uint16_t, uint16_t);
329203134Sthompsastatic int	run_write(struct run_softc *, uint16_t, uint32_t);
330203134Sthompsastatic int	run_write_region_1(struct run_softc *, uint16_t,
331203134Sthompsa		    const uint8_t *, int);
332203134Sthompsastatic int	run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
333203134Sthompsastatic int	run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
334203134Sthompsastatic int	run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
335203134Sthompsastatic int	run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t);
336203134Sthompsastatic int	run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
337203134Sthompsastatic int	run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
338203134Sthompsastatic int	run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
339203134Sthompsastatic int	run_bbp_write(struct run_softc *, uint8_t, uint8_t);
340203134Sthompsastatic int	run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
341203134Sthompsastatic const char *run_get_rf(int);
342203134Sthompsastatic int	run_read_eeprom(struct run_softc *);
343203134Sthompsastatic struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
344203134Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
345203134Sthompsastatic int	run_media_change(struct ifnet *);
346203134Sthompsastatic int	run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
347203134Sthompsastatic int	run_wme_update(struct ieee80211com *);
348208019Sthompsastatic void	run_wme_update_cb(void *);
349203134Sthompsastatic void	run_key_update_begin(struct ieee80211vap *);
350203134Sthompsastatic void	run_key_update_end(struct ieee80211vap *);
351208019Sthompsastatic void	run_key_set_cb(void *);
352208019Sthompsastatic int	run_key_set(struct ieee80211vap *, struct ieee80211_key *,
353203134Sthompsa			    const uint8_t mac[IEEE80211_ADDR_LEN]);
354208019Sthompsastatic void	run_key_delete_cb(void *);
355208019Sthompsastatic int	run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
356206358Srpaulostatic void	run_ratectl_to(void *);
357206358Srpaulostatic void	run_ratectl_cb(void *, int);
358208019Sthompsastatic void	run_drain_fifo(void *);
359203134Sthompsastatic void	run_iter_func(void *, struct ieee80211_node *);
360208019Sthompsastatic void	run_newassoc_cb(void *);
361203134Sthompsastatic void	run_newassoc(struct ieee80211_node *, int);
362203134Sthompsastatic void	run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
363203134Sthompsastatic void	run_tx_free(struct run_endpoint_queue *pq,
364203134Sthompsa		    struct run_tx_data *, int);
365208019Sthompsastatic void	run_set_tx_desc(struct run_softc *, struct run_tx_data *);
366203134Sthompsastatic int	run_tx(struct run_softc *, struct mbuf *,
367203134Sthompsa		    struct ieee80211_node *);
368203134Sthompsastatic int	run_tx_mgt(struct run_softc *, struct mbuf *,
369203134Sthompsa		    struct ieee80211_node *);
370203134Sthompsastatic int	run_sendprot(struct run_softc *, const struct mbuf *,
371203134Sthompsa		    struct ieee80211_node *, int, int);
372203134Sthompsastatic int	run_tx_param(struct run_softc *, struct mbuf *,
373203134Sthompsa		    struct ieee80211_node *,
374203134Sthompsa		    const struct ieee80211_bpf_params *);
375203134Sthompsastatic int	run_raw_xmit(struct ieee80211_node *, struct mbuf *,
376203134Sthompsa		    const struct ieee80211_bpf_params *);
377203134Sthompsastatic void	run_start(struct ifnet *);
378203134Sthompsastatic int	run_ioctl(struct ifnet *, u_long, caddr_t);
379205042Sthompsastatic void	run_set_agc(struct run_softc *, uint8_t);
380203134Sthompsastatic void	run_select_chan_group(struct run_softc *, int);
381203134Sthompsastatic void	run_set_rx_antenna(struct run_softc *, int);
382203134Sthompsastatic void	run_rt2870_set_chan(struct run_softc *, u_int);
383203134Sthompsastatic void	run_rt3070_set_chan(struct run_softc *, u_int);
384205042Sthompsastatic void	run_rt3572_set_chan(struct run_softc *, u_int);
385203134Sthompsastatic int	run_set_chan(struct run_softc *, struct ieee80211_channel *);
386203134Sthompsastatic void	run_set_channel(struct ieee80211com *);
387203134Sthompsastatic void	run_scan_start(struct ieee80211com *);
388203134Sthompsastatic void	run_scan_end(struct ieee80211com *);
389203134Sthompsastatic void	run_update_beacon(struct ieee80211vap *, int);
390208019Sthompsastatic void	run_update_beacon_cb(void *);
391203134Sthompsastatic void	run_updateprot(struct ieee80211com *);
392218492Sbschmidtstatic void	run_updateprot_cb(void *);
393208019Sthompsastatic void	run_usb_timeout_cb(void *);
394203134Sthompsastatic void	run_reset_livelock(struct run_softc *);
395203134Sthompsastatic void	run_enable_tsf_sync(struct run_softc *);
396203134Sthompsastatic void	run_enable_mrr(struct run_softc *);
397203134Sthompsastatic void	run_set_txpreamble(struct run_softc *);
398203134Sthompsastatic void	run_set_basicrates(struct run_softc *);
399203134Sthompsastatic void	run_set_leds(struct run_softc *, uint16_t);
400203134Sthompsastatic void	run_set_bssid(struct run_softc *, const uint8_t *);
401203134Sthompsastatic void	run_set_macaddr(struct run_softc *, const uint8_t *);
402203134Sthompsastatic void	run_updateslot(struct ifnet *);
403218492Sbschmidtstatic void	run_updateslot_cb(void *);
404208019Sthompsastatic void	run_update_mcast(struct ifnet *);
405203134Sthompsastatic int8_t	run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
406203134Sthompsastatic void	run_update_promisc_locked(struct ifnet *);
407203134Sthompsastatic void	run_update_promisc(struct ifnet *);
408203134Sthompsastatic int	run_bbp_init(struct run_softc *);
409203134Sthompsastatic int	run_rt3070_rf_init(struct run_softc *);
410203134Sthompsastatic int	run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
411203134Sthompsa		    uint8_t *);
412205042Sthompsastatic void	run_rt3070_rf_setup(struct run_softc *);
413203134Sthompsastatic int	run_txrx_enable(struct run_softc *);
414203134Sthompsastatic void	run_init(void *);
415203134Sthompsastatic void	run_init_locked(struct run_softc *);
416203134Sthompsastatic void	run_stop(void *);
417203134Sthompsastatic void	run_delay(struct run_softc *, unsigned int);
418203134Sthompsa
419203134Sthompsastatic const struct {
420208019Sthompsa	uint16_t	reg;
421203134Sthompsa	uint32_t	val;
422203134Sthompsa} rt2870_def_mac[] = {
423203134Sthompsa	RT2870_DEF_MAC
424203134Sthompsa};
425203134Sthompsa
426203134Sthompsastatic const struct {
427203134Sthompsa	uint8_t	reg;
428203134Sthompsa	uint8_t	val;
429203134Sthompsa} rt2860_def_bbp[] = {
430203134Sthompsa	RT2860_DEF_BBP
431203134Sthompsa};
432203134Sthompsa
433203134Sthompsastatic const struct rfprog {
434203134Sthompsa	uint8_t		chan;
435203134Sthompsa	uint32_t	r1, r2, r3, r4;
436203134Sthompsa} rt2860_rf2850[] = {
437203134Sthompsa	RT2860_RF2850
438203134Sthompsa};
439203134Sthompsa
440203134Sthompsastruct {
441203134Sthompsa	uint8_t	n, r, k;
442205042Sthompsa} rt3070_freqs[] = {
443205042Sthompsa	RT3070_RF3052
444203134Sthompsa};
445203134Sthompsa
446203134Sthompsastatic const struct {
447203134Sthompsa	uint8_t	reg;
448203134Sthompsa	uint8_t	val;
449203134Sthompsa} rt3070_def_rf[] = {
450203134Sthompsa	RT3070_DEF_RF
451205042Sthompsa},rt3572_def_rf[] = {
452205042Sthompsa	RT3572_DEF_RF
453203134Sthompsa};
454203134Sthompsa
455203134Sthompsastatic const struct usb_config run_config[RUN_N_XFER] = {
456203134Sthompsa    [RUN_BULK_TX_BE] = {
457203134Sthompsa	.type = UE_BULK,
458203134Sthompsa	.endpoint = UE_ADDR_ANY,
459203134Sthompsa	.ep_index = 0,
460203134Sthompsa	.direction = UE_DIR_OUT,
461203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
462203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
463203134Sthompsa	.callback = run_bulk_tx_callback0,
464203134Sthompsa	.timeout = 5000,	/* ms */
465203134Sthompsa    },
466203134Sthompsa    [RUN_BULK_TX_BK] = {
467203134Sthompsa	.type = UE_BULK,
468203134Sthompsa	.endpoint = UE_ADDR_ANY,
469203134Sthompsa	.direction = UE_DIR_OUT,
470203134Sthompsa	.ep_index = 1,
471203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
472203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
473203134Sthompsa	.callback = run_bulk_tx_callback1,
474203134Sthompsa	.timeout = 5000,	/* ms */
475203134Sthompsa    },
476203134Sthompsa    [RUN_BULK_TX_VI] = {
477203134Sthompsa	.type = UE_BULK,
478203134Sthompsa	.endpoint = UE_ADDR_ANY,
479203134Sthompsa	.direction = UE_DIR_OUT,
480203134Sthompsa	.ep_index = 2,
481203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
482203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
483203134Sthompsa	.callback = run_bulk_tx_callback2,
484203134Sthompsa	.timeout = 5000,	/* ms */
485203134Sthompsa    },
486203134Sthompsa    [RUN_BULK_TX_VO] = {
487203134Sthompsa	.type = UE_BULK,
488203134Sthompsa	.endpoint = UE_ADDR_ANY,
489203134Sthompsa	.direction = UE_DIR_OUT,
490203134Sthompsa	.ep_index = 3,
491203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
492203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
493203134Sthompsa	.callback = run_bulk_tx_callback3,
494203134Sthompsa	.timeout = 5000,	/* ms */
495203134Sthompsa    },
496203134Sthompsa    [RUN_BULK_TX_HCCA] = {
497203134Sthompsa	.type = UE_BULK,
498203134Sthompsa	.endpoint = UE_ADDR_ANY,
499203134Sthompsa	.direction = UE_DIR_OUT,
500203134Sthompsa	.ep_index = 4,
501203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
502203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
503203134Sthompsa	.callback = run_bulk_tx_callback4,
504203134Sthompsa	.timeout = 5000,	/* ms */
505203134Sthompsa    },
506203134Sthompsa    [RUN_BULK_TX_PRIO] = {
507203134Sthompsa	.type = UE_BULK,
508203134Sthompsa	.endpoint = UE_ADDR_ANY,
509203134Sthompsa	.direction = UE_DIR_OUT,
510203134Sthompsa	.ep_index = 5,
511203134Sthompsa	.bufsize = RUN_MAX_TXSZ,
512203134Sthompsa	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
513203134Sthompsa	.callback = run_bulk_tx_callback5,
514203134Sthompsa	.timeout = 5000,	/* ms */
515203134Sthompsa    },
516203134Sthompsa    [RUN_BULK_RX] = {
517203134Sthompsa	.type = UE_BULK,
518203134Sthompsa	.endpoint = UE_ADDR_ANY,
519203134Sthompsa	.direction = UE_DIR_IN,
520203134Sthompsa	.bufsize = RUN_MAX_RXSZ,
521203134Sthompsa	.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
522203134Sthompsa	.callback = run_bulk_rx_callback,
523203134Sthompsa    }
524203134Sthompsa};
525203134Sthompsa
526203134Sthompsaint
527203134Sthompsarun_match(device_t self)
528203134Sthompsa{
529203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
530203134Sthompsa
531203134Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
532203134Sthompsa		return (ENXIO);
533203134Sthompsa	if (uaa->info.bConfigIndex != 0)
534203134Sthompsa		return (ENXIO);
535203134Sthompsa	if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
536203134Sthompsa		return (ENXIO);
537203134Sthompsa
538203134Sthompsa	return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
539203134Sthompsa}
540203134Sthompsa
541203134Sthompsastatic int
542203134Sthompsarun_attach(device_t self)
543203134Sthompsa{
544203134Sthompsa	struct run_softc *sc = device_get_softc(self);
545203134Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
546203134Sthompsa	struct ieee80211com *ic;
547203134Sthompsa	struct ifnet *ifp;
548205042Sthompsa	uint32_t ver;
549203134Sthompsa	int i, ntries, error;
550203134Sthompsa	uint8_t iface_index, bands;
551203134Sthompsa
552203134Sthompsa	device_set_usb_desc(self);
553203134Sthompsa	sc->sc_udev = uaa->device;
554203134Sthompsa	sc->sc_dev = self;
555203134Sthompsa
556203134Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
557203134Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
558203134Sthompsa
559203134Sthompsa	iface_index = RT2860_IFACE_INDEX;
560208019Sthompsa
561203134Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
562203134Sthompsa	    sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
563203134Sthompsa	if (error) {
564205042Sthompsa		device_printf(self, "could not allocate USB transfers, "
565203134Sthompsa		    "err=%s\n", usbd_errstr(error));
566203134Sthompsa		goto detach;
567203134Sthompsa	}
568203134Sthompsa
569203134Sthompsa	RUN_LOCK(sc);
570203134Sthompsa
571203134Sthompsa	/* wait for the chip to settle */
572203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
573209917Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
574203134Sthompsa			RUN_UNLOCK(sc);
575203134Sthompsa			goto detach;
576203134Sthompsa		}
577205042Sthompsa		if (ver != 0 && ver != 0xffffffff)
578203134Sthompsa			break;
579203134Sthompsa		run_delay(sc, 10);
580203134Sthompsa	}
581203134Sthompsa	if (ntries == 100) {
582203138Sthompsa		device_printf(sc->sc_dev,
583203138Sthompsa		    "timeout waiting for NIC to initialize\n");
584203134Sthompsa		RUN_UNLOCK(sc);
585203134Sthompsa		goto detach;
586203134Sthompsa	}
587205042Sthompsa	sc->mac_ver = ver >> 16;
588205042Sthompsa	sc->mac_rev = ver & 0xffff;
589203134Sthompsa
590203134Sthompsa	/* retrieve RF rev. no and various other things from EEPROM */
591203134Sthompsa	run_read_eeprom(sc);
592203134Sthompsa
593203138Sthompsa	device_printf(sc->sc_dev,
594203138Sthompsa	    "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
595205042Sthompsa	    sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
596203138Sthompsa	    sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
597203134Sthompsa
598203134Sthompsa	if ((error = run_load_microcode(sc)) != 0) {
599203138Sthompsa		device_printf(sc->sc_dev, "could not load 8051 microcode\n");
600203134Sthompsa		RUN_UNLOCK(sc);
601203134Sthompsa		goto detach;
602203134Sthompsa	}
603203134Sthompsa
604203134Sthompsa	RUN_UNLOCK(sc);
605203134Sthompsa
606203134Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
607203134Sthompsa	if(ifp == NULL){
608203138Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
609203134Sthompsa		goto detach;
610203134Sthompsa	}
611203134Sthompsa	ic = ifp->if_l2com;
612203134Sthompsa
613203134Sthompsa	ifp->if_softc = sc;
614203134Sthompsa	if_initname(ifp, "run", device_get_unit(sc->sc_dev));
615203134Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
616203134Sthompsa	ifp->if_init = run_init;
617203134Sthompsa	ifp->if_ioctl = run_ioctl;
618203134Sthompsa	ifp->if_start = run_start;
619207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
620207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
621203134Sthompsa	IFQ_SET_READY(&ifp->if_snd);
622203134Sthompsa
623203134Sthompsa	ic->ic_ifp = ifp;
624203134Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
625203134Sthompsa	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
626208019Sthompsa
627203134Sthompsa	/* set device capabilities */
628203134Sthompsa	ic->ic_caps =
629203134Sthompsa	    IEEE80211_C_STA |		/* station mode supported */
630203134Sthompsa	    IEEE80211_C_MONITOR |	/* monitor mode supported */
631203134Sthompsa	    IEEE80211_C_IBSS |
632203134Sthompsa	    IEEE80211_C_HOSTAP |
633208019Sthompsa	    IEEE80211_C_WDS |		/* 4-address traffic works */
634208019Sthompsa	    IEEE80211_C_MBSS |
635203134Sthompsa	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
636203134Sthompsa	    IEEE80211_C_SHSLOT |	/* short slot time supported */
637203134Sthompsa	    IEEE80211_C_WME |		/* WME */
638214894Sbschmidt	    IEEE80211_C_WPA;		/* WPA1|WPA2(RSN) */
639203134Sthompsa
640203134Sthompsa	ic->ic_cryptocaps =
641203134Sthompsa	    IEEE80211_CRYPTO_WEP |
642203134Sthompsa	    IEEE80211_CRYPTO_AES_CCM |
643203134Sthompsa	    IEEE80211_CRYPTO_TKIPMIC |
644203134Sthompsa	    IEEE80211_CRYPTO_TKIP;
645203134Sthompsa
646203134Sthompsa	ic->ic_flags |= IEEE80211_F_DATAPAD;
647203134Sthompsa	ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
648203134Sthompsa
649203134Sthompsa	bands = 0;
650203134Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
651203134Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
652203134Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
653203134Sthompsa
654203134Sthompsa	/*
655203134Sthompsa	 * Do this by own because h/w supports
656203134Sthompsa	 * more channels than ieee80211_init_channels()
657203134Sthompsa	 */
658205042Sthompsa	if (sc->rf_rev == RT2860_RF_2750 ||
659205042Sthompsa	    sc->rf_rev == RT2860_RF_2850 ||
660205042Sthompsa	    sc->rf_rev == RT3070_RF_3052) {
661203134Sthompsa		/* set supported .11a rates */
662203134Sthompsa		for (i = 14; i < nitems(rt2860_rf2850); i++) {
663203134Sthompsa			uint8_t chan = rt2860_rf2850[i].chan;
664203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_freq =
665203134Sthompsa			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A);
666203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_ieee = chan;
667203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A;
668203134Sthompsa			ic->ic_channels[ic->ic_nchans].ic_extieee = 0;
669203134Sthompsa			ic->ic_nchans++;
670203134Sthompsa		}
671203134Sthompsa	}
672203134Sthompsa
673203134Sthompsa	ieee80211_ifattach(ic, sc->sc_bssid);
674203134Sthompsa
675203134Sthompsa	ic->ic_scan_start = run_scan_start;
676203134Sthompsa	ic->ic_scan_end = run_scan_end;
677203134Sthompsa	ic->ic_set_channel = run_set_channel;
678203134Sthompsa	ic->ic_node_alloc = run_node_alloc;
679203134Sthompsa	ic->ic_newassoc = run_newassoc;
680218492Sbschmidt	ic->ic_updateslot = run_updateslot;
681208019Sthompsa	ic->ic_update_mcast = run_update_mcast;
682203134Sthompsa	ic->ic_wme.wme_update = run_wme_update;
683203134Sthompsa	ic->ic_raw_xmit = run_raw_xmit;
684203134Sthompsa	ic->ic_update_promisc = run_update_promisc;
685203134Sthompsa
686203134Sthompsa	ic->ic_vap_create = run_vap_create;
687203134Sthompsa	ic->ic_vap_delete = run_vap_delete;
688203134Sthompsa
689203134Sthompsa	ieee80211_radiotap_attach(ic,
690203134Sthompsa	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
691203134Sthompsa		RUN_TX_RADIOTAP_PRESENT,
692203134Sthompsa	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
693203134Sthompsa		RUN_RX_RADIOTAP_PRESENT);
694203134Sthompsa
695208019Sthompsa	TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
696208019Sthompsa	TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
697208019Sthompsa	callout_init((struct callout *)&sc->ratectl_ch, 1);
698208019Sthompsa
699203134Sthompsa	if (bootverbose)
700203134Sthompsa		ieee80211_announce(ic);
701203134Sthompsa
702209917Sthompsa	return (0);
703203134Sthompsa
704203134Sthompsadetach:
705203134Sthompsa	run_detach(self);
706209917Sthompsa	return (ENXIO);
707203134Sthompsa}
708203134Sthompsa
709203134Sthompsastatic int
710203134Sthompsarun_detach(device_t self)
711203134Sthompsa{
712203134Sthompsa	struct run_softc *sc = device_get_softc(self);
713203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
714203134Sthompsa	struct ieee80211com *ic;
715203134Sthompsa	int i;
716203134Sthompsa
717203134Sthompsa	/* stop all USB transfers */
718203134Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
719203134Sthompsa
720203134Sthompsa	RUN_LOCK(sc);
721209144Sthompsa
722209144Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
723209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
724209144Sthompsa
725203134Sthompsa	/* free TX list, if any */
726203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
727203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
728203134Sthompsa	RUN_UNLOCK(sc);
729203134Sthompsa
730203134Sthompsa	if (ifp) {
731203134Sthompsa		ic = ifp->if_l2com;
732208019Sthompsa		/* drain tasks */
733208019Sthompsa		usb_callout_drain(&sc->ratectl_ch);
734208019Sthompsa		ieee80211_draintask(ic, &sc->cmdq_task);
735208019Sthompsa		ieee80211_draintask(ic, &sc->ratectl_task);
736203134Sthompsa		ieee80211_ifdetach(ic);
737203134Sthompsa		if_free(ifp);
738203134Sthompsa	}
739203134Sthompsa
740203134Sthompsa	mtx_destroy(&sc->sc_mtx);
741203134Sthompsa
742203134Sthompsa	return (0);
743203134Sthompsa}
744203134Sthompsa
745203134Sthompsastatic struct ieee80211vap *
746203134Sthompsarun_vap_create(struct ieee80211com *ic,
747203134Sthompsa    const char name[IFNAMSIZ], int unit, int opmode, int flags,
748203134Sthompsa    const uint8_t bssid[IEEE80211_ADDR_LEN],
749203134Sthompsa    const uint8_t mac[IEEE80211_ADDR_LEN])
750203134Sthompsa{
751208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
752208019Sthompsa	struct run_softc *sc = ifp->if_softc;
753203134Sthompsa	struct run_vap *rvp;
754203134Sthompsa	struct ieee80211vap *vap;
755208019Sthompsa	int i;
756203134Sthompsa
757209917Sthompsa	if (sc->rvp_cnt >= RUN_VAP_MAX) {
758208019Sthompsa		if_printf(ifp, "number of VAPs maxed out\n");
759209917Sthompsa		return (NULL);
760208019Sthompsa	}
761208019Sthompsa
762208019Sthompsa	switch (opmode) {
763208019Sthompsa	case IEEE80211_M_STA:
764208019Sthompsa		/* enable s/w bmiss handling for sta mode */
765208019Sthompsa		flags |= IEEE80211_CLONE_NOBEACONS;
766208019Sthompsa		/* fall though */
767208019Sthompsa	case IEEE80211_M_IBSS:
768208019Sthompsa	case IEEE80211_M_MONITOR:
769208019Sthompsa	case IEEE80211_M_HOSTAP:
770208019Sthompsa	case IEEE80211_M_MBSS:
771208019Sthompsa		/* other than WDS vaps, only one at a time */
772208019Sthompsa		if (!TAILQ_EMPTY(&ic->ic_vaps))
773209917Sthompsa			return (NULL);
774208019Sthompsa		break;
775208019Sthompsa	case IEEE80211_M_WDS:
776208019Sthompsa		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
777208019Sthompsa			if(vap->iv_opmode != IEEE80211_M_HOSTAP)
778208019Sthompsa				continue;
779208019Sthompsa			/* WDS vap's always share the local mac address. */
780208019Sthompsa			flags &= ~IEEE80211_CLONE_BSSID;
781208019Sthompsa			break;
782208019Sthompsa		}
783209917Sthompsa		if (vap == NULL) {
784208019Sthompsa			if_printf(ifp, "wds only supported in ap mode\n");
785209917Sthompsa			return (NULL);
786208019Sthompsa		}
787208019Sthompsa		break;
788208019Sthompsa	default:
789208019Sthompsa		if_printf(ifp, "unknown opmode %d\n", opmode);
790209917Sthompsa		return (NULL);
791208019Sthompsa	}
792208019Sthompsa
793208019Sthompsa	rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
794203134Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
795203134Sthompsa	if (rvp == NULL)
796209917Sthompsa		return (NULL);
797203134Sthompsa	vap = &rvp->vap;
798208019Sthompsa	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
799203134Sthompsa
800203134Sthompsa	vap->iv_key_update_begin = run_key_update_begin;
801203134Sthompsa	vap->iv_key_update_end = run_key_update_end;
802203134Sthompsa	vap->iv_update_beacon = run_update_beacon;
803208019Sthompsa	vap->iv_max_aid = RT2870_WCID_MAX;
804208019Sthompsa	/*
805208019Sthompsa	 * To delete the right key from h/w, we need wcid.
806208019Sthompsa	 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
807208019Sthompsa	 * and matching wcid will be written into there. So, cast
808208019Sthompsa	 * some spells to remove 'const' from ieee80211_key{}
809208019Sthompsa	 */
810208019Sthompsa	vap->iv_key_delete = (void *)run_key_delete;
811208019Sthompsa	vap->iv_key_set = (void *)run_key_set;
812203134Sthompsa
813203134Sthompsa	/* override state transition machine */
814203134Sthompsa	rvp->newstate = vap->iv_newstate;
815203134Sthompsa	vap->iv_newstate = run_newstate;
816203134Sthompsa
817206358Srpaulo	ieee80211_ratectl_init(vap);
818206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
819203134Sthompsa
820203134Sthompsa	/* complete setup */
821203134Sthompsa	ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
822208019Sthompsa
823208019Sthompsa	/* make sure id is always unique */
824209917Sthompsa	for (i = 0; i < RUN_VAP_MAX; i++) {
825208019Sthompsa		if((sc->rvp_bmap & 1 << i) == 0){
826208019Sthompsa			sc->rvp_bmap |= 1 << i;
827208019Sthompsa			rvp->rvp_id = i;
828208019Sthompsa			break;
829208019Sthompsa		}
830208019Sthompsa	}
831209917Sthompsa	if (sc->rvp_cnt++ == 0)
832208019Sthompsa		ic->ic_opmode = opmode;
833208019Sthompsa
834209917Sthompsa	if (opmode == IEEE80211_M_HOSTAP)
835209144Sthompsa		sc->cmdq_run = RUN_CMDQ_GO;
836209144Sthompsa
837208019Sthompsa	DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
838208019Sthompsa	    rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
839208019Sthompsa
840209917Sthompsa	return (vap);
841203134Sthompsa}
842203134Sthompsa
843203134Sthompsastatic void
844203134Sthompsarun_vap_delete(struct ieee80211vap *vap)
845203134Sthompsa{
846203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
847203134Sthompsa	struct ifnet *ifp;
848203134Sthompsa	struct ieee80211com *ic;
849203134Sthompsa	struct run_softc *sc;
850208019Sthompsa	uint8_t rvp_id;
851203134Sthompsa
852209917Sthompsa	if (vap == NULL)
853203134Sthompsa		return;
854203134Sthompsa
855203134Sthompsa	ic = vap->iv_ic;
856203134Sthompsa	ifp = ic->ic_ifp;
857203134Sthompsa
858203134Sthompsa	sc = ifp->if_softc;
859203134Sthompsa
860205042Sthompsa	RUN_LOCK(sc);
861208019Sthompsa
862218492Sbschmidt	m_freem(rvp->beacon_mbuf);
863218492Sbschmidt	rvp->beacon_mbuf = NULL;
864218492Sbschmidt
865208019Sthompsa	rvp_id = rvp->rvp_id;
866208019Sthompsa	sc->ratectl_run &= ~(1 << rvp_id);
867208019Sthompsa	sc->rvp_bmap &= ~(1 << rvp_id);
868208019Sthompsa	run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
869208019Sthompsa	run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
870208019Sthompsa	--sc->rvp_cnt;
871208019Sthompsa
872208019Sthompsa	DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
873208019Sthompsa	    vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
874208019Sthompsa
875205042Sthompsa	RUN_UNLOCK(sc);
876203134Sthompsa
877206358Srpaulo	ieee80211_ratectl_deinit(vap);
878203134Sthompsa	ieee80211_vap_detach(vap);
879203134Sthompsa	free(rvp, M_80211_VAP);
880203134Sthompsa}
881203134Sthompsa
882208019Sthompsa/*
883208019Sthompsa * There are numbers of functions need to be called in context thread.
884208019Sthompsa * Rather than creating taskqueue event for each of those functions,
885208019Sthompsa * here is all-for-one taskqueue callback function. This function
886208019Sthompsa * gurantees deferred functions are executed in the same order they
887208019Sthompsa * were enqueued.
888208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[].
889208019Sthompsa */
890203134Sthompsastatic void
891208019Sthompsarun_cmdq_cb(void *arg, int pending)
892208019Sthompsa{
893208019Sthompsa	struct run_softc *sc = arg;
894208019Sthompsa	uint8_t i;
895208019Sthompsa
896208019Sthompsa	/* call cmdq[].func locked */
897208019Sthompsa	RUN_LOCK(sc);
898209917Sthompsa	for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
899209917Sthompsa	    i = sc->cmdq_exec, pending--) {
900208019Sthompsa		DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
901209917Sthompsa		if (sc->cmdq_run == RUN_CMDQ_GO) {
902208019Sthompsa			/*
903208019Sthompsa			 * If arg0 is NULL, callback func needs more
904208019Sthompsa			 * than one arg. So, pass ptr to cmdq struct.
905208019Sthompsa			 */
906209917Sthompsa			if (sc->cmdq[i].arg0)
907208019Sthompsa				sc->cmdq[i].func(sc->cmdq[i].arg0);
908208019Sthompsa			else
909208019Sthompsa				sc->cmdq[i].func(&sc->cmdq[i]);
910208019Sthompsa		}
911208019Sthompsa		sc->cmdq[i].arg0 = NULL;
912208019Sthompsa		sc->cmdq[i].func = NULL;
913208019Sthompsa		sc->cmdq_exec++;
914208019Sthompsa		sc->cmdq_exec &= RUN_CMDQ_MASQ;
915208019Sthompsa	}
916208019Sthompsa	RUN_UNLOCK(sc);
917208019Sthompsa}
918208019Sthompsa
919208019Sthompsastatic void
920203134Sthompsarun_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
921203134Sthompsa{
922203134Sthompsa	struct run_tx_data *data;
923203134Sthompsa
924203134Sthompsa	memset(pq, 0, sizeof(*pq));
925203134Sthompsa
926203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
927203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
928203134Sthompsa
929203134Sthompsa	for (data = &pq->tx_data[0];
930203134Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
931203134Sthompsa		data->sc = sc;
932203134Sthompsa		STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
933203134Sthompsa	}
934203134Sthompsa	pq->tx_nfree = RUN_TX_RING_COUNT;
935203134Sthompsa}
936203134Sthompsa
937203134Sthompsastatic void
938203134Sthompsarun_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
939203134Sthompsa{
940203134Sthompsa	struct run_tx_data *data;
941203134Sthompsa
942203134Sthompsa	/* make sure any subsequent use of the queues will fail */
943203134Sthompsa	pq->tx_nfree = 0;
944203134Sthompsa	STAILQ_INIT(&pq->tx_fh);
945203134Sthompsa	STAILQ_INIT(&pq->tx_qh);
946203134Sthompsa
947203134Sthompsa	/* free up all node references and mbufs */
948203134Sthompsa	for (data = &pq->tx_data[0];
949209917Sthompsa	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
950203134Sthompsa		if (data->m != NULL) {
951203134Sthompsa			m_freem(data->m);
952203134Sthompsa			data->m = NULL;
953203134Sthompsa		}
954203134Sthompsa		if (data->ni != NULL) {
955203134Sthompsa			ieee80211_free_node(data->ni);
956203134Sthompsa			data->ni = NULL;
957203134Sthompsa		}
958203134Sthompsa	}
959203134Sthompsa}
960203134Sthompsa
961203134Sthompsaint
962203134Sthompsarun_load_microcode(struct run_softc *sc)
963203134Sthompsa{
964203134Sthompsa	usb_device_request_t req;
965203137Sthompsa	const struct firmware *fw;
966203134Sthompsa	const u_char *base;
967203134Sthompsa	uint32_t tmp;
968203134Sthompsa	int ntries, error;
969203134Sthompsa	const uint64_t *temp;
970203134Sthompsa	uint64_t bytes;
971203134Sthompsa
972205042Sthompsa	RUN_UNLOCK(sc);
973203137Sthompsa	fw = firmware_get("runfw");
974205042Sthompsa	RUN_LOCK(sc);
975209917Sthompsa	if (fw == NULL) {
976203138Sthompsa		device_printf(sc->sc_dev,
977203138Sthompsa		    "failed loadfirmware of file %s\n", "runfw");
978203134Sthompsa		return ENOENT;
979203134Sthompsa	}
980203134Sthompsa
981203137Sthompsa	if (fw->datasize != 8192) {
982203138Sthompsa		device_printf(sc->sc_dev,
983203138Sthompsa		    "invalid firmware size (should be 8KB)\n");
984203137Sthompsa		error = EINVAL;
985203137Sthompsa		goto fail;
986203134Sthompsa	}
987203134Sthompsa
988203134Sthompsa	/*
989203134Sthompsa	 * RT3071/RT3072 use a different firmware
990203134Sthompsa	 * run-rt2870 (8KB) contains both,
991203134Sthompsa	 * first half (4KB) is for rt2870,
992203134Sthompsa	 * last half is for rt3071.
993203134Sthompsa	 */
994203137Sthompsa	base = fw->data;
995205042Sthompsa	if ((sc->mac_ver) != 0x2860 &&
996205042Sthompsa	    (sc->mac_ver) != 0x2872 &&
997209917Sthompsa	    (sc->mac_ver) != 0x3070) {
998203134Sthompsa		base += 4096;
999205042Sthompsa	}
1000203134Sthompsa
1001203134Sthompsa	/* cheap sanity check */
1002203137Sthompsa	temp = fw->data;
1003203134Sthompsa	bytes = *temp;
1004209917Sthompsa	if (bytes != be64toh(0xffffff0210280210)) {
1005203138Sthompsa		device_printf(sc->sc_dev, "firmware checksum failed\n");
1006203137Sthompsa		error = EINVAL;
1007203137Sthompsa		goto fail;
1008203137Sthompsa	}
1009203134Sthompsa
1010203134Sthompsa	run_read(sc, RT2860_ASIC_VER_ID, &tmp);
1011203134Sthompsa	/* write microcode image */
1012203134Sthompsa	run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1013203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1014203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1015203134Sthompsa
1016203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1017203134Sthompsa	req.bRequest = RT2870_RESET;
1018203134Sthompsa	USETW(req.wValue, 8);
1019203134Sthompsa	USETW(req.wIndex, 0);
1020203134Sthompsa	USETW(req.wLength, 0);
1021203137Sthompsa	if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL)) != 0) {
1022203138Sthompsa		device_printf(sc->sc_dev, "firmware reset failed\n");
1023203137Sthompsa		goto fail;
1024203137Sthompsa	}
1025203134Sthompsa
1026203134Sthompsa	run_delay(sc, 10);
1027203134Sthompsa
1028203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
1029205042Sthompsa	if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1030203137Sthompsa		goto fail;
1031203134Sthompsa
1032203134Sthompsa	/* wait until microcontroller is ready */
1033203134Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
1034203137Sthompsa		if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1035203137Sthompsa			goto fail;
1036203137Sthompsa		}
1037203134Sthompsa		if (tmp & RT2860_MCU_READY)
1038203134Sthompsa			break;
1039203134Sthompsa		run_delay(sc, 10);
1040203134Sthompsa	}
1041203134Sthompsa	if (ntries == 1000) {
1042203138Sthompsa		device_printf(sc->sc_dev,
1043203138Sthompsa		    "timeout waiting for MCU to initialize\n");
1044203137Sthompsa		error = ETIMEDOUT;
1045203137Sthompsa		goto fail;
1046203134Sthompsa	}
1047205042Sthompsa	device_printf(sc->sc_dev, "firmware %s loaded\n",
1048205042Sthompsa	    (base == fw->data) ? "RT2870" : "RT3071");
1049203134Sthompsa
1050203137Sthompsafail:
1051203137Sthompsa	firmware_put(fw, FIRMWARE_UNLOAD);
1052203137Sthompsa	return (error);
1053203134Sthompsa}
1054203134Sthompsa
1055203134Sthompsaint
1056203134Sthompsarun_reset(struct run_softc *sc)
1057203134Sthompsa{
1058203134Sthompsa	usb_device_request_t req;
1059203134Sthompsa
1060203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1061203134Sthompsa	req.bRequest = RT2870_RESET;
1062203134Sthompsa	USETW(req.wValue, 1);
1063203134Sthompsa	USETW(req.wIndex, 0);
1064203134Sthompsa	USETW(req.wLength, 0);
1065209917Sthompsa	return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1066203134Sthompsa}
1067203134Sthompsa
1068203134Sthompsastatic usb_error_t
1069203134Sthompsarun_do_request(struct run_softc *sc,
1070203134Sthompsa    struct usb_device_request *req, void *data)
1071203134Sthompsa{
1072203134Sthompsa	usb_error_t err;
1073203134Sthompsa	int ntries = 10;
1074203134Sthompsa
1075203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1076203134Sthompsa
1077203134Sthompsa	while (ntries--) {
1078203134Sthompsa		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1079203134Sthompsa		    req, data, 0, NULL, 250 /* ms */);
1080203134Sthompsa		if (err == 0)
1081203134Sthompsa			break;
1082203134Sthompsa		DPRINTFN(1, "Control request failed, %s (retrying)\n",
1083203134Sthompsa		    usbd_errstr(err));
1084203134Sthompsa		run_delay(sc, 10);
1085203134Sthompsa	}
1086203134Sthompsa	return (err);
1087203134Sthompsa}
1088203134Sthompsa
1089203134Sthompsastatic int
1090203134Sthompsarun_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1091203134Sthompsa{
1092203134Sthompsa	uint32_t tmp;
1093203134Sthompsa	int error;
1094203134Sthompsa
1095203134Sthompsa	error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1096203134Sthompsa	if (error == 0)
1097203134Sthompsa		*val = le32toh(tmp);
1098203134Sthompsa	else
1099203134Sthompsa		*val = 0xffffffff;
1100209917Sthompsa	return (error);
1101203134Sthompsa}
1102203134Sthompsa
1103203134Sthompsastatic int
1104203134Sthompsarun_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1105203134Sthompsa{
1106203134Sthompsa	usb_device_request_t req;
1107203134Sthompsa
1108203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1109203134Sthompsa	req.bRequest = RT2870_READ_REGION_1;
1110203134Sthompsa	USETW(req.wValue, 0);
1111203134Sthompsa	USETW(req.wIndex, reg);
1112203134Sthompsa	USETW(req.wLength, len);
1113203134Sthompsa
1114209917Sthompsa	return (run_do_request(sc, &req, buf));
1115203134Sthompsa}
1116203134Sthompsa
1117203134Sthompsastatic int
1118203134Sthompsarun_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1119203134Sthompsa{
1120203134Sthompsa	usb_device_request_t req;
1121203134Sthompsa
1122203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1123203134Sthompsa	req.bRequest = RT2870_WRITE_2;
1124203134Sthompsa	USETW(req.wValue, val);
1125203134Sthompsa	USETW(req.wIndex, reg);
1126203134Sthompsa	USETW(req.wLength, 0);
1127203134Sthompsa
1128209917Sthompsa	return (run_do_request(sc, &req, NULL));
1129203134Sthompsa}
1130203134Sthompsa
1131203134Sthompsastatic int
1132203134Sthompsarun_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1133203134Sthompsa{
1134203134Sthompsa	int error;
1135203134Sthompsa
1136203134Sthompsa	if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1137203134Sthompsa		error = run_write_2(sc, reg + 2, val >> 16);
1138209917Sthompsa	return (error);
1139203134Sthompsa}
1140203134Sthompsa
1141203134Sthompsastatic int
1142203134Sthompsarun_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1143203134Sthompsa    int len)
1144203134Sthompsa{
1145203134Sthompsa#if 1
1146203134Sthompsa	int i, error = 0;
1147203134Sthompsa	/*
1148203134Sthompsa	 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1149203134Sthompsa	 * We thus issue multiple WRITE_2 commands instead.
1150203134Sthompsa	 */
1151203134Sthompsa	KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1152203134Sthompsa	for (i = 0; i < len && error == 0; i += 2)
1153203134Sthompsa		error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1154209917Sthompsa	return (error);
1155203134Sthompsa#else
1156203134Sthompsa	usb_device_request_t req;
1157203134Sthompsa
1158203134Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1159203134Sthompsa	req.bRequest = RT2870_WRITE_REGION_1;
1160203134Sthompsa	USETW(req.wValue, 0);
1161203134Sthompsa	USETW(req.wIndex, reg);
1162203134Sthompsa	USETW(req.wLength, len);
1163209917Sthompsa	return (run_do_request(sc, &req, buf));
1164203134Sthompsa#endif
1165203134Sthompsa}
1166203134Sthompsa
1167203134Sthompsastatic int
1168203134Sthompsarun_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1169203134Sthompsa{
1170203134Sthompsa	int i, error = 0;
1171203134Sthompsa
1172203134Sthompsa	KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1173203134Sthompsa	for (i = 0; i < len && error == 0; i += 4)
1174203134Sthompsa		error = run_write(sc, reg + i, val);
1175209917Sthompsa	return (error);
1176203134Sthompsa}
1177203134Sthompsa
1178203134Sthompsa/* Read 16-bit from eFUSE ROM (RT3070 only.) */
1179203134Sthompsastatic int
1180203134Sthompsarun_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1181203134Sthompsa{
1182203134Sthompsa	uint32_t tmp;
1183203134Sthompsa	uint16_t reg;
1184203134Sthompsa	int error, ntries;
1185203134Sthompsa
1186203134Sthompsa	if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1187209917Sthompsa		return (error);
1188203134Sthompsa
1189203134Sthompsa	addr *= 2;
1190203134Sthompsa	/*-
1191203134Sthompsa	 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1192203134Sthompsa	 * DATA0: F E D C
1193203134Sthompsa	 * DATA1: B A 9 8
1194203134Sthompsa	 * DATA2: 7 6 5 4
1195203134Sthompsa	 * DATA3: 3 2 1 0
1196203134Sthompsa	 */
1197203134Sthompsa	tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1198203134Sthompsa	tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1199203134Sthompsa	run_write(sc, RT3070_EFUSE_CTRL, tmp);
1200203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1201203134Sthompsa		if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1202209917Sthompsa			return (error);
1203203134Sthompsa		if (!(tmp & RT3070_EFSROM_KICK))
1204203134Sthompsa			break;
1205203134Sthompsa		run_delay(sc, 2);
1206203134Sthompsa	}
1207203134Sthompsa	if (ntries == 100)
1208209917Sthompsa		return (ETIMEDOUT);
1209203134Sthompsa
1210203134Sthompsa	if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1211203134Sthompsa		*val = 0xffff;	/* address not found */
1212209917Sthompsa		return (0);
1213203134Sthompsa	}
1214203134Sthompsa	/* determine to which 32-bit register our 16-bit word belongs */
1215203134Sthompsa	reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1216203134Sthompsa	if ((error = run_read(sc, reg, &tmp)) != 0)
1217209917Sthompsa		return (error);
1218203134Sthompsa
1219203134Sthompsa	*val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1220209917Sthompsa	return (0);
1221203134Sthompsa}
1222203134Sthompsa
1223203134Sthompsastatic int
1224203134Sthompsarun_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1225203134Sthompsa{
1226203134Sthompsa	usb_device_request_t req;
1227203134Sthompsa	uint16_t tmp;
1228203134Sthompsa	int error;
1229203134Sthompsa
1230203134Sthompsa	addr *= 2;
1231203134Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1232203134Sthompsa	req.bRequest = RT2870_EEPROM_READ;
1233203134Sthompsa	USETW(req.wValue, 0);
1234203134Sthompsa	USETW(req.wIndex, addr);
1235203134Sthompsa	USETW(req.wLength, sizeof tmp);
1236203134Sthompsa
1237203134Sthompsa	error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1238203134Sthompsa	if (error == 0)
1239203134Sthompsa		*val = le16toh(tmp);
1240203134Sthompsa	else
1241203134Sthompsa		*val = 0xffff;
1242209917Sthompsa	return (error);
1243203134Sthompsa}
1244203134Sthompsa
1245203134Sthompsastatic __inline int
1246203134Sthompsarun_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1247203134Sthompsa{
1248203134Sthompsa	/* either eFUSE ROM or EEPROM */
1249203134Sthompsa	return sc->sc_srom_read(sc, addr, val);
1250203134Sthompsa}
1251203134Sthompsa
1252203134Sthompsastatic int
1253203134Sthompsarun_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val)
1254203134Sthompsa{
1255203134Sthompsa	uint32_t tmp;
1256203134Sthompsa	int error, ntries;
1257203134Sthompsa
1258203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1259203134Sthompsa		if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1260209917Sthompsa			return (error);
1261203134Sthompsa		if (!(tmp & RT2860_RF_REG_CTRL))
1262203134Sthompsa			break;
1263203134Sthompsa	}
1264203134Sthompsa	if (ntries == 10)
1265209917Sthompsa		return (ETIMEDOUT);
1266203134Sthompsa
1267203134Sthompsa	/* RF registers are 24-bit on the RT2860 */
1268203134Sthompsa	tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1269203134Sthompsa	    (val & 0x3fffff) << 2 | (reg & 3);
1270209917Sthompsa	return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1271203134Sthompsa}
1272203134Sthompsa
1273203134Sthompsastatic int
1274203134Sthompsarun_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1275203134Sthompsa{
1276203134Sthompsa	uint32_t tmp;
1277203134Sthompsa	int error, ntries;
1278203134Sthompsa
1279203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1280203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1281209917Sthompsa			return (error);
1282203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1283203134Sthompsa			break;
1284203134Sthompsa	}
1285203134Sthompsa	if (ntries == 100)
1286209917Sthompsa		return (ETIMEDOUT);
1287203134Sthompsa
1288203134Sthompsa	tmp = RT3070_RF_KICK | reg << 8;
1289203134Sthompsa	if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1290209917Sthompsa		return (error);
1291203134Sthompsa
1292203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1293203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1294209917Sthompsa			return (error);
1295203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1296203134Sthompsa			break;
1297203134Sthompsa	}
1298203134Sthompsa	if (ntries == 100)
1299209917Sthompsa		return (ETIMEDOUT);
1300203134Sthompsa
1301203134Sthompsa	*val = tmp & 0xff;
1302209917Sthompsa	return (0);
1303203134Sthompsa}
1304203134Sthompsa
1305203134Sthompsastatic int
1306203134Sthompsarun_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1307203134Sthompsa{
1308203134Sthompsa	uint32_t tmp;
1309203134Sthompsa	int error, ntries;
1310203134Sthompsa
1311203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1312203134Sthompsa		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1313209917Sthompsa			return (error);
1314203134Sthompsa		if (!(tmp & RT3070_RF_KICK))
1315203134Sthompsa			break;
1316203134Sthompsa	}
1317203134Sthompsa	if (ntries == 10)
1318209917Sthompsa		return (ETIMEDOUT);
1319203134Sthompsa
1320203134Sthompsa	tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1321209917Sthompsa	return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1322203134Sthompsa}
1323203134Sthompsa
1324203134Sthompsastatic int
1325203134Sthompsarun_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1326203134Sthompsa{
1327203134Sthompsa	uint32_t tmp;
1328203134Sthompsa	int ntries, error;
1329203134Sthompsa
1330203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1331203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1332209917Sthompsa			return (error);
1333203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1334203134Sthompsa			break;
1335203134Sthompsa	}
1336203134Sthompsa	if (ntries == 10)
1337209917Sthompsa		return (ETIMEDOUT);
1338203134Sthompsa
1339203134Sthompsa	tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1340203134Sthompsa	if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1341209917Sthompsa		return (error);
1342203134Sthompsa
1343203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1344203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1345209917Sthompsa			return (error);
1346203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1347203134Sthompsa			break;
1348203134Sthompsa	}
1349203134Sthompsa	if (ntries == 10)
1350209917Sthompsa		return (ETIMEDOUT);
1351203134Sthompsa
1352203134Sthompsa	*val = tmp & 0xff;
1353209917Sthompsa	return (0);
1354203134Sthompsa}
1355203134Sthompsa
1356203134Sthompsastatic int
1357203134Sthompsarun_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1358203134Sthompsa{
1359203134Sthompsa	uint32_t tmp;
1360203134Sthompsa	int ntries, error;
1361203134Sthompsa
1362203134Sthompsa	for (ntries = 0; ntries < 10; ntries++) {
1363203134Sthompsa		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1364209917Sthompsa			return (error);
1365203134Sthompsa		if (!(tmp & RT2860_BBP_CSR_KICK))
1366203134Sthompsa			break;
1367203134Sthompsa	}
1368203134Sthompsa	if (ntries == 10)
1369209917Sthompsa		return (ETIMEDOUT);
1370203134Sthompsa
1371203134Sthompsa	tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1372209917Sthompsa	return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1373203134Sthompsa}
1374203134Sthompsa
1375203134Sthompsa/*
1376203134Sthompsa * Send a command to the 8051 microcontroller unit.
1377203134Sthompsa */
1378203134Sthompsastatic int
1379203134Sthompsarun_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1380203134Sthompsa{
1381203134Sthompsa	uint32_t tmp;
1382203134Sthompsa	int error, ntries;
1383203134Sthompsa
1384203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1385203134Sthompsa		if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1386203134Sthompsa			return error;
1387203134Sthompsa		if (!(tmp & RT2860_H2M_BUSY))
1388203134Sthompsa			break;
1389203134Sthompsa	}
1390203134Sthompsa	if (ntries == 100)
1391203134Sthompsa		return ETIMEDOUT;
1392203134Sthompsa
1393203134Sthompsa	tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1394203134Sthompsa	if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1395203134Sthompsa		error = run_write(sc, RT2860_HOST_CMD, cmd);
1396209917Sthompsa	return (error);
1397203134Sthompsa}
1398203134Sthompsa
1399203134Sthompsa/*
1400203134Sthompsa * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1401203134Sthompsa * Used to adjust per-rate Tx power registers.
1402203134Sthompsa */
1403203134Sthompsastatic __inline uint32_t
1404203134Sthompsab4inc(uint32_t b32, int8_t delta)
1405203134Sthompsa{
1406203134Sthompsa	int8_t i, b4;
1407203134Sthompsa
1408203134Sthompsa	for (i = 0; i < 8; i++) {
1409203134Sthompsa		b4 = b32 & 0xf;
1410203134Sthompsa		b4 += delta;
1411203134Sthompsa		if (b4 < 0)
1412203134Sthompsa			b4 = 0;
1413203134Sthompsa		else if (b4 > 0xf)
1414203134Sthompsa			b4 = 0xf;
1415203134Sthompsa		b32 = b32 >> 4 | b4 << 28;
1416203134Sthompsa	}
1417209917Sthompsa	return (b32);
1418203134Sthompsa}
1419203134Sthompsa
1420203134Sthompsastatic const char *
1421203134Sthompsarun_get_rf(int rev)
1422203134Sthompsa{
1423203134Sthompsa	switch (rev) {
1424203134Sthompsa	case RT2860_RF_2820:	return "RT2820";
1425203134Sthompsa	case RT2860_RF_2850:	return "RT2850";
1426203134Sthompsa	case RT2860_RF_2720:	return "RT2720";
1427203134Sthompsa	case RT2860_RF_2750:	return "RT2750";
1428203134Sthompsa	case RT3070_RF_3020:	return "RT3020";
1429203134Sthompsa	case RT3070_RF_2020:	return "RT2020";
1430203134Sthompsa	case RT3070_RF_3021:	return "RT3021";
1431203134Sthompsa	case RT3070_RF_3022:	return "RT3022";
1432203134Sthompsa	case RT3070_RF_3052:	return "RT3052";
1433203134Sthompsa	}
1434209917Sthompsa	return ("unknown");
1435203134Sthompsa}
1436203134Sthompsa
1437203134Sthompsaint
1438203134Sthompsarun_read_eeprom(struct run_softc *sc)
1439203134Sthompsa{
1440203134Sthompsa	int8_t delta_2ghz, delta_5ghz;
1441203134Sthompsa	uint32_t tmp;
1442203134Sthompsa	uint16_t val;
1443203134Sthompsa	int ridx, ant, i;
1444203134Sthompsa
1445203134Sthompsa	/* check whether the ROM is eFUSE ROM or EEPROM */
1446203134Sthompsa	sc->sc_srom_read = run_eeprom_read_2;
1447205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1448203134Sthompsa		run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1449203134Sthompsa		DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1450203134Sthompsa		if (tmp & RT3070_SEL_EFUSE)
1451203134Sthompsa			sc->sc_srom_read = run_efuse_read_2;
1452203134Sthompsa	}
1453203134Sthompsa
1454203134Sthompsa	/* read ROM version */
1455203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1456203134Sthompsa	DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1457203134Sthompsa
1458203134Sthompsa	/* read MAC address */
1459203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1460203134Sthompsa	sc->sc_bssid[0] = val & 0xff;
1461203134Sthompsa	sc->sc_bssid[1] = val >> 8;
1462203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1463203134Sthompsa	sc->sc_bssid[2] = val & 0xff;
1464203134Sthompsa	sc->sc_bssid[3] = val >> 8;
1465203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1466203134Sthompsa	sc->sc_bssid[4] = val & 0xff;
1467203134Sthompsa	sc->sc_bssid[5] = val >> 8;
1468203134Sthompsa
1469205042Sthompsa	/* read vender BBP settings */
1470205042Sthompsa	for (i = 0; i < 10; i++) {
1471203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1472203134Sthompsa		sc->bbp[i].val = val & 0xff;
1473203134Sthompsa		sc->bbp[i].reg = val >> 8;
1474203134Sthompsa		DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val);
1475203134Sthompsa	}
1476205042Sthompsa	if (sc->mac_ver >= 0x3071) {
1477205042Sthompsa		/* read vendor RF settings */
1478205042Sthompsa		for (i = 0; i < 10; i++) {
1479205042Sthompsa			run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val);
1480205042Sthompsa			sc->rf[i].val = val & 0xff;
1481205042Sthompsa			sc->rf[i].reg = val >> 8;
1482205042Sthompsa			DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1483205042Sthompsa			    sc->rf[i].val);
1484205042Sthompsa		}
1485205042Sthompsa	}
1486203134Sthompsa
1487203134Sthompsa	/* read RF frequency offset from EEPROM */
1488203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1489203134Sthompsa	sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1490203134Sthompsa	DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1491203134Sthompsa
1492205042Sthompsa	if (val >> 8 != 0xff) {
1493203134Sthompsa		/* read LEDs operating mode */
1494205042Sthompsa		sc->leds = val >> 8;
1495203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1496203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1497203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1498203134Sthompsa	} else {
1499203134Sthompsa		/* broken EEPROM, use default settings */
1500203134Sthompsa		sc->leds = 0x01;
1501203134Sthompsa		sc->led[0] = 0x5555;
1502203134Sthompsa		sc->led[1] = 0x2221;
1503203134Sthompsa		sc->led[2] = 0x5627;	/* differs from RT2860 */
1504203134Sthompsa	}
1505203134Sthompsa	DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1506203134Sthompsa	    sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1507203134Sthompsa
1508203134Sthompsa	/* read RF information */
1509203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1510203134Sthompsa	if (val == 0xffff) {
1511203134Sthompsa		DPRINTF("invalid EEPROM antenna info, using default\n");
1512205042Sthompsa		if (sc->mac_ver == 0x3572) {
1513205042Sthompsa			/* default to RF3052 2T2R */
1514205042Sthompsa			sc->rf_rev = RT3070_RF_3052;
1515205042Sthompsa			sc->ntxchains = 2;
1516205042Sthompsa			sc->nrxchains = 2;
1517205042Sthompsa		} else if (sc->mac_ver >= 0x3070) {
1518203134Sthompsa			/* default to RF3020 1T1R */
1519203134Sthompsa			sc->rf_rev = RT3070_RF_3020;
1520203134Sthompsa			sc->ntxchains = 1;
1521203134Sthompsa			sc->nrxchains = 1;
1522203134Sthompsa		} else {
1523203134Sthompsa			/* default to RF2820 1T2R */
1524203134Sthompsa			sc->rf_rev = RT2860_RF_2820;
1525203134Sthompsa			sc->ntxchains = 1;
1526203134Sthompsa			sc->nrxchains = 2;
1527203134Sthompsa		}
1528203134Sthompsa	} else {
1529203134Sthompsa		sc->rf_rev = (val >> 8) & 0xf;
1530203134Sthompsa		sc->ntxchains = (val >> 4) & 0xf;
1531203134Sthompsa		sc->nrxchains = val & 0xf;
1532203134Sthompsa	}
1533203134Sthompsa	DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n",
1534203134Sthompsa	    sc->rf_rev, sc->ntxchains, sc->nrxchains);
1535203134Sthompsa
1536208019Sthompsa	/* check if RF supports automatic Tx access gain control */
1537203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1538203134Sthompsa	DPRINTF("EEPROM CFG 0x%04x\n", val);
1539205042Sthompsa	/* check if driver should patch the DAC issue */
1540205042Sthompsa	if ((val >> 8) != 0xff)
1541205042Sthompsa		sc->patch_dac = (val >> 15) & 1;
1542203134Sthompsa	if ((val & 0xff) != 0xff) {
1543203134Sthompsa		sc->ext_5ghz_lna = (val >> 3) & 1;
1544203134Sthompsa		sc->ext_2ghz_lna = (val >> 2) & 1;
1545205042Sthompsa		/* check if RF supports automatic Tx access gain control */
1546203134Sthompsa		sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1547205042Sthompsa		/* check if we have a hardware radio switch */
1548205042Sthompsa		sc->rfswitch = val & 1;
1549203134Sthompsa	}
1550203134Sthompsa
1551203134Sthompsa	/* read power settings for 2GHz channels */
1552203134Sthompsa	for (i = 0; i < 14; i += 2) {
1553203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1554203134Sthompsa		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1555203134Sthompsa		sc->txpow1[i + 1] = (int8_t)(val >> 8);
1556203134Sthompsa
1557203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1558203134Sthompsa		sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1559203134Sthompsa		sc->txpow2[i + 1] = (int8_t)(val >> 8);
1560203134Sthompsa	}
1561203134Sthompsa	/* fix broken Tx power entries */
1562203134Sthompsa	for (i = 0; i < 14; i++) {
1563203134Sthompsa		if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1564203134Sthompsa			sc->txpow1[i] = 5;
1565203134Sthompsa		if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1566203134Sthompsa			sc->txpow2[i] = 5;
1567203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1568203134Sthompsa		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1569203134Sthompsa	}
1570203134Sthompsa	/* read power settings for 5GHz channels */
1571205042Sthompsa	for (i = 0; i < 40; i += 2) {
1572203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1573203134Sthompsa		sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1574203134Sthompsa		sc->txpow1[i + 15] = (int8_t)(val >> 8);
1575203134Sthompsa
1576203134Sthompsa		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1577203134Sthompsa		sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1578203134Sthompsa		sc->txpow2[i + 15] = (int8_t)(val >> 8);
1579203134Sthompsa	}
1580203134Sthompsa	/* fix broken Tx power entries */
1581205042Sthompsa	for (i = 0; i < 40; i++) {
1582203134Sthompsa		if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1583203134Sthompsa			sc->txpow1[14 + i] = 5;
1584203134Sthompsa		if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1585203134Sthompsa			sc->txpow2[14 + i] = 5;
1586203134Sthompsa		DPRINTF("chan %d: power1=%d, power2=%d\n",
1587203134Sthompsa		    rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1588203134Sthompsa		    sc->txpow2[14 + i]);
1589203134Sthompsa	}
1590203134Sthompsa
1591203134Sthompsa	/* read Tx power compensation for each Tx rate */
1592203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1593203134Sthompsa	delta_2ghz = delta_5ghz = 0;
1594203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1595203134Sthompsa		delta_2ghz = val & 0xf;
1596203134Sthompsa		if (!(val & 0x40))	/* negative number */
1597203134Sthompsa			delta_2ghz = -delta_2ghz;
1598203134Sthompsa	}
1599203134Sthompsa	val >>= 8;
1600203134Sthompsa	if ((val & 0xff) != 0xff && (val & 0x80)) {
1601203134Sthompsa		delta_5ghz = val & 0xf;
1602203134Sthompsa		if (!(val & 0x40))	/* negative number */
1603203134Sthompsa			delta_5ghz = -delta_5ghz;
1604203134Sthompsa	}
1605203134Sthompsa	DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1606203134Sthompsa	    delta_2ghz, delta_5ghz);
1607203134Sthompsa
1608203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
1609203134Sthompsa		uint32_t reg;
1610203134Sthompsa
1611208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1612208019Sthompsa		reg = val;
1613208019Sthompsa		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1614208019Sthompsa		reg |= (uint32_t)val << 16;
1615203134Sthompsa
1616203134Sthompsa		sc->txpow20mhz[ridx] = reg;
1617203134Sthompsa		sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1618203134Sthompsa		sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1619203134Sthompsa
1620203134Sthompsa		DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1621203134Sthompsa		    "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1622203134Sthompsa		    sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1623203134Sthompsa	}
1624203134Sthompsa
1625203134Sthompsa	/* read RSSI offsets and LNA gains from EEPROM */
1626203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1627203134Sthompsa	sc->rssi_2ghz[0] = val & 0xff;	/* Ant A */
1628203134Sthompsa	sc->rssi_2ghz[1] = val >> 8;	/* Ant B */
1629203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1630205042Sthompsa	if (sc->mac_ver >= 0x3070) {
1631205042Sthompsa		/*
1632205042Sthompsa		 * On RT3070 chips (limited to 2 Rx chains), this ROM
1633205042Sthompsa		 * field contains the Tx mixer gain for the 2GHz band.
1634205042Sthompsa		 */
1635205042Sthompsa		if ((val & 0xff) != 0xff)
1636205042Sthompsa			sc->txmixgain_2ghz = val & 0x7;
1637205042Sthompsa		DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1638205042Sthompsa	} else
1639205042Sthompsa		sc->rssi_2ghz[2] = val & 0xff;	/* Ant C */
1640203134Sthompsa	sc->lna[2] = val >> 8;		/* channel group 2 */
1641203134Sthompsa
1642203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1643203134Sthompsa	sc->rssi_5ghz[0] = val & 0xff;	/* Ant A */
1644203134Sthompsa	sc->rssi_5ghz[1] = val >> 8;	/* Ant B */
1645203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1646205042Sthompsa	if (sc->mac_ver == 0x3572) {
1647205042Sthompsa		/*
1648205042Sthompsa		 * On RT3572 chips (limited to 2 Rx chains), this ROM
1649205042Sthompsa		 * field contains the Tx mixer gain for the 5GHz band.
1650205042Sthompsa		 */
1651205042Sthompsa		if ((val & 0xff) != 0xff)
1652205042Sthompsa			sc->txmixgain_5ghz = val & 0x7;
1653205042Sthompsa		DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1654205042Sthompsa	} else
1655205042Sthompsa		sc->rssi_5ghz[2] = val & 0xff;	/* Ant C */
1656203134Sthompsa	sc->lna[3] = val >> 8;		/* channel group 3 */
1657203134Sthompsa
1658203134Sthompsa	run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1659203134Sthompsa	sc->lna[0] = val & 0xff;	/* channel group 0 */
1660203134Sthompsa	sc->lna[1] = val >> 8;		/* channel group 1 */
1661203134Sthompsa
1662203134Sthompsa	/* fix broken 5GHz LNA entries */
1663203134Sthompsa	if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1664203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 2);
1665203134Sthompsa		sc->lna[2] = sc->lna[1];
1666203134Sthompsa	}
1667203134Sthompsa	if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1668203134Sthompsa		DPRINTF("invalid LNA for channel group %d\n", 3);
1669203134Sthompsa		sc->lna[3] = sc->lna[1];
1670203134Sthompsa	}
1671203134Sthompsa
1672203134Sthompsa	/* fix broken RSSI offset entries */
1673203134Sthompsa	for (ant = 0; ant < 3; ant++) {
1674203134Sthompsa		if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1675203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1676203134Sthompsa			    ant + 1, sc->rssi_2ghz[ant]);
1677203134Sthompsa			sc->rssi_2ghz[ant] = 0;
1678203134Sthompsa		}
1679203134Sthompsa		if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1680203134Sthompsa			DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1681203134Sthompsa			    ant + 1, sc->rssi_5ghz[ant]);
1682203134Sthompsa			sc->rssi_5ghz[ant] = 0;
1683203134Sthompsa		}
1684203134Sthompsa	}
1685209917Sthompsa	return (0);
1686203134Sthompsa}
1687203134Sthompsa
1688218676Shselaskystatic struct ieee80211_node *
1689203134Sthompsarun_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1690203134Sthompsa{
1691203134Sthompsa	return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1692203134Sthompsa}
1693203134Sthompsa
1694203134Sthompsastatic int
1695203134Sthompsarun_media_change(struct ifnet *ifp)
1696203134Sthompsa{
1697208019Sthompsa	struct ieee80211vap *vap = ifp->if_softc;
1698208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1699203134Sthompsa	const struct ieee80211_txparam *tp;
1700208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1701203134Sthompsa	uint8_t rate, ridx;
1702203134Sthompsa	int error;
1703203134Sthompsa
1704203134Sthompsa	RUN_LOCK(sc);
1705203134Sthompsa
1706203134Sthompsa	error = ieee80211_media_change(ifp);
1707209917Sthompsa	if (error != ENETRESET) {
1708203134Sthompsa		RUN_UNLOCK(sc);
1709209917Sthompsa		return (error);
1710208019Sthompsa	}
1711203134Sthompsa
1712203134Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1713203134Sthompsa	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1714212127Sthompsa		struct ieee80211_node *ni;
1715212127Sthompsa		struct run_node	*rn;
1716212127Sthompsa
1717203134Sthompsa		rate = ic->ic_sup_rates[ic->ic_curmode].
1718203134Sthompsa		    rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1719203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1720203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
1721203134Sthompsa				break;
1722212127Sthompsa		ni = ieee80211_ref_node(vap->iv_bss);
1723212127Sthompsa		rn = (struct run_node *)ni;
1724208019Sthompsa		rn->fix_ridx = ridx;
1725208019Sthompsa		DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1726212127Sthompsa		ieee80211_free_node(ni);
1727203134Sthompsa	}
1728203134Sthompsa
1729208019Sthompsa#if 0
1730203134Sthompsa	if ((ifp->if_flags & IFF_UP) &&
1731203134Sthompsa	    (ifp->if_drv_flags &  IFF_DRV_RUNNING)){
1732203134Sthompsa		run_init_locked(sc);
1733203134Sthompsa	}
1734208019Sthompsa#endif
1735203134Sthompsa
1736203134Sthompsa	RUN_UNLOCK(sc);
1737203134Sthompsa
1738209917Sthompsa	return (0);
1739203134Sthompsa}
1740203134Sthompsa
1741203134Sthompsastatic int
1742203134Sthompsarun_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1743203134Sthompsa{
1744203134Sthompsa	const struct ieee80211_txparam *tp;
1745203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1746203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1747203134Sthompsa	struct run_vap *rvp = RUN_VAP(vap);
1748203134Sthompsa	enum ieee80211_state ostate;
1749208019Sthompsa	uint32_t sta[3];
1750203134Sthompsa	uint32_t tmp;
1751208019Sthompsa	uint8_t ratectl;
1752208019Sthompsa	uint8_t restart_ratectl = 0;
1753208019Sthompsa	uint8_t bid = 1 << rvp->rvp_id;
1754203134Sthompsa
1755203134Sthompsa	ostate = vap->iv_state;
1756203134Sthompsa	DPRINTF("%s -> %s\n",
1757203134Sthompsa		ieee80211_state_name[ostate],
1758203134Sthompsa		ieee80211_state_name[nstate]);
1759203134Sthompsa
1760203134Sthompsa	IEEE80211_UNLOCK(ic);
1761203134Sthompsa	RUN_LOCK(sc);
1762203134Sthompsa
1763208019Sthompsa	ratectl = sc->ratectl_run; /* remember current state */
1764208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
1765208019Sthompsa	usb_callout_stop(&sc->ratectl_ch);
1766203134Sthompsa
1767203134Sthompsa	if (ostate == IEEE80211_S_RUN) {
1768203134Sthompsa		/* turn link LED off */
1769203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO);
1770203134Sthompsa	}
1771203134Sthompsa
1772203134Sthompsa	switch (nstate) {
1773203134Sthompsa	case IEEE80211_S_INIT:
1774208019Sthompsa		restart_ratectl = 1;
1775208019Sthompsa
1776208019Sthompsa		if (ostate != IEEE80211_S_RUN)
1777208019Sthompsa			break;
1778208019Sthompsa
1779208019Sthompsa		ratectl &= ~bid;
1780208019Sthompsa		sc->runbmap &= ~bid;
1781208019Sthompsa
1782208019Sthompsa		/* abort TSF synchronization if there is no vap running */
1783209917Sthompsa		if (--sc->running == 0) {
1784203134Sthompsa			run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1785203134Sthompsa			run_write(sc, RT2860_BCN_TIME_CFG,
1786203134Sthompsa			    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1787203134Sthompsa			    RT2860_TBTT_TIMER_EN));
1788203134Sthompsa		}
1789203134Sthompsa		break;
1790203134Sthompsa
1791203134Sthompsa	case IEEE80211_S_RUN:
1792209917Sthompsa		if (!(sc->runbmap & bid)) {
1793208019Sthompsa			if(sc->running++)
1794208019Sthompsa				restart_ratectl = 1;
1795208019Sthompsa			sc->runbmap |= bid;
1796208019Sthompsa		}
1797203134Sthompsa
1798218492Sbschmidt		m_freem(rvp->beacon_mbuf);
1799218492Sbschmidt		rvp->beacon_mbuf = NULL;
1800218492Sbschmidt
1801209917Sthompsa		switch (vap->iv_opmode) {
1802208019Sthompsa		case IEEE80211_M_HOSTAP:
1803208019Sthompsa		case IEEE80211_M_MBSS:
1804208019Sthompsa			sc->ap_running |= bid;
1805208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1806208019Sthompsa			run_update_beacon_cb(vap);
1807208019Sthompsa			break;
1808208019Sthompsa		case IEEE80211_M_IBSS:
1809208019Sthompsa			sc->adhoc_running |= bid;
1810209917Sthompsa			if (!sc->ap_running)
1811208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1812208019Sthompsa			run_update_beacon_cb(vap);
1813208019Sthompsa			break;
1814208019Sthompsa		case IEEE80211_M_STA:
1815208019Sthompsa			sc->sta_running |= bid;
1816209917Sthompsa			if (!sc->ap_running && !sc->adhoc_running)
1817208019Sthompsa				ic->ic_opmode = vap->iv_opmode;
1818208019Sthompsa
1819208019Sthompsa			/* read statistic counters (clear on read) */
1820208019Sthompsa			run_read_region_1(sc, RT2860_TX_STA_CNT0,
1821208019Sthompsa			    (uint8_t *)sta, sizeof sta);
1822208019Sthompsa
1823208019Sthompsa			break;
1824208019Sthompsa		default:
1825208019Sthompsa			ic->ic_opmode = vap->iv_opmode;
1826208019Sthompsa			break;
1827208019Sthompsa		}
1828208019Sthompsa
1829203134Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1830212127Sthompsa			struct ieee80211_node *ni;
1831212127Sthompsa
1832203134Sthompsa			run_updateslot(ic->ic_ifp);
1833203134Sthompsa			run_enable_mrr(sc);
1834203134Sthompsa			run_set_txpreamble(sc);
1835203134Sthompsa			run_set_basicrates(sc);
1836212127Sthompsa			ni = ieee80211_ref_node(vap->iv_bss);
1837203134Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1838203134Sthompsa			run_set_bssid(sc, ni->ni_bssid);
1839212127Sthompsa			ieee80211_free_node(ni);
1840208019Sthompsa			run_enable_tsf_sync(sc);
1841203134Sthompsa
1842208019Sthompsa			/* enable automatic rate adaptation */
1843208019Sthompsa			tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1844208019Sthompsa			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1845208019Sthompsa				ratectl |= bid;
1846203134Sthompsa		}
1847203134Sthompsa
1848203134Sthompsa		/* turn link LED on */
1849203134Sthompsa		run_set_leds(sc, RT2860_LED_RADIO |
1850208019Sthompsa		    (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1851203134Sthompsa		     RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1852203134Sthompsa
1853203134Sthompsa		break;
1854203134Sthompsa	default:
1855203134Sthompsa		DPRINTFN(6, "undefined case\n");
1856203134Sthompsa		break;
1857203134Sthompsa	}
1858203134Sthompsa
1859208019Sthompsa	/* restart amrr for running VAPs */
1860209917Sthompsa	if ((sc->ratectl_run = ratectl) && restart_ratectl)
1861208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1862208019Sthompsa
1863203134Sthompsa	RUN_UNLOCK(sc);
1864203134Sthompsa	IEEE80211_LOCK(ic);
1865203134Sthompsa
1866203134Sthompsa	return(rvp->newstate(vap, nstate, arg));
1867203134Sthompsa}
1868203134Sthompsa
1869203134Sthompsa/* ARGSUSED */
1870203134Sthompsastatic void
1871208019Sthompsarun_wme_update_cb(void *arg)
1872203134Sthompsa{
1873203134Sthompsa	struct ieee80211com *ic = arg;
1874203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1875203134Sthompsa	struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1876203134Sthompsa	int aci, error = 0;
1877203134Sthompsa
1878208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1879203134Sthompsa
1880203134Sthompsa	/* update MAC TX configuration registers */
1881203134Sthompsa	for (aci = 0; aci < WME_NUM_AC; aci++) {
1882203134Sthompsa		error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1883203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmax << 16 |
1884203134Sthompsa		    wmesp->wme_params[aci].wmep_logcwmin << 12 |
1885203134Sthompsa		    wmesp->wme_params[aci].wmep_aifsn  <<  8 |
1886203134Sthompsa		    wmesp->wme_params[aci].wmep_txopLimit);
1887209917Sthompsa		if (error) goto err;
1888203134Sthompsa	}
1889203134Sthompsa
1890203134Sthompsa	/* update SCH/DMA registers too */
1891203134Sthompsa	error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1892203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_aifsn  << 12 |
1893203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_aifsn  <<  8 |
1894203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_aifsn  <<  4 |
1895203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1896209917Sthompsa	if (error) goto err;
1897203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1898203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1899203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmin <<  8 |
1900203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmin <<  4 |
1901203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1902209917Sthompsa	if (error) goto err;
1903203134Sthompsa	error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1904203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1905203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_logcwmax <<  8 |
1906203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_logcwmax <<  4 |
1907203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1908209917Sthompsa	if (error) goto err;
1909203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1910203134Sthompsa	    wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1911203134Sthompsa	    wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1912209917Sthompsa	if (error) goto err;
1913203134Sthompsa	error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1914203134Sthompsa	    wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1915203134Sthompsa	    wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1916203134Sthompsa
1917203134Sthompsaerr:
1918209917Sthompsa	if (error)
1919203134Sthompsa		DPRINTF("WME update failed\n");
1920203134Sthompsa
1921203134Sthompsa	return;
1922203134Sthompsa}
1923203134Sthompsa
1924208019Sthompsastatic int
1925208019Sthompsarun_wme_update(struct ieee80211com *ic)
1926208019Sthompsa{
1927208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1928208019Sthompsa
1929208019Sthompsa	/* sometime called wothout lock */
1930209917Sthompsa	if (mtx_owned(&ic->ic_comlock.mtx)) {
1931208019Sthompsa		uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1932208019Sthompsa		DPRINTF("cmdq_store=%d\n", i);
1933208019Sthompsa		sc->cmdq[i].func = run_wme_update_cb;
1934208019Sthompsa		sc->cmdq[i].arg0 = ic;
1935208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
1936209918Sthompsa		return (0);
1937208019Sthompsa	}
1938208019Sthompsa
1939208019Sthompsa	RUN_LOCK(sc);
1940208019Sthompsa	run_wme_update_cb(ic);
1941208019Sthompsa	RUN_UNLOCK(sc);
1942208019Sthompsa
1943208019Sthompsa	/* return whatever, upper layer desn't care anyway */
1944208019Sthompsa	return (0);
1945208019Sthompsa}
1946208019Sthompsa
1947203134Sthompsastatic void
1948203134Sthompsarun_key_update_begin(struct ieee80211vap *vap)
1949203134Sthompsa{
1950203134Sthompsa	/*
1951208019Sthompsa	 * To avoid out-of-order events, both run_key_set() and
1952208019Sthompsa	 * _delete() are deferred and handled by run_cmdq_cb().
1953208019Sthompsa	 * So, there is nothing we need to do here.
1954203134Sthompsa	 */
1955203134Sthompsa}
1956203134Sthompsa
1957203134Sthompsastatic void
1958203134Sthompsarun_key_update_end(struct ieee80211vap *vap)
1959203134Sthompsa{
1960203134Sthompsa	/* null */
1961203134Sthompsa}
1962203134Sthompsa
1963208019Sthompsastatic void
1964208019Sthompsarun_key_set_cb(void *arg)
1965203134Sthompsa{
1966208019Sthompsa	struct run_cmdq *cmdq = arg;
1967208019Sthompsa	struct ieee80211vap *vap = cmdq->arg1;
1968208019Sthompsa	struct ieee80211_key *k = cmdq->k;
1969203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
1970208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
1971203134Sthompsa	struct ieee80211_node *ni;
1972203134Sthompsa	uint32_t attr;
1973203134Sthompsa	uint16_t base, associd;
1974209144Sthompsa	uint8_t mode, wcid, iv[8];
1975203134Sthompsa
1976208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
1977203134Sthompsa
1978209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1979208019Sthompsa		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
1980209144Sthompsa	else
1981203134Sthompsa		ni = vap->iv_bss;
1982208019Sthompsa	associd = (ni != NULL) ? ni->ni_associd : 0;
1983203134Sthompsa
1984203134Sthompsa	/* map net80211 cipher to RT2860 security mode */
1985203134Sthompsa	switch (k->wk_cipher->ic_cipher) {
1986203134Sthompsa	case IEEE80211_CIPHER_WEP:
1987203134Sthompsa		if(k->wk_keylen < 8)
1988203134Sthompsa			mode = RT2860_MODE_WEP40;
1989203134Sthompsa		else
1990203134Sthompsa			mode = RT2860_MODE_WEP104;
1991203134Sthompsa		break;
1992203134Sthompsa	case IEEE80211_CIPHER_TKIP:
1993203134Sthompsa		mode = RT2860_MODE_TKIP;
1994203134Sthompsa		break;
1995203134Sthompsa	case IEEE80211_CIPHER_AES_CCM:
1996203134Sthompsa		mode = RT2860_MODE_AES_CCMP;
1997203134Sthompsa		break;
1998203134Sthompsa	default:
1999203134Sthompsa		DPRINTF("undefined case\n");
2000208019Sthompsa		return;
2001203134Sthompsa	}
2002203134Sthompsa
2003208019Sthompsa	DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2004203134Sthompsa	    associd, k->wk_keyix, mode,
2005208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2006208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2007208019Sthompsa	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2008203134Sthompsa
2009203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2010203134Sthompsa		wcid = 0;	/* NB: update WCID0 for group keys */
2011208019Sthompsa		base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2012203134Sthompsa	} else {
2013203134Sthompsa		wcid = RUN_AID2WCID(associd);
2014203134Sthompsa		base = RT2860_PKEY(wcid);
2015203134Sthompsa	}
2016203134Sthompsa
2017203134Sthompsa	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2018203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, 16))
2019208019Sthompsa			return;
2020209144Sthompsa		if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8))	/* wk_txmic */
2021208019Sthompsa			return;
2022209144Sthompsa		if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8))	/* wk_rxmic */
2023208019Sthompsa			return;
2024203134Sthompsa	} else {
2025203134Sthompsa		/* roundup len to 16-bit: XXX fix write_region_1() instead */
2026203134Sthompsa		if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2027208019Sthompsa			return;
2028203134Sthompsa	}
2029203134Sthompsa
2030203134Sthompsa	if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2031203134Sthompsa	    (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2032203134Sthompsa		/* set initial packet number in IV+EIV */
2033209917Sthompsa		if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2034203134Sthompsa			memset(iv, 0, sizeof iv);
2035208019Sthompsa			iv[3] = vap->iv_def_txkey << 6;
2036203134Sthompsa		} else {
2037203134Sthompsa			if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2038203134Sthompsa				iv[0] = k->wk_keytsc >> 8;
2039203134Sthompsa				iv[1] = (iv[0] | 0x20) & 0x7f;
2040203134Sthompsa				iv[2] = k->wk_keytsc;
2041203134Sthompsa			} else /* CCMP */ {
2042203134Sthompsa				iv[0] = k->wk_keytsc;
2043203134Sthompsa				iv[1] = k->wk_keytsc >> 8;
2044203134Sthompsa				iv[2] = 0;
2045203134Sthompsa			}
2046203134Sthompsa			iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2047203134Sthompsa			iv[4] = k->wk_keytsc >> 16;
2048203134Sthompsa			iv[5] = k->wk_keytsc >> 24;
2049203134Sthompsa			iv[6] = k->wk_keytsc >> 32;
2050203134Sthompsa			iv[7] = k->wk_keytsc >> 40;
2051203134Sthompsa		}
2052209917Sthompsa		if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2053208019Sthompsa			return;
2054203134Sthompsa	}
2055203134Sthompsa
2056203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2057203134Sthompsa		/* install group key */
2058209917Sthompsa		if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2059208019Sthompsa			return;
2060203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2061203134Sthompsa		attr |= mode << (k->wk_keyix * 4);
2062209917Sthompsa		if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2063208019Sthompsa			return;
2064203134Sthompsa	} else {
2065203134Sthompsa		/* install pairwise key */
2066209917Sthompsa		if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2067208019Sthompsa			return;
2068203134Sthompsa		attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2069209917Sthompsa		if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2070208019Sthompsa			return;
2071203134Sthompsa	}
2072203134Sthompsa
2073203134Sthompsa	/* TODO create a pass-thru key entry? */
2074203134Sthompsa
2075208019Sthompsa	/* need wcid to delete the right key later */
2076208019Sthompsa	k->wk_pad = wcid;
2077203134Sthompsa}
2078203134Sthompsa
2079203134Sthompsa/*
2080208019Sthompsa * Don't have to be deferred, but in order to keep order of
2081208019Sthompsa * execution, i.e. with run_key_delete(), defer this and let
2082208019Sthompsa * run_cmdq_cb() maintain the order.
2083208019Sthompsa *
2084203134Sthompsa * return 0 on error
2085203134Sthompsa */
2086203134Sthompsastatic int
2087208019Sthompsarun_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2088208019Sthompsa		const uint8_t mac[IEEE80211_ADDR_LEN])
2089203134Sthompsa{
2090203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2091203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2092208019Sthompsa	uint32_t i;
2093208019Sthompsa
2094208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2095208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2096208019Sthompsa	sc->cmdq[i].func = run_key_set_cb;
2097208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2098208019Sthompsa	sc->cmdq[i].arg1 = vap;
2099208019Sthompsa	sc->cmdq[i].k = k;
2100208019Sthompsa	IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2101208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2102208019Sthompsa
2103209144Sthompsa	/*
2104209144Sthompsa	 * To make sure key will be set when hostapd
2105209144Sthompsa	 * calls iv_key_set() before if_init().
2106209144Sthompsa	 */
2107209917Sthompsa	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2108209144Sthompsa		RUN_LOCK(sc);
2109209144Sthompsa		sc->cmdq_key_set = RUN_CMDQ_GO;
2110209144Sthompsa		RUN_UNLOCK(sc);
2111209144Sthompsa	}
2112209144Sthompsa
2113209917Sthompsa	return (1);
2114208019Sthompsa}
2115208019Sthompsa
2116208019Sthompsa/*
2117208019Sthompsa * If wlan is destroyed without being brought down i.e. without
2118208019Sthompsa * wlan down or wpa_cli terminate, this function is called after
2119208019Sthompsa * vap is gone. Don't refer it.
2120208019Sthompsa */
2121208019Sthompsastatic void
2122208019Sthompsarun_key_delete_cb(void *arg)
2123208019Sthompsa{
2124208019Sthompsa	struct run_cmdq *cmdq = arg;
2125208019Sthompsa	struct run_softc *sc = cmdq->arg1;
2126208019Sthompsa	struct ieee80211_key *k = &cmdq->key;
2127203134Sthompsa	uint32_t attr;
2128203134Sthompsa	uint8_t wcid;
2129203134Sthompsa
2130208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2131203134Sthompsa
2132203134Sthompsa	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2133203134Sthompsa		/* remove group key */
2134208019Sthompsa		DPRINTF("removing group key\n");
2135208019Sthompsa		run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2136203134Sthompsa		attr &= ~(0xf << (k->wk_keyix * 4));
2137208019Sthompsa		run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2138203134Sthompsa	} else {
2139203134Sthompsa		/* remove pairwise key */
2140208019Sthompsa		DPRINTF("removing key for wcid %x\n", k->wk_pad);
2141208019Sthompsa		/* matching wcid was written to wk_pad in run_key_set() */
2142208019Sthompsa		wcid = k->wk_pad;
2143208019Sthompsa		run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2144203134Sthompsa		attr &= ~0xf;
2145208019Sthompsa		run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2146208019Sthompsa		run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2147203134Sthompsa	}
2148203134Sthompsa
2149208019Sthompsa	k->wk_pad = 0;
2150203134Sthompsa}
2151203134Sthompsa
2152208019Sthompsa/*
2153208019Sthompsa * return 0 on error
2154208019Sthompsa */
2155208019Sthompsastatic int
2156208019Sthompsarun_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2157203134Sthompsa{
2158208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2159208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2160208019Sthompsa	struct ieee80211_key *k0;
2161208019Sthompsa	uint32_t i;
2162203134Sthompsa
2163208019Sthompsa	/*
2164208019Sthompsa	 * When called back, key might be gone. So, make a copy
2165208019Sthompsa	 * of some values need to delete keys before deferring.
2166208019Sthompsa	 * But, because of LOR with node lock, cannot use lock here.
2167208019Sthompsa	 * So, use atomic instead.
2168208019Sthompsa	 */
2169208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
2170208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
2171208019Sthompsa	sc->cmdq[i].func = run_key_delete_cb;
2172208019Sthompsa	sc->cmdq[i].arg0 = NULL;
2173208019Sthompsa	sc->cmdq[i].arg1 = sc;
2174208019Sthompsa	k0 = &sc->cmdq[i].key;
2175208019Sthompsa	k0->wk_flags = k->wk_flags;
2176208019Sthompsa	k0->wk_keyix = k->wk_keyix;
2177208019Sthompsa	/* matching wcid was written to wk_pad in run_key_set() */
2178208019Sthompsa	k0->wk_pad = k->wk_pad;
2179208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
2180208019Sthompsa	return (1);	/* return fake success */
2181203134Sthompsa
2182203134Sthompsa}
2183203134Sthompsa
2184203134Sthompsastatic void
2185206358Srpaulorun_ratectl_to(void *arg)
2186203134Sthompsa{
2187208019Sthompsa	struct run_softc *sc = arg;
2188203134Sthompsa
2189203134Sthompsa	/* do it in a process context, so it can go sleep */
2190208019Sthompsa	ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2191203134Sthompsa	/* next timeout will be rescheduled in the callback task */
2192203134Sthompsa}
2193203134Sthompsa
2194203134Sthompsa/* ARGSUSED */
2195203134Sthompsastatic void
2196206358Srpaulorun_ratectl_cb(void *arg, int pending)
2197203134Sthompsa{
2198208019Sthompsa	struct run_softc *sc = arg;
2199208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2200208019Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2201203134Sthompsa
2202209917Sthompsa	if (vap == NULL)
2203208019Sthompsa		return;
2204208019Sthompsa
2205209917Sthompsa	if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2206208019Sthompsa		run_iter_func(sc, vap->iv_bss);
2207203134Sthompsa	else {
2208203134Sthompsa		/*
2209203134Sthompsa		 * run_reset_livelock() doesn't do anything with AMRR,
2210203134Sthompsa		 * but Ralink wants us to call it every 1 sec. So, we
2211203134Sthompsa		 * piggyback here rather than creating another callout.
2212203134Sthompsa		 * Livelock may occur only in HOSTAP or IBSS mode
2213203134Sthompsa		 * (when h/w is sending beacons).
2214203134Sthompsa		 */
2215203134Sthompsa		RUN_LOCK(sc);
2216203134Sthompsa		run_reset_livelock(sc);
2217208019Sthompsa		/* just in case, there are some stats to drain */
2218208019Sthompsa		run_drain_fifo(sc);
2219203134Sthompsa		RUN_UNLOCK(sc);
2220208019Sthompsa		ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2221203134Sthompsa	}
2222203134Sthompsa
2223208019Sthompsa	if(sc->ratectl_run != RUN_RATECTL_OFF)
2224208019Sthompsa		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2225203134Sthompsa}
2226203134Sthompsa
2227203134Sthompsastatic void
2228208019Sthompsarun_drain_fifo(void *arg)
2229203134Sthompsa{
2230208019Sthompsa	struct run_softc *sc = arg;
2231208019Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2232208019Sthompsa	uint32_t stat;
2233218676Shselasky	uint16_t (*wstat)[3];
2234203134Sthompsa	uint8_t wcid, mcs, pid;
2235218676Shselasky	int8_t retry;
2236203134Sthompsa
2237208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2238203134Sthompsa
2239208019Sthompsa	for (;;) {
2240203134Sthompsa		/* drain Tx status FIFO (maxsize = 16) */
2241203134Sthompsa		run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2242208019Sthompsa		DPRINTFN(4, "tx stat 0x%08x\n", stat);
2243209917Sthompsa		if (!(stat & RT2860_TXQ_VLD))
2244208019Sthompsa			break;
2245203134Sthompsa
2246208019Sthompsa		wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2247203134Sthompsa
2248208019Sthompsa		/* if no ACK was requested, no feedback is available */
2249208019Sthompsa		if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2250208019Sthompsa		    wcid == 0)
2251208019Sthompsa			continue;
2252203134Sthompsa
2253218676Shselasky		/*
2254218676Shselasky		 * Even though each stat is Tx-complete-status like format,
2255218676Shselasky		 * the device can poll stats. Because there is no guarantee
2256218676Shselasky		 * that the referring node is still around when read the stats.
2257218676Shselasky		 * So that, if we use ieee80211_ratectl_tx_update(), we will
2258218676Shselasky		 * have hard time not to refer already freed node.
2259218676Shselasky		 *
2260218676Shselasky		 * To eliminate such page faults, we poll stats in softc.
2261218676Shselasky		 * Then, update the rates later with ieee80211_ratectl_tx_update().
2262218676Shselasky		 */
2263218676Shselasky		wstat = &(sc->wcid_stats[wcid]);
2264218676Shselasky		(*wstat)[RUN_TXCNT]++;
2265218676Shselasky		if (stat & RT2860_TXQ_OK)
2266218676Shselasky			(*wstat)[RUN_SUCCESS]++;
2267218676Shselasky		else
2268208019Sthompsa			ifp->if_oerrors++;
2269218676Shselasky		/*
2270218676Shselasky		 * Check if there were retries, ie if the Tx success rate is
2271218676Shselasky		 * different from the requested rate. Note that it works only
2272218676Shselasky		 * because we do not allow rate fallback from OFDM to CCK.
2273218676Shselasky		 */
2274218676Shselasky		mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2275218676Shselasky		pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2276218676Shselasky		if ((retry = pid -1 - mcs) > 0) {
2277218676Shselasky			(*wstat)[RUN_TXCNT] += retry;
2278218676Shselasky			(*wstat)[RUN_RETRY] += retry;
2279203134Sthompsa		}
2280208019Sthompsa	}
2281208019Sthompsa	DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2282208019Sthompsa
2283208019Sthompsa	sc->fifo_cnt = 0;
2284208019Sthompsa}
2285208019Sthompsa
2286208019Sthompsastatic void
2287208019Sthompsarun_iter_func(void *arg, struct ieee80211_node *ni)
2288208019Sthompsa{
2289208019Sthompsa	struct run_softc *sc = arg;
2290208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2291208019Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2292208019Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2293208019Sthompsa	struct run_node *rn = (void *)ni;
2294218676Shselasky	union run_stats sta[2];
2295218676Shselasky	uint16_t (*wstat)[3];
2296218676Shselasky	int txcnt, success, retrycnt, error;
2297208019Sthompsa
2298218676Shselasky	RUN_LOCK(sc);
2299218676Shselasky
2300209917Sthompsa	if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2301209917Sthompsa	    vap->iv_opmode == IEEE80211_M_STA)) {
2302203134Sthompsa		/* read statistic counters (clear on read) and update AMRR state */
2303203134Sthompsa		error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2304203134Sthompsa		    sizeof sta);
2305203134Sthompsa		if (error != 0)
2306218676Shselasky			goto fail;
2307203134Sthompsa
2308203134Sthompsa		/* count failed TX as errors */
2309218676Shselasky		ifp->if_oerrors += le16toh(sta[0].error.fail);
2310203134Sthompsa
2311218676Shselasky		retrycnt = le16toh(sta[1].tx.retry);
2312218676Shselasky		success = le16toh(sta[1].tx.success);
2313218676Shselasky		txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2314203134Sthompsa
2315218676Shselasky		DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2316218676Shselasky			retrycnt, success, le16toh(sta[0].error.fail));
2317218676Shselasky	} else {
2318218676Shselasky		wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2319203134Sthompsa
2320218676Shselasky		if (wstat == &(sc->wcid_stats[0]) ||
2321218676Shselasky		    wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2322218676Shselasky			goto fail;
2323208019Sthompsa
2324218676Shselasky		txcnt = (*wstat)[RUN_TXCNT];
2325218676Shselasky		success = (*wstat)[RUN_SUCCESS];
2326218676Shselasky		retrycnt = (*wstat)[RUN_RETRY];
2327218676Shselasky		DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2328218676Shselasky		    retrycnt, txcnt, success);
2329208019Sthompsa
2330218676Shselasky		memset(wstat, 0, sizeof(*wstat));
2331203134Sthompsa	}
2332203134Sthompsa
2333218676Shselasky	ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2334208019Sthompsa	rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2335218676Shselasky
2336218676Shselaskyfail:
2337218676Shselasky	RUN_UNLOCK(sc);
2338218676Shselasky
2339208019Sthompsa	DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2340208019Sthompsa}
2341203134Sthompsa
2342208019Sthompsastatic void
2343208019Sthompsarun_newassoc_cb(void *arg)
2344208019Sthompsa{
2345208019Sthompsa	struct run_cmdq *cmdq = arg;
2346208019Sthompsa	struct ieee80211_node *ni = cmdq->arg1;
2347208019Sthompsa	struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2348208019Sthompsa	uint8_t wcid = cmdq->wcid;
2349203134Sthompsa
2350208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2351208019Sthompsa
2352208019Sthompsa	run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2353208019Sthompsa	    ni->ni_macaddr, IEEE80211_ADDR_LEN);
2354218676Shselasky
2355218676Shselasky	memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2356203134Sthompsa}
2357203134Sthompsa
2358203134Sthompsastatic void
2359203134Sthompsarun_newassoc(struct ieee80211_node *ni, int isnew)
2360203134Sthompsa{
2361203134Sthompsa	struct run_node *rn = (void *)ni;
2362203134Sthompsa	struct ieee80211_rateset *rs = &ni->ni_rates;
2363208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2364208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2365208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
2366203134Sthompsa	uint8_t rate;
2367208019Sthompsa	uint8_t ridx;
2368208019Sthompsa	uint8_t wcid = RUN_AID2WCID(ni->ni_associd);
2369208019Sthompsa	int i, j;
2370203134Sthompsa
2371209917Sthompsa	if (wcid > RT2870_WCID_MAX) {
2372208019Sthompsa		device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2373208019Sthompsa		return;
2374208019Sthompsa	}
2375203134Sthompsa
2376208019Sthompsa	/* only interested in true associations */
2377209917Sthompsa	if (isnew && ni->ni_associd != 0) {
2378208019Sthompsa
2379208019Sthompsa		/*
2380208019Sthompsa		 * This function could is called though timeout function.
2381208019Sthompsa		 * Need to defer.
2382208019Sthompsa		 */
2383208019Sthompsa		uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2384208019Sthompsa		DPRINTF("cmdq_store=%d\n", cnt);
2385208019Sthompsa		sc->cmdq[cnt].func = run_newassoc_cb;
2386208019Sthompsa		sc->cmdq[cnt].arg0 = NULL;
2387208019Sthompsa		sc->cmdq[cnt].arg1 = ni;
2388208019Sthompsa		sc->cmdq[cnt].wcid = wcid;
2389208019Sthompsa		ieee80211_runtask(ic, &sc->cmdq_task);
2390208019Sthompsa	}
2391208019Sthompsa
2392208019Sthompsa	DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2393208019Sthompsa	    isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2394208019Sthompsa
2395203134Sthompsa	for (i = 0; i < rs->rs_nrates; i++) {
2396203134Sthompsa		rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2397203134Sthompsa		/* convert 802.11 rate to hardware rate index */
2398203134Sthompsa		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2399203134Sthompsa			if (rt2860_rates[ridx].rate == rate)
2400203134Sthompsa				break;
2401203134Sthompsa		rn->ridx[i] = ridx;
2402203134Sthompsa		/* determine rate of control response frames */
2403203134Sthompsa		for (j = i; j >= 0; j--) {
2404203134Sthompsa			if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2405203134Sthompsa			    rt2860_rates[rn->ridx[i]].phy ==
2406203134Sthompsa			    rt2860_rates[rn->ridx[j]].phy)
2407203134Sthompsa				break;
2408203134Sthompsa		}
2409203134Sthompsa		if (j >= 0) {
2410203134Sthompsa			rn->ctl_ridx[i] = rn->ridx[j];
2411203134Sthompsa		} else {
2412203134Sthompsa			/* no basic rate found, use mandatory one */
2413203134Sthompsa			rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2414203134Sthompsa		}
2415203134Sthompsa		DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2416203134Sthompsa		    rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2417203134Sthompsa	}
2418208019Sthompsa	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2419208019Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2420208019Sthompsa		if (rt2860_rates[ridx].rate == rate)
2421208019Sthompsa			break;
2422208019Sthompsa	rn->mgt_ridx = ridx;
2423208019Sthompsa	DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2424208019Sthompsa
2425208019Sthompsa	usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2426203134Sthompsa}
2427203134Sthompsa
2428203134Sthompsa/*
2429203134Sthompsa * Return the Rx chain with the highest RSSI for a given frame.
2430203134Sthompsa */
2431203134Sthompsastatic __inline uint8_t
2432203134Sthompsarun_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2433203134Sthompsa{
2434203134Sthompsa	uint8_t rxchain = 0;
2435203134Sthompsa
2436203134Sthompsa	if (sc->nrxchains > 1) {
2437203134Sthompsa		if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2438203134Sthompsa			rxchain = 1;
2439203134Sthompsa		if (sc->nrxchains > 2)
2440203134Sthompsa			if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2441203134Sthompsa				rxchain = 2;
2442203134Sthompsa	}
2443209917Sthompsa	return (rxchain);
2444203134Sthompsa}
2445203134Sthompsa
2446203134Sthompsastatic void
2447203134Sthompsarun_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2448203134Sthompsa{
2449203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2450203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2451203134Sthompsa	struct ieee80211_frame *wh;
2452203134Sthompsa	struct ieee80211_node *ni;
2453203134Sthompsa	struct rt2870_rxd *rxd;
2454203134Sthompsa	struct rt2860_rxwi *rxwi;
2455203134Sthompsa	uint32_t flags;
2456203134Sthompsa	uint16_t len, phy;
2457203134Sthompsa	uint8_t ant, rssi;
2458203134Sthompsa	int8_t nf;
2459203134Sthompsa
2460203134Sthompsa	rxwi = mtod(m, struct rt2860_rxwi *);
2461203134Sthompsa	len = le16toh(rxwi->len) & 0xfff;
2462203134Sthompsa	if (__predict_false(len > dmalen)) {
2463203134Sthompsa		m_freem(m);
2464203134Sthompsa		ifp->if_ierrors++;
2465203134Sthompsa		DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2466203134Sthompsa		return;
2467203134Sthompsa	}
2468203134Sthompsa	/* Rx descriptor is located at the end */
2469203134Sthompsa	rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2470203134Sthompsa	flags = le32toh(rxd->flags);
2471203134Sthompsa
2472203134Sthompsa	if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2473203134Sthompsa		m_freem(m);
2474203134Sthompsa		ifp->if_ierrors++;
2475203134Sthompsa		DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2476203134Sthompsa		return;
2477203134Sthompsa	}
2478203134Sthompsa
2479203134Sthompsa	m->m_data += sizeof(struct rt2860_rxwi);
2480203134Sthompsa	m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2481203134Sthompsa
2482203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2483203134Sthompsa
2484209917Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2485203134Sthompsa		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2486203134Sthompsa		m->m_flags |= M_WEP;
2487203134Sthompsa	}
2488203134Sthompsa
2489209917Sthompsa	if (flags & RT2860_RX_L2PAD) {
2490203134Sthompsa		DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2491203134Sthompsa		len += 2;
2492203134Sthompsa	}
2493203134Sthompsa
2494208019Sthompsa	ni = ieee80211_find_rxnode(ic,
2495208019Sthompsa	    mtod(m, struct ieee80211_frame_min *));
2496208019Sthompsa
2497203134Sthompsa	if (__predict_false(flags & RT2860_RX_MICERR)) {
2498203134Sthompsa		/* report MIC failures to net80211 for TKIP */
2499209917Sthompsa		if (ni != NULL)
2500208019Sthompsa			ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2501203134Sthompsa		m_freem(m);
2502203134Sthompsa		ifp->if_ierrors++;
2503203134Sthompsa		DPRINTF("MIC error. Someone is lying.\n");
2504203134Sthompsa		return;
2505203134Sthompsa	}
2506203134Sthompsa
2507203134Sthompsa	ant = run_maxrssi_chain(sc, rxwi);
2508203134Sthompsa	rssi = rxwi->rssi[ant];
2509203134Sthompsa	nf = run_rssi2dbm(sc, rssi, ant);
2510203134Sthompsa
2511203134Sthompsa	m->m_pkthdr.rcvif = ifp;
2512203134Sthompsa	m->m_pkthdr.len = m->m_len = len;
2513203134Sthompsa
2514203134Sthompsa	if (ni != NULL) {
2515203134Sthompsa		(void)ieee80211_input(ni, m, rssi, nf);
2516203134Sthompsa		ieee80211_free_node(ni);
2517203134Sthompsa	} else {
2518203134Sthompsa		(void)ieee80211_input_all(ic, m, rssi, nf);
2519203134Sthompsa	}
2520203134Sthompsa
2521209917Sthompsa	if (__predict_false(ieee80211_radiotap_active(ic))) {
2522203134Sthompsa		struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2523203134Sthompsa
2524203134Sthompsa		tap->wr_flags = 0;
2525203134Sthompsa		tap->wr_chan_freq = htole16(ic->ic_bsschan->ic_freq);
2526203134Sthompsa		tap->wr_chan_flags = htole16(ic->ic_bsschan->ic_flags);
2527203134Sthompsa		tap->wr_antsignal = rssi;
2528203134Sthompsa		tap->wr_antenna = ant;
2529203134Sthompsa		tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2530203134Sthompsa		tap->wr_rate = 2;	/* in case it can't be found below */
2531203134Sthompsa		phy = le16toh(rxwi->phy);
2532203134Sthompsa		switch (phy & RT2860_PHY_MODE) {
2533203134Sthompsa		case RT2860_PHY_CCK:
2534203134Sthompsa			switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2535203134Sthompsa			case 0:	tap->wr_rate =   2; break;
2536203134Sthompsa			case 1:	tap->wr_rate =   4; break;
2537203134Sthompsa			case 2:	tap->wr_rate =  11; break;
2538203134Sthompsa			case 3:	tap->wr_rate =  22; break;
2539203134Sthompsa			}
2540203134Sthompsa			if (phy & RT2860_PHY_SHPRE)
2541203134Sthompsa				tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2542203134Sthompsa			break;
2543203134Sthompsa		case RT2860_PHY_OFDM:
2544203134Sthompsa			switch (phy & RT2860_PHY_MCS) {
2545203134Sthompsa			case 0:	tap->wr_rate =  12; break;
2546203134Sthompsa			case 1:	tap->wr_rate =  18; break;
2547203134Sthompsa			case 2:	tap->wr_rate =  24; break;
2548203134Sthompsa			case 3:	tap->wr_rate =  36; break;
2549203134Sthompsa			case 4:	tap->wr_rate =  48; break;
2550203134Sthompsa			case 5:	tap->wr_rate =  72; break;
2551203134Sthompsa			case 6:	tap->wr_rate =  96; break;
2552203134Sthompsa			case 7:	tap->wr_rate = 108; break;
2553203134Sthompsa			}
2554203134Sthompsa			break;
2555203134Sthompsa		}
2556203134Sthompsa	}
2557203134Sthompsa}
2558203134Sthompsa
2559203134Sthompsastatic void
2560203134Sthompsarun_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2561203134Sthompsa{
2562203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2563203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2564203134Sthompsa	struct mbuf *m = NULL;
2565203134Sthompsa	struct mbuf *m0;
2566203134Sthompsa	uint32_t dmalen;
2567203134Sthompsa	int xferlen;
2568203134Sthompsa
2569203134Sthompsa	usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2570203134Sthompsa
2571203134Sthompsa	switch (USB_GET_STATE(xfer)) {
2572203134Sthompsa	case USB_ST_TRANSFERRED:
2573203134Sthompsa
2574203134Sthompsa		DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2575203134Sthompsa
2576203134Sthompsa		if (xferlen < sizeof (uint32_t) +
2577203134Sthompsa		    sizeof (struct rt2860_rxwi) + sizeof (struct rt2870_rxd)) {
2578203134Sthompsa			DPRINTF("xfer too short %d\n", xferlen);
2579203134Sthompsa			goto tr_setup;
2580203134Sthompsa		}
2581203134Sthompsa
2582203134Sthompsa		m = sc->rx_m;
2583203134Sthompsa		sc->rx_m = NULL;
2584203134Sthompsa
2585203134Sthompsa		/* FALLTHROUGH */
2586203134Sthompsa	case USB_ST_SETUP:
2587203134Sthompsatr_setup:
2588203134Sthompsa		if (sc->rx_m == NULL) {
2589203134Sthompsa			sc->rx_m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR,
2590203134Sthompsa			    MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2591203134Sthompsa		}
2592203134Sthompsa		if (sc->rx_m == NULL) {
2593203134Sthompsa			DPRINTF("could not allocate mbuf - idle with stall\n");
2594203134Sthompsa			ifp->if_ierrors++;
2595203134Sthompsa			usbd_xfer_set_stall(xfer);
2596203134Sthompsa			usbd_xfer_set_frames(xfer, 0);
2597203134Sthompsa		} else {
2598203134Sthompsa			/*
2599203134Sthompsa			 * Directly loading a mbuf cluster into DMA to
2600203134Sthompsa			 * save some data copying. This works because
2601203134Sthompsa			 * there is only one cluster.
2602203134Sthompsa			 */
2603203134Sthompsa			usbd_xfer_set_frame_data(xfer, 0,
2604203134Sthompsa			    mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2605203134Sthompsa			usbd_xfer_set_frames(xfer, 1);
2606203134Sthompsa		}
2607203134Sthompsa		usbd_transfer_submit(xfer);
2608203134Sthompsa		break;
2609203134Sthompsa
2610203134Sthompsa	default:	/* Error */
2611203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2612203134Sthompsa			/* try to clear stall first */
2613203134Sthompsa			usbd_xfer_set_stall(xfer);
2614203134Sthompsa
2615203134Sthompsa			if (error == USB_ERR_TIMEOUT)
2616203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2617203134Sthompsa
2618203134Sthompsa			ifp->if_ierrors++;
2619203134Sthompsa
2620203134Sthompsa			goto tr_setup;
2621203134Sthompsa		}
2622209917Sthompsa		if (sc->rx_m != NULL) {
2623203134Sthompsa			m_freem(sc->rx_m);
2624203134Sthompsa			sc->rx_m = NULL;
2625203134Sthompsa		}
2626203134Sthompsa		break;
2627203134Sthompsa	}
2628203134Sthompsa
2629203134Sthompsa	if (m == NULL)
2630203134Sthompsa		return;
2631203134Sthompsa
2632203134Sthompsa	/* inputting all the frames must be last */
2633203134Sthompsa
2634203134Sthompsa	RUN_UNLOCK(sc);
2635203134Sthompsa
2636203134Sthompsa	m->m_pkthdr.len = m->m_len = xferlen;
2637203134Sthompsa
2638203134Sthompsa	/* HW can aggregate multiple 802.11 frames in a single USB xfer */
2639203134Sthompsa	for(;;) {
2640203134Sthompsa		dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2641203134Sthompsa
2642203134Sthompsa		if ((dmalen == 0) || ((dmalen & 3) != 0)) {
2643203134Sthompsa			DPRINTF("bad DMA length %u\n", dmalen);
2644203134Sthompsa			break;
2645203134Sthompsa		}
2646203134Sthompsa		if ((dmalen + 8) > xferlen) {
2647203134Sthompsa			DPRINTF("bad DMA length %u > %d\n",
2648203134Sthompsa			dmalen + 8, xferlen);
2649203134Sthompsa			break;
2650203134Sthompsa		}
2651203134Sthompsa
2652203134Sthompsa		/* If it is the last one or a single frame, we won't copy. */
2653209917Sthompsa		if ((xferlen -= dmalen + 8) <= 8) {
2654203134Sthompsa			/* trim 32-bit DMA-len header */
2655203134Sthompsa			m->m_data += 4;
2656203134Sthompsa			m->m_pkthdr.len = m->m_len -= 4;
2657203134Sthompsa			run_rx_frame(sc, m, dmalen);
2658203134Sthompsa			break;
2659203134Sthompsa		}
2660203134Sthompsa
2661203134Sthompsa		/* copy aggregated frames to another mbuf */
2662203134Sthompsa		m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2663203134Sthompsa		if (__predict_false(m0 == NULL)) {
2664203134Sthompsa			DPRINTF("could not allocate mbuf\n");
2665203134Sthompsa			ifp->if_ierrors++;
2666203134Sthompsa			break;
2667203134Sthompsa		}
2668203134Sthompsa		m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2669203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2670203134Sthompsa		m0->m_pkthdr.len = m0->m_len =
2671203134Sthompsa		    dmalen + sizeof(struct rt2870_rxd);
2672203134Sthompsa		run_rx_frame(sc, m0, dmalen);
2673203134Sthompsa
2674203134Sthompsa		/* update data ptr */
2675203134Sthompsa		m->m_data += dmalen + 8;
2676203134Sthompsa		m->m_pkthdr.len = m->m_len -= dmalen + 8;
2677203134Sthompsa	}
2678203134Sthompsa
2679203134Sthompsa	RUN_LOCK(sc);
2680203134Sthompsa}
2681203134Sthompsa
2682203134Sthompsastatic void
2683203134Sthompsarun_tx_free(struct run_endpoint_queue *pq,
2684203134Sthompsa    struct run_tx_data *data, int txerr)
2685203134Sthompsa{
2686203134Sthompsa	if (data->m != NULL) {
2687203134Sthompsa		if (data->m->m_flags & M_TXCB)
2688203134Sthompsa			ieee80211_process_callback(data->ni, data->m,
2689203134Sthompsa			    txerr ? ETIMEDOUT : 0);
2690203134Sthompsa		m_freem(data->m);
2691203134Sthompsa		data->m = NULL;
2692203134Sthompsa
2693209917Sthompsa		if (data->ni == NULL) {
2694203134Sthompsa			DPRINTF("no node\n");
2695203134Sthompsa		} else {
2696203134Sthompsa			ieee80211_free_node(data->ni);
2697203134Sthompsa			data->ni = NULL;
2698203134Sthompsa		}
2699203134Sthompsa	}
2700203134Sthompsa
2701203134Sthompsa	STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2702203134Sthompsa	pq->tx_nfree++;
2703203134Sthompsa}
2704203134Sthompsa
2705203134Sthompsastatic void
2706203134Sthompsarun_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index)
2707203134Sthompsa{
2708203134Sthompsa	struct run_softc *sc = usbd_xfer_softc(xfer);
2709203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2710208019Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2711203134Sthompsa	struct run_tx_data *data;
2712203134Sthompsa	struct ieee80211vap *vap = NULL;
2713203134Sthompsa	struct usb_page_cache *pc;
2714203134Sthompsa	struct run_endpoint_queue *pq = &sc->sc_epq[index];
2715203134Sthompsa	struct mbuf *m;
2716203134Sthompsa	usb_frlength_t size;
2717203134Sthompsa	unsigned int len;
2718203134Sthompsa	int actlen;
2719203134Sthompsa	int sumlen;
2720203134Sthompsa
2721203134Sthompsa	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2722203134Sthompsa
2723209917Sthompsa	switch (USB_GET_STATE(xfer)) {
2724203134Sthompsa	case USB_ST_TRANSFERRED:
2725203134Sthompsa		DPRINTFN(11, "transfer complete: %d "
2726203134Sthompsa		    "bytes @ index %d\n", actlen, index);
2727203134Sthompsa
2728203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2729203134Sthompsa
2730203134Sthompsa		run_tx_free(pq, data, 0);
2731203134Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2732203134Sthompsa
2733203134Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2734203134Sthompsa
2735203134Sthompsa		ifp->if_opackets++;
2736203134Sthompsa
2737203134Sthompsa		/* FALLTHROUGH */
2738203134Sthompsa	case USB_ST_SETUP:
2739203134Sthompsatr_setup:
2740203134Sthompsa		data = STAILQ_FIRST(&pq->tx_qh);
2741209917Sthompsa		if (data == NULL)
2742203134Sthompsa			break;
2743203134Sthompsa
2744203134Sthompsa		STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2745203134Sthompsa
2746203134Sthompsa		m = data->m;
2747203134Sthompsa		if (m->m_pkthdr.len > RUN_MAX_TXSZ) {
2748203134Sthompsa			DPRINTF("data overflow, %u bytes\n",
2749203134Sthompsa			    m->m_pkthdr.len);
2750203134Sthompsa
2751203134Sthompsa			ifp->if_oerrors++;
2752203134Sthompsa
2753203134Sthompsa			run_tx_free(pq, data, 1);
2754203134Sthompsa
2755203134Sthompsa			goto tr_setup;
2756203134Sthompsa		}
2757203134Sthompsa
2758203134Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2759203134Sthompsa		size = sizeof(data->desc);
2760203134Sthompsa		usbd_copy_in(pc, 0, &data->desc, size);
2761203134Sthompsa		usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2762203134Sthompsa
2763203134Sthompsa		vap = data->ni->ni_vap;
2764203134Sthompsa		if (ieee80211_radiotap_active_vap(vap)) {
2765203134Sthompsa			struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2766208019Sthompsa			struct rt2860_txwi *txwi =
2767208019Sthompsa			    (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2768203134Sthompsa
2769203134Sthompsa			tap->wt_flags = 0;
2770203134Sthompsa			tap->wt_rate = rt2860_rates[data->ridx].rate;
2771203134Sthompsa			tap->wt_chan_freq = htole16(vap->iv_bss->ni_chan->ic_freq);
2772203134Sthompsa			tap->wt_chan_flags = htole16(vap->iv_bss->ni_chan->ic_flags);
2773203134Sthompsa			tap->wt_hwqueue = index;
2774208019Sthompsa			if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2775203134Sthompsa				tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2776203134Sthompsa
2777203134Sthompsa			ieee80211_radiotap_tx(vap, m);
2778203134Sthompsa		}
2779203134Sthompsa
2780203134Sthompsa		/* align end on a 4-bytes boundary */
2781208019Sthompsa		len = (size + IEEE80211_CRC_LEN + m->m_pkthdr.len + 3) & ~3;
2782203134Sthompsa
2783203134Sthompsa		DPRINTFN(11, "sending frame len=%u xferlen=%u @ index %d\n",
2784203134Sthompsa			m->m_pkthdr.len, len, index);
2785203134Sthompsa
2786203134Sthompsa		usbd_xfer_set_frame_len(xfer, 0, len);
2787203134Sthompsa		usbd_xfer_set_priv(xfer, data);
2788203134Sthompsa
2789203134Sthompsa		usbd_transfer_submit(xfer);
2790203134Sthompsa
2791203134Sthompsa		RUN_UNLOCK(sc);
2792203134Sthompsa		run_start(ifp);
2793203134Sthompsa		RUN_LOCK(sc);
2794203134Sthompsa
2795203134Sthompsa		break;
2796203134Sthompsa
2797203134Sthompsa	default:
2798203134Sthompsa		DPRINTF("USB transfer error, %s\n",
2799203134Sthompsa		    usbd_errstr(error));
2800203134Sthompsa
2801203134Sthompsa		data = usbd_xfer_get_priv(xfer);
2802203134Sthompsa
2803203134Sthompsa		ifp->if_oerrors++;
2804203134Sthompsa
2805203134Sthompsa		if (data != NULL) {
2806208019Sthompsa			if(data->ni != NULL)
2807208019Sthompsa				vap = data->ni->ni_vap;
2808203134Sthompsa			run_tx_free(pq, data, error);
2809203134Sthompsa			usbd_xfer_set_priv(xfer, NULL);
2810203134Sthompsa		}
2811209917Sthompsa		if (vap == NULL)
2812208019Sthompsa			vap = TAILQ_FIRST(&ic->ic_vaps);
2813203134Sthompsa
2814203134Sthompsa		if (error != USB_ERR_CANCELLED) {
2815203134Sthompsa			if (error == USB_ERR_TIMEOUT) {
2816203134Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2817208019Sthompsa				uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2818208019Sthompsa				DPRINTF("cmdq_store=%d\n", i);
2819208019Sthompsa				sc->cmdq[i].func = run_usb_timeout_cb;
2820208019Sthompsa				sc->cmdq[i].arg0 = vap;
2821208019Sthompsa				ieee80211_runtask(ic, &sc->cmdq_task);
2822203134Sthompsa			}
2823203134Sthompsa
2824203134Sthompsa			/*
2825203134Sthompsa			 * Try to clear stall first, also if other
2826203134Sthompsa			 * errors occur, hence clearing stall
2827203134Sthompsa			 * introduces a 50 ms delay:
2828203134Sthompsa			 */
2829203134Sthompsa			usbd_xfer_set_stall(xfer);
2830203134Sthompsa			goto tr_setup;
2831203134Sthompsa		}
2832203134Sthompsa		break;
2833203134Sthompsa	}
2834203134Sthompsa}
2835203134Sthompsa
2836203134Sthompsastatic void
2837203134Sthompsarun_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2838203134Sthompsa{
2839203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 0);
2840203134Sthompsa}
2841203134Sthompsa
2842203134Sthompsastatic void
2843203134Sthompsarun_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2844203134Sthompsa{
2845203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 1);
2846203134Sthompsa}
2847203134Sthompsa
2848203134Sthompsastatic void
2849203134Sthompsarun_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2850203134Sthompsa{
2851203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 2);
2852203134Sthompsa}
2853203134Sthompsa
2854203134Sthompsastatic void
2855203134Sthompsarun_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2856203134Sthompsa{
2857203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 3);
2858203134Sthompsa}
2859203134Sthompsa
2860203134Sthompsastatic void
2861203134Sthompsarun_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2862203134Sthompsa{
2863203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 4);
2864203134Sthompsa}
2865203134Sthompsa
2866203134Sthompsastatic void
2867203134Sthompsarun_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2868203134Sthompsa{
2869203134Sthompsa	run_bulk_tx_callbackN(xfer, error, 5);
2870203134Sthompsa}
2871203134Sthompsa
2872203134Sthompsastatic void
2873208019Sthompsarun_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2874203134Sthompsa{
2875203134Sthompsa	struct mbuf *m = data->m;
2876203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2877208019Sthompsa	struct ieee80211vap *vap = data->ni->ni_vap;
2878203134Sthompsa	struct ieee80211_frame *wh;
2879203134Sthompsa	struct rt2870_txd *txd;
2880203134Sthompsa	struct rt2860_txwi *txwi;
2881208019Sthompsa	uint16_t xferlen;
2882208019Sthompsa	uint16_t mcs;
2883203134Sthompsa	uint8_t ridx = data->ridx;
2884208019Sthompsa	uint8_t pad;
2885203134Sthompsa
2886203134Sthompsa	/* get MCS code from rate index */
2887208019Sthompsa	mcs = rt2860_rates[ridx].mcs;
2888203134Sthompsa
2889203134Sthompsa	xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2890203134Sthompsa
2891203134Sthompsa	/* roundup to 32-bit alignment */
2892203134Sthompsa	xferlen = (xferlen + 3) & ~3;
2893203134Sthompsa
2894203134Sthompsa	txd = (struct rt2870_txd *)&data->desc;
2895203134Sthompsa	txd->len = htole16(xferlen);
2896203134Sthompsa
2897208019Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2898208019Sthompsa
2899208019Sthompsa	/*
2900208019Sthompsa	 * Ether both are true or both are false, the header
2901208019Sthompsa	 * are nicely aligned to 32-bit. So, no L2 padding.
2902208019Sthompsa	 */
2903208019Sthompsa	if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2904208019Sthompsa		pad = 0;
2905208019Sthompsa	else
2906208019Sthompsa		pad = 2;
2907208019Sthompsa
2908203134Sthompsa	/* setup TX Wireless Information */
2909203134Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
2910203134Sthompsa	txwi->len = htole16(m->m_pkthdr.len - pad);
2911203134Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2912203134Sthompsa		txwi->phy = htole16(RT2860_PHY_CCK);
2913203134Sthompsa		if (ridx != RT2860_RIDX_CCK1 &&
2914203134Sthompsa		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2915203134Sthompsa			mcs |= RT2860_PHY_SHPRE;
2916203134Sthompsa	} else
2917203134Sthompsa		txwi->phy = htole16(RT2860_PHY_OFDM);
2918203134Sthompsa	txwi->phy |= htole16(mcs);
2919203134Sthompsa
2920203134Sthompsa	/* check if RTS/CTS or CTS-to-self protection is required */
2921203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2922203134Sthompsa	    (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2923203134Sthompsa	     ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2924203134Sthompsa	      rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
2925208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_HT;
2926203134Sthompsa	else
2927208019Sthompsa		txwi->txop |= RT2860_TX_TXOP_BACKOFF;
2928209144Sthompsa
2929209917Sthompsa	if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
2930209144Sthompsa		txwi->xflags |= RT2860_TX_NSEQ;
2931203134Sthompsa}
2932203134Sthompsa
2933203134Sthompsa/* This function must be called locked */
2934203134Sthompsastatic int
2935203134Sthompsarun_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2936203134Sthompsa{
2937203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2938208019Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2939203134Sthompsa	struct ieee80211_frame *wh;
2940208019Sthompsa	struct ieee80211_channel *chan;
2941203134Sthompsa	const struct ieee80211_txparam *tp;
2942208019Sthompsa	struct run_node *rn = (void *)ni;
2943203134Sthompsa	struct run_tx_data *data;
2944208019Sthompsa	struct rt2870_txd *txd;
2945208019Sthompsa	struct rt2860_txwi *txwi;
2946203134Sthompsa	uint16_t qos;
2947203134Sthompsa	uint16_t dur;
2948208019Sthompsa	uint16_t qid;
2949203134Sthompsa	uint8_t type;
2950203134Sthompsa	uint8_t tid;
2951208019Sthompsa	uint8_t ridx;
2952208019Sthompsa	uint8_t ctl_ridx;
2953203134Sthompsa	uint8_t qflags;
2954203134Sthompsa	uint8_t xflags = 0;
2955203134Sthompsa	int hasqos;
2956203134Sthompsa
2957203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
2958203134Sthompsa
2959203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
2960203134Sthompsa
2961203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2962203134Sthompsa
2963203134Sthompsa	/*
2964203134Sthompsa	 * There are 7 bulk endpoints: 1 for RX
2965203134Sthompsa	 * and 6 for TX (4 EDCAs + HCCA + Prio).
2966203134Sthompsa	 * Update 03-14-2009:  some devices like the Planex GW-US300MiniS
2967203134Sthompsa	 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
2968203134Sthompsa	 */
2969203134Sthompsa	if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
2970203134Sthompsa		uint8_t *frm;
2971203134Sthompsa
2972203134Sthompsa		if(IEEE80211_HAS_ADDR4(wh))
2973203134Sthompsa			frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
2974203134Sthompsa		else
2975203134Sthompsa			frm =((struct ieee80211_qosframe *)wh)->i_qos;
2976203134Sthompsa
2977203134Sthompsa		qos = le16toh(*(const uint16_t *)frm);
2978203134Sthompsa		tid = qos & IEEE80211_QOS_TID;
2979203134Sthompsa		qid = TID_TO_WME_AC(tid);
2980203134Sthompsa	} else {
2981203134Sthompsa		qos = 0;
2982203134Sthompsa		tid = 0;
2983203134Sthompsa		qid = WME_AC_BE;
2984203134Sthompsa	}
2985203134Sthompsa	qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
2986203134Sthompsa
2987203134Sthompsa	DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
2988203134Sthompsa	    qos, qid, tid, qflags);
2989203134Sthompsa
2990208019Sthompsa	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
2991208019Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2992203134Sthompsa
2993203134Sthompsa	/* pickup a rate index */
2994203134Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2995203134Sthompsa	    type != IEEE80211_FC0_TYPE_DATA) {
2996203134Sthompsa		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2997203134Sthompsa		    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
2998203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
2999203134Sthompsa	} else {
3000208019Sthompsa		if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3001208019Sthompsa			ridx = rn->fix_ridx;
3002208019Sthompsa		else
3003208019Sthompsa			ridx = rn->amrr_ridx;
3004203134Sthompsa		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3005203134Sthompsa	}
3006203134Sthompsa
3007203134Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3008203134Sthompsa	    (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3009203134Sthompsa	     IEEE80211_QOS_ACKPOLICY_NOACK)) {
3010209144Sthompsa		xflags |= RT2860_TX_ACK;
3011203134Sthompsa		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3012208019Sthompsa			dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3013203134Sthompsa		else
3014208019Sthompsa			dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3015205042Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3016203134Sthompsa	}
3017203134Sthompsa
3018203134Sthompsa	/* reserve slots for mgmt packets, just in case */
3019203134Sthompsa	if (sc->sc_epq[qid].tx_nfree < 3) {
3020203134Sthompsa		DPRINTFN(10, "tx ring %d is full\n", qid);
3021203134Sthompsa		return (-1);
3022203134Sthompsa	}
3023203134Sthompsa
3024203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3025203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3026203134Sthompsa	sc->sc_epq[qid].tx_nfree--;
3027203134Sthompsa
3028208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3029208019Sthompsa	txd->flags = qflags;
3030208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3031208019Sthompsa	txwi->xflags = xflags;
3032209144Sthompsa	txwi->wcid = IEEE80211_IS_MULTICAST(wh->i_addr1) ?
3033209144Sthompsa	    0 : RUN_AID2WCID(ni->ni_associd);
3034208019Sthompsa	/* clear leftover garbage bits */
3035208019Sthompsa	txwi->flags = 0;
3036208019Sthompsa	txwi->txop = 0;
3037208019Sthompsa
3038203134Sthompsa	data->m = m;
3039203134Sthompsa	data->ni = ni;
3040203134Sthompsa	data->ridx = ridx;
3041203134Sthompsa
3042208019Sthompsa	run_set_tx_desc(sc, data);
3043203134Sthompsa
3044208019Sthompsa	/*
3045208019Sthompsa	 * The chip keeps track of 2 kind of Tx stats,
3046208019Sthompsa	 *  * TX_STAT_FIFO, for per WCID stats, and
3047208019Sthompsa	 *  * TX_STA_CNT0 for all-TX-in-one stats.
3048208019Sthompsa	 *
3049208019Sthompsa	 * To use FIFO stats, we need to store MCS into the driver-private
3050208019Sthompsa 	 * PacketID field. So that, we can tell whose stats when we read them.
3051208019Sthompsa 	 * We add 1 to the MCS because setting the PacketID field to 0 means
3052208019Sthompsa 	 * that we don't want feedback in TX_STAT_FIFO.
3053208019Sthompsa 	 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3054208019Sthompsa 	 *
3055208019Sthompsa 	 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3056208019Sthompsa 	 */
3057209917Sthompsa	if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3058209917Sthompsa	    vap->iv_opmode == IEEE80211_M_MBSS) {
3059208019Sthompsa		uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3060208019Sthompsa		txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3061208019Sthompsa
3062208019Sthompsa		/*
3063208019Sthompsa		 * Unlike PCI based devices, we don't get any interrupt from
3064208019Sthompsa		 * USB devices, so we simulate FIFO-is-full interrupt here.
3065208019Sthompsa		 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3066208019Sthompsa		 * quickly get fulled. To prevent overflow, increment a counter on
3067208019Sthompsa		 * every FIFO stat request, so we know how many slots are left.
3068208019Sthompsa		 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3069208019Sthompsa		 * are used only in those modes.
3070208019Sthompsa		 * We just drain stats. AMRR gets updated every 1 sec by
3071208019Sthompsa		 * run_ratectl_cb() via callout.
3072208019Sthompsa		 * Call it early. Otherwise overflow.
3073208019Sthompsa		 */
3074209917Sthompsa		if (sc->fifo_cnt++ == 10) {
3075208019Sthompsa			/*
3076208019Sthompsa			 * With multiple vaps or if_bridge, if_start() is called
3077208019Sthompsa			 * with a non-sleepable lock, tcpinp. So, need to defer.
3078208019Sthompsa			 */
3079208019Sthompsa			uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3080208019Sthompsa			DPRINTFN(6, "cmdq_store=%d\n", i);
3081208019Sthompsa			sc->cmdq[i].func = run_drain_fifo;
3082208019Sthompsa			sc->cmdq[i].arg0 = sc;
3083208019Sthompsa			ieee80211_runtask(ic, &sc->cmdq_task);
3084208019Sthompsa		}
3085208019Sthompsa	}
3086208019Sthompsa
3087203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3088203134Sthompsa
3089203134Sthompsa	usbd_transfer_start(sc->sc_xfer[qid]);
3090203134Sthompsa
3091203134Sthompsa	DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3092203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3093203134Sthompsa	    rt2860_rates[ridx].rate, qid);
3094203134Sthompsa
3095203134Sthompsa	return (0);
3096203134Sthompsa}
3097203134Sthompsa
3098203134Sthompsastatic int
3099203134Sthompsarun_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3100203134Sthompsa{
3101203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3102203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3103208019Sthompsa	struct run_node *rn = (void *)ni;
3104203134Sthompsa	struct run_tx_data *data;
3105203134Sthompsa	struct ieee80211_frame *wh;
3106208019Sthompsa	struct rt2870_txd *txd;
3107208019Sthompsa	struct rt2860_txwi *txwi;
3108203134Sthompsa	uint16_t dur;
3109208019Sthompsa	uint8_t ridx = rn->mgt_ridx;
3110203134Sthompsa	uint8_t type;
3111203134Sthompsa	uint8_t xflags = 0;
3112208019Sthompsa	uint8_t wflags = 0;
3113203134Sthompsa
3114203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3115203134Sthompsa
3116203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3117203134Sthompsa
3118203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3119203134Sthompsa
3120208019Sthompsa	/* tell hardware to add timestamp for probe responses */
3121208019Sthompsa	if ((wh->i_fc[0] &
3122208019Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3123208019Sthompsa	    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3124208019Sthompsa		wflags |= RT2860_TX_TS;
3125208019Sthompsa	else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3126203134Sthompsa		xflags |= RT2860_TX_ACK;
3127203134Sthompsa
3128208019Sthompsa		dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3129203134Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3130203134Sthompsa		*(uint16_t *)wh->i_dur = htole16(dur);
3131203134Sthompsa	}
3132203134Sthompsa
3133203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3134203134Sthompsa		/* let caller free mbuf */
3135203134Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3136203134Sthompsa		return (EIO);
3137203134Sthompsa	}
3138203134Sthompsa	data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3139203134Sthompsa	STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3140203134Sthompsa	sc->sc_epq[0].tx_nfree--;
3141203134Sthompsa
3142208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3143208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3144208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3145208019Sthompsa	txwi->wcid = 0xff;
3146208019Sthompsa	txwi->flags = wflags;
3147208019Sthompsa	txwi->xflags = xflags;
3148208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3149208019Sthompsa
3150203134Sthompsa	data->m = m;
3151203134Sthompsa	data->ni = ni;
3152203134Sthompsa	data->ridx = ridx;
3153203134Sthompsa
3154208019Sthompsa	run_set_tx_desc(sc, data);
3155203134Sthompsa
3156203134Sthompsa	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3157203134Sthompsa	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3158208019Sthompsa	    rt2860_rates[ridx].rate);
3159203134Sthompsa
3160203134Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3161203134Sthompsa
3162203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3163203134Sthompsa
3164203134Sthompsa	return (0);
3165203134Sthompsa}
3166203134Sthompsa
3167203134Sthompsastatic int
3168203134Sthompsarun_sendprot(struct run_softc *sc,
3169203134Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3170203134Sthompsa{
3171203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3172203134Sthompsa	struct ieee80211_frame *wh;
3173203134Sthompsa	struct run_tx_data *data;
3174208019Sthompsa	struct rt2870_txd *txd;
3175208019Sthompsa	struct rt2860_txwi *txwi;
3176203134Sthompsa	struct mbuf *mprot;
3177203134Sthompsa	int ridx;
3178203134Sthompsa	int protrate;
3179203134Sthompsa	int ackrate;
3180203134Sthompsa	int pktlen;
3181203134Sthompsa	int isshort;
3182203134Sthompsa	uint16_t dur;
3183203134Sthompsa	uint8_t type;
3184208019Sthompsa	uint8_t wflags = 0;
3185208019Sthompsa	uint8_t xflags = 0;
3186203134Sthompsa
3187203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3188203134Sthompsa
3189203134Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3190203134Sthompsa	    ("protection %d", prot));
3191203134Sthompsa
3192203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3193203134Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3194203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3195203134Sthompsa
3196203134Sthompsa	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3197203134Sthompsa	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3198203134Sthompsa
3199203134Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3200209189Sjkim	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3201203134Sthompsa	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3202203134Sthompsa	wflags = RT2860_TX_FRAG;
3203203134Sthompsa
3204203134Sthompsa	/* check that there are free slots before allocating the mbuf */
3205203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3206203134Sthompsa		/* let caller free mbuf */
3207203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3208203134Sthompsa		return (ENOBUFS);
3209203134Sthompsa	}
3210203134Sthompsa
3211203134Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
3212203134Sthompsa		/* NB: CTS is the same size as an ACK */
3213203134Sthompsa		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3214208019Sthompsa		xflags |= RT2860_TX_ACK;
3215203134Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3216203134Sthompsa	} else {
3217203134Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3218203134Sthompsa	}
3219203134Sthompsa	if (mprot == NULL) {
3220203134Sthompsa		sc->sc_ifp->if_oerrors++;
3221203134Sthompsa		DPRINTF("could not allocate mbuf\n");
3222203134Sthompsa		return (ENOBUFS);
3223203134Sthompsa	}
3224203134Sthompsa
3225203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3226203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3227203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3228203134Sthompsa
3229208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3230208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3231208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3232208019Sthompsa	txwi->wcid = 0xff;
3233208019Sthompsa	txwi->flags = wflags;
3234208019Sthompsa	txwi->xflags = xflags;
3235208019Sthompsa	txwi->txop = 0;	/* clear leftover garbage bits */
3236208019Sthompsa
3237203134Sthompsa	data->m = mprot;
3238203134Sthompsa	data->ni = ieee80211_ref_node(ni);
3239203134Sthompsa
3240203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3241203134Sthompsa		if (rt2860_rates[ridx].rate == protrate)
3242203134Sthompsa			break;
3243203134Sthompsa	data->ridx = ridx;
3244203134Sthompsa
3245208019Sthompsa	run_set_tx_desc(sc, data);
3246203134Sthompsa
3247203134Sthompsa        DPRINTFN(1, "sending prot len=%u rate=%u\n",
3248203134Sthompsa            m->m_pkthdr.len, rate);
3249203134Sthompsa
3250203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3251203134Sthompsa
3252203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3253203134Sthompsa
3254203134Sthompsa	return (0);
3255203134Sthompsa}
3256203134Sthompsa
3257203134Sthompsastatic int
3258203134Sthompsarun_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3259203134Sthompsa    const struct ieee80211_bpf_params *params)
3260203134Sthompsa{
3261203134Sthompsa	struct ieee80211com *ic = ni->ni_ic;
3262203134Sthompsa	struct ieee80211_frame *wh;
3263203134Sthompsa	struct run_tx_data *data;
3264208019Sthompsa	struct rt2870_txd *txd;
3265208019Sthompsa	struct rt2860_txwi *txwi;
3266203134Sthompsa	uint8_t type;
3267208019Sthompsa	uint8_t ridx;
3268208019Sthompsa	uint8_t rate;
3269208019Sthompsa	uint8_t opflags = 0;
3270208019Sthompsa	uint8_t xflags = 0;
3271203134Sthompsa	int error;
3272203134Sthompsa
3273203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
3274203134Sthompsa
3275203134Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
3276203134Sthompsa
3277203134Sthompsa	wh = mtod(m, struct ieee80211_frame *);
3278203134Sthompsa	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3279203134Sthompsa
3280203134Sthompsa	rate = params->ibp_rate0;
3281203134Sthompsa	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3282203134Sthompsa		/* let caller free mbuf */
3283203134Sthompsa		return (EINVAL);
3284203134Sthompsa	}
3285203134Sthompsa
3286203134Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3287208019Sthompsa		xflags |= RT2860_TX_ACK;
3288203134Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3289203134Sthompsa		error = run_sendprot(sc, m, ni,
3290203134Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
3291203134Sthompsa			IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3292203134Sthompsa		    rate);
3293203134Sthompsa		if (error) {
3294203134Sthompsa			/* let caller free mbuf */
3295209917Sthompsa			return error;
3296203134Sthompsa		}
3297203134Sthompsa		opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3298203134Sthompsa	}
3299203134Sthompsa
3300203134Sthompsa	if (sc->sc_epq[0].tx_nfree == 0) {
3301203134Sthompsa		/* let caller free mbuf */
3302203134Sthompsa		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3303203134Sthompsa		DPRINTF("sending raw frame, but tx ring is full\n");
3304203134Sthompsa		return (EIO);
3305203134Sthompsa	}
3306203134Sthompsa        data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3307203134Sthompsa        STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3308203134Sthompsa        sc->sc_epq[0].tx_nfree--;
3309203134Sthompsa
3310208019Sthompsa	txd = (struct rt2870_txd *)&data->desc;
3311208019Sthompsa	txd->flags = RT2860_TX_QSEL_EDCA;
3312208019Sthompsa	txwi = (struct rt2860_txwi *)(txd + 1);
3313208019Sthompsa	txwi->wcid = 0xff;
3314208019Sthompsa	txwi->xflags = xflags;
3315208019Sthompsa	txwi->txop = opflags;
3316208019Sthompsa	txwi->flags = 0;	/* clear leftover garbage bits */
3317208019Sthompsa
3318203134Sthompsa        data->m = m;
3319203134Sthompsa        data->ni = ni;
3320203134Sthompsa	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3321203134Sthompsa		if (rt2860_rates[ridx].rate == rate)
3322203134Sthompsa			break;
3323203134Sthompsa	data->ridx = ridx;
3324203134Sthompsa
3325208019Sthompsa        run_set_tx_desc(sc, data);
3326203134Sthompsa
3327203134Sthompsa        DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3328203134Sthompsa            m->m_pkthdr.len, rate);
3329203134Sthompsa
3330203134Sthompsa        STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3331203134Sthompsa
3332203134Sthompsa	usbd_transfer_start(sc->sc_xfer[0]);
3333203134Sthompsa
3334209917Sthompsa        return (0);
3335203134Sthompsa}
3336203134Sthompsa
3337203134Sthompsastatic int
3338203134Sthompsarun_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3339203134Sthompsa    const struct ieee80211_bpf_params *params)
3340203134Sthompsa{
3341203134Sthompsa	struct ifnet *ifp = ni->ni_ic->ic_ifp;
3342203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3343208019Sthompsa	int error = 0;
3344208019Sthompsa
3345203134Sthompsa	RUN_LOCK(sc);
3346203134Sthompsa
3347203134Sthompsa	/* prevent management frames from being sent if we're not ready */
3348203134Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3349203134Sthompsa		error =  ENETDOWN;
3350208019Sthompsa		goto done;
3351203134Sthompsa	}
3352203134Sthompsa
3353203134Sthompsa	if (params == NULL) {
3354203134Sthompsa		/* tx mgt packet */
3355209917Sthompsa		if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3356203134Sthompsa			ifp->if_oerrors++;
3357203134Sthompsa			DPRINTF("mgt tx failed\n");
3358208019Sthompsa			goto done;
3359203134Sthompsa		}
3360203134Sthompsa	} else {
3361203134Sthompsa		/* tx raw packet with param */
3362209917Sthompsa		if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3363203134Sthompsa			ifp->if_oerrors++;
3364203134Sthompsa			DPRINTF("tx with param failed\n");
3365208019Sthompsa			goto done;
3366203134Sthompsa		}
3367203134Sthompsa	}
3368203134Sthompsa
3369203134Sthompsa	ifp->if_opackets++;
3370203134Sthompsa
3371208019Sthompsadone:
3372203134Sthompsa	RUN_UNLOCK(sc);
3373203134Sthompsa
3374209917Sthompsa	if (error != 0) {
3375208019Sthompsa		if(m != NULL)
3376208019Sthompsa			m_freem(m);
3377208019Sthompsa		ieee80211_free_node(ni);
3378208019Sthompsa	}
3379203134Sthompsa
3380203134Sthompsa	return (error);
3381203134Sthompsa}
3382203134Sthompsa
3383203134Sthompsastatic void
3384203134Sthompsarun_start(struct ifnet *ifp)
3385203134Sthompsa{
3386203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3387203134Sthompsa	struct ieee80211_node *ni;
3388203134Sthompsa	struct mbuf *m;
3389203134Sthompsa
3390203134Sthompsa	RUN_LOCK(sc);
3391203134Sthompsa
3392203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3393203134Sthompsa		RUN_UNLOCK(sc);
3394203134Sthompsa		return;
3395203134Sthompsa	}
3396203134Sthompsa
3397203134Sthompsa	for (;;) {
3398203134Sthompsa		/* send data frames */
3399203134Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3400203134Sthompsa		if (m == NULL)
3401203134Sthompsa			break;
3402203134Sthompsa
3403203134Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3404203134Sthompsa		if (run_tx(sc, m, ni) != 0) {
3405203134Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
3406203134Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3407203134Sthompsa			break;
3408203134Sthompsa		}
3409203134Sthompsa	}
3410203134Sthompsa
3411203134Sthompsa	RUN_UNLOCK(sc);
3412203134Sthompsa}
3413203134Sthompsa
3414203134Sthompsastatic int
3415203134Sthompsarun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3416203134Sthompsa{
3417203134Sthompsa	struct run_softc *sc = ifp->if_softc;
3418203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3419203134Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
3420208019Sthompsa	int startall = 0;
3421208019Sthompsa	int error = 0;
3422203134Sthompsa
3423203134Sthompsa	switch (cmd) {
3424203134Sthompsa	case SIOCSIFFLAGS:
3425203134Sthompsa		RUN_LOCK(sc);
3426203134Sthompsa		if (ifp->if_flags & IFF_UP) {
3427203134Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3428208019Sthompsa				startall = 1;
3429203134Sthompsa				run_init_locked(sc);
3430203134Sthompsa			} else
3431203134Sthompsa				run_update_promisc_locked(ifp);
3432203134Sthompsa		} else {
3433209917Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3434209917Sthompsa			    (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3435208019Sthompsa					run_stop(sc);
3436208019Sthompsa			}
3437203134Sthompsa		}
3438203134Sthompsa		RUN_UNLOCK(sc);
3439209917Sthompsa		if (startall)
3440208019Sthompsa			ieee80211_start_all(ic);
3441203134Sthompsa		break;
3442203134Sthompsa	case SIOCGIFMEDIA:
3443203134Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3444203134Sthompsa		break;
3445203134Sthompsa	case SIOCGIFADDR:
3446203134Sthompsa		error = ether_ioctl(ifp, cmd, data);
3447203134Sthompsa		break;
3448203134Sthompsa	default:
3449203134Sthompsa		error = EINVAL;
3450203134Sthompsa		break;
3451203134Sthompsa	}
3452203134Sthompsa
3453203134Sthompsa	return (error);
3454203134Sthompsa}
3455203134Sthompsa
3456203134Sthompsastatic void
3457205042Sthompsarun_set_agc(struct run_softc *sc, uint8_t agc)
3458205042Sthompsa{
3459205042Sthompsa	uint8_t bbp;
3460205042Sthompsa
3461205042Sthompsa	if (sc->mac_ver == 0x3572) {
3462205042Sthompsa		run_bbp_read(sc, 27, &bbp);
3463205042Sthompsa		bbp &= ~(0x3 << 5);
3464205042Sthompsa		run_bbp_write(sc, 27, bbp | 0 << 5);	/* select Rx0 */
3465205042Sthompsa		run_bbp_write(sc, 66, agc);
3466205042Sthompsa		run_bbp_write(sc, 27, bbp | 1 << 5);	/* select Rx1 */
3467205042Sthompsa		run_bbp_write(sc, 66, agc);
3468205042Sthompsa	} else
3469205042Sthompsa		run_bbp_write(sc, 66, agc);
3470205042Sthompsa}
3471205042Sthompsa
3472205042Sthompsastatic void
3473203134Sthompsarun_select_chan_group(struct run_softc *sc, int group)
3474203134Sthompsa{
3475203134Sthompsa	uint32_t tmp;
3476205042Sthompsa	uint8_t agc;
3477203134Sthompsa
3478203134Sthompsa	run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3479203134Sthompsa	run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3480203134Sthompsa	run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3481203134Sthompsa	run_bbp_write(sc, 86, 0x00);
3482203134Sthompsa
3483203134Sthompsa	if (group == 0) {
3484203134Sthompsa		if (sc->ext_2ghz_lna) {
3485203134Sthompsa			run_bbp_write(sc, 82, 0x62);
3486203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3487203134Sthompsa		} else {
3488203134Sthompsa			run_bbp_write(sc, 82, 0x84);
3489203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3490203134Sthompsa		}
3491203134Sthompsa	} else {
3492205042Sthompsa		if (sc->mac_ver == 0x3572)
3493205042Sthompsa			run_bbp_write(sc, 82, 0x94);
3494205042Sthompsa		else
3495203134Sthompsa			run_bbp_write(sc, 82, 0xf2);
3496205042Sthompsa		if (sc->ext_5ghz_lna)
3497203134Sthompsa			run_bbp_write(sc, 75, 0x46);
3498205042Sthompsa		else
3499203134Sthompsa			run_bbp_write(sc, 75, 0x50);
3500203134Sthompsa	}
3501203134Sthompsa
3502203134Sthompsa	run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3503203134Sthompsa	tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3504203134Sthompsa	tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3505203134Sthompsa	run_write(sc, RT2860_TX_BAND_CFG, tmp);
3506203134Sthompsa
3507203134Sthompsa	/* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3508208019Sthompsa	tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3509208019Sthompsa	if (sc->nrxchains > 1)
3510208019Sthompsa		tmp |= RT2860_LNA_PE1_EN;
3511203134Sthompsa	if (group == 0) {	/* 2GHz */
3512208019Sthompsa		tmp |= RT2860_PA_PE_G0_EN;
3513203134Sthompsa		if (sc->ntxchains > 1)
3514203134Sthompsa			tmp |= RT2860_PA_PE_G1_EN;
3515203134Sthompsa	} else {		/* 5GHz */
3516208019Sthompsa		tmp |= RT2860_PA_PE_A0_EN;
3517203134Sthompsa		if (sc->ntxchains > 1)
3518203134Sthompsa			tmp |= RT2860_PA_PE_A1_EN;
3519203134Sthompsa	}
3520205042Sthompsa	if (sc->mac_ver == 0x3572) {
3521205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x00);
3522205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3523205042Sthompsa		run_rt3070_rf_write(sc, 8, 0x80);
3524205042Sthompsa	} else
3525205042Sthompsa		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3526203134Sthompsa
3527203134Sthompsa	/* set initial AGC value */
3528205042Sthompsa	if (group == 0) {	/* 2GHz band */
3529205042Sthompsa		if (sc->mac_ver >= 0x3070)
3530205042Sthompsa			agc = 0x1c + sc->lna[0] * 2;
3531205042Sthompsa		else
3532205042Sthompsa			agc = 0x2e + sc->lna[0];
3533205042Sthompsa	} else {		/* 5GHz band */
3534205042Sthompsa		if (sc->mac_ver == 0x3572)
3535205042Sthompsa			agc = 0x22 + (sc->lna[group] * 5) / 3;
3536205042Sthompsa		else
3537205042Sthompsa			agc = 0x32 + (sc->lna[group] * 5) / 3;
3538205042Sthompsa	}
3539205042Sthompsa	run_set_agc(sc, agc);
3540203134Sthompsa}
3541203134Sthompsa
3542203134Sthompsastatic void
3543203134Sthompsarun_rt2870_set_chan(struct run_softc *sc, uint32_t chan)
3544203134Sthompsa{
3545203134Sthompsa	const struct rfprog *rfprog = rt2860_rf2850;
3546203134Sthompsa	uint32_t r2, r3, r4;
3547203134Sthompsa	int8_t txpow1, txpow2;
3548203134Sthompsa	int i;
3549203134Sthompsa
3550203134Sthompsa	/* find the settings for this channel (we know it exists) */
3551203134Sthompsa	for (i = 0; rfprog[i].chan != chan; i++);
3552203134Sthompsa
3553203134Sthompsa	r2 = rfprog[i].r2;
3554203134Sthompsa	if (sc->ntxchains == 1)
3555203134Sthompsa		r2 |= 1 << 12;		/* 1T: disable Tx chain 2 */
3556203134Sthompsa	if (sc->nrxchains == 1)
3557203134Sthompsa		r2 |= 1 << 15 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3558203134Sthompsa	else if (sc->nrxchains == 2)
3559203134Sthompsa		r2 |= 1 << 4;		/* 2R: disable Rx chain 3 */
3560203134Sthompsa
3561203134Sthompsa	/* use Tx power values from EEPROM */
3562203134Sthompsa	txpow1 = sc->txpow1[i];
3563203134Sthompsa	txpow2 = sc->txpow2[i];
3564203134Sthompsa	if (chan > 14) {
3565203134Sthompsa		if (txpow1 >= 0)
3566208019Sthompsa			txpow1 = txpow1 << 1 | 1;
3567203134Sthompsa		else
3568208019Sthompsa			txpow1 = (7 + txpow1) << 1;
3569203134Sthompsa		if (txpow2 >= 0)
3570208019Sthompsa			txpow2 = txpow2 << 1 | 1;
3571203134Sthompsa		else
3572208019Sthompsa			txpow2 = (7 + txpow2) << 1;
3573203134Sthompsa	}
3574203134Sthompsa	r3 = rfprog[i].r3 | txpow1 << 7;
3575203134Sthompsa	r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3576203134Sthompsa
3577203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3578203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3579203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3580203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3581203134Sthompsa
3582203134Sthompsa	run_delay(sc, 10);
3583203134Sthompsa
3584203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3585203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3586203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3587203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3588203134Sthompsa
3589203134Sthompsa	run_delay(sc, 10);
3590203134Sthompsa
3591203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3592203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3593203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3594203134Sthompsa	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3595203134Sthompsa}
3596203134Sthompsa
3597203134Sthompsastatic void
3598203134Sthompsarun_rt3070_set_chan(struct run_softc *sc, uint32_t chan)
3599203134Sthompsa{
3600203134Sthompsa	int8_t txpow1, txpow2;
3601203134Sthompsa	uint8_t rf;
3602205042Sthompsa	int i;
3603203134Sthompsa
3604203134Sthompsa	/* RT3070 is 2GHz only */
3605203134Sthompsa	KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3606203134Sthompsa
3607205042Sthompsa	/* find the settings for this channel (we know it exists) */
3608205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3609205042Sthompsa
3610203134Sthompsa	/* use Tx power values from EEPROM */
3611205042Sthompsa	txpow1 = sc->txpow1[i];
3612205042Sthompsa	txpow2 = sc->txpow2[i];
3613203134Sthompsa
3614205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3615205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3616203134Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3617205042Sthompsa	rf = (rf & ~0x03) | rt3070_freqs[i].r;
3618203134Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3619203134Sthompsa
3620203134Sthompsa	/* set Tx0 power */
3621203134Sthompsa	run_rt3070_rf_read(sc, 12, &rf);
3622203134Sthompsa	rf = (rf & ~0x1f) | txpow1;
3623203134Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3624203134Sthompsa
3625203134Sthompsa	/* set Tx1 power */
3626203134Sthompsa	run_rt3070_rf_read(sc, 13, &rf);
3627203134Sthompsa	rf = (rf & ~0x1f) | txpow2;
3628203134Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3629203134Sthompsa
3630203134Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3631203134Sthompsa	rf &= ~0xfc;
3632203134Sthompsa	if (sc->ntxchains == 1)
3633203134Sthompsa		rf |= 1 << 7 | 1 << 5;	/* 1T: disable Tx chains 2 & 3 */
3634203134Sthompsa	else if (sc->ntxchains == 2)
3635203134Sthompsa		rf |= 1 << 7;		/* 2T: disable Tx chain 3 */
3636203134Sthompsa	if (sc->nrxchains == 1)
3637203134Sthompsa		rf |= 1 << 6 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3638203134Sthompsa	else if (sc->nrxchains == 2)
3639203134Sthompsa		rf |= 1 << 6;		/* 2R: disable Rx chain 3 */
3640203134Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3641203134Sthompsa
3642203134Sthompsa	/* set RF offset */
3643203134Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3644203134Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3645203134Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3646203134Sthompsa
3647203134Sthompsa	/* program RF filter */
3648205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf);	/* Tx */
3649205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3650205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);
3651205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);	/* Rx */
3652205042Sthompsa	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3653205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);
3654203134Sthompsa
3655203134Sthompsa	/* enable RF tuning */
3656203134Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3657203134Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3658203134Sthompsa}
3659203134Sthompsa
3660203134Sthompsastatic void
3661205042Sthompsarun_rt3572_set_chan(struct run_softc *sc, u_int chan)
3662205042Sthompsa{
3663205042Sthompsa	int8_t txpow1, txpow2;
3664205042Sthompsa	uint32_t tmp;
3665205042Sthompsa	uint8_t rf;
3666205042Sthompsa	int i;
3667205042Sthompsa
3668205042Sthompsa	/* find the settings for this channel (we know it exists) */
3669205042Sthompsa	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3670205042Sthompsa
3671205042Sthompsa	/* use Tx power values from EEPROM */
3672205042Sthompsa	txpow1 = sc->txpow1[i];
3673205042Sthompsa	txpow2 = sc->txpow2[i];
3674205042Sthompsa
3675205042Sthompsa	if (chan <= 14) {
3676205042Sthompsa		run_bbp_write(sc, 25, sc->bbp25);
3677205042Sthompsa		run_bbp_write(sc, 26, sc->bbp26);
3678205042Sthompsa	} else {
3679205042Sthompsa		/* enable IQ phase correction */
3680205042Sthompsa		run_bbp_write(sc, 25, 0x09);
3681205042Sthompsa		run_bbp_write(sc, 26, 0xff);
3682205042Sthompsa	}
3683205042Sthompsa
3684205042Sthompsa	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3685205042Sthompsa	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3686205042Sthompsa	run_rt3070_rf_read(sc, 6, &rf);
3687205042Sthompsa	rf  = (rf & ~0x0f) | rt3070_freqs[i].r;
3688205042Sthompsa	rf |= (chan <= 14) ? 0x08 : 0x04;
3689205042Sthompsa	run_rt3070_rf_write(sc, 6, rf);
3690205042Sthompsa
3691205042Sthompsa	/* set PLL mode */
3692205042Sthompsa	run_rt3070_rf_read(sc, 5, &rf);
3693205042Sthompsa	rf &= ~(0x08 | 0x04);
3694205042Sthompsa	rf |= (chan <= 14) ? 0x04 : 0x08;
3695205042Sthompsa	run_rt3070_rf_write(sc, 5, rf);
3696205042Sthompsa
3697205042Sthompsa	/* set Tx power for chain 0 */
3698205042Sthompsa	if (chan <= 14)
3699205042Sthompsa		rf = 0x60 | txpow1;
3700205042Sthompsa	else
3701205042Sthompsa		rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3702205042Sthompsa	run_rt3070_rf_write(sc, 12, rf);
3703205042Sthompsa
3704205042Sthompsa	/* set Tx power for chain 1 */
3705205042Sthompsa	if (chan <= 14)
3706205042Sthompsa		rf = 0x60 | txpow2;
3707205042Sthompsa	else
3708205042Sthompsa		rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3709205042Sthompsa	run_rt3070_rf_write(sc, 13, rf);
3710205042Sthompsa
3711205042Sthompsa	/* set Tx/Rx streams */
3712205042Sthompsa	run_rt3070_rf_read(sc, 1, &rf);
3713205042Sthompsa	rf &= ~0xfc;
3714205042Sthompsa	if (sc->ntxchains == 1)
3715205042Sthompsa		rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3716205042Sthompsa	else if (sc->ntxchains == 2)
3717205042Sthompsa		rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3718205042Sthompsa	if (sc->nrxchains == 1)
3719205042Sthompsa		rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3720205042Sthompsa	else if (sc->nrxchains == 2)
3721205042Sthompsa		rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3722205042Sthompsa	run_rt3070_rf_write(sc, 1, rf);
3723205042Sthompsa
3724205042Sthompsa	/* set RF offset */
3725205042Sthompsa	run_rt3070_rf_read(sc, 23, &rf);
3726205042Sthompsa	rf = (rf & ~0x7f) | sc->freq;
3727205042Sthompsa	run_rt3070_rf_write(sc, 23, rf);
3728205042Sthompsa
3729205042Sthompsa	/* program RF filter */
3730205042Sthompsa	rf = sc->rf24_20mhz;
3731205042Sthompsa	run_rt3070_rf_write(sc, 24, rf);	/* Tx */
3732205042Sthompsa	run_rt3070_rf_write(sc, 31, rf);	/* Rx */
3733205042Sthompsa
3734205042Sthompsa	/* enable RF tuning */
3735205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3736205042Sthompsa	rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3737205042Sthompsa	run_rt3070_rf_write(sc, 7, rf);
3738205042Sthompsa
3739205042Sthompsa	/* TSSI */
3740205042Sthompsa	rf = (chan <= 14) ? 0xc3 : 0xc0;
3741205042Sthompsa	run_rt3070_rf_write(sc, 9, rf);
3742205042Sthompsa
3743205042Sthompsa	/* set loop filter 1 */
3744205042Sthompsa	run_rt3070_rf_write(sc, 10, 0xf1);
3745205042Sthompsa	/* set loop filter 2 */
3746205042Sthompsa	run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3747205042Sthompsa
3748205042Sthompsa	/* set tx_mx2_ic */
3749205042Sthompsa	run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3750205042Sthompsa	/* set tx_mx1_ic */
3751205042Sthompsa	if (chan <= 14)
3752205042Sthompsa		rf = 0x48 | sc->txmixgain_2ghz;
3753205042Sthompsa	else
3754205042Sthompsa		rf = 0x78 | sc->txmixgain_5ghz;
3755205042Sthompsa	run_rt3070_rf_write(sc, 16, rf);
3756205042Sthompsa
3757205042Sthompsa	/* set tx_lo1 */
3758205042Sthompsa	run_rt3070_rf_write(sc, 17, 0x23);
3759205042Sthompsa	/* set tx_lo2 */
3760205042Sthompsa	if (chan <= 14)
3761205042Sthompsa		rf = 0x93;
3762205042Sthompsa	else if (chan <= 64)
3763205042Sthompsa		rf = 0xb7;
3764205042Sthompsa	else if (chan <= 128)
3765205042Sthompsa		rf = 0x74;
3766205042Sthompsa	else
3767205042Sthompsa		rf = 0x72;
3768205042Sthompsa	run_rt3070_rf_write(sc, 19, rf);
3769205042Sthompsa
3770205042Sthompsa	/* set rx_lo1 */
3771205042Sthompsa	if (chan <= 14)
3772205042Sthompsa		rf = 0xb3;
3773205042Sthompsa	else if (chan <= 64)
3774205042Sthompsa		rf = 0xf6;
3775205042Sthompsa	else if (chan <= 128)
3776205042Sthompsa		rf = 0xf4;
3777205042Sthompsa	else
3778205042Sthompsa		rf = 0xf3;
3779205042Sthompsa	run_rt3070_rf_write(sc, 20, rf);
3780205042Sthompsa
3781205042Sthompsa	/* set pfd_delay */
3782205042Sthompsa	if (chan <= 14)
3783205042Sthompsa		rf = 0x15;
3784205042Sthompsa	else if (chan <= 64)
3785205042Sthompsa		rf = 0x3d;
3786205042Sthompsa	else
3787205042Sthompsa		rf = 0x01;
3788205042Sthompsa	run_rt3070_rf_write(sc, 25, rf);
3789205042Sthompsa
3790205042Sthompsa	/* set rx_lo2 */
3791205042Sthompsa	run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3792205042Sthompsa	/* set ldo_rf_vc */
3793205042Sthompsa	run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3794205042Sthompsa	/* set drv_cc */
3795205042Sthompsa	run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3796205042Sthompsa
3797205042Sthompsa	run_read(sc, RT2860_GPIO_CTRL, &tmp);
3798205042Sthompsa	tmp &= ~0x8080;
3799205042Sthompsa	if (chan <= 14)
3800205042Sthompsa		tmp |= 0x80;
3801205042Sthompsa	run_write(sc, RT2860_GPIO_CTRL, tmp);
3802205042Sthompsa
3803205042Sthompsa	/* enable RF tuning */
3804205042Sthompsa	run_rt3070_rf_read(sc, 7, &rf);
3805205042Sthompsa	run_rt3070_rf_write(sc, 7, rf | 0x01);
3806205042Sthompsa
3807205042Sthompsa	run_delay(sc, 2);
3808205042Sthompsa}
3809205042Sthompsa
3810205042Sthompsastatic void
3811203134Sthompsarun_set_rx_antenna(struct run_softc *sc, int aux)
3812203134Sthompsa{
3813203134Sthompsa	uint32_t tmp;
3814203134Sthompsa
3815203134Sthompsa	if (aux) {
3816205042Sthompsa		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
3817203134Sthompsa		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3818203134Sthompsa		run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
3819203134Sthompsa	} else {
3820205042Sthompsa		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
3821203134Sthompsa		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3822203134Sthompsa		run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
3823203134Sthompsa	}
3824203134Sthompsa}
3825203134Sthompsa
3826203134Sthompsastatic int
3827203134Sthompsarun_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
3828203134Sthompsa{
3829203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3830203134Sthompsa	uint32_t chan, group;
3831203134Sthompsa
3832203134Sthompsa	chan = ieee80211_chan2ieee(ic, c);
3833203134Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
3834209917Sthompsa		return (EINVAL);
3835203134Sthompsa
3836205042Sthompsa	if (sc->mac_ver == 0x3572)
3837205042Sthompsa		run_rt3572_set_chan(sc, chan);
3838205042Sthompsa	else if (sc->mac_ver >= 0x3070)
3839203134Sthompsa		run_rt3070_set_chan(sc, chan);
3840203134Sthompsa	else
3841203134Sthompsa		run_rt2870_set_chan(sc, chan);
3842203134Sthompsa
3843203134Sthompsa	/* determine channel group */
3844203134Sthompsa	if (chan <= 14)
3845203134Sthompsa		group = 0;
3846203134Sthompsa	else if (chan <= 64)
3847203134Sthompsa		group = 1;
3848203134Sthompsa	else if (chan <= 128)
3849203134Sthompsa		group = 2;
3850203134Sthompsa	else
3851203134Sthompsa		group = 3;
3852203134Sthompsa
3853203134Sthompsa	/* XXX necessary only when group has changed! */
3854203134Sthompsa	run_select_chan_group(sc, group);
3855203134Sthompsa
3856203134Sthompsa	run_delay(sc, 10);
3857203134Sthompsa
3858209917Sthompsa	return (0);
3859203134Sthompsa}
3860203134Sthompsa
3861203134Sthompsastatic void
3862203134Sthompsarun_set_channel(struct ieee80211com *ic)
3863203134Sthompsa{
3864203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3865203134Sthompsa
3866203134Sthompsa	RUN_LOCK(sc);
3867203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
3868203134Sthompsa	RUN_UNLOCK(sc);
3869203134Sthompsa
3870203134Sthompsa	return;
3871203134Sthompsa}
3872203134Sthompsa
3873203134Sthompsastatic void
3874203134Sthompsarun_scan_start(struct ieee80211com *ic)
3875203134Sthompsa{
3876203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3877203134Sthompsa	uint32_t tmp;
3878203134Sthompsa
3879203134Sthompsa	RUN_LOCK(sc);
3880203134Sthompsa
3881203134Sthompsa	/* abort TSF synchronization */
3882203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
3883203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG,
3884203134Sthompsa	    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
3885203134Sthompsa	    RT2860_TBTT_TIMER_EN));
3886203134Sthompsa	run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
3887203134Sthompsa
3888203134Sthompsa	RUN_UNLOCK(sc);
3889203134Sthompsa
3890203134Sthompsa	return;
3891203134Sthompsa}
3892203134Sthompsa
3893203134Sthompsastatic void
3894203134Sthompsarun_scan_end(struct ieee80211com *ic)
3895203134Sthompsa{
3896203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3897203134Sthompsa
3898203134Sthompsa	RUN_LOCK(sc);
3899203134Sthompsa
3900203134Sthompsa	run_enable_tsf_sync(sc);
3901203134Sthompsa	/* XXX keep local copy */
3902203134Sthompsa	run_set_bssid(sc, sc->sc_bssid);
3903203134Sthompsa
3904203134Sthompsa	RUN_UNLOCK(sc);
3905203134Sthompsa
3906203134Sthompsa	return;
3907203134Sthompsa}
3908203134Sthompsa
3909208019Sthompsa/*
3910208019Sthompsa * Could be called from ieee80211_node_timeout()
3911208019Sthompsa * (non-sleepable thread)
3912208019Sthompsa */
3913208019Sthompsastatic void
3914208019Sthompsarun_update_beacon(struct ieee80211vap *vap, int item)
3915203134Sthompsa{
3916208019Sthompsa	struct ieee80211com *ic = vap->iv_ic;
3917208019Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3918218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
3919218492Sbschmidt	int mcast = 0;
3920208019Sthompsa	uint32_t i;
3921208019Sthompsa
3922218492Sbschmidt	KASSERT(vap != NULL, ("no beacon"));
3923218492Sbschmidt
3924218492Sbschmidt	switch (item) {
3925218492Sbschmidt	case IEEE80211_BEACON_ERP:
3926218492Sbschmidt		run_updateslot(ic->ic_ifp);
3927218492Sbschmidt		break;
3928218492Sbschmidt	case IEEE80211_BEACON_HTINFO:
3929218492Sbschmidt		run_updateprot(ic);
3930218492Sbschmidt		break;
3931218492Sbschmidt	case IEEE80211_BEACON_TIM:
3932218492Sbschmidt		mcast = 1;	/*TODO*/
3933218492Sbschmidt		break;
3934218492Sbschmidt	default:
3935218492Sbschmidt		break;
3936218492Sbschmidt	}
3937218492Sbschmidt
3938218492Sbschmidt	setbit(rvp->bo.bo_flags, item);
3939218492Sbschmidt	ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
3940218492Sbschmidt
3941208019Sthompsa	i = RUN_CMDQ_GET(&sc->cmdq_store);
3942208019Sthompsa	DPRINTF("cmdq_store=%d\n", i);
3943208019Sthompsa	sc->cmdq[i].func = run_update_beacon_cb;
3944208019Sthompsa	sc->cmdq[i].arg0 = vap;
3945208019Sthompsa	ieee80211_runtask(ic, &sc->cmdq_task);
3946208019Sthompsa
3947208019Sthompsa	return;
3948203134Sthompsa}
3949203134Sthompsa
3950203134Sthompsastatic void
3951208019Sthompsarun_update_beacon_cb(void *arg)
3952203134Sthompsa{
3953208019Sthompsa	struct ieee80211vap *vap = arg;
3954218492Sbschmidt	struct run_vap *rvp = RUN_VAP(vap);
3955203134Sthompsa	struct ieee80211com *ic = vap->iv_ic;
3956203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
3957203134Sthompsa	struct rt2860_txwi txwi;
3958203134Sthompsa	struct mbuf *m;
3959208019Sthompsa	uint8_t ridx;
3960203134Sthompsa
3961209917Sthompsa	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
3962208019Sthompsa		return;
3963208019Sthompsa
3964218492Sbschmidt	/*
3965218492Sbschmidt	 * No need to call ieee80211_beacon_update(), run_update_beacon()
3966218492Sbschmidt	 * is taking care of apropriate calls.
3967218492Sbschmidt	 */
3968218492Sbschmidt	if (rvp->beacon_mbuf == NULL) {
3969218492Sbschmidt		rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
3970218492Sbschmidt		    &rvp->bo);
3971218492Sbschmidt		if (rvp->beacon_mbuf == NULL)
3972218492Sbschmidt			return;
3973218492Sbschmidt	}
3974218492Sbschmidt	m = rvp->beacon_mbuf;
3975203134Sthompsa
3976203134Sthompsa	memset(&txwi, 0, sizeof txwi);
3977203134Sthompsa	txwi.wcid = 0xff;
3978203134Sthompsa	txwi.len = htole16(m->m_pkthdr.len);
3979203134Sthompsa	/* send beacons at the lowest available rate */
3980208019Sthompsa	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3981208019Sthompsa	    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3982208019Sthompsa	txwi.phy = htole16(rt2860_rates[ridx].mcs);
3983208019Sthompsa	if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
3984203134Sthompsa	        txwi.phy |= htole16(RT2860_PHY_OFDM);
3985203134Sthompsa	txwi.txop = RT2860_TX_TXOP_HT;
3986203134Sthompsa	txwi.flags = RT2860_TX_TS;
3987209144Sthompsa	txwi.xflags = RT2860_TX_NSEQ;
3988203134Sthompsa
3989218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
3990208019Sthompsa	    (uint8_t *)&txwi, sizeof txwi);
3991218492Sbschmidt	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
3992203134Sthompsa	    mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);	/* roundup len */
3993203134Sthompsa
3994203134Sthompsa	return;
3995203134Sthompsa}
3996203134Sthompsa
3997203134Sthompsastatic void
3998203134Sthompsarun_updateprot(struct ieee80211com *ic)
3999203134Sthompsa{
4000203134Sthompsa	struct run_softc *sc = ic->ic_ifp->if_softc;
4001218492Sbschmidt	uint32_t i;
4002218492Sbschmidt
4003218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4004218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4005218492Sbschmidt	sc->cmdq[i].func = run_updateprot_cb;
4006218492Sbschmidt	sc->cmdq[i].arg0 = ic;
4007218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4008218492Sbschmidt}
4009218492Sbschmidt
4010218492Sbschmidtstatic void
4011218492Sbschmidtrun_updateprot_cb(void *arg)
4012218492Sbschmidt{
4013218492Sbschmidt	struct ieee80211com *ic = arg;
4014218492Sbschmidt	struct run_softc *sc = ic->ic_ifp->if_softc;
4015203134Sthompsa	uint32_t tmp;
4016203134Sthompsa
4017203134Sthompsa	tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4018203134Sthompsa	/* setup protection frame rate (MCS code) */
4019203134Sthompsa	tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4020203134Sthompsa	    rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4021203134Sthompsa	    rt2860_rates[RT2860_RIDX_CCK11].mcs;
4022203134Sthompsa
4023203134Sthompsa	/* CCK frames don't require protection */
4024203134Sthompsa	run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4025203134Sthompsa	if (ic->ic_flags & IEEE80211_F_USEPROT) {
4026203134Sthompsa		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4027203134Sthompsa			tmp |= RT2860_PROT_CTRL_RTS_CTS;
4028203134Sthompsa		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4029203134Sthompsa			tmp |= RT2860_PROT_CTRL_CTS;
4030203134Sthompsa	}
4031203134Sthompsa	run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4032203134Sthompsa}
4033203134Sthompsa
4034203134Sthompsastatic void
4035208019Sthompsarun_usb_timeout_cb(void *arg)
4036203134Sthompsa{
4037208019Sthompsa	struct ieee80211vap *vap = arg;
4038208019Sthompsa	struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4039203134Sthompsa
4040208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4041203134Sthompsa
4042203134Sthompsa	if(vap->iv_state == IEEE80211_S_RUN &&
4043203134Sthompsa	    vap->iv_opmode != IEEE80211_M_STA)
4044203134Sthompsa		run_reset_livelock(sc);
4045209917Sthompsa	else if (vap->iv_state == IEEE80211_S_SCAN) {
4046203134Sthompsa		DPRINTF("timeout caused by scan\n");
4047203134Sthompsa		/* cancel bgscan */
4048203134Sthompsa		ieee80211_cancel_scan(vap);
4049203134Sthompsa	} else
4050203134Sthompsa		DPRINTF("timeout by unknown cause\n");
4051203134Sthompsa}
4052203134Sthompsa
4053203134Sthompsastatic void
4054203134Sthompsarun_reset_livelock(struct run_softc *sc)
4055203134Sthompsa{
4056203134Sthompsa	uint32_t tmp;
4057203134Sthompsa
4058208019Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4059208019Sthompsa
4060203134Sthompsa	/*
4061203134Sthompsa	 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4062203134Sthompsa	 * can run into a livelock and start sending CTS-to-self frames like
4063203134Sthompsa	 * crazy if protection is enabled.  Reset MAC/BBP for a while
4064203134Sthompsa	 */
4065203134Sthompsa	run_read(sc, RT2860_DEBUG, &tmp);
4066208019Sthompsa	DPRINTFN(3, "debug reg %08x\n", tmp);
4067209917Sthompsa	if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4068203134Sthompsa		DPRINTF("CTS-to-self livelock detected\n");
4069203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4070203134Sthompsa		run_delay(sc, 1);
4071203134Sthompsa		run_write(sc, RT2860_MAC_SYS_CTRL,
4072203134Sthompsa		    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4073203134Sthompsa	}
4074203134Sthompsa}
4075203134Sthompsa
4076203134Sthompsastatic void
4077203134Sthompsarun_update_promisc_locked(struct ifnet *ifp)
4078203134Sthompsa{
4079203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4080203134Sthompsa        uint32_t tmp;
4081203134Sthompsa
4082203134Sthompsa	run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4083203134Sthompsa
4084203134Sthompsa	tmp |= RT2860_DROP_UC_NOME;
4085203134Sthompsa        if (ifp->if_flags & IFF_PROMISC)
4086203134Sthompsa		tmp &= ~RT2860_DROP_UC_NOME;
4087203134Sthompsa
4088203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4089203134Sthompsa
4090203134Sthompsa        DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4091203134Sthompsa            "entering" : "leaving");
4092203134Sthompsa}
4093203134Sthompsa
4094203134Sthompsastatic void
4095203134Sthompsarun_update_promisc(struct ifnet *ifp)
4096203134Sthompsa{
4097203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4098203134Sthompsa
4099203134Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4100203134Sthompsa		return;
4101203134Sthompsa
4102203134Sthompsa	RUN_LOCK(sc);
4103203134Sthompsa	run_update_promisc_locked(ifp);
4104203134Sthompsa	RUN_UNLOCK(sc);
4105203134Sthompsa}
4106203134Sthompsa
4107203134Sthompsastatic void
4108203134Sthompsarun_enable_tsf_sync(struct run_softc *sc)
4109203134Sthompsa{
4110208019Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4111203134Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4112203134Sthompsa	uint32_t tmp;
4113203134Sthompsa
4114208019Sthompsa	DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode);
4115208019Sthompsa
4116203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4117203134Sthompsa	tmp &= ~0x1fffff;
4118203134Sthompsa	tmp |= vap->iv_bss->ni_intval * 16;
4119203134Sthompsa	tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4120203134Sthompsa
4121208019Sthompsa	if (ic->ic_opmode == IEEE80211_M_STA) {
4122203134Sthompsa		/*
4123203134Sthompsa		 * Local TSF is always updated with remote TSF on beacon
4124203134Sthompsa		 * reception.
4125203134Sthompsa		 */
4126203134Sthompsa		tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4127208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4128203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4129203134Sthompsa	        /*
4130203134Sthompsa	         * Local TSF is updated with remote TSF on beacon reception
4131203134Sthompsa	         * only if the remote TSF is greater than local TSF.
4132203134Sthompsa	         */
4133203134Sthompsa	        tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4134208019Sthompsa	} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4135208019Sthompsa		    ic->ic_opmode == IEEE80211_M_MBSS) {
4136203134Sthompsa	        tmp |= RT2860_BCN_TX_EN;
4137203134Sthompsa	        /* SYNC with nobody */
4138203134Sthompsa	        tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4139208019Sthompsa	} else {
4140203134Sthompsa		DPRINTF("Enabling TSF failed. undefined opmode\n");
4141208019Sthompsa		return;
4142208019Sthompsa	}
4143203134Sthompsa
4144203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4145203134Sthompsa}
4146203134Sthompsa
4147203134Sthompsastatic void
4148203134Sthompsarun_enable_mrr(struct run_softc *sc)
4149203134Sthompsa{
4150203134Sthompsa#define CCK(mcs)	(mcs)
4151203134Sthompsa#define OFDM(mcs)	(1 << 3 | (mcs))
4152203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG0,
4153203134Sthompsa	    OFDM(6) << 28 |	/* 54->48 */
4154203134Sthompsa	    OFDM(5) << 24 |	/* 48->36 */
4155203134Sthompsa	    OFDM(4) << 20 |	/* 36->24 */
4156203134Sthompsa	    OFDM(3) << 16 |	/* 24->18 */
4157203134Sthompsa	    OFDM(2) << 12 |	/* 18->12 */
4158203134Sthompsa	    OFDM(1) <<  8 |	/* 12-> 9 */
4159203134Sthompsa	    OFDM(0) <<  4 |	/*  9-> 6 */
4160203134Sthompsa	    OFDM(0));		/*  6-> 6 */
4161203134Sthompsa
4162203134Sthompsa	run_write(sc, RT2860_LG_FBK_CFG1,
4163203134Sthompsa	    CCK(2) << 12 |	/* 11->5.5 */
4164203134Sthompsa	    CCK(1) <<  8 |	/* 5.5-> 2 */
4165203134Sthompsa	    CCK(0) <<  4 |	/*   2-> 1 */
4166203134Sthompsa	    CCK(0));		/*   1-> 1 */
4167203134Sthompsa#undef OFDM
4168203134Sthompsa#undef CCK
4169203134Sthompsa}
4170203134Sthompsa
4171203134Sthompsastatic void
4172203134Sthompsarun_set_txpreamble(struct run_softc *sc)
4173203134Sthompsa{
4174203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4175203134Sthompsa	uint32_t tmp;
4176203134Sthompsa
4177203134Sthompsa	run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4178203134Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4179203134Sthompsa		tmp |= RT2860_CCK_SHORT_EN;
4180203134Sthompsa	else
4181203134Sthompsa		tmp &= ~RT2860_CCK_SHORT_EN;
4182203134Sthompsa	run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4183203134Sthompsa}
4184203134Sthompsa
4185203134Sthompsastatic void
4186203134Sthompsarun_set_basicrates(struct run_softc *sc)
4187203134Sthompsa{
4188203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4189203134Sthompsa
4190203134Sthompsa	/* set basic rates mask */
4191203134Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
4192203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4193203134Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
4194203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4195203134Sthompsa	else	/* 11g */
4196203134Sthompsa		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4197203134Sthompsa}
4198203134Sthompsa
4199203134Sthompsastatic void
4200203134Sthompsarun_set_leds(struct run_softc *sc, uint16_t which)
4201203134Sthompsa{
4202203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4203203134Sthompsa	    which | (sc->leds & 0x7f));
4204203134Sthompsa}
4205203134Sthompsa
4206203134Sthompsastatic void
4207203134Sthompsarun_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4208203134Sthompsa{
4209203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW0,
4210203134Sthompsa	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4211203134Sthompsa	run_write(sc, RT2860_MAC_BSSID_DW1,
4212203134Sthompsa	    bssid[4] | bssid[5] << 8);
4213203134Sthompsa}
4214203134Sthompsa
4215203134Sthompsastatic void
4216203134Sthompsarun_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4217203134Sthompsa{
4218203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW0,
4219203134Sthompsa	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4220203134Sthompsa	run_write(sc, RT2860_MAC_ADDR_DW1,
4221203134Sthompsa	    addr[4] | addr[5] << 8 | 0xff << 16);
4222203134Sthompsa}
4223203134Sthompsa
4224203134Sthompsastatic void
4225203134Sthompsarun_updateslot(struct ifnet *ifp)
4226203134Sthompsa{
4227203134Sthompsa	struct run_softc *sc = ifp->if_softc;
4228203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4229218492Sbschmidt	uint32_t i;
4230218492Sbschmidt
4231218492Sbschmidt	i = RUN_CMDQ_GET(&sc->cmdq_store);
4232218492Sbschmidt	DPRINTF("cmdq_store=%d\n", i);
4233218492Sbschmidt	sc->cmdq[i].func = run_updateslot_cb;
4234218492Sbschmidt	sc->cmdq[i].arg0 = ifp;
4235218492Sbschmidt	ieee80211_runtask(ic, &sc->cmdq_task);
4236218492Sbschmidt
4237218492Sbschmidt	return;
4238218492Sbschmidt}
4239218492Sbschmidt
4240218492Sbschmidt/* ARGSUSED */
4241218492Sbschmidtstatic void
4242218492Sbschmidtrun_updateslot_cb(void *arg)
4243218492Sbschmidt{
4244218492Sbschmidt	struct ifnet *ifp = arg;
4245218492Sbschmidt	struct run_softc *sc = ifp->if_softc;
4246218492Sbschmidt	struct ieee80211com *ic = ifp->if_l2com;
4247203134Sthompsa	uint32_t tmp;
4248203134Sthompsa
4249203134Sthompsa	run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4250203134Sthompsa	tmp &= ~0xff;
4251203134Sthompsa	tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4252203134Sthompsa	run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4253203134Sthompsa}
4254203134Sthompsa
4255208019Sthompsastatic void
4256208019Sthompsarun_update_mcast(struct ifnet *ifp)
4257208019Sthompsa{
4258208019Sthompsa	/* h/w filter supports getting everything or nothing */
4259208019Sthompsa	ifp->if_flags |= IFF_ALLMULTI;
4260208019Sthompsa}
4261208019Sthompsa
4262203134Sthompsastatic int8_t
4263203134Sthompsarun_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4264203134Sthompsa{
4265203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4266203134Sthompsa	struct ieee80211_channel *c = ic->ic_curchan;
4267203134Sthompsa	int delta;
4268203134Sthompsa
4269203134Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
4270203134Sthompsa		uint32_t chan = ieee80211_chan2ieee(ic, c);
4271203134Sthompsa		delta = sc->rssi_5ghz[rxchain];
4272203134Sthompsa
4273203134Sthompsa		/* determine channel group */
4274203134Sthompsa		if (chan <= 64)
4275203134Sthompsa			delta -= sc->lna[1];
4276203134Sthompsa		else if (chan <= 128)
4277203134Sthompsa			delta -= sc->lna[2];
4278203134Sthompsa		else
4279203134Sthompsa			delta -= sc->lna[3];
4280203134Sthompsa	} else
4281203134Sthompsa		delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4282203134Sthompsa
4283209917Sthompsa	return (-12 - delta - rssi);
4284203134Sthompsa}
4285203134Sthompsa
4286203134Sthompsastatic int
4287203134Sthompsarun_bbp_init(struct run_softc *sc)
4288203134Sthompsa{
4289203134Sthompsa	int i, error, ntries;
4290203134Sthompsa	uint8_t bbp0;
4291203134Sthompsa
4292203134Sthompsa	/* wait for BBP to wake up */
4293203134Sthompsa	for (ntries = 0; ntries < 20; ntries++) {
4294203134Sthompsa		if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4295203134Sthompsa			return error;
4296203134Sthompsa		if (bbp0 != 0 && bbp0 != 0xff)
4297203134Sthompsa			break;
4298203134Sthompsa	}
4299203134Sthompsa	if (ntries == 20)
4300209917Sthompsa		return (ETIMEDOUT);
4301203134Sthompsa
4302203134Sthompsa	/* initialize BBP registers to default values */
4303203134Sthompsa	for (i = 0; i < nitems(rt2860_def_bbp); i++) {
4304203134Sthompsa		run_bbp_write(sc, rt2860_def_bbp[i].reg,
4305203134Sthompsa		    rt2860_def_bbp[i].val);
4306203134Sthompsa	}
4307203134Sthompsa
4308203134Sthompsa	/* fix BBP84 for RT2860E */
4309205042Sthompsa	if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4310205042Sthompsa		run_bbp_write(sc, 84, 0x19);
4311203134Sthompsa
4312205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4313203134Sthompsa		run_bbp_write(sc, 79, 0x13);
4314203134Sthompsa		run_bbp_write(sc, 80, 0x05);
4315203134Sthompsa		run_bbp_write(sc, 81, 0x33);
4316205042Sthompsa	} else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4317203134Sthompsa		run_bbp_write(sc, 69, 0x16);
4318203134Sthompsa		run_bbp_write(sc, 73, 0x12);
4319203134Sthompsa	}
4320209917Sthompsa	return (0);
4321203134Sthompsa}
4322203134Sthompsa
4323203134Sthompsastatic int
4324203134Sthompsarun_rt3070_rf_init(struct run_softc *sc)
4325203134Sthompsa{
4326203134Sthompsa	uint32_t tmp;
4327205042Sthompsa	uint8_t rf, target, bbp4;
4328203134Sthompsa	int i;
4329203134Sthompsa
4330203134Sthompsa	run_rt3070_rf_read(sc, 30, &rf);
4331203134Sthompsa	/* toggle RF R30 bit 7 */
4332203134Sthompsa	run_rt3070_rf_write(sc, 30, rf | 0x80);
4333203134Sthompsa	run_delay(sc, 10);
4334203134Sthompsa	run_rt3070_rf_write(sc, 30, rf & ~0x80);
4335203134Sthompsa
4336203134Sthompsa	/* initialize RF registers to default value */
4337205042Sthompsa	if (sc->mac_ver == 0x3572) {
4338205042Sthompsa		for (i = 0; i < nitems(rt3572_def_rf); i++) {
4339205042Sthompsa			run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4340205042Sthompsa			    rt3572_def_rf[i].val);
4341205042Sthompsa		}
4342205042Sthompsa	} else {
4343205042Sthompsa		for (i = 0; i < nitems(rt3070_def_rf); i++) {
4344205042Sthompsa			run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4345205042Sthompsa			    rt3070_def_rf[i].val);
4346205042Sthompsa		}
4347203134Sthompsa	}
4348205042Sthompsa
4349205042Sthompsa	if (sc->mac_ver == 0x3070) {
4350203134Sthompsa		/* change voltage from 1.2V to 1.35V for RT3070 */
4351203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4352203134Sthompsa		tmp = (tmp & ~0x0f000000) | 0x0d000000;
4353203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4354203134Sthompsa
4355205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4356203134Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4357203134Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4358203134Sthompsa		run_rt3070_rf_write(sc, 31, 0x14);
4359203134Sthompsa
4360203134Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4361203134Sthompsa		tmp &= ~0x1f000000;
4362205042Sthompsa		if (sc->mac_rev < 0x0211)
4363205042Sthompsa			tmp |= 0x0d000000;	/* 1.3V */
4364203134Sthompsa		else
4365205042Sthompsa			tmp |= 0x01000000;	/* 1.2V */
4366203134Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4367203134Sthompsa
4368203134Sthompsa		/* patch LNA_PE_G1 */
4369203134Sthompsa		run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4370203134Sthompsa		run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4371208019Sthompsa
4372209917Sthompsa	} else if (sc->mac_ver == 0x3572) {
4373205042Sthompsa		run_rt3070_rf_read(sc, 6, &rf);
4374205042Sthompsa		run_rt3070_rf_write(sc, 6, rf | 0x40);
4375205042Sthompsa
4376208019Sthompsa		/* increase voltage from 1.2V to 1.35V */
4377208019Sthompsa		run_read(sc, RT3070_LDO_CFG0, &tmp);
4378208019Sthompsa		tmp = (tmp & ~0x1f000000) | 0x0d000000;
4379208019Sthompsa		run_write(sc, RT3070_LDO_CFG0, tmp);
4380203134Sthompsa
4381209917Sthompsa		if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4382203134Sthompsa			run_delay(sc, 1);	/* wait for 1msec */
4383205042Sthompsa			/* decrease voltage back to 1.2V */
4384203134Sthompsa			tmp = (tmp & ~0x1f000000) | 0x01000000;
4385203134Sthompsa			run_write(sc, RT3070_LDO_CFG0, tmp);
4386203134Sthompsa		}
4387203134Sthompsa	}
4388203134Sthompsa
4389203134Sthompsa	/* select 20MHz bandwidth */
4390203134Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4391203134Sthompsa	run_rt3070_rf_write(sc, 31, rf & ~0x20);
4392203134Sthompsa
4393203134Sthompsa	/* calibrate filter for 20MHz bandwidth */
4394203134Sthompsa	sc->rf24_20mhz = 0x1f;	/* default value */
4395205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4396205042Sthompsa	run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4397203134Sthompsa
4398203134Sthompsa	/* select 40MHz bandwidth */
4399203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4400203134Sthompsa	run_bbp_write(sc, 4, (bbp4 & ~0x08) | 0x10);
4401205042Sthompsa	run_rt3070_rf_read(sc, 31, &rf);
4402205042Sthompsa	run_rt3070_rf_write(sc, 31, rf | 0x20);
4403203134Sthompsa
4404203134Sthompsa	/* calibrate filter for 40MHz bandwidth */
4405203134Sthompsa	sc->rf24_40mhz = 0x2f;	/* default value */
4406205042Sthompsa	target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4407205042Sthompsa	run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4408203134Sthompsa
4409203134Sthompsa	/* go back to 20MHz bandwidth */
4410203134Sthompsa	run_bbp_read(sc, 4, &bbp4);
4411203134Sthompsa	run_bbp_write(sc, 4, bbp4 & ~0x18);
4412203134Sthompsa
4413205042Sthompsa	if (sc->mac_ver == 0x3572) {
4414205042Sthompsa		/* save default BBP registers 25 and 26 values */
4415205042Sthompsa		run_bbp_read(sc, 25, &sc->bbp25);
4416205042Sthompsa		run_bbp_read(sc, 26, &sc->bbp26);
4417205042Sthompsa	} else if (sc->mac_rev < 0x0211)
4418203134Sthompsa		run_rt3070_rf_write(sc, 27, 0x03);
4419203134Sthompsa
4420203134Sthompsa	run_read(sc, RT3070_OPT_14, &tmp);
4421203134Sthompsa	run_write(sc, RT3070_OPT_14, tmp | 1);
4422203134Sthompsa
4423205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4424205042Sthompsa		run_rt3070_rf_read(sc, 17, &rf);
4425205042Sthompsa		rf &= ~RT3070_TX_LO1;
4426205042Sthompsa		if ((sc->mac_ver == 0x3070 ||
4427205042Sthompsa		     (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4428205042Sthompsa		    !sc->ext_2ghz_lna)
4429205042Sthompsa			rf |= 0x20;	/* fix for long range Rx issue */
4430205042Sthompsa		if (sc->txmixgain_2ghz >= 1)
4431205042Sthompsa			rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4432205042Sthompsa		run_rt3070_rf_write(sc, 17, rf);
4433205042Sthompsa	}
4434205042Sthompsa
4435205042Sthompsa	if (sc->mac_rev == 0x3071) {
4436203134Sthompsa		run_rt3070_rf_read(sc, 1, &rf);
4437203134Sthompsa		rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4438203134Sthompsa		rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4439203134Sthompsa		run_rt3070_rf_write(sc, 1, rf);
4440203134Sthompsa
4441203134Sthompsa		run_rt3070_rf_read(sc, 15, &rf);
4442203134Sthompsa		run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4443203134Sthompsa
4444203134Sthompsa		run_rt3070_rf_read(sc, 20, &rf);
4445203134Sthompsa		run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4446203134Sthompsa
4447203134Sthompsa		run_rt3070_rf_read(sc, 21, &rf);
4448203134Sthompsa		run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4449205042Sthompsa	}
4450203134Sthompsa
4451205042Sthompsa	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4452205042Sthompsa		/* fix Tx to Rx IQ glitch by raising RF voltage */
4453203134Sthompsa		run_rt3070_rf_read(sc, 27, &rf);
4454203134Sthompsa		rf &= ~0x77;
4455205042Sthompsa		if (sc->mac_rev < 0x0211)
4456203134Sthompsa			rf |= 0x03;
4457203134Sthompsa		run_rt3070_rf_write(sc, 27, rf);
4458203134Sthompsa	}
4459209917Sthompsa	return (0);
4460203134Sthompsa}
4461203134Sthompsa
4462203134Sthompsastatic int
4463203134Sthompsarun_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4464203134Sthompsa    uint8_t *val)
4465203134Sthompsa{
4466203134Sthompsa	uint8_t rf22, rf24;
4467203134Sthompsa	uint8_t bbp55_pb, bbp55_sb, delta;
4468203134Sthompsa	int ntries;
4469203134Sthompsa
4470203134Sthompsa	/* program filter */
4471205042Sthompsa	run_rt3070_rf_read(sc, 24, &rf24);
4472205042Sthompsa	rf24 = (rf24 & 0xc0) | init;	/* initial filter value */
4473203134Sthompsa	run_rt3070_rf_write(sc, 24, rf24);
4474203134Sthompsa
4475203134Sthompsa	/* enable baseband loopback mode */
4476203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4477203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4478203134Sthompsa
4479203134Sthompsa	/* set power and frequency of passband test tone */
4480203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4481203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4482203134Sthompsa		/* transmit test tone */
4483203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4484203134Sthompsa		run_delay(sc, 10);
4485203134Sthompsa		/* read received power */
4486203134Sthompsa		run_bbp_read(sc, 55, &bbp55_pb);
4487203134Sthompsa		if (bbp55_pb != 0)
4488203134Sthompsa			break;
4489203134Sthompsa	}
4490203134Sthompsa	if (ntries == 100)
4491203134Sthompsa		return ETIMEDOUT;
4492203134Sthompsa
4493203134Sthompsa	/* set power and frequency of stopband test tone */
4494203134Sthompsa	run_bbp_write(sc, 24, 0x06);
4495203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4496203134Sthompsa		/* transmit test tone */
4497203134Sthompsa		run_bbp_write(sc, 25, 0x90);
4498203134Sthompsa		run_delay(sc, 10);
4499203134Sthompsa		/* read received power */
4500203134Sthompsa		run_bbp_read(sc, 55, &bbp55_sb);
4501203134Sthompsa
4502203134Sthompsa		delta = bbp55_pb - bbp55_sb;
4503203134Sthompsa		if (delta > target)
4504203134Sthompsa			break;
4505203134Sthompsa
4506203134Sthompsa		/* reprogram filter */
4507203134Sthompsa		rf24++;
4508203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4509203134Sthompsa	}
4510203134Sthompsa	if (ntries < 100) {
4511203134Sthompsa		if (rf24 != init)
4512203134Sthompsa			rf24--;	/* backtrack */
4513203134Sthompsa		*val = rf24;
4514203134Sthompsa		run_rt3070_rf_write(sc, 24, rf24);
4515203134Sthompsa	}
4516203134Sthompsa
4517203134Sthompsa	/* restore initial state */
4518203134Sthompsa	run_bbp_write(sc, 24, 0x00);
4519203134Sthompsa
4520203134Sthompsa	/* disable baseband loopback mode */
4521203134Sthompsa	run_rt3070_rf_read(sc, 22, &rf22);
4522203134Sthompsa	run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4523203134Sthompsa
4524209917Sthompsa	return (0);
4525203134Sthompsa}
4526203134Sthompsa
4527205042Sthompsastatic void
4528205042Sthompsarun_rt3070_rf_setup(struct run_softc *sc)
4529205042Sthompsa{
4530205042Sthompsa	uint8_t bbp, rf;
4531205042Sthompsa	int i;
4532205042Sthompsa
4533205042Sthompsa	if (sc->mac_ver == 0x3572) {
4534205042Sthompsa		/* enable DC filter */
4535205042Sthompsa		if (sc->mac_rev >= 0x0201)
4536205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4537205042Sthompsa
4538205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4539205042Sthompsa		if (sc->ntxchains == 1)
4540205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4541205042Sthompsa		if (sc->nrxchains == 1)
4542205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4543205042Sthompsa		run_bbp_write(sc, 138, bbp);
4544205042Sthompsa
4545205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4546205042Sthompsa			/* improve power consumption */
4547205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4548205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4549205042Sthompsa		}
4550205042Sthompsa
4551205042Sthompsa		run_rt3070_rf_read(sc, 16, &rf);
4552205042Sthompsa		rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4553205042Sthompsa		run_rt3070_rf_write(sc, 16, rf);
4554205042Sthompsa
4555205042Sthompsa	} else if (sc->mac_ver == 0x3071) {
4556205042Sthompsa		/* enable DC filter */
4557205042Sthompsa		if (sc->mac_rev >= 0x0201)
4558205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4559205042Sthompsa
4560205042Sthompsa		run_bbp_read(sc, 138, &bbp);
4561205042Sthompsa		if (sc->ntxchains == 1)
4562205042Sthompsa			bbp |= 0x20;	/* turn off DAC1 */
4563205042Sthompsa		if (sc->nrxchains == 1)
4564205042Sthompsa			bbp &= ~0x02;	/* turn off ADC1 */
4565205042Sthompsa		run_bbp_write(sc, 138, bbp);
4566205042Sthompsa
4567205042Sthompsa		if (sc->mac_rev >= 0x0211) {
4568205042Sthompsa			/* improve power consumption */
4569205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4570205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4571205042Sthompsa		}
4572205042Sthompsa
4573205042Sthompsa		run_write(sc, RT2860_TX_SW_CFG1, 0);
4574205042Sthompsa		if (sc->mac_rev < 0x0211) {
4575205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2,
4576205042Sthompsa			    sc->patch_dac ? 0x2c : 0x0f);
4577205042Sthompsa		} else
4578205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4579205042Sthompsa
4580205042Sthompsa	} else if (sc->mac_ver == 0x3070) {
4581205042Sthompsa		if (sc->mac_rev >= 0x0201) {
4582205042Sthompsa			/* enable DC filter */
4583205042Sthompsa			run_bbp_write(sc, 103, 0xc0);
4584205042Sthompsa
4585205042Sthompsa			/* improve power consumption */
4586205042Sthompsa			run_bbp_read(sc, 31, &bbp);
4587205042Sthompsa			run_bbp_write(sc, 31, bbp & ~0x03);
4588205042Sthompsa		}
4589205042Sthompsa
4590205042Sthompsa		if (sc->mac_rev < 0x0211) {
4591205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG1, 0);
4592205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4593205042Sthompsa		} else
4594205042Sthompsa			run_write(sc, RT2860_TX_SW_CFG2, 0);
4595205042Sthompsa	}
4596205042Sthompsa
4597205042Sthompsa	/* initialize RF registers from ROM for >=RT3071*/
4598205042Sthompsa	if (sc->mac_ver >= 0x3071) {
4599205042Sthompsa		for (i = 0; i < 10; i++) {
4600205042Sthompsa			if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4601205042Sthompsa				continue;
4602205042Sthompsa			run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4603205042Sthompsa		}
4604205042Sthompsa	}
4605205042Sthompsa}
4606205042Sthompsa
4607203134Sthompsastatic int
4608203134Sthompsarun_txrx_enable(struct run_softc *sc)
4609203134Sthompsa{
4610203134Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4611203134Sthompsa	uint32_t tmp;
4612203134Sthompsa	int error, ntries;
4613203134Sthompsa
4614203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4615203134Sthompsa	for (ntries = 0; ntries < 200; ntries++) {
4616203134Sthompsa		if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4617203134Sthompsa			return error;
4618203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4619203134Sthompsa			break;
4620203134Sthompsa		run_delay(sc, 50);
4621203134Sthompsa	}
4622203134Sthompsa	if (ntries == 200)
4623203134Sthompsa		return ETIMEDOUT;
4624203134Sthompsa
4625203134Sthompsa	run_delay(sc, 50);
4626203134Sthompsa
4627203134Sthompsa	tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4628203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4629203134Sthompsa
4630203134Sthompsa	/* enable Rx bulk aggregation (set timeout and limit) */
4631203134Sthompsa	tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
4632203134Sthompsa	    RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
4633203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, tmp);
4634203134Sthompsa
4635203134Sthompsa	/* set Rx filter */
4636203134Sthompsa	tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
4637203134Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
4638203134Sthompsa		tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
4639203134Sthompsa		    RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
4640203134Sthompsa		    RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
4641203134Sthompsa		    RT2860_DROP_CFACK | RT2860_DROP_CFEND;
4642203134Sthompsa		if (ic->ic_opmode == IEEE80211_M_STA)
4643203134Sthompsa			tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
4644203134Sthompsa	}
4645203134Sthompsa	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4646203134Sthompsa
4647203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
4648203134Sthompsa	    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4649203134Sthompsa
4650209917Sthompsa	return (0);
4651203134Sthompsa}
4652203134Sthompsa
4653203134Sthompsastatic void
4654203134Sthompsarun_init_locked(struct run_softc *sc)
4655203134Sthompsa{
4656203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4657203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4658203134Sthompsa	uint32_t tmp;
4659203134Sthompsa	uint8_t bbp1, bbp3;
4660203134Sthompsa	int i;
4661203134Sthompsa	int ridx;
4662203134Sthompsa	int ntries;
4663203134Sthompsa
4664209917Sthompsa	if (ic->ic_nrunning > 1)
4665208019Sthompsa		return;
4666208019Sthompsa
4667203134Sthompsa	run_stop(sc);
4668203134Sthompsa
4669203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4670203134Sthompsa		if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
4671203134Sthompsa			goto fail;
4672203134Sthompsa		if (tmp != 0 && tmp != 0xffffffff)
4673203134Sthompsa			break;
4674203134Sthompsa		run_delay(sc, 10);
4675203134Sthompsa	}
4676203134Sthompsa	if (ntries == 100)
4677203134Sthompsa		goto fail;
4678203134Sthompsa
4679203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
4680203134Sthompsa		run_setup_tx_list(sc, &sc->sc_epq[i]);
4681203134Sthompsa
4682203134Sthompsa	run_set_macaddr(sc, IF_LLADDR(ifp));
4683203134Sthompsa
4684203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4685203134Sthompsa		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
4686203134Sthompsa			goto fail;
4687203134Sthompsa		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4688203134Sthompsa			break;
4689203134Sthompsa		run_delay(sc, 10);
4690203134Sthompsa	}
4691203134Sthompsa	if (ntries == 100) {
4692203138Sthompsa		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
4693203134Sthompsa		goto fail;
4694203134Sthompsa	}
4695203134Sthompsa	tmp &= 0xff0;
4696203134Sthompsa	tmp |= RT2860_TX_WB_DDONE;
4697203134Sthompsa	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4698203134Sthompsa
4699203134Sthompsa	/* turn off PME_OEN to solve high-current issue */
4700203134Sthompsa	run_read(sc, RT2860_SYS_CTRL, &tmp);
4701203134Sthompsa	run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
4702203134Sthompsa
4703203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL,
4704203134Sthompsa	    RT2860_BBP_HRST | RT2860_MAC_SRST);
4705203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
4706203134Sthompsa
4707203134Sthompsa	if (run_reset(sc) != 0) {
4708203138Sthompsa		device_printf(sc->sc_dev, "could not reset chipset\n");
4709203134Sthompsa		goto fail;
4710203134Sthompsa	}
4711203134Sthompsa
4712203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4713203134Sthompsa
4714203134Sthompsa	/* init Tx power for all Tx rates (from EEPROM) */
4715203134Sthompsa	for (ridx = 0; ridx < 5; ridx++) {
4716203134Sthompsa		if (sc->txpow20mhz[ridx] == 0xffffffff)
4717203134Sthompsa			continue;
4718203134Sthompsa		run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
4719203134Sthompsa	}
4720203134Sthompsa
4721203134Sthompsa	for (i = 0; i < nitems(rt2870_def_mac); i++)
4722203134Sthompsa		run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
4723203134Sthompsa	run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
4724203134Sthompsa	run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
4725203134Sthompsa	run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
4726203134Sthompsa
4727205042Sthompsa	if (sc->mac_ver >= 0x3070) {
4728203134Sthompsa		/* set delay of PA_PE assertion to 1us (unit of 0.25us) */
4729203134Sthompsa		run_write(sc, RT2860_TX_SW_CFG0,
4730203134Sthompsa		    4 << RT2860_DLY_PAPE_EN_SHIFT);
4731203134Sthompsa	}
4732203134Sthompsa
4733203134Sthompsa	/* wait while MAC is busy */
4734203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4735203134Sthompsa		if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
4736203134Sthompsa			goto fail;
4737203134Sthompsa		if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
4738203134Sthompsa			break;
4739203134Sthompsa		run_delay(sc, 10);
4740203134Sthompsa	}
4741203134Sthompsa	if (ntries == 100)
4742203134Sthompsa		goto fail;
4743203134Sthompsa
4744203134Sthompsa	/* clear Host to MCU mailbox */
4745203134Sthompsa	run_write(sc, RT2860_H2M_BBPAGENT, 0);
4746203134Sthompsa	run_write(sc, RT2860_H2M_MAILBOX, 0);
4747203134Sthompsa	run_delay(sc, 10);
4748203134Sthompsa
4749203134Sthompsa	if (run_bbp_init(sc) != 0) {
4750203138Sthompsa		device_printf(sc->sc_dev, "could not initialize BBP\n");
4751203134Sthompsa		goto fail;
4752203134Sthompsa	}
4753203134Sthompsa
4754203134Sthompsa	/* abort TSF synchronization */
4755203134Sthompsa	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4756203134Sthompsa	tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4757203134Sthompsa	    RT2860_TBTT_TIMER_EN);
4758203134Sthompsa	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4759203134Sthompsa
4760203134Sthompsa	/* clear RX WCID search table */
4761203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
4762203134Sthompsa	/* clear WCID attribute table */
4763203134Sthompsa	run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
4764203134Sthompsa
4765209144Sthompsa	/* hostapd sets a key before init. So, don't clear it. */
4766209917Sthompsa	if (sc->cmdq_key_set != RUN_CMDQ_GO) {
4767209144Sthompsa		/* clear shared key table */
4768209144Sthompsa		run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
4769209144Sthompsa		/* clear shared key mode */
4770209144Sthompsa		run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
4771209144Sthompsa	}
4772209144Sthompsa
4773203134Sthompsa	run_read(sc, RT2860_US_CYC_CNT, &tmp);
4774203134Sthompsa	tmp = (tmp & ~0xff) | 0x1e;
4775203134Sthompsa	run_write(sc, RT2860_US_CYC_CNT, tmp);
4776203134Sthompsa
4777205042Sthompsa	if (sc->mac_rev != 0x0101)
4778203134Sthompsa		run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
4779203134Sthompsa
4780203134Sthompsa	run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
4781203134Sthompsa	run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
4782203134Sthompsa
4783203134Sthompsa	/* write vendor-specific BBP values (from EEPROM) */
4784208019Sthompsa	for (i = 0; i < 10; i++) {
4785203134Sthompsa		if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
4786203134Sthompsa			continue;
4787203134Sthompsa		run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
4788203134Sthompsa	}
4789203134Sthompsa
4790203134Sthompsa	/* select Main antenna for 1T1R devices */
4791203134Sthompsa	if (sc->rf_rev == RT3070_RF_3020)
4792203134Sthompsa		run_set_rx_antenna(sc, 0);
4793203134Sthompsa
4794203134Sthompsa	/* send LEDs operating mode to microcontroller */
4795203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
4796203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
4797203134Sthompsa	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
4798203134Sthompsa
4799205042Sthompsa	if (sc->mac_ver >= 0x3070)
4800205042Sthompsa		run_rt3070_rf_init(sc);
4801205042Sthompsa
4802203134Sthompsa	/* disable non-existing Rx chains */
4803203134Sthompsa	run_bbp_read(sc, 3, &bbp3);
4804203134Sthompsa	bbp3 &= ~(1 << 3 | 1 << 4);
4805203134Sthompsa	if (sc->nrxchains == 2)
4806203134Sthompsa		bbp3 |= 1 << 3;
4807203134Sthompsa	else if (sc->nrxchains == 3)
4808203134Sthompsa		bbp3 |= 1 << 4;
4809203134Sthompsa	run_bbp_write(sc, 3, bbp3);
4810203134Sthompsa
4811203134Sthompsa	/* disable non-existing Tx chains */
4812203134Sthompsa	run_bbp_read(sc, 1, &bbp1);
4813203134Sthompsa	if (sc->ntxchains == 1)
4814203134Sthompsa		bbp1 &= ~(1 << 3 | 1 << 4);
4815203134Sthompsa	run_bbp_write(sc, 1, bbp1);
4816203134Sthompsa
4817205042Sthompsa	if (sc->mac_ver >= 0x3070)
4818205042Sthompsa		run_rt3070_rf_setup(sc);
4819203134Sthompsa
4820203134Sthompsa	/* select default channel */
4821203134Sthompsa	run_set_chan(sc, ic->ic_curchan);
4822203134Sthompsa
4823203134Sthompsa	/* setup initial protection mode */
4824218492Sbschmidt	run_updateprot_cb(ic);
4825203134Sthompsa
4826203134Sthompsa	/* turn radio LED on */
4827203134Sthompsa	run_set_leds(sc, RT2860_LED_RADIO);
4828203134Sthompsa
4829203134Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4830203134Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
4831208019Sthompsa	sc->cmdq_run = RUN_CMDQ_GO;
4832203134Sthompsa
4833209917Sthompsa	for (i = 0; i != RUN_N_XFER; i++)
4834203134Sthompsa		usbd_xfer_set_stall(sc->sc_xfer[i]);
4835203134Sthompsa
4836203134Sthompsa	usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
4837203134Sthompsa
4838203134Sthompsa	if (run_txrx_enable(sc) != 0)
4839203134Sthompsa		goto fail;
4840203134Sthompsa
4841203134Sthompsa	return;
4842203134Sthompsa
4843203134Sthompsafail:
4844203134Sthompsa	run_stop(sc);
4845203134Sthompsa}
4846203134Sthompsa
4847203134Sthompsastatic void
4848203134Sthompsarun_init(void *arg)
4849203134Sthompsa{
4850203134Sthompsa	struct run_softc *sc = arg;
4851203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4852203134Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
4853203134Sthompsa
4854203134Sthompsa	RUN_LOCK(sc);
4855203134Sthompsa	run_init_locked(sc);
4856203134Sthompsa	RUN_UNLOCK(sc);
4857203134Sthompsa
4858203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4859208019Sthompsa		ieee80211_start_all(ic);
4860203134Sthompsa}
4861203134Sthompsa
4862203134Sthompsastatic void
4863203134Sthompsarun_stop(void *arg)
4864203134Sthompsa{
4865203134Sthompsa	struct run_softc *sc = (struct run_softc *)arg;
4866203134Sthompsa	struct ifnet *ifp = sc->sc_ifp;
4867203134Sthompsa	uint32_t tmp;
4868203134Sthompsa	int i;
4869203134Sthompsa	int ntries;
4870203134Sthompsa
4871203134Sthompsa	RUN_LOCK_ASSERT(sc, MA_OWNED);
4872203134Sthompsa
4873203134Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4874203134Sthompsa		run_set_leds(sc, 0);	/* turn all LEDs off */
4875203134Sthompsa
4876203134Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4877203134Sthompsa
4878208019Sthompsa	sc->ratectl_run = RUN_RATECTL_OFF;
4879209144Sthompsa	sc->cmdq_run = sc->cmdq_key_set;
4880208019Sthompsa
4881203134Sthompsa	RUN_UNLOCK(sc);
4882203134Sthompsa
4883203134Sthompsa	for(i = 0; i < RUN_N_XFER; i++)
4884203134Sthompsa		usbd_transfer_drain(sc->sc_xfer[i]);
4885203134Sthompsa
4886203134Sthompsa	RUN_LOCK(sc);
4887203134Sthompsa
4888209917Sthompsa	if (sc->rx_m != NULL) {
4889203134Sthompsa		m_free(sc->rx_m);
4890203134Sthompsa		sc->rx_m = NULL;
4891203134Sthompsa	}
4892203134Sthompsa
4893203134Sthompsa	/* disable Tx/Rx */
4894203134Sthompsa	run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
4895203134Sthompsa	tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4896203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
4897203134Sthompsa
4898203134Sthompsa	/* wait for pending Tx to complete */
4899203134Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
4900209917Sthompsa		if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
4901203134Sthompsa			DPRINTF("Cannot read Tx queue count\n");
4902203134Sthompsa			break;
4903203134Sthompsa		}
4904209917Sthompsa		if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
4905203134Sthompsa			DPRINTF("All Tx cleared\n");
4906203134Sthompsa			break;
4907203134Sthompsa		}
4908203134Sthompsa		run_delay(sc, 10);
4909203134Sthompsa	}
4910209917Sthompsa	if (ntries >= 100)
4911203134Sthompsa		DPRINTF("There are still pending Tx\n");
4912203134Sthompsa	run_delay(sc, 10);
4913203134Sthompsa	run_write(sc, RT2860_USB_DMA_CFG, 0);
4914203134Sthompsa
4915203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
4916203134Sthompsa	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4917203134Sthompsa
4918203134Sthompsa	for (i = 0; i != RUN_EP_QUEUES; i++)
4919203134Sthompsa		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
4920203134Sthompsa
4921203134Sthompsa	return;
4922203134Sthompsa}
4923203134Sthompsa
4924203134Sthompsastatic void
4925203134Sthompsarun_delay(struct run_softc *sc, unsigned int ms)
4926203134Sthompsa{
4927203134Sthompsa	usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
4928203134Sthompsa	    &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
4929203134Sthompsa}
4930203134Sthompsa
4931203134Sthompsastatic device_method_t run_methods[] = {
4932203134Sthompsa	/* Device interface */
4933203134Sthompsa	DEVMETHOD(device_probe,		run_match),
4934203134Sthompsa	DEVMETHOD(device_attach,	run_attach),
4935203134Sthompsa	DEVMETHOD(device_detach,	run_detach),
4936203134Sthompsa
4937203134Sthompsa	{ 0, 0 }
4938203134Sthompsa};
4939203134Sthompsa
4940203134Sthompsastatic driver_t run_driver = {
4941203134Sthompsa	"run",
4942203134Sthompsa	run_methods,
4943203134Sthompsa	sizeof(struct run_softc)
4944203134Sthompsa};
4945203134Sthompsa
4946203134Sthompsastatic devclass_t run_devclass;
4947203134Sthompsa
4948203134SthompsaDRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0);
4949212122SthompsaMODULE_DEPEND(run, wlan, 1, 1, 1);
4950212122SthompsaMODULE_DEPEND(run, usb, 1, 1, 1);
4951212122SthompsaMODULE_DEPEND(run, firmware, 1, 1, 1);
4952212122SthompsaMODULE_VERSION(run, 1);
4953