xref: /freebsd/sys/dev/usb/wlan/if_run.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr>
3  * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca>
4  * USB Consulting, Hans Petter Selasky <hselasky@freebsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
21 
22 /*-
23  * Ralink Technology RT2700U/RT2800U/RT3000U chipset driver.
24  * http://www.ralinktech.com/
25  */
26 
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/sysctl.h>
30 #include <sys/lock.h>
31 #include <sys/mutex.h>
32 #include <sys/mbuf.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/linker.h>
41 #include <sys/firmware.h>
42 #include <sys/kdb.h>
43 
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <sys/rman.h>
47 
48 #include <net/bpf.h>
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/if_ether.h>
60 #include <netinet/ip.h>
61 
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_regdomain.h>
64 #include <net80211/ieee80211_radiotap.h>
65 #include <net80211/ieee80211_ratectl.h>
66 
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include "usbdevs.h"
70 
71 #define USB_DEBUG_VAR run_debug
72 #include <dev/usb/usb_debug.h>
73 
74 #include <dev/usb/wlan/if_runreg.h>
75 #include <dev/usb/wlan/if_runvar.h>
76 
77 #define nitems(_a)      (sizeof((_a)) / sizeof((_a)[0]))
78 
79 #ifdef	USB_DEBUG
80 #define RUN_DEBUG
81 #endif
82 
83 #ifdef	RUN_DEBUG
84 int run_debug = 0;
85 SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run");
86 SYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0,
87     "run debug level");
88 #endif
89 
90 #define IEEE80211_HAS_ADDR4(wh) \
91 	(((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
92 
93 /*
94  * Because of LOR in run_key_delete(), use atomic instead.
95  * '& RUN_CMDQ_MASQ' is to loop cmdq[].
96  */
97 #define RUN_CMDQ_GET(c)	(atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
98 
99 static const struct usb_device_id run_devs[] = {
100 #define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
101     RUN_DEV(ABOCOM,		RT2770),
102     RUN_DEV(ABOCOM,		RT2870),
103     RUN_DEV(ABOCOM,		RT3070),
104     RUN_DEV(ABOCOM,		RT3071),
105     RUN_DEV(ABOCOM,		RT3072),
106     RUN_DEV(ABOCOM2,		RT2870_1),
107     RUN_DEV(ACCTON,		RT2770),
108     RUN_DEV(ACCTON,		RT2870_1),
109     RUN_DEV(ACCTON,		RT2870_2),
110     RUN_DEV(ACCTON,		RT2870_3),
111     RUN_DEV(ACCTON,		RT2870_4),
112     RUN_DEV(ACCTON,		RT2870_5),
113     RUN_DEV(ACCTON,		RT3070),
114     RUN_DEV(ACCTON,		RT3070_1),
115     RUN_DEV(ACCTON,		RT3070_2),
116     RUN_DEV(ACCTON,		RT3070_3),
117     RUN_DEV(ACCTON,		RT3070_4),
118     RUN_DEV(ACCTON,		RT3070_5),
119     RUN_DEV(AIRTIES,		RT3070),
120     RUN_DEV(ALLWIN,		RT2070),
121     RUN_DEV(ALLWIN,		RT2770),
122     RUN_DEV(ALLWIN,		RT2870),
123     RUN_DEV(ALLWIN,		RT3070),
124     RUN_DEV(ALLWIN,		RT3071),
125     RUN_DEV(ALLWIN,		RT3072),
126     RUN_DEV(ALLWIN,		RT3572),
127     RUN_DEV(AMIGO,		RT2870_1),
128     RUN_DEV(AMIGO,		RT2870_2),
129     RUN_DEV(AMIT,		CGWLUSB2GNR),
130     RUN_DEV(AMIT,		RT2870_1),
131     RUN_DEV(AMIT2,		RT2870),
132     RUN_DEV(ASUS,		RT2870_1),
133     RUN_DEV(ASUS,		RT2870_2),
134     RUN_DEV(ASUS,		RT2870_3),
135     RUN_DEV(ASUS,		RT2870_4),
136     RUN_DEV(ASUS,		RT2870_5),
137     RUN_DEV(ASUS,		USBN13),
138     RUN_DEV(ASUS,		RT3070_1),
139     RUN_DEV(ASUS2,		USBN11),
140     RUN_DEV(AZUREWAVE,		RT2870_1),
141     RUN_DEV(AZUREWAVE,		RT2870_2),
142     RUN_DEV(AZUREWAVE,		RT3070_1),
143     RUN_DEV(AZUREWAVE,		RT3070_2),
144     RUN_DEV(AZUREWAVE,		RT3070_3),
145     RUN_DEV(BELKIN,		F5D8053V3),
146     RUN_DEV(BELKIN,		F5D8055),
147     RUN_DEV(BELKIN,		F6D4050V1),
148     RUN_DEV(BELKIN,		RT2870_1),
149     RUN_DEV(BELKIN,		RT2870_2),
150     RUN_DEV(CISCOLINKSYS2,	RT3070),
151     RUN_DEV(CISCOLINKSYS3,	RT3070),
152     RUN_DEV(CONCEPTRONIC2,	RT2870_1),
153     RUN_DEV(CONCEPTRONIC2,	RT2870_2),
154     RUN_DEV(CONCEPTRONIC2,	RT2870_3),
155     RUN_DEV(CONCEPTRONIC2,	RT2870_4),
156     RUN_DEV(CONCEPTRONIC2,	RT2870_5),
157     RUN_DEV(CONCEPTRONIC2,	RT2870_6),
158     RUN_DEV(CONCEPTRONIC2,	RT2870_7),
159     RUN_DEV(CONCEPTRONIC2,	RT2870_8),
160     RUN_DEV(CONCEPTRONIC2,	RT3070_1),
161     RUN_DEV(CONCEPTRONIC2,	RT3070_2),
162     RUN_DEV(CONCEPTRONIC2,	VIGORN61),
163     RUN_DEV(COREGA,		CGWLUSB300GNM),
164     RUN_DEV(COREGA,		RT2870_1),
165     RUN_DEV(COREGA,		RT2870_2),
166     RUN_DEV(COREGA,		RT2870_3),
167     RUN_DEV(COREGA,		RT3070),
168     RUN_DEV(CYBERTAN,		RT2870),
169     RUN_DEV(DLINK,		RT2870),
170     RUN_DEV(DLINK,		RT3072),
171     RUN_DEV(DLINK2,		DWA130),
172     RUN_DEV(DLINK2,		RT2870_1),
173     RUN_DEV(DLINK2,		RT2870_2),
174     RUN_DEV(DLINK2,		RT3070_1),
175     RUN_DEV(DLINK2,		RT3070_2),
176     RUN_DEV(DLINK2,		RT3070_3),
177     RUN_DEV(DLINK2,		RT3070_4),
178     RUN_DEV(DLINK2,		RT3070_5),
179     RUN_DEV(DLINK2,		RT3072),
180     RUN_DEV(DLINK2,		RT3072_1),
181     RUN_DEV(EDIMAX,		EW7717),
182     RUN_DEV(EDIMAX,		EW7718),
183     RUN_DEV(EDIMAX,		RT2870_1),
184     RUN_DEV(ENCORE,		RT3070_1),
185     RUN_DEV(ENCORE,		RT3070_2),
186     RUN_DEV(ENCORE,		RT3070_3),
187     RUN_DEV(GIGABYTE,		GNWB31N),
188     RUN_DEV(GIGABYTE,		GNWB32L),
189     RUN_DEV(GIGABYTE,		RT2870_1),
190     RUN_DEV(GIGASET,		RT3070_1),
191     RUN_DEV(GIGASET,		RT3070_2),
192     RUN_DEV(GUILLEMOT,		HWNU300),
193     RUN_DEV(HAWKING,		HWUN2),
194     RUN_DEV(HAWKING,		RT2870_1),
195     RUN_DEV(HAWKING,		RT2870_2),
196     RUN_DEV(HAWKING,		RT3070),
197     RUN_DEV(IODATA,		RT3072_1),
198     RUN_DEV(IODATA,		RT3072_2),
199     RUN_DEV(IODATA,		RT3072_3),
200     RUN_DEV(IODATA,		RT3072_4),
201     RUN_DEV(LINKSYS4,		RT3070),
202     RUN_DEV(LINKSYS4,		WUSB100),
203     RUN_DEV(LINKSYS4,		WUSB54GCV3),
204     RUN_DEV(LINKSYS4,		WUSB600N),
205     RUN_DEV(LINKSYS4,		WUSB600NV2),
206     RUN_DEV(LOGITEC,		RT2870_1),
207     RUN_DEV(LOGITEC,		RT2870_2),
208     RUN_DEV(LOGITEC,		RT2870_3),
209     RUN_DEV(MELCO,		RT2870_1),
210     RUN_DEV(MELCO,		RT2870_2),
211     RUN_DEV(MELCO,		WLIUCAG300N),
212     RUN_DEV(MELCO,		WLIUCG300N),
213     RUN_DEV(MELCO,		WLIUCG301N),
214     RUN_DEV(MELCO,		WLIUCGN),
215     RUN_DEV(MOTOROLA4,		RT2770),
216     RUN_DEV(MOTOROLA4,		RT3070),
217     RUN_DEV(MSI,		RT3070_1),
218     RUN_DEV(MSI,		RT3070_2),
219     RUN_DEV(MSI,		RT3070_3),
220     RUN_DEV(MSI,		RT3070_4),
221     RUN_DEV(MSI,		RT3070_5),
222     RUN_DEV(MSI,		RT3070_6),
223     RUN_DEV(MSI,		RT3070_7),
224     RUN_DEV(MSI,		RT3070_8),
225     RUN_DEV(MSI,		RT3070_9),
226     RUN_DEV(MSI,		RT3070_10),
227     RUN_DEV(MSI,		RT3070_11),
228     RUN_DEV(OVISLINK,		RT3072),
229     RUN_DEV(PARA,		RT3070),
230     RUN_DEV(PEGATRON,		RT2870),
231     RUN_DEV(PEGATRON,		RT3070),
232     RUN_DEV(PEGATRON,		RT3070_2),
233     RUN_DEV(PEGATRON,		RT3070_3),
234     RUN_DEV(PHILIPS,		RT2870),
235     RUN_DEV(PLANEX2,		GWUS300MINIS),
236     RUN_DEV(PLANEX2,		GWUSMICRON),
237     RUN_DEV(PLANEX2,		RT2870),
238     RUN_DEV(PLANEX2,		RT3070),
239     RUN_DEV(QCOM,		RT2870),
240     RUN_DEV(QUANTA,		RT3070),
241     RUN_DEV(RALINK,		RT2070),
242     RUN_DEV(RALINK,		RT2770),
243     RUN_DEV(RALINK,		RT2870),
244     RUN_DEV(RALINK,		RT3070),
245     RUN_DEV(RALINK,		RT3071),
246     RUN_DEV(RALINK,		RT3072),
247     RUN_DEV(RALINK,		RT3370),
248     RUN_DEV(RALINK,		RT3572),
249     RUN_DEV(RALINK,		RT8070),
250     RUN_DEV(SAMSUNG2,		RT2870_1),
251     RUN_DEV(SENAO,		RT2870_1),
252     RUN_DEV(SENAO,		RT2870_2),
253     RUN_DEV(SENAO,		RT2870_3),
254     RUN_DEV(SENAO,		RT2870_4),
255     RUN_DEV(SENAO,		RT3070),
256     RUN_DEV(SENAO,		RT3071),
257     RUN_DEV(SENAO,		RT3072_1),
258     RUN_DEV(SENAO,		RT3072_2),
259     RUN_DEV(SENAO,		RT3072_3),
260     RUN_DEV(SENAO,		RT3072_4),
261     RUN_DEV(SENAO,		RT3072_5),
262     RUN_DEV(SITECOMEU,		RT2770),
263     RUN_DEV(SITECOMEU,		RT2870_1),
264     RUN_DEV(SITECOMEU,		RT2870_2),
265     RUN_DEV(SITECOMEU,		RT2870_3),
266     RUN_DEV(SITECOMEU,		RT2870_4),
267     RUN_DEV(SITECOMEU,		RT3070),
268     RUN_DEV(SITECOMEU,		RT3070_2),
269     RUN_DEV(SITECOMEU,		RT3070_3),
270     RUN_DEV(SITECOMEU,		RT3070_4),
271     RUN_DEV(SITECOMEU,		RT3071),
272     RUN_DEV(SITECOMEU,		RT3072_1),
273     RUN_DEV(SITECOMEU,		RT3072_2),
274     RUN_DEV(SITECOMEU,		RT3072_3),
275     RUN_DEV(SITECOMEU,		RT3072_4),
276     RUN_DEV(SITECOMEU,		RT3072_5),
277     RUN_DEV(SITECOMEU,		RT3072_6),
278     RUN_DEV(SITECOMEU,		WL608),
279     RUN_DEV(SPARKLAN,		RT2870_1),
280     RUN_DEV(SPARKLAN,		RT3070),
281     RUN_DEV(SWEEX2,		LW153),
282     RUN_DEV(SWEEX2,		LW303),
283     RUN_DEV(SWEEX2,		LW313),
284     RUN_DEV(TOSHIBA,		RT3070),
285     RUN_DEV(UMEDIA,		RT2870_1),
286     RUN_DEV(ZCOM,		RT2870_1),
287     RUN_DEV(ZCOM,		RT2870_2),
288     RUN_DEV(ZINWELL,		RT2870_1),
289     RUN_DEV(ZINWELL,		RT2870_2),
290     RUN_DEV(ZINWELL,		RT3070),
291     RUN_DEV(ZINWELL,		RT3072_1),
292     RUN_DEV(ZINWELL,		RT3072_2),
293     RUN_DEV(ZYXEL,		RT2870_1),
294     RUN_DEV(ZYXEL,		RT2870_2),
295 #undef RUN_DEV
296 };
297 
298 static device_probe_t	run_match;
299 static device_attach_t	run_attach;
300 static device_detach_t	run_detach;
301 
302 static usb_callback_t	run_bulk_rx_callback;
303 static usb_callback_t	run_bulk_tx_callback0;
304 static usb_callback_t	run_bulk_tx_callback1;
305 static usb_callback_t	run_bulk_tx_callback2;
306 static usb_callback_t	run_bulk_tx_callback3;
307 static usb_callback_t	run_bulk_tx_callback4;
308 static usb_callback_t	run_bulk_tx_callback5;
309 
310 static void	run_bulk_tx_callbackN(struct usb_xfer *xfer,
311 		    usb_error_t error, unsigned int index);
312 static struct ieee80211vap *run_vap_create(struct ieee80211com *,
313 		    const char name[IFNAMSIZ], int unit, int opmode, int flags,
314 		    const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t
315 		    mac[IEEE80211_ADDR_LEN]);
316 static void	run_vap_delete(struct ieee80211vap *);
317 static void	run_cmdq_cb(void *, int);
318 static void	run_setup_tx_list(struct run_softc *,
319 		    struct run_endpoint_queue *);
320 static void	run_unsetup_tx_list(struct run_softc *,
321 		    struct run_endpoint_queue *);
322 static int	run_load_microcode(struct run_softc *);
323 static int	run_reset(struct run_softc *);
324 static usb_error_t run_do_request(struct run_softc *,
325 		    struct usb_device_request *, void *);
326 static int	run_read(struct run_softc *, uint16_t, uint32_t *);
327 static int	run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
328 static int	run_write_2(struct run_softc *, uint16_t, uint16_t);
329 static int	run_write(struct run_softc *, uint16_t, uint32_t);
330 static int	run_write_region_1(struct run_softc *, uint16_t,
331 		    const uint8_t *, int);
332 static int	run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
333 static int	run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
334 static int	run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
335 static int	run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t);
336 static int	run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
337 static int	run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
338 static int	run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
339 static int	run_bbp_write(struct run_softc *, uint8_t, uint8_t);
340 static int	run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
341 static const char *run_get_rf(int);
342 static int	run_read_eeprom(struct run_softc *);
343 static struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
344 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
345 static int	run_media_change(struct ifnet *);
346 static int	run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
347 static int	run_wme_update(struct ieee80211com *);
348 static void	run_wme_update_cb(void *);
349 static void	run_key_update_begin(struct ieee80211vap *);
350 static void	run_key_update_end(struct ieee80211vap *);
351 static void	run_key_set_cb(void *);
352 static int	run_key_set(struct ieee80211vap *, struct ieee80211_key *,
353 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
354 static void	run_key_delete_cb(void *);
355 static int	run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
356 static void	run_ratectl_to(void *);
357 static void	run_ratectl_cb(void *, int);
358 static void	run_drain_fifo(void *);
359 static void	run_iter_func(void *, struct ieee80211_node *);
360 static void	run_newassoc_cb(void *);
361 static void	run_newassoc(struct ieee80211_node *, int);
362 static void	run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
363 static void	run_tx_free(struct run_endpoint_queue *pq,
364 		    struct run_tx_data *, int);
365 static void	run_set_tx_desc(struct run_softc *, struct run_tx_data *);
366 static int	run_tx(struct run_softc *, struct mbuf *,
367 		    struct ieee80211_node *);
368 static int	run_tx_mgt(struct run_softc *, struct mbuf *,
369 		    struct ieee80211_node *);
370 static int	run_sendprot(struct run_softc *, const struct mbuf *,
371 		    struct ieee80211_node *, int, int);
372 static int	run_tx_param(struct run_softc *, struct mbuf *,
373 		    struct ieee80211_node *,
374 		    const struct ieee80211_bpf_params *);
375 static int	run_raw_xmit(struct ieee80211_node *, struct mbuf *,
376 		    const struct ieee80211_bpf_params *);
377 static void	run_start(struct ifnet *);
378 static int	run_ioctl(struct ifnet *, u_long, caddr_t);
379 static void	run_set_agc(struct run_softc *, uint8_t);
380 static void	run_select_chan_group(struct run_softc *, int);
381 static void	run_set_rx_antenna(struct run_softc *, int);
382 static void	run_rt2870_set_chan(struct run_softc *, u_int);
383 static void	run_rt3070_set_chan(struct run_softc *, u_int);
384 static void	run_rt3572_set_chan(struct run_softc *, u_int);
385 static int	run_set_chan(struct run_softc *, struct ieee80211_channel *);
386 static void	run_set_channel(struct ieee80211com *);
387 static void	run_scan_start(struct ieee80211com *);
388 static void	run_scan_end(struct ieee80211com *);
389 static void	run_update_beacon(struct ieee80211vap *, int);
390 static void	run_update_beacon_cb(void *);
391 static void	run_updateprot(struct ieee80211com *);
392 static void	run_updateprot_cb(void *);
393 static void	run_usb_timeout_cb(void *);
394 static void	run_reset_livelock(struct run_softc *);
395 static void	run_enable_tsf_sync(struct run_softc *);
396 static void	run_enable_mrr(struct run_softc *);
397 static void	run_set_txpreamble(struct run_softc *);
398 static void	run_set_basicrates(struct run_softc *);
399 static void	run_set_leds(struct run_softc *, uint16_t);
400 static void	run_set_bssid(struct run_softc *, const uint8_t *);
401 static void	run_set_macaddr(struct run_softc *, const uint8_t *);
402 static void	run_updateslot(struct ifnet *);
403 static void	run_updateslot_cb(void *);
404 static void	run_update_mcast(struct ifnet *);
405 static int8_t	run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
406 static void	run_update_promisc_locked(struct ifnet *);
407 static void	run_update_promisc(struct ifnet *);
408 static int	run_bbp_init(struct run_softc *);
409 static int	run_rt3070_rf_init(struct run_softc *);
410 static int	run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
411 		    uint8_t *);
412 static void	run_rt3070_rf_setup(struct run_softc *);
413 static int	run_txrx_enable(struct run_softc *);
414 static void	run_init(void *);
415 static void	run_init_locked(struct run_softc *);
416 static void	run_stop(void *);
417 static void	run_delay(struct run_softc *, unsigned int);
418 
419 static const struct {
420 	uint16_t	reg;
421 	uint32_t	val;
422 } rt2870_def_mac[] = {
423 	RT2870_DEF_MAC
424 };
425 
426 static const struct {
427 	uint8_t	reg;
428 	uint8_t	val;
429 } rt2860_def_bbp[] = {
430 	RT2860_DEF_BBP
431 };
432 
433 static const struct rfprog {
434 	uint8_t		chan;
435 	uint32_t	r1, r2, r3, r4;
436 } rt2860_rf2850[] = {
437 	RT2860_RF2850
438 };
439 
440 struct {
441 	uint8_t	n, r, k;
442 } rt3070_freqs[] = {
443 	RT3070_RF3052
444 };
445 
446 static const struct {
447 	uint8_t	reg;
448 	uint8_t	val;
449 } rt3070_def_rf[] = {
450 	RT3070_DEF_RF
451 },rt3572_def_rf[] = {
452 	RT3572_DEF_RF
453 };
454 
455 static const struct usb_config run_config[RUN_N_XFER] = {
456     [RUN_BULK_TX_BE] = {
457 	.type = UE_BULK,
458 	.endpoint = UE_ADDR_ANY,
459 	.ep_index = 0,
460 	.direction = UE_DIR_OUT,
461 	.bufsize = RUN_MAX_TXSZ,
462 	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
463 	.callback = run_bulk_tx_callback0,
464 	.timeout = 5000,	/* ms */
465     },
466     [RUN_BULK_TX_BK] = {
467 	.type = UE_BULK,
468 	.endpoint = UE_ADDR_ANY,
469 	.direction = UE_DIR_OUT,
470 	.ep_index = 1,
471 	.bufsize = RUN_MAX_TXSZ,
472 	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
473 	.callback = run_bulk_tx_callback1,
474 	.timeout = 5000,	/* ms */
475     },
476     [RUN_BULK_TX_VI] = {
477 	.type = UE_BULK,
478 	.endpoint = UE_ADDR_ANY,
479 	.direction = UE_DIR_OUT,
480 	.ep_index = 2,
481 	.bufsize = RUN_MAX_TXSZ,
482 	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
483 	.callback = run_bulk_tx_callback2,
484 	.timeout = 5000,	/* ms */
485     },
486     [RUN_BULK_TX_VO] = {
487 	.type = UE_BULK,
488 	.endpoint = UE_ADDR_ANY,
489 	.direction = UE_DIR_OUT,
490 	.ep_index = 3,
491 	.bufsize = RUN_MAX_TXSZ,
492 	.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
493 	.callback = run_bulk_tx_callback3,
494 	.timeout = 5000,	/* ms */
495     },
496     [RUN_BULK_TX_HCCA] = {
497 	.type = UE_BULK,
498 	.endpoint = UE_ADDR_ANY,
499 	.direction = UE_DIR_OUT,
500 	.ep_index = 4,
501 	.bufsize = RUN_MAX_TXSZ,
502 	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
503 	.callback = run_bulk_tx_callback4,
504 	.timeout = 5000,	/* ms */
505     },
506     [RUN_BULK_TX_PRIO] = {
507 	.type = UE_BULK,
508 	.endpoint = UE_ADDR_ANY,
509 	.direction = UE_DIR_OUT,
510 	.ep_index = 5,
511 	.bufsize = RUN_MAX_TXSZ,
512 	.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
513 	.callback = run_bulk_tx_callback5,
514 	.timeout = 5000,	/* ms */
515     },
516     [RUN_BULK_RX] = {
517 	.type = UE_BULK,
518 	.endpoint = UE_ADDR_ANY,
519 	.direction = UE_DIR_IN,
520 	.bufsize = RUN_MAX_RXSZ,
521 	.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
522 	.callback = run_bulk_rx_callback,
523     }
524 };
525 
526 static int
527 run_match(device_t self)
528 {
529 	struct usb_attach_arg *uaa = device_get_ivars(self);
530 
531 	if (uaa->usb_mode != USB_MODE_HOST)
532 		return (ENXIO);
533 	if (uaa->info.bConfigIndex != 0)
534 		return (ENXIO);
535 	if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
536 		return (ENXIO);
537 
538 	return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
539 }
540 
541 static int
542 run_attach(device_t self)
543 {
544 	struct run_softc *sc = device_get_softc(self);
545 	struct usb_attach_arg *uaa = device_get_ivars(self);
546 	struct ieee80211com *ic;
547 	struct ifnet *ifp;
548 	uint32_t ver;
549 	int i, ntries, error;
550 	uint8_t iface_index, bands;
551 
552 	device_set_usb_desc(self);
553 	sc->sc_udev = uaa->device;
554 	sc->sc_dev = self;
555 
556 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
557 	    MTX_NETWORK_LOCK, MTX_DEF);
558 
559 	iface_index = RT2860_IFACE_INDEX;
560 
561 	error = usbd_transfer_setup(uaa->device, &iface_index,
562 	    sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
563 	if (error) {
564 		device_printf(self, "could not allocate USB transfers, "
565 		    "err=%s\n", usbd_errstr(error));
566 		goto detach;
567 	}
568 
569 	RUN_LOCK(sc);
570 
571 	/* wait for the chip to settle */
572 	for (ntries = 0; ntries < 100; ntries++) {
573 		if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
574 			RUN_UNLOCK(sc);
575 			goto detach;
576 		}
577 		if (ver != 0 && ver != 0xffffffff)
578 			break;
579 		run_delay(sc, 10);
580 	}
581 	if (ntries == 100) {
582 		device_printf(sc->sc_dev,
583 		    "timeout waiting for NIC to initialize\n");
584 		RUN_UNLOCK(sc);
585 		goto detach;
586 	}
587 	sc->mac_ver = ver >> 16;
588 	sc->mac_rev = ver & 0xffff;
589 
590 	/* retrieve RF rev. no and various other things from EEPROM */
591 	run_read_eeprom(sc);
592 
593 	device_printf(sc->sc_dev,
594 	    "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
595 	    sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
596 	    sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
597 
598 	if ((error = run_load_microcode(sc)) != 0) {
599 		device_printf(sc->sc_dev, "could not load 8051 microcode\n");
600 		RUN_UNLOCK(sc);
601 		goto detach;
602 	}
603 
604 	RUN_UNLOCK(sc);
605 
606 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
607 	if (ifp == NULL) {
608 		device_printf(sc->sc_dev, "can not if_alloc()\n");
609 		goto detach;
610 	}
611 	ic = ifp->if_l2com;
612 
613 	ifp->if_softc = sc;
614 	if_initname(ifp, "run", device_get_unit(sc->sc_dev));
615 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
616 	ifp->if_init = run_init;
617 	ifp->if_ioctl = run_ioctl;
618 	ifp->if_start = run_start;
619 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
620 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
621 	IFQ_SET_READY(&ifp->if_snd);
622 
623 	ic->ic_ifp = ifp;
624 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
625 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
626 
627 	/* set device capabilities */
628 	ic->ic_caps =
629 	    IEEE80211_C_STA |		/* station mode supported */
630 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
631 	    IEEE80211_C_IBSS |
632 	    IEEE80211_C_HOSTAP |
633 	    IEEE80211_C_WDS |		/* 4-address traffic works */
634 	    IEEE80211_C_MBSS |
635 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
636 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
637 	    IEEE80211_C_WME |		/* WME */
638 	    IEEE80211_C_WPA;		/* WPA1|WPA2(RSN) */
639 
640 	ic->ic_cryptocaps =
641 	    IEEE80211_CRYPTO_WEP |
642 	    IEEE80211_CRYPTO_AES_CCM |
643 	    IEEE80211_CRYPTO_TKIPMIC |
644 	    IEEE80211_CRYPTO_TKIP;
645 
646 	ic->ic_flags |= IEEE80211_F_DATAPAD;
647 	ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
648 
649 	bands = 0;
650 	setbit(&bands, IEEE80211_MODE_11B);
651 	setbit(&bands, IEEE80211_MODE_11G);
652 	ieee80211_init_channels(ic, NULL, &bands);
653 
654 	/*
655 	 * Do this by own because h/w supports
656 	 * more channels than ieee80211_init_channels()
657 	 */
658 	if (sc->rf_rev == RT2860_RF_2750 ||
659 	    sc->rf_rev == RT2860_RF_2850 ||
660 	    sc->rf_rev == RT3070_RF_3052) {
661 		/* set supported .11a rates */
662 		for (i = 14; i < nitems(rt2860_rf2850); i++) {
663 			uint8_t chan = rt2860_rf2850[i].chan;
664 			ic->ic_channels[ic->ic_nchans].ic_freq =
665 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A);
666 			ic->ic_channels[ic->ic_nchans].ic_ieee = chan;
667 			ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A;
668 			ic->ic_channels[ic->ic_nchans].ic_extieee = 0;
669 			ic->ic_nchans++;
670 		}
671 	}
672 
673 	ieee80211_ifattach(ic, sc->sc_bssid);
674 
675 	ic->ic_scan_start = run_scan_start;
676 	ic->ic_scan_end = run_scan_end;
677 	ic->ic_set_channel = run_set_channel;
678 	ic->ic_node_alloc = run_node_alloc;
679 	ic->ic_newassoc = run_newassoc;
680 	ic->ic_updateslot = run_updateslot;
681 	ic->ic_update_mcast = run_update_mcast;
682 	ic->ic_wme.wme_update = run_wme_update;
683 	ic->ic_raw_xmit = run_raw_xmit;
684 	ic->ic_update_promisc = run_update_promisc;
685 
686 	ic->ic_vap_create = run_vap_create;
687 	ic->ic_vap_delete = run_vap_delete;
688 
689 	ieee80211_radiotap_attach(ic,
690 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
691 		RUN_TX_RADIOTAP_PRESENT,
692 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
693 		RUN_RX_RADIOTAP_PRESENT);
694 
695 	TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
696 	TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
697 	callout_init((struct callout *)&sc->ratectl_ch, 1);
698 
699 	if (bootverbose)
700 		ieee80211_announce(ic);
701 
702 	return (0);
703 
704 detach:
705 	run_detach(self);
706 	return (ENXIO);
707 }
708 
709 static int
710 run_detach(device_t self)
711 {
712 	struct run_softc *sc = device_get_softc(self);
713 	struct ifnet *ifp = sc->sc_ifp;
714 	struct ieee80211com *ic;
715 	int i;
716 
717 	/* stop all USB transfers */
718 	usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
719 
720 	RUN_LOCK(sc);
721 
722 	sc->ratectl_run = RUN_RATECTL_OFF;
723 	sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
724 
725 	/* free TX list, if any */
726 	for (i = 0; i != RUN_EP_QUEUES; i++)
727 		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
728 	RUN_UNLOCK(sc);
729 
730 	if (ifp) {
731 		ic = ifp->if_l2com;
732 		/* drain tasks */
733 		usb_callout_drain(&sc->ratectl_ch);
734 		ieee80211_draintask(ic, &sc->cmdq_task);
735 		ieee80211_draintask(ic, &sc->ratectl_task);
736 		ieee80211_ifdetach(ic);
737 		if_free(ifp);
738 	}
739 
740 	mtx_destroy(&sc->sc_mtx);
741 
742 	return (0);
743 }
744 
745 static struct ieee80211vap *
746 run_vap_create(struct ieee80211com *ic,
747     const char name[IFNAMSIZ], int unit, int opmode, int flags,
748     const uint8_t bssid[IEEE80211_ADDR_LEN],
749     const uint8_t mac[IEEE80211_ADDR_LEN])
750 {
751 	struct ifnet *ifp = ic->ic_ifp;
752 	struct run_softc *sc = ifp->if_softc;
753 	struct run_vap *rvp;
754 	struct ieee80211vap *vap;
755 	int i;
756 
757 	if (sc->rvp_cnt >= RUN_VAP_MAX) {
758 		if_printf(ifp, "number of VAPs maxed out\n");
759 		return (NULL);
760 	}
761 
762 	switch (opmode) {
763 	case IEEE80211_M_STA:
764 		/* enable s/w bmiss handling for sta mode */
765 		flags |= IEEE80211_CLONE_NOBEACONS;
766 		/* fall though */
767 	case IEEE80211_M_IBSS:
768 	case IEEE80211_M_MONITOR:
769 	case IEEE80211_M_HOSTAP:
770 	case IEEE80211_M_MBSS:
771 		/* other than WDS vaps, only one at a time */
772 		if (!TAILQ_EMPTY(&ic->ic_vaps))
773 			return (NULL);
774 		break;
775 	case IEEE80211_M_WDS:
776 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
777 			if(vap->iv_opmode != IEEE80211_M_HOSTAP)
778 				continue;
779 			/* WDS vap's always share the local mac address. */
780 			flags &= ~IEEE80211_CLONE_BSSID;
781 			break;
782 		}
783 		if (vap == NULL) {
784 			if_printf(ifp, "wds only supported in ap mode\n");
785 			return (NULL);
786 		}
787 		break;
788 	default:
789 		if_printf(ifp, "unknown opmode %d\n", opmode);
790 		return (NULL);
791 	}
792 
793 	rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
794 	    M_80211_VAP, M_NOWAIT | M_ZERO);
795 	if (rvp == NULL)
796 		return (NULL);
797 	vap = &rvp->vap;
798 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
799 
800 	vap->iv_key_update_begin = run_key_update_begin;
801 	vap->iv_key_update_end = run_key_update_end;
802 	vap->iv_update_beacon = run_update_beacon;
803 	vap->iv_max_aid = RT2870_WCID_MAX;
804 	/*
805 	 * To delete the right key from h/w, we need wcid.
806 	 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
807 	 * and matching wcid will be written into there. So, cast
808 	 * some spells to remove 'const' from ieee80211_key{}
809 	 */
810 	vap->iv_key_delete = (void *)run_key_delete;
811 	vap->iv_key_set = (void *)run_key_set;
812 
813 	/* override state transition machine */
814 	rvp->newstate = vap->iv_newstate;
815 	vap->iv_newstate = run_newstate;
816 
817 	ieee80211_ratectl_init(vap);
818 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
819 
820 	/* complete setup */
821 	ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
822 
823 	/* make sure id is always unique */
824 	for (i = 0; i < RUN_VAP_MAX; i++) {
825 		if((sc->rvp_bmap & 1 << i) == 0){
826 			sc->rvp_bmap |= 1 << i;
827 			rvp->rvp_id = i;
828 			break;
829 		}
830 	}
831 	if (sc->rvp_cnt++ == 0)
832 		ic->ic_opmode = opmode;
833 
834 	if (opmode == IEEE80211_M_HOSTAP)
835 		sc->cmdq_run = RUN_CMDQ_GO;
836 
837 	DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
838 	    rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
839 
840 	return (vap);
841 }
842 
843 static void
844 run_vap_delete(struct ieee80211vap *vap)
845 {
846 	struct run_vap *rvp = RUN_VAP(vap);
847 	struct ifnet *ifp;
848 	struct ieee80211com *ic;
849 	struct run_softc *sc;
850 	uint8_t rvp_id;
851 
852 	if (vap == NULL)
853 		return;
854 
855 	ic = vap->iv_ic;
856 	ifp = ic->ic_ifp;
857 
858 	sc = ifp->if_softc;
859 
860 	RUN_LOCK(sc);
861 
862 	m_freem(rvp->beacon_mbuf);
863 	rvp->beacon_mbuf = NULL;
864 
865 	rvp_id = rvp->rvp_id;
866 	sc->ratectl_run &= ~(1 << rvp_id);
867 	sc->rvp_bmap &= ~(1 << rvp_id);
868 	run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
869 	run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
870 	--sc->rvp_cnt;
871 
872 	DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
873 	    vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
874 
875 	RUN_UNLOCK(sc);
876 
877 	ieee80211_ratectl_deinit(vap);
878 	ieee80211_vap_detach(vap);
879 	free(rvp, M_80211_VAP);
880 }
881 
882 /*
883  * There are numbers of functions need to be called in context thread.
884  * Rather than creating taskqueue event for each of those functions,
885  * here is all-for-one taskqueue callback function. This function
886  * gurantees deferred functions are executed in the same order they
887  * were enqueued.
888  * '& RUN_CMDQ_MASQ' is to loop cmdq[].
889  */
890 static void
891 run_cmdq_cb(void *arg, int pending)
892 {
893 	struct run_softc *sc = arg;
894 	uint8_t i;
895 
896 	/* call cmdq[].func locked */
897 	RUN_LOCK(sc);
898 	for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
899 	    i = sc->cmdq_exec, pending--) {
900 		DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
901 		if (sc->cmdq_run == RUN_CMDQ_GO) {
902 			/*
903 			 * If arg0 is NULL, callback func needs more
904 			 * than one arg. So, pass ptr to cmdq struct.
905 			 */
906 			if (sc->cmdq[i].arg0)
907 				sc->cmdq[i].func(sc->cmdq[i].arg0);
908 			else
909 				sc->cmdq[i].func(&sc->cmdq[i]);
910 		}
911 		sc->cmdq[i].arg0 = NULL;
912 		sc->cmdq[i].func = NULL;
913 		sc->cmdq_exec++;
914 		sc->cmdq_exec &= RUN_CMDQ_MASQ;
915 	}
916 	RUN_UNLOCK(sc);
917 }
918 
919 static void
920 run_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
921 {
922 	struct run_tx_data *data;
923 
924 	memset(pq, 0, sizeof(*pq));
925 
926 	STAILQ_INIT(&pq->tx_qh);
927 	STAILQ_INIT(&pq->tx_fh);
928 
929 	for (data = &pq->tx_data[0];
930 	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
931 		data->sc = sc;
932 		STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
933 	}
934 	pq->tx_nfree = RUN_TX_RING_COUNT;
935 }
936 
937 static void
938 run_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
939 {
940 	struct run_tx_data *data;
941 
942 	/* make sure any subsequent use of the queues will fail */
943 	pq->tx_nfree = 0;
944 	STAILQ_INIT(&pq->tx_fh);
945 	STAILQ_INIT(&pq->tx_qh);
946 
947 	/* free up all node references and mbufs */
948 	for (data = &pq->tx_data[0];
949 	    data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
950 		if (data->m != NULL) {
951 			m_freem(data->m);
952 			data->m = NULL;
953 		}
954 		if (data->ni != NULL) {
955 			ieee80211_free_node(data->ni);
956 			data->ni = NULL;
957 		}
958 	}
959 }
960 
961 static int
962 run_load_microcode(struct run_softc *sc)
963 {
964 	usb_device_request_t req;
965 	const struct firmware *fw;
966 	const u_char *base;
967 	uint32_t tmp;
968 	int ntries, error;
969 	const uint64_t *temp;
970 	uint64_t bytes;
971 
972 	RUN_UNLOCK(sc);
973 	fw = firmware_get("runfw");
974 	RUN_LOCK(sc);
975 	if (fw == NULL) {
976 		device_printf(sc->sc_dev,
977 		    "failed loadfirmware of file %s\n", "runfw");
978 		return ENOENT;
979 	}
980 
981 	if (fw->datasize != 8192) {
982 		device_printf(sc->sc_dev,
983 		    "invalid firmware size (should be 8KB)\n");
984 		error = EINVAL;
985 		goto fail;
986 	}
987 
988 	/*
989 	 * RT3071/RT3072 use a different firmware
990 	 * run-rt2870 (8KB) contains both,
991 	 * first half (4KB) is for rt2870,
992 	 * last half is for rt3071.
993 	 */
994 	base = fw->data;
995 	if ((sc->mac_ver) != 0x2860 &&
996 	    (sc->mac_ver) != 0x2872 &&
997 	    (sc->mac_ver) != 0x3070) {
998 		base += 4096;
999 	}
1000 
1001 	/* cheap sanity check */
1002 	temp = fw->data;
1003 	bytes = *temp;
1004 	if (bytes != be64toh(0xffffff0210280210)) {
1005 		device_printf(sc->sc_dev, "firmware checksum failed\n");
1006 		error = EINVAL;
1007 		goto fail;
1008 	}
1009 
1010 	run_read(sc, RT2860_ASIC_VER_ID, &tmp);
1011 	/* write microcode image */
1012 	run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1013 	run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1014 	run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1015 
1016 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1017 	req.bRequest = RT2870_RESET;
1018 	USETW(req.wValue, 8);
1019 	USETW(req.wIndex, 0);
1020 	USETW(req.wLength, 0);
1021 	if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL))
1022 	    != 0) {
1023 		device_printf(sc->sc_dev, "firmware reset failed\n");
1024 		goto fail;
1025 	}
1026 
1027 	run_delay(sc, 10);
1028 
1029 	run_write(sc, RT2860_H2M_MAILBOX, 0);
1030 	if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1031 		goto fail;
1032 
1033 	/* wait until microcontroller is ready */
1034 	for (ntries = 0; ntries < 1000; ntries++) {
1035 		if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1036 			goto fail;
1037 		}
1038 		if (tmp & RT2860_MCU_READY)
1039 			break;
1040 		run_delay(sc, 10);
1041 	}
1042 	if (ntries == 1000) {
1043 		device_printf(sc->sc_dev,
1044 		    "timeout waiting for MCU to initialize\n");
1045 		error = ETIMEDOUT;
1046 		goto fail;
1047 	}
1048 	device_printf(sc->sc_dev, "firmware %s loaded\n",
1049 	    (base == fw->data) ? "RT2870" : "RT3071");
1050 
1051 fail:
1052 	firmware_put(fw, FIRMWARE_UNLOAD);
1053 	return (error);
1054 }
1055 
1056 int
1057 run_reset(struct run_softc *sc)
1058 {
1059 	usb_device_request_t req;
1060 
1061 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1062 	req.bRequest = RT2870_RESET;
1063 	USETW(req.wValue, 1);
1064 	USETW(req.wIndex, 0);
1065 	USETW(req.wLength, 0);
1066 	return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1067 }
1068 
1069 static usb_error_t
1070 run_do_request(struct run_softc *sc,
1071     struct usb_device_request *req, void *data)
1072 {
1073 	usb_error_t err;
1074 	int ntries = 10;
1075 
1076 	RUN_LOCK_ASSERT(sc, MA_OWNED);
1077 
1078 	while (ntries--) {
1079 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1080 		    req, data, 0, NULL, 250 /* ms */);
1081 		if (err == 0)
1082 			break;
1083 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
1084 		    usbd_errstr(err));
1085 		run_delay(sc, 10);
1086 	}
1087 	return (err);
1088 }
1089 
1090 static int
1091 run_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1092 {
1093 	uint32_t tmp;
1094 	int error;
1095 
1096 	error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1097 	if (error == 0)
1098 		*val = le32toh(tmp);
1099 	else
1100 		*val = 0xffffffff;
1101 	return (error);
1102 }
1103 
1104 static int
1105 run_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1106 {
1107 	usb_device_request_t req;
1108 
1109 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1110 	req.bRequest = RT2870_READ_REGION_1;
1111 	USETW(req.wValue, 0);
1112 	USETW(req.wIndex, reg);
1113 	USETW(req.wLength, len);
1114 
1115 	return (run_do_request(sc, &req, buf));
1116 }
1117 
1118 static int
1119 run_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1120 {
1121 	usb_device_request_t req;
1122 
1123 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1124 	req.bRequest = RT2870_WRITE_2;
1125 	USETW(req.wValue, val);
1126 	USETW(req.wIndex, reg);
1127 	USETW(req.wLength, 0);
1128 
1129 	return (run_do_request(sc, &req, NULL));
1130 }
1131 
1132 static int
1133 run_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1134 {
1135 	int error;
1136 
1137 	if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1138 		error = run_write_2(sc, reg + 2, val >> 16);
1139 	return (error);
1140 }
1141 
1142 static int
1143 run_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1144     int len)
1145 {
1146 #if 1
1147 	int i, error = 0;
1148 	/*
1149 	 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1150 	 * We thus issue multiple WRITE_2 commands instead.
1151 	 */
1152 	KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1153 	for (i = 0; i < len && error == 0; i += 2)
1154 		error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1155 	return (error);
1156 #else
1157 	usb_device_request_t req;
1158 
1159 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1160 	req.bRequest = RT2870_WRITE_REGION_1;
1161 	USETW(req.wValue, 0);
1162 	USETW(req.wIndex, reg);
1163 	USETW(req.wLength, len);
1164 	return (run_do_request(sc, &req, buf));
1165 #endif
1166 }
1167 
1168 static int
1169 run_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1170 {
1171 	int i, error = 0;
1172 
1173 	KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1174 	for (i = 0; i < len && error == 0; i += 4)
1175 		error = run_write(sc, reg + i, val);
1176 	return (error);
1177 }
1178 
1179 /* Read 16-bit from eFUSE ROM (RT3070 only.) */
1180 static int
1181 run_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1182 {
1183 	uint32_t tmp;
1184 	uint16_t reg;
1185 	int error, ntries;
1186 
1187 	if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1188 		return (error);
1189 
1190 	addr *= 2;
1191 	/*-
1192 	 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1193 	 * DATA0: F E D C
1194 	 * DATA1: B A 9 8
1195 	 * DATA2: 7 6 5 4
1196 	 * DATA3: 3 2 1 0
1197 	 */
1198 	tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1199 	tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1200 	run_write(sc, RT3070_EFUSE_CTRL, tmp);
1201 	for (ntries = 0; ntries < 100; ntries++) {
1202 		if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1203 			return (error);
1204 		if (!(tmp & RT3070_EFSROM_KICK))
1205 			break;
1206 		run_delay(sc, 2);
1207 	}
1208 	if (ntries == 100)
1209 		return (ETIMEDOUT);
1210 
1211 	if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1212 		*val = 0xffff;	/* address not found */
1213 		return (0);
1214 	}
1215 	/* determine to which 32-bit register our 16-bit word belongs */
1216 	reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1217 	if ((error = run_read(sc, reg, &tmp)) != 0)
1218 		return (error);
1219 
1220 	*val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1221 	return (0);
1222 }
1223 
1224 static int
1225 run_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1226 {
1227 	usb_device_request_t req;
1228 	uint16_t tmp;
1229 	int error;
1230 
1231 	addr *= 2;
1232 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1233 	req.bRequest = RT2870_EEPROM_READ;
1234 	USETW(req.wValue, 0);
1235 	USETW(req.wIndex, addr);
1236 	USETW(req.wLength, sizeof tmp);
1237 
1238 	error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1239 	if (error == 0)
1240 		*val = le16toh(tmp);
1241 	else
1242 		*val = 0xffff;
1243 	return (error);
1244 }
1245 
1246 static __inline int
1247 run_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1248 {
1249 	/* either eFUSE ROM or EEPROM */
1250 	return sc->sc_srom_read(sc, addr, val);
1251 }
1252 
1253 static int
1254 run_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val)
1255 {
1256 	uint32_t tmp;
1257 	int error, ntries;
1258 
1259 	for (ntries = 0; ntries < 10; ntries++) {
1260 		if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1261 			return (error);
1262 		if (!(tmp & RT2860_RF_REG_CTRL))
1263 			break;
1264 	}
1265 	if (ntries == 10)
1266 		return (ETIMEDOUT);
1267 
1268 	/* RF registers are 24-bit on the RT2860 */
1269 	tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1270 	    (val & 0x3fffff) << 2 | (reg & 3);
1271 	return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1272 }
1273 
1274 static int
1275 run_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1276 {
1277 	uint32_t tmp;
1278 	int error, ntries;
1279 
1280 	for (ntries = 0; ntries < 100; ntries++) {
1281 		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1282 			return (error);
1283 		if (!(tmp & RT3070_RF_KICK))
1284 			break;
1285 	}
1286 	if (ntries == 100)
1287 		return (ETIMEDOUT);
1288 
1289 	tmp = RT3070_RF_KICK | reg << 8;
1290 	if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1291 		return (error);
1292 
1293 	for (ntries = 0; ntries < 100; ntries++) {
1294 		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1295 			return (error);
1296 		if (!(tmp & RT3070_RF_KICK))
1297 			break;
1298 	}
1299 	if (ntries == 100)
1300 		return (ETIMEDOUT);
1301 
1302 	*val = tmp & 0xff;
1303 	return (0);
1304 }
1305 
1306 static int
1307 run_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1308 {
1309 	uint32_t tmp;
1310 	int error, ntries;
1311 
1312 	for (ntries = 0; ntries < 10; ntries++) {
1313 		if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1314 			return (error);
1315 		if (!(tmp & RT3070_RF_KICK))
1316 			break;
1317 	}
1318 	if (ntries == 10)
1319 		return (ETIMEDOUT);
1320 
1321 	tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1322 	return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1323 }
1324 
1325 static int
1326 run_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1327 {
1328 	uint32_t tmp;
1329 	int ntries, error;
1330 
1331 	for (ntries = 0; ntries < 10; ntries++) {
1332 		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1333 			return (error);
1334 		if (!(tmp & RT2860_BBP_CSR_KICK))
1335 			break;
1336 	}
1337 	if (ntries == 10)
1338 		return (ETIMEDOUT);
1339 
1340 	tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1341 	if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1342 		return (error);
1343 
1344 	for (ntries = 0; ntries < 10; ntries++) {
1345 		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1346 			return (error);
1347 		if (!(tmp & RT2860_BBP_CSR_KICK))
1348 			break;
1349 	}
1350 	if (ntries == 10)
1351 		return (ETIMEDOUT);
1352 
1353 	*val = tmp & 0xff;
1354 	return (0);
1355 }
1356 
1357 static int
1358 run_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1359 {
1360 	uint32_t tmp;
1361 	int ntries, error;
1362 
1363 	for (ntries = 0; ntries < 10; ntries++) {
1364 		if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1365 			return (error);
1366 		if (!(tmp & RT2860_BBP_CSR_KICK))
1367 			break;
1368 	}
1369 	if (ntries == 10)
1370 		return (ETIMEDOUT);
1371 
1372 	tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1373 	return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1374 }
1375 
1376 /*
1377  * Send a command to the 8051 microcontroller unit.
1378  */
1379 static int
1380 run_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1381 {
1382 	uint32_t tmp;
1383 	int error, ntries;
1384 
1385 	for (ntries = 0; ntries < 100; ntries++) {
1386 		if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1387 			return error;
1388 		if (!(tmp & RT2860_H2M_BUSY))
1389 			break;
1390 	}
1391 	if (ntries == 100)
1392 		return ETIMEDOUT;
1393 
1394 	tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1395 	if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1396 		error = run_write(sc, RT2860_HOST_CMD, cmd);
1397 	return (error);
1398 }
1399 
1400 /*
1401  * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1402  * Used to adjust per-rate Tx power registers.
1403  */
1404 static __inline uint32_t
1405 b4inc(uint32_t b32, int8_t delta)
1406 {
1407 	int8_t i, b4;
1408 
1409 	for (i = 0; i < 8; i++) {
1410 		b4 = b32 & 0xf;
1411 		b4 += delta;
1412 		if (b4 < 0)
1413 			b4 = 0;
1414 		else if (b4 > 0xf)
1415 			b4 = 0xf;
1416 		b32 = b32 >> 4 | b4 << 28;
1417 	}
1418 	return (b32);
1419 }
1420 
1421 static const char *
1422 run_get_rf(int rev)
1423 {
1424 	switch (rev) {
1425 	case RT2860_RF_2820:	return "RT2820";
1426 	case RT2860_RF_2850:	return "RT2850";
1427 	case RT2860_RF_2720:	return "RT2720";
1428 	case RT2860_RF_2750:	return "RT2750";
1429 	case RT3070_RF_3020:	return "RT3020";
1430 	case RT3070_RF_2020:	return "RT2020";
1431 	case RT3070_RF_3021:	return "RT3021";
1432 	case RT3070_RF_3022:	return "RT3022";
1433 	case RT3070_RF_3052:	return "RT3052";
1434 	}
1435 	return ("unknown");
1436 }
1437 
1438 int
1439 run_read_eeprom(struct run_softc *sc)
1440 {
1441 	int8_t delta_2ghz, delta_5ghz;
1442 	uint32_t tmp;
1443 	uint16_t val;
1444 	int ridx, ant, i;
1445 
1446 	/* check whether the ROM is eFUSE ROM or EEPROM */
1447 	sc->sc_srom_read = run_eeprom_read_2;
1448 	if (sc->mac_ver >= 0x3070) {
1449 		run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1450 		DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1451 		if (tmp & RT3070_SEL_EFUSE)
1452 			sc->sc_srom_read = run_efuse_read_2;
1453 	}
1454 
1455 	/* read ROM version */
1456 	run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1457 	DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1458 
1459 	/* read MAC address */
1460 	run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1461 	sc->sc_bssid[0] = val & 0xff;
1462 	sc->sc_bssid[1] = val >> 8;
1463 	run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1464 	sc->sc_bssid[2] = val & 0xff;
1465 	sc->sc_bssid[3] = val >> 8;
1466 	run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1467 	sc->sc_bssid[4] = val & 0xff;
1468 	sc->sc_bssid[5] = val >> 8;
1469 
1470 	/* read vender BBP settings */
1471 	for (i = 0; i < 10; i++) {
1472 		run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1473 		sc->bbp[i].val = val & 0xff;
1474 		sc->bbp[i].reg = val >> 8;
1475 		DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val);
1476 	}
1477 	if (sc->mac_ver >= 0x3071) {
1478 		/* read vendor RF settings */
1479 		for (i = 0; i < 10; i++) {
1480 			run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val);
1481 			sc->rf[i].val = val & 0xff;
1482 			sc->rf[i].reg = val >> 8;
1483 			DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1484 			    sc->rf[i].val);
1485 		}
1486 	}
1487 
1488 	/* read RF frequency offset from EEPROM */
1489 	run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1490 	sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1491 	DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1492 
1493 	if (val >> 8 != 0xff) {
1494 		/* read LEDs operating mode */
1495 		sc->leds = val >> 8;
1496 		run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1497 		run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1498 		run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1499 	} else {
1500 		/* broken EEPROM, use default settings */
1501 		sc->leds = 0x01;
1502 		sc->led[0] = 0x5555;
1503 		sc->led[1] = 0x2221;
1504 		sc->led[2] = 0x5627;	/* differs from RT2860 */
1505 	}
1506 	DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1507 	    sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1508 
1509 	/* read RF information */
1510 	run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1511 	if (val == 0xffff) {
1512 		DPRINTF("invalid EEPROM antenna info, using default\n");
1513 		if (sc->mac_ver == 0x3572) {
1514 			/* default to RF3052 2T2R */
1515 			sc->rf_rev = RT3070_RF_3052;
1516 			sc->ntxchains = 2;
1517 			sc->nrxchains = 2;
1518 		} else if (sc->mac_ver >= 0x3070) {
1519 			/* default to RF3020 1T1R */
1520 			sc->rf_rev = RT3070_RF_3020;
1521 			sc->ntxchains = 1;
1522 			sc->nrxchains = 1;
1523 		} else {
1524 			/* default to RF2820 1T2R */
1525 			sc->rf_rev = RT2860_RF_2820;
1526 			sc->ntxchains = 1;
1527 			sc->nrxchains = 2;
1528 		}
1529 	} else {
1530 		sc->rf_rev = (val >> 8) & 0xf;
1531 		sc->ntxchains = (val >> 4) & 0xf;
1532 		sc->nrxchains = val & 0xf;
1533 	}
1534 	DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n",
1535 	    sc->rf_rev, sc->ntxchains, sc->nrxchains);
1536 
1537 	/* check if RF supports automatic Tx access gain control */
1538 	run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1539 	DPRINTF("EEPROM CFG 0x%04x\n", val);
1540 	/* check if driver should patch the DAC issue */
1541 	if ((val >> 8) != 0xff)
1542 		sc->patch_dac = (val >> 15) & 1;
1543 	if ((val & 0xff) != 0xff) {
1544 		sc->ext_5ghz_lna = (val >> 3) & 1;
1545 		sc->ext_2ghz_lna = (val >> 2) & 1;
1546 		/* check if RF supports automatic Tx access gain control */
1547 		sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1548 		/* check if we have a hardware radio switch */
1549 		sc->rfswitch = val & 1;
1550 	}
1551 
1552 	/* read power settings for 2GHz channels */
1553 	for (i = 0; i < 14; i += 2) {
1554 		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1555 		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1556 		sc->txpow1[i + 1] = (int8_t)(val >> 8);
1557 
1558 		run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1559 		sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1560 		sc->txpow2[i + 1] = (int8_t)(val >> 8);
1561 	}
1562 	/* fix broken Tx power entries */
1563 	for (i = 0; i < 14; i++) {
1564 		if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1565 			sc->txpow1[i] = 5;
1566 		if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1567 			sc->txpow2[i] = 5;
1568 		DPRINTF("chan %d: power1=%d, power2=%d\n",
1569 		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1570 	}
1571 	/* read power settings for 5GHz channels */
1572 	for (i = 0; i < 40; i += 2) {
1573 		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1574 		sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1575 		sc->txpow1[i + 15] = (int8_t)(val >> 8);
1576 
1577 		run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1578 		sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1579 		sc->txpow2[i + 15] = (int8_t)(val >> 8);
1580 	}
1581 	/* fix broken Tx power entries */
1582 	for (i = 0; i < 40; i++) {
1583 		if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1584 			sc->txpow1[14 + i] = 5;
1585 		if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1586 			sc->txpow2[14 + i] = 5;
1587 		DPRINTF("chan %d: power1=%d, power2=%d\n",
1588 		    rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1589 		    sc->txpow2[14 + i]);
1590 	}
1591 
1592 	/* read Tx power compensation for each Tx rate */
1593 	run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1594 	delta_2ghz = delta_5ghz = 0;
1595 	if ((val & 0xff) != 0xff && (val & 0x80)) {
1596 		delta_2ghz = val & 0xf;
1597 		if (!(val & 0x40))	/* negative number */
1598 			delta_2ghz = -delta_2ghz;
1599 	}
1600 	val >>= 8;
1601 	if ((val & 0xff) != 0xff && (val & 0x80)) {
1602 		delta_5ghz = val & 0xf;
1603 		if (!(val & 0x40))	/* negative number */
1604 			delta_5ghz = -delta_5ghz;
1605 	}
1606 	DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1607 	    delta_2ghz, delta_5ghz);
1608 
1609 	for (ridx = 0; ridx < 5; ridx++) {
1610 		uint32_t reg;
1611 
1612 		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1613 		reg = val;
1614 		run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1615 		reg |= (uint32_t)val << 16;
1616 
1617 		sc->txpow20mhz[ridx] = reg;
1618 		sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1619 		sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1620 
1621 		DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1622 		    "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1623 		    sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1624 	}
1625 
1626 	/* read RSSI offsets and LNA gains from EEPROM */
1627 	run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1628 	sc->rssi_2ghz[0] = val & 0xff;	/* Ant A */
1629 	sc->rssi_2ghz[1] = val >> 8;	/* Ant B */
1630 	run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1631 	if (sc->mac_ver >= 0x3070) {
1632 		/*
1633 		 * On RT3070 chips (limited to 2 Rx chains), this ROM
1634 		 * field contains the Tx mixer gain for the 2GHz band.
1635 		 */
1636 		if ((val & 0xff) != 0xff)
1637 			sc->txmixgain_2ghz = val & 0x7;
1638 		DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1639 	} else
1640 		sc->rssi_2ghz[2] = val & 0xff;	/* Ant C */
1641 	sc->lna[2] = val >> 8;		/* channel group 2 */
1642 
1643 	run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1644 	sc->rssi_5ghz[0] = val & 0xff;	/* Ant A */
1645 	sc->rssi_5ghz[1] = val >> 8;	/* Ant B */
1646 	run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1647 	if (sc->mac_ver == 0x3572) {
1648 		/*
1649 		 * On RT3572 chips (limited to 2 Rx chains), this ROM
1650 		 * field contains the Tx mixer gain for the 5GHz band.
1651 		 */
1652 		if ((val & 0xff) != 0xff)
1653 			sc->txmixgain_5ghz = val & 0x7;
1654 		DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1655 	} else
1656 		sc->rssi_5ghz[2] = val & 0xff;	/* Ant C */
1657 	sc->lna[3] = val >> 8;		/* channel group 3 */
1658 
1659 	run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1660 	sc->lna[0] = val & 0xff;	/* channel group 0 */
1661 	sc->lna[1] = val >> 8;		/* channel group 1 */
1662 
1663 	/* fix broken 5GHz LNA entries */
1664 	if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1665 		DPRINTF("invalid LNA for channel group %d\n", 2);
1666 		sc->lna[2] = sc->lna[1];
1667 	}
1668 	if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1669 		DPRINTF("invalid LNA for channel group %d\n", 3);
1670 		sc->lna[3] = sc->lna[1];
1671 	}
1672 
1673 	/* fix broken RSSI offset entries */
1674 	for (ant = 0; ant < 3; ant++) {
1675 		if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1676 			DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1677 			    ant + 1, sc->rssi_2ghz[ant]);
1678 			sc->rssi_2ghz[ant] = 0;
1679 		}
1680 		if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1681 			DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1682 			    ant + 1, sc->rssi_5ghz[ant]);
1683 			sc->rssi_5ghz[ant] = 0;
1684 		}
1685 	}
1686 	return (0);
1687 }
1688 
1689 static struct ieee80211_node *
1690 run_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1691 {
1692 	return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1693 }
1694 
1695 static int
1696 run_media_change(struct ifnet *ifp)
1697 {
1698 	struct ieee80211vap *vap = ifp->if_softc;
1699 	struct ieee80211com *ic = vap->iv_ic;
1700 	const struct ieee80211_txparam *tp;
1701 	struct run_softc *sc = ic->ic_ifp->if_softc;
1702 	uint8_t rate, ridx;
1703 	int error;
1704 
1705 	RUN_LOCK(sc);
1706 
1707 	error = ieee80211_media_change(ifp);
1708 	if (error != ENETRESET) {
1709 		RUN_UNLOCK(sc);
1710 		return (error);
1711 	}
1712 
1713 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1714 	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1715 		struct ieee80211_node *ni;
1716 		struct run_node	*rn;
1717 
1718 		rate = ic->ic_sup_rates[ic->ic_curmode].
1719 		    rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1720 		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1721 			if (rt2860_rates[ridx].rate == rate)
1722 				break;
1723 		ni = ieee80211_ref_node(vap->iv_bss);
1724 		rn = (struct run_node *)ni;
1725 		rn->fix_ridx = ridx;
1726 		DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1727 		ieee80211_free_node(ni);
1728 	}
1729 
1730 #if 0
1731 	if ((ifp->if_flags & IFF_UP) &&
1732 	    (ifp->if_drv_flags &  IFF_DRV_RUNNING)){
1733 		run_init_locked(sc);
1734 	}
1735 #endif
1736 
1737 	RUN_UNLOCK(sc);
1738 
1739 	return (0);
1740 }
1741 
1742 static int
1743 run_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1744 {
1745 	const struct ieee80211_txparam *tp;
1746 	struct ieee80211com *ic = vap->iv_ic;
1747 	struct run_softc *sc = ic->ic_ifp->if_softc;
1748 	struct run_vap *rvp = RUN_VAP(vap);
1749 	enum ieee80211_state ostate;
1750 	uint32_t sta[3];
1751 	uint32_t tmp;
1752 	uint8_t ratectl;
1753 	uint8_t restart_ratectl = 0;
1754 	uint8_t bid = 1 << rvp->rvp_id;
1755 
1756 	ostate = vap->iv_state;
1757 	DPRINTF("%s -> %s\n",
1758 		ieee80211_state_name[ostate],
1759 		ieee80211_state_name[nstate]);
1760 
1761 	IEEE80211_UNLOCK(ic);
1762 	RUN_LOCK(sc);
1763 
1764 	ratectl = sc->ratectl_run; /* remember current state */
1765 	sc->ratectl_run = RUN_RATECTL_OFF;
1766 	usb_callout_stop(&sc->ratectl_ch);
1767 
1768 	if (ostate == IEEE80211_S_RUN) {
1769 		/* turn link LED off */
1770 		run_set_leds(sc, RT2860_LED_RADIO);
1771 	}
1772 
1773 	switch (nstate) {
1774 	case IEEE80211_S_INIT:
1775 		restart_ratectl = 1;
1776 
1777 		if (ostate != IEEE80211_S_RUN)
1778 			break;
1779 
1780 		ratectl &= ~bid;
1781 		sc->runbmap &= ~bid;
1782 
1783 		/* abort TSF synchronization if there is no vap running */
1784 		if (--sc->running == 0) {
1785 			run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1786 			run_write(sc, RT2860_BCN_TIME_CFG,
1787 			    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1788 			    RT2860_TBTT_TIMER_EN));
1789 		}
1790 		break;
1791 
1792 	case IEEE80211_S_RUN:
1793 		if (!(sc->runbmap & bid)) {
1794 			if(sc->running++)
1795 				restart_ratectl = 1;
1796 			sc->runbmap |= bid;
1797 		}
1798 
1799 		m_freem(rvp->beacon_mbuf);
1800 		rvp->beacon_mbuf = NULL;
1801 
1802 		switch (vap->iv_opmode) {
1803 		case IEEE80211_M_HOSTAP:
1804 		case IEEE80211_M_MBSS:
1805 			sc->ap_running |= bid;
1806 			ic->ic_opmode = vap->iv_opmode;
1807 			run_update_beacon_cb(vap);
1808 			break;
1809 		case IEEE80211_M_IBSS:
1810 			sc->adhoc_running |= bid;
1811 			if (!sc->ap_running)
1812 				ic->ic_opmode = vap->iv_opmode;
1813 			run_update_beacon_cb(vap);
1814 			break;
1815 		case IEEE80211_M_STA:
1816 			sc->sta_running |= bid;
1817 			if (!sc->ap_running && !sc->adhoc_running)
1818 				ic->ic_opmode = vap->iv_opmode;
1819 
1820 			/* read statistic counters (clear on read) */
1821 			run_read_region_1(sc, RT2860_TX_STA_CNT0,
1822 			    (uint8_t *)sta, sizeof sta);
1823 
1824 			break;
1825 		default:
1826 			ic->ic_opmode = vap->iv_opmode;
1827 			break;
1828 		}
1829 
1830 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1831 			struct ieee80211_node *ni;
1832 
1833 			run_updateslot(ic->ic_ifp);
1834 			run_enable_mrr(sc);
1835 			run_set_txpreamble(sc);
1836 			run_set_basicrates(sc);
1837 			ni = ieee80211_ref_node(vap->iv_bss);
1838 			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1839 			run_set_bssid(sc, ni->ni_bssid);
1840 			ieee80211_free_node(ni);
1841 			run_enable_tsf_sync(sc);
1842 
1843 			/* enable automatic rate adaptation */
1844 			tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1845 			if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1846 				ratectl |= bid;
1847 		}
1848 
1849 		/* turn link LED on */
1850 		run_set_leds(sc, RT2860_LED_RADIO |
1851 		    (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1852 		     RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1853 
1854 		break;
1855 	default:
1856 		DPRINTFN(6, "undefined case\n");
1857 		break;
1858 	}
1859 
1860 	/* restart amrr for running VAPs */
1861 	if ((sc->ratectl_run = ratectl) && restart_ratectl)
1862 		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1863 
1864 	RUN_UNLOCK(sc);
1865 	IEEE80211_LOCK(ic);
1866 
1867 	return(rvp->newstate(vap, nstate, arg));
1868 }
1869 
1870 /* ARGSUSED */
1871 static void
1872 run_wme_update_cb(void *arg)
1873 {
1874 	struct ieee80211com *ic = arg;
1875 	struct run_softc *sc = ic->ic_ifp->if_softc;
1876 	struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1877 	int aci, error = 0;
1878 
1879 	RUN_LOCK_ASSERT(sc, MA_OWNED);
1880 
1881 	/* update MAC TX configuration registers */
1882 	for (aci = 0; aci < WME_NUM_AC; aci++) {
1883 		error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1884 		    wmesp->wme_params[aci].wmep_logcwmax << 16 |
1885 		    wmesp->wme_params[aci].wmep_logcwmin << 12 |
1886 		    wmesp->wme_params[aci].wmep_aifsn  <<  8 |
1887 		    wmesp->wme_params[aci].wmep_txopLimit);
1888 		if (error) goto err;
1889 	}
1890 
1891 	/* update SCH/DMA registers too */
1892 	error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1893 	    wmesp->wme_params[WME_AC_VO].wmep_aifsn  << 12 |
1894 	    wmesp->wme_params[WME_AC_VI].wmep_aifsn  <<  8 |
1895 	    wmesp->wme_params[WME_AC_BK].wmep_aifsn  <<  4 |
1896 	    wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1897 	if (error) goto err;
1898 	error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1899 	    wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1900 	    wmesp->wme_params[WME_AC_VI].wmep_logcwmin <<  8 |
1901 	    wmesp->wme_params[WME_AC_BK].wmep_logcwmin <<  4 |
1902 	    wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1903 	if (error) goto err;
1904 	error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1905 	    wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1906 	    wmesp->wme_params[WME_AC_VI].wmep_logcwmax <<  8 |
1907 	    wmesp->wme_params[WME_AC_BK].wmep_logcwmax <<  4 |
1908 	    wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1909 	if (error) goto err;
1910 	error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1911 	    wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1912 	    wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1913 	if (error) goto err;
1914 	error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1915 	    wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1916 	    wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1917 
1918 err:
1919 	if (error)
1920 		DPRINTF("WME update failed\n");
1921 
1922 	return;
1923 }
1924 
1925 static int
1926 run_wme_update(struct ieee80211com *ic)
1927 {
1928 	struct run_softc *sc = ic->ic_ifp->if_softc;
1929 
1930 	/* sometime called wothout lock */
1931 	if (mtx_owned(&ic->ic_comlock.mtx)) {
1932 		uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1933 		DPRINTF("cmdq_store=%d\n", i);
1934 		sc->cmdq[i].func = run_wme_update_cb;
1935 		sc->cmdq[i].arg0 = ic;
1936 		ieee80211_runtask(ic, &sc->cmdq_task);
1937 		return (0);
1938 	}
1939 
1940 	RUN_LOCK(sc);
1941 	run_wme_update_cb(ic);
1942 	RUN_UNLOCK(sc);
1943 
1944 	/* return whatever, upper layer desn't care anyway */
1945 	return (0);
1946 }
1947 
1948 static void
1949 run_key_update_begin(struct ieee80211vap *vap)
1950 {
1951 	/*
1952 	 * To avoid out-of-order events, both run_key_set() and
1953 	 * _delete() are deferred and handled by run_cmdq_cb().
1954 	 * So, there is nothing we need to do here.
1955 	 */
1956 }
1957 
1958 static void
1959 run_key_update_end(struct ieee80211vap *vap)
1960 {
1961 	/* null */
1962 }
1963 
1964 static void
1965 run_key_set_cb(void *arg)
1966 {
1967 	struct run_cmdq *cmdq = arg;
1968 	struct ieee80211vap *vap = cmdq->arg1;
1969 	struct ieee80211_key *k = cmdq->k;
1970 	struct ieee80211com *ic = vap->iv_ic;
1971 	struct run_softc *sc = ic->ic_ifp->if_softc;
1972 	struct ieee80211_node *ni;
1973 	uint32_t attr;
1974 	uint16_t base, associd;
1975 	uint8_t mode, wcid, iv[8];
1976 
1977 	RUN_LOCK_ASSERT(sc, MA_OWNED);
1978 
1979 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1980 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
1981 	else
1982 		ni = vap->iv_bss;
1983 	associd = (ni != NULL) ? ni->ni_associd : 0;
1984 
1985 	/* map net80211 cipher to RT2860 security mode */
1986 	switch (k->wk_cipher->ic_cipher) {
1987 	case IEEE80211_CIPHER_WEP:
1988 		if(k->wk_keylen < 8)
1989 			mode = RT2860_MODE_WEP40;
1990 		else
1991 			mode = RT2860_MODE_WEP104;
1992 		break;
1993 	case IEEE80211_CIPHER_TKIP:
1994 		mode = RT2860_MODE_TKIP;
1995 		break;
1996 	case IEEE80211_CIPHER_AES_CCM:
1997 		mode = RT2860_MODE_AES_CCMP;
1998 		break;
1999 	default:
2000 		DPRINTF("undefined case\n");
2001 		return;
2002 	}
2003 
2004 	DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2005 	    associd, k->wk_keyix, mode,
2006 	    (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2007 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2008 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2009 
2010 	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2011 		wcid = 0;	/* NB: update WCID0 for group keys */
2012 		base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2013 	} else {
2014 		wcid = RUN_AID2WCID(associd);
2015 		base = RT2860_PKEY(wcid);
2016 	}
2017 
2018 	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2019 		if(run_write_region_1(sc, base, k->wk_key, 16))
2020 			return;
2021 		if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8))	/* wk_txmic */
2022 			return;
2023 		if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8))	/* wk_rxmic */
2024 			return;
2025 	} else {
2026 		/* roundup len to 16-bit: XXX fix write_region_1() instead */
2027 		if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2028 			return;
2029 	}
2030 
2031 	if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2032 	    (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2033 		/* set initial packet number in IV+EIV */
2034 		if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2035 			memset(iv, 0, sizeof iv);
2036 			iv[3] = vap->iv_def_txkey << 6;
2037 		} else {
2038 			if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2039 				iv[0] = k->wk_keytsc >> 8;
2040 				iv[1] = (iv[0] | 0x20) & 0x7f;
2041 				iv[2] = k->wk_keytsc;
2042 			} else /* CCMP */ {
2043 				iv[0] = k->wk_keytsc;
2044 				iv[1] = k->wk_keytsc >> 8;
2045 				iv[2] = 0;
2046 			}
2047 			iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2048 			iv[4] = k->wk_keytsc >> 16;
2049 			iv[5] = k->wk_keytsc >> 24;
2050 			iv[6] = k->wk_keytsc >> 32;
2051 			iv[7] = k->wk_keytsc >> 40;
2052 		}
2053 		if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2054 			return;
2055 	}
2056 
2057 	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2058 		/* install group key */
2059 		if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2060 			return;
2061 		attr &= ~(0xf << (k->wk_keyix * 4));
2062 		attr |= mode << (k->wk_keyix * 4);
2063 		if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2064 			return;
2065 	} else {
2066 		/* install pairwise key */
2067 		if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2068 			return;
2069 		attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2070 		if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2071 			return;
2072 	}
2073 
2074 	/* TODO create a pass-thru key entry? */
2075 
2076 	/* need wcid to delete the right key later */
2077 	k->wk_pad = wcid;
2078 }
2079 
2080 /*
2081  * Don't have to be deferred, but in order to keep order of
2082  * execution, i.e. with run_key_delete(), defer this and let
2083  * run_cmdq_cb() maintain the order.
2084  *
2085  * return 0 on error
2086  */
2087 static int
2088 run_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2089 		const uint8_t mac[IEEE80211_ADDR_LEN])
2090 {
2091 	struct ieee80211com *ic = vap->iv_ic;
2092 	struct run_softc *sc = ic->ic_ifp->if_softc;
2093 	uint32_t i;
2094 
2095 	i = RUN_CMDQ_GET(&sc->cmdq_store);
2096 	DPRINTF("cmdq_store=%d\n", i);
2097 	sc->cmdq[i].func = run_key_set_cb;
2098 	sc->cmdq[i].arg0 = NULL;
2099 	sc->cmdq[i].arg1 = vap;
2100 	sc->cmdq[i].k = k;
2101 	IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2102 	ieee80211_runtask(ic, &sc->cmdq_task);
2103 
2104 	/*
2105 	 * To make sure key will be set when hostapd
2106 	 * calls iv_key_set() before if_init().
2107 	 */
2108 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2109 		RUN_LOCK(sc);
2110 		sc->cmdq_key_set = RUN_CMDQ_GO;
2111 		RUN_UNLOCK(sc);
2112 	}
2113 
2114 	return (1);
2115 }
2116 
2117 /*
2118  * If wlan is destroyed without being brought down i.e. without
2119  * wlan down or wpa_cli terminate, this function is called after
2120  * vap is gone. Don't refer it.
2121  */
2122 static void
2123 run_key_delete_cb(void *arg)
2124 {
2125 	struct run_cmdq *cmdq = arg;
2126 	struct run_softc *sc = cmdq->arg1;
2127 	struct ieee80211_key *k = &cmdq->key;
2128 	uint32_t attr;
2129 	uint8_t wcid;
2130 
2131 	RUN_LOCK_ASSERT(sc, MA_OWNED);
2132 
2133 	if (k->wk_flags & IEEE80211_KEY_GROUP) {
2134 		/* remove group key */
2135 		DPRINTF("removing group key\n");
2136 		run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2137 		attr &= ~(0xf << (k->wk_keyix * 4));
2138 		run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2139 	} else {
2140 		/* remove pairwise key */
2141 		DPRINTF("removing key for wcid %x\n", k->wk_pad);
2142 		/* matching wcid was written to wk_pad in run_key_set() */
2143 		wcid = k->wk_pad;
2144 		run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2145 		attr &= ~0xf;
2146 		run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2147 		run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2148 	}
2149 
2150 	k->wk_pad = 0;
2151 }
2152 
2153 /*
2154  * return 0 on error
2155  */
2156 static int
2157 run_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2158 {
2159 	struct ieee80211com *ic = vap->iv_ic;
2160 	struct run_softc *sc = ic->ic_ifp->if_softc;
2161 	struct ieee80211_key *k0;
2162 	uint32_t i;
2163 
2164 	/*
2165 	 * When called back, key might be gone. So, make a copy
2166 	 * of some values need to delete keys before deferring.
2167 	 * But, because of LOR with node lock, cannot use lock here.
2168 	 * So, use atomic instead.
2169 	 */
2170 	i = RUN_CMDQ_GET(&sc->cmdq_store);
2171 	DPRINTF("cmdq_store=%d\n", i);
2172 	sc->cmdq[i].func = run_key_delete_cb;
2173 	sc->cmdq[i].arg0 = NULL;
2174 	sc->cmdq[i].arg1 = sc;
2175 	k0 = &sc->cmdq[i].key;
2176 	k0->wk_flags = k->wk_flags;
2177 	k0->wk_keyix = k->wk_keyix;
2178 	/* matching wcid was written to wk_pad in run_key_set() */
2179 	k0->wk_pad = k->wk_pad;
2180 	ieee80211_runtask(ic, &sc->cmdq_task);
2181 	return (1);	/* return fake success */
2182 
2183 }
2184 
2185 static void
2186 run_ratectl_to(void *arg)
2187 {
2188 	struct run_softc *sc = arg;
2189 
2190 	/* do it in a process context, so it can go sleep */
2191 	ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2192 	/* next timeout will be rescheduled in the callback task */
2193 }
2194 
2195 /* ARGSUSED */
2196 static void
2197 run_ratectl_cb(void *arg, int pending)
2198 {
2199 	struct run_softc *sc = arg;
2200 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2201 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2202 
2203 	if (vap == NULL)
2204 		return;
2205 
2206 	if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2207 		run_iter_func(sc, vap->iv_bss);
2208 	else {
2209 		/*
2210 		 * run_reset_livelock() doesn't do anything with AMRR,
2211 		 * but Ralink wants us to call it every 1 sec. So, we
2212 		 * piggyback here rather than creating another callout.
2213 		 * Livelock may occur only in HOSTAP or IBSS mode
2214 		 * (when h/w is sending beacons).
2215 		 */
2216 		RUN_LOCK(sc);
2217 		run_reset_livelock(sc);
2218 		/* just in case, there are some stats to drain */
2219 		run_drain_fifo(sc);
2220 		RUN_UNLOCK(sc);
2221 		ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2222 	}
2223 
2224 	if(sc->ratectl_run != RUN_RATECTL_OFF)
2225 		usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2226 }
2227 
2228 static void
2229 run_drain_fifo(void *arg)
2230 {
2231 	struct run_softc *sc = arg;
2232 	struct ifnet *ifp = sc->sc_ifp;
2233 	uint32_t stat;
2234 	uint16_t (*wstat)[3];
2235 	uint8_t wcid, mcs, pid;
2236 	int8_t retry;
2237 
2238 	RUN_LOCK_ASSERT(sc, MA_OWNED);
2239 
2240 	for (;;) {
2241 		/* drain Tx status FIFO (maxsize = 16) */
2242 		run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2243 		DPRINTFN(4, "tx stat 0x%08x\n", stat);
2244 		if (!(stat & RT2860_TXQ_VLD))
2245 			break;
2246 
2247 		wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2248 
2249 		/* if no ACK was requested, no feedback is available */
2250 		if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2251 		    wcid == 0)
2252 			continue;
2253 
2254 		/*
2255 		 * Even though each stat is Tx-complete-status like format,
2256 		 * the device can poll stats. Because there is no guarantee
2257 		 * that the referring node is still around when read the stats.
2258 		 * So that, if we use ieee80211_ratectl_tx_update(), we will
2259 		 * have hard time not to refer already freed node.
2260 		 *
2261 		 * To eliminate such page faults, we poll stats in softc.
2262 		 * Then, update the rates later with ieee80211_ratectl_tx_update().
2263 		 */
2264 		wstat = &(sc->wcid_stats[wcid]);
2265 		(*wstat)[RUN_TXCNT]++;
2266 		if (stat & RT2860_TXQ_OK)
2267 			(*wstat)[RUN_SUCCESS]++;
2268 		else
2269 			ifp->if_oerrors++;
2270 		/*
2271 		 * Check if there were retries, ie if the Tx success rate is
2272 		 * different from the requested rate. Note that it works only
2273 		 * because we do not allow rate fallback from OFDM to CCK.
2274 		 */
2275 		mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2276 		pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2277 		if ((retry = pid -1 - mcs) > 0) {
2278 			(*wstat)[RUN_TXCNT] += retry;
2279 			(*wstat)[RUN_RETRY] += retry;
2280 		}
2281 	}
2282 	DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2283 
2284 	sc->fifo_cnt = 0;
2285 }
2286 
2287 static void
2288 run_iter_func(void *arg, struct ieee80211_node *ni)
2289 {
2290 	struct run_softc *sc = arg;
2291 	struct ieee80211vap *vap = ni->ni_vap;
2292 	struct ieee80211com *ic = ni->ni_ic;
2293 	struct ifnet *ifp = ic->ic_ifp;
2294 	struct run_node *rn = (void *)ni;
2295 	union run_stats sta[2];
2296 	uint16_t (*wstat)[3];
2297 	int txcnt, success, retrycnt, error;
2298 
2299 	RUN_LOCK(sc);
2300 
2301 	if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2302 	    vap->iv_opmode == IEEE80211_M_STA)) {
2303 		/* read statistic counters (clear on read) and update AMRR state */
2304 		error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2305 		    sizeof sta);
2306 		if (error != 0)
2307 			goto fail;
2308 
2309 		/* count failed TX as errors */
2310 		ifp->if_oerrors += le16toh(sta[0].error.fail);
2311 
2312 		retrycnt = le16toh(sta[1].tx.retry);
2313 		success = le16toh(sta[1].tx.success);
2314 		txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2315 
2316 		DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2317 			retrycnt, success, le16toh(sta[0].error.fail));
2318 	} else {
2319 		wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2320 
2321 		if (wstat == &(sc->wcid_stats[0]) ||
2322 		    wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2323 			goto fail;
2324 
2325 		txcnt = (*wstat)[RUN_TXCNT];
2326 		success = (*wstat)[RUN_SUCCESS];
2327 		retrycnt = (*wstat)[RUN_RETRY];
2328 		DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2329 		    retrycnt, txcnt, success);
2330 
2331 		memset(wstat, 0, sizeof(*wstat));
2332 	}
2333 
2334 	ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2335 	rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2336 
2337 fail:
2338 	RUN_UNLOCK(sc);
2339 
2340 	DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2341 }
2342 
2343 static void
2344 run_newassoc_cb(void *arg)
2345 {
2346 	struct run_cmdq *cmdq = arg;
2347 	struct ieee80211_node *ni = cmdq->arg1;
2348 	struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2349 	uint8_t wcid = cmdq->wcid;
2350 
2351 	RUN_LOCK_ASSERT(sc, MA_OWNED);
2352 
2353 	run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2354 	    ni->ni_macaddr, IEEE80211_ADDR_LEN);
2355 
2356 	memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2357 }
2358 
2359 static void
2360 run_newassoc(struct ieee80211_node *ni, int isnew)
2361 {
2362 	struct run_node *rn = (void *)ni;
2363 	struct ieee80211_rateset *rs = &ni->ni_rates;
2364 	struct ieee80211vap *vap = ni->ni_vap;
2365 	struct ieee80211com *ic = vap->iv_ic;
2366 	struct run_softc *sc = ic->ic_ifp->if_softc;
2367 	uint8_t rate;
2368 	uint8_t ridx;
2369 	uint8_t wcid = RUN_AID2WCID(ni->ni_associd);
2370 	int i, j;
2371 
2372 	if (wcid > RT2870_WCID_MAX) {
2373 		device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2374 		return;
2375 	}
2376 
2377 	/* only interested in true associations */
2378 	if (isnew && ni->ni_associd != 0) {
2379 
2380 		/*
2381 		 * This function could is called though timeout function.
2382 		 * Need to defer.
2383 		 */
2384 		uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2385 		DPRINTF("cmdq_store=%d\n", cnt);
2386 		sc->cmdq[cnt].func = run_newassoc_cb;
2387 		sc->cmdq[cnt].arg0 = NULL;
2388 		sc->cmdq[cnt].arg1 = ni;
2389 		sc->cmdq[cnt].wcid = wcid;
2390 		ieee80211_runtask(ic, &sc->cmdq_task);
2391 	}
2392 
2393 	DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2394 	    isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2395 
2396 	for (i = 0; i < rs->rs_nrates; i++) {
2397 		rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2398 		/* convert 802.11 rate to hardware rate index */
2399 		for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2400 			if (rt2860_rates[ridx].rate == rate)
2401 				break;
2402 		rn->ridx[i] = ridx;
2403 		/* determine rate of control response frames */
2404 		for (j = i; j >= 0; j--) {
2405 			if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2406 			    rt2860_rates[rn->ridx[i]].phy ==
2407 			    rt2860_rates[rn->ridx[j]].phy)
2408 				break;
2409 		}
2410 		if (j >= 0) {
2411 			rn->ctl_ridx[i] = rn->ridx[j];
2412 		} else {
2413 			/* no basic rate found, use mandatory one */
2414 			rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2415 		}
2416 		DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2417 		    rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2418 	}
2419 	rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2420 	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2421 		if (rt2860_rates[ridx].rate == rate)
2422 			break;
2423 	rn->mgt_ridx = ridx;
2424 	DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2425 
2426 	usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2427 }
2428 
2429 /*
2430  * Return the Rx chain with the highest RSSI for a given frame.
2431  */
2432 static __inline uint8_t
2433 run_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2434 {
2435 	uint8_t rxchain = 0;
2436 
2437 	if (sc->nrxchains > 1) {
2438 		if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2439 			rxchain = 1;
2440 		if (sc->nrxchains > 2)
2441 			if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2442 				rxchain = 2;
2443 	}
2444 	return (rxchain);
2445 }
2446 
2447 static void
2448 run_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2449 {
2450 	struct ifnet *ifp = sc->sc_ifp;
2451 	struct ieee80211com *ic = ifp->if_l2com;
2452 	struct ieee80211_frame *wh;
2453 	struct ieee80211_node *ni;
2454 	struct rt2870_rxd *rxd;
2455 	struct rt2860_rxwi *rxwi;
2456 	uint32_t flags;
2457 	uint16_t len, phy;
2458 	uint8_t ant, rssi;
2459 	int8_t nf;
2460 
2461 	rxwi = mtod(m, struct rt2860_rxwi *);
2462 	len = le16toh(rxwi->len) & 0xfff;
2463 	if (__predict_false(len > dmalen)) {
2464 		m_freem(m);
2465 		ifp->if_ierrors++;
2466 		DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2467 		return;
2468 	}
2469 	/* Rx descriptor is located at the end */
2470 	rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2471 	flags = le32toh(rxd->flags);
2472 
2473 	if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2474 		m_freem(m);
2475 		ifp->if_ierrors++;
2476 		DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2477 		return;
2478 	}
2479 
2480 	m->m_data += sizeof(struct rt2860_rxwi);
2481 	m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2482 
2483 	wh = mtod(m, struct ieee80211_frame *);
2484 
2485 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2486 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2487 		m->m_flags |= M_WEP;
2488 	}
2489 
2490 	if (flags & RT2860_RX_L2PAD) {
2491 		DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2492 		len += 2;
2493 	}
2494 
2495 	ni = ieee80211_find_rxnode(ic,
2496 	    mtod(m, struct ieee80211_frame_min *));
2497 
2498 	if (__predict_false(flags & RT2860_RX_MICERR)) {
2499 		/* report MIC failures to net80211 for TKIP */
2500 		if (ni != NULL)
2501 			ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2502 		m_freem(m);
2503 		ifp->if_ierrors++;
2504 		DPRINTF("MIC error. Someone is lying.\n");
2505 		return;
2506 	}
2507 
2508 	ant = run_maxrssi_chain(sc, rxwi);
2509 	rssi = rxwi->rssi[ant];
2510 	nf = run_rssi2dbm(sc, rssi, ant);
2511 
2512 	m->m_pkthdr.rcvif = ifp;
2513 	m->m_pkthdr.len = m->m_len = len;
2514 
2515 	if (ni != NULL) {
2516 		(void)ieee80211_input(ni, m, rssi, nf);
2517 		ieee80211_free_node(ni);
2518 	} else {
2519 		(void)ieee80211_input_all(ic, m, rssi, nf);
2520 	}
2521 
2522 	if (__predict_false(ieee80211_radiotap_active(ic))) {
2523 		struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2524 
2525 		tap->wr_flags = 0;
2526 		tap->wr_chan_freq = htole16(ic->ic_bsschan->ic_freq);
2527 		tap->wr_chan_flags = htole16(ic->ic_bsschan->ic_flags);
2528 		tap->wr_antsignal = rssi;
2529 		tap->wr_antenna = ant;
2530 		tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2531 		tap->wr_rate = 2;	/* in case it can't be found below */
2532 		phy = le16toh(rxwi->phy);
2533 		switch (phy & RT2860_PHY_MODE) {
2534 		case RT2860_PHY_CCK:
2535 			switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2536 			case 0:	tap->wr_rate =   2; break;
2537 			case 1:	tap->wr_rate =   4; break;
2538 			case 2:	tap->wr_rate =  11; break;
2539 			case 3:	tap->wr_rate =  22; break;
2540 			}
2541 			if (phy & RT2860_PHY_SHPRE)
2542 				tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2543 			break;
2544 		case RT2860_PHY_OFDM:
2545 			switch (phy & RT2860_PHY_MCS) {
2546 			case 0:	tap->wr_rate =  12; break;
2547 			case 1:	tap->wr_rate =  18; break;
2548 			case 2:	tap->wr_rate =  24; break;
2549 			case 3:	tap->wr_rate =  36; break;
2550 			case 4:	tap->wr_rate =  48; break;
2551 			case 5:	tap->wr_rate =  72; break;
2552 			case 6:	tap->wr_rate =  96; break;
2553 			case 7:	tap->wr_rate = 108; break;
2554 			}
2555 			break;
2556 		}
2557 	}
2558 }
2559 
2560 static void
2561 run_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2562 {
2563 	struct run_softc *sc = usbd_xfer_softc(xfer);
2564 	struct ifnet *ifp = sc->sc_ifp;
2565 	struct mbuf *m = NULL;
2566 	struct mbuf *m0;
2567 	uint32_t dmalen;
2568 	int xferlen;
2569 
2570 	usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2571 
2572 	switch (USB_GET_STATE(xfer)) {
2573 	case USB_ST_TRANSFERRED:
2574 
2575 		DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2576 
2577 		if (xferlen < sizeof (uint32_t) +
2578 		    sizeof (struct rt2860_rxwi) + sizeof (struct rt2870_rxd)) {
2579 			DPRINTF("xfer too short %d\n", xferlen);
2580 			goto tr_setup;
2581 		}
2582 
2583 		m = sc->rx_m;
2584 		sc->rx_m = NULL;
2585 
2586 		/* FALLTHROUGH */
2587 	case USB_ST_SETUP:
2588 tr_setup:
2589 		if (sc->rx_m == NULL) {
2590 			sc->rx_m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR,
2591 			    MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2592 		}
2593 		if (sc->rx_m == NULL) {
2594 			DPRINTF("could not allocate mbuf - idle with stall\n");
2595 			ifp->if_ierrors++;
2596 			usbd_xfer_set_stall(xfer);
2597 			usbd_xfer_set_frames(xfer, 0);
2598 		} else {
2599 			/*
2600 			 * Directly loading a mbuf cluster into DMA to
2601 			 * save some data copying. This works because
2602 			 * there is only one cluster.
2603 			 */
2604 			usbd_xfer_set_frame_data(xfer, 0,
2605 			    mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2606 			usbd_xfer_set_frames(xfer, 1);
2607 		}
2608 		usbd_transfer_submit(xfer);
2609 		break;
2610 
2611 	default:	/* Error */
2612 		if (error != USB_ERR_CANCELLED) {
2613 			/* try to clear stall first */
2614 			usbd_xfer_set_stall(xfer);
2615 
2616 			if (error == USB_ERR_TIMEOUT)
2617 				device_printf(sc->sc_dev, "device timeout\n");
2618 
2619 			ifp->if_ierrors++;
2620 
2621 			goto tr_setup;
2622 		}
2623 		if (sc->rx_m != NULL) {
2624 			m_freem(sc->rx_m);
2625 			sc->rx_m = NULL;
2626 		}
2627 		break;
2628 	}
2629 
2630 	if (m == NULL)
2631 		return;
2632 
2633 	/* inputting all the frames must be last */
2634 
2635 	RUN_UNLOCK(sc);
2636 
2637 	m->m_pkthdr.len = m->m_len = xferlen;
2638 
2639 	/* HW can aggregate multiple 802.11 frames in a single USB xfer */
2640 	for(;;) {
2641 		dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2642 
2643 		if ((dmalen == 0) || ((dmalen & 3) != 0)) {
2644 			DPRINTF("bad DMA length %u\n", dmalen);
2645 			break;
2646 		}
2647 		if ((dmalen + 8) > xferlen) {
2648 			DPRINTF("bad DMA length %u > %d\n",
2649 			dmalen + 8, xferlen);
2650 			break;
2651 		}
2652 
2653 		/* If it is the last one or a single frame, we won't copy. */
2654 		if ((xferlen -= dmalen + 8) <= 8) {
2655 			/* trim 32-bit DMA-len header */
2656 			m->m_data += 4;
2657 			m->m_pkthdr.len = m->m_len -= 4;
2658 			run_rx_frame(sc, m, dmalen);
2659 			break;
2660 		}
2661 
2662 		/* copy aggregated frames to another mbuf */
2663 		m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2664 		if (__predict_false(m0 == NULL)) {
2665 			DPRINTF("could not allocate mbuf\n");
2666 			ifp->if_ierrors++;
2667 			break;
2668 		}
2669 		m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2670 		    dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2671 		m0->m_pkthdr.len = m0->m_len =
2672 		    dmalen + sizeof(struct rt2870_rxd);
2673 		run_rx_frame(sc, m0, dmalen);
2674 
2675 		/* update data ptr */
2676 		m->m_data += dmalen + 8;
2677 		m->m_pkthdr.len = m->m_len -= dmalen + 8;
2678 	}
2679 
2680 	RUN_LOCK(sc);
2681 }
2682 
2683 static void
2684 run_tx_free(struct run_endpoint_queue *pq,
2685     struct run_tx_data *data, int txerr)
2686 {
2687 	if (data->m != NULL) {
2688 		if (data->m->m_flags & M_TXCB)
2689 			ieee80211_process_callback(data->ni, data->m,
2690 			    txerr ? ETIMEDOUT : 0);
2691 		m_freem(data->m);
2692 		data->m = NULL;
2693 
2694 		if (data->ni == NULL) {
2695 			DPRINTF("no node\n");
2696 		} else {
2697 			ieee80211_free_node(data->ni);
2698 			data->ni = NULL;
2699 		}
2700 	}
2701 
2702 	STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2703 	pq->tx_nfree++;
2704 }
2705 
2706 static void
2707 run_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index)
2708 {
2709 	struct run_softc *sc = usbd_xfer_softc(xfer);
2710 	struct ifnet *ifp = sc->sc_ifp;
2711 	struct ieee80211com *ic = ifp->if_l2com;
2712 	struct run_tx_data *data;
2713 	struct ieee80211vap *vap = NULL;
2714 	struct usb_page_cache *pc;
2715 	struct run_endpoint_queue *pq = &sc->sc_epq[index];
2716 	struct mbuf *m;
2717 	usb_frlength_t size;
2718 	unsigned int len;
2719 	int actlen;
2720 	int sumlen;
2721 
2722 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2723 
2724 	switch (USB_GET_STATE(xfer)) {
2725 	case USB_ST_TRANSFERRED:
2726 		DPRINTFN(11, "transfer complete: %d "
2727 		    "bytes @ index %d\n", actlen, index);
2728 
2729 		data = usbd_xfer_get_priv(xfer);
2730 
2731 		run_tx_free(pq, data, 0);
2732 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2733 
2734 		usbd_xfer_set_priv(xfer, NULL);
2735 
2736 		ifp->if_opackets++;
2737 
2738 		/* FALLTHROUGH */
2739 	case USB_ST_SETUP:
2740 tr_setup:
2741 		data = STAILQ_FIRST(&pq->tx_qh);
2742 		if (data == NULL)
2743 			break;
2744 
2745 		STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2746 
2747 		m = data->m;
2748 		if (m->m_pkthdr.len > RUN_MAX_TXSZ) {
2749 			DPRINTF("data overflow, %u bytes\n",
2750 			    m->m_pkthdr.len);
2751 
2752 			ifp->if_oerrors++;
2753 
2754 			run_tx_free(pq, data, 1);
2755 
2756 			goto tr_setup;
2757 		}
2758 
2759 		pc = usbd_xfer_get_frame(xfer, 0);
2760 		size = sizeof(data->desc);
2761 		usbd_copy_in(pc, 0, &data->desc, size);
2762 		usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2763 
2764 		vap = data->ni->ni_vap;
2765 		if (ieee80211_radiotap_active_vap(vap)) {
2766 			struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2767 			struct rt2860_txwi *txwi =
2768 			    (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2769 
2770 			tap->wt_flags = 0;
2771 			tap->wt_rate = rt2860_rates[data->ridx].rate;
2772 			tap->wt_chan_freq = htole16(vap->iv_bss->ni_chan->ic_freq);
2773 			tap->wt_chan_flags = htole16(vap->iv_bss->ni_chan->ic_flags);
2774 			tap->wt_hwqueue = index;
2775 			if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2776 				tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2777 
2778 			ieee80211_radiotap_tx(vap, m);
2779 		}
2780 
2781 		/* align end on a 4-bytes boundary */
2782 		len = (size + IEEE80211_CRC_LEN + m->m_pkthdr.len + 3) & ~3;
2783 
2784 		DPRINTFN(11, "sending frame len=%u xferlen=%u @ index %d\n",
2785 			m->m_pkthdr.len, len, index);
2786 
2787 		usbd_xfer_set_frame_len(xfer, 0, len);
2788 		usbd_xfer_set_priv(xfer, data);
2789 
2790 		usbd_transfer_submit(xfer);
2791 
2792 		RUN_UNLOCK(sc);
2793 		run_start(ifp);
2794 		RUN_LOCK(sc);
2795 
2796 		break;
2797 
2798 	default:
2799 		DPRINTF("USB transfer error, %s\n",
2800 		    usbd_errstr(error));
2801 
2802 		data = usbd_xfer_get_priv(xfer);
2803 
2804 		ifp->if_oerrors++;
2805 
2806 		if (data != NULL) {
2807 			if(data->ni != NULL)
2808 				vap = data->ni->ni_vap;
2809 			run_tx_free(pq, data, error);
2810 			usbd_xfer_set_priv(xfer, NULL);
2811 		}
2812 		if (vap == NULL)
2813 			vap = TAILQ_FIRST(&ic->ic_vaps);
2814 
2815 		if (error != USB_ERR_CANCELLED) {
2816 			if (error == USB_ERR_TIMEOUT) {
2817 				device_printf(sc->sc_dev, "device timeout\n");
2818 				uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2819 				DPRINTF("cmdq_store=%d\n", i);
2820 				sc->cmdq[i].func = run_usb_timeout_cb;
2821 				sc->cmdq[i].arg0 = vap;
2822 				ieee80211_runtask(ic, &sc->cmdq_task);
2823 			}
2824 
2825 			/*
2826 			 * Try to clear stall first, also if other
2827 			 * errors occur, hence clearing stall
2828 			 * introduces a 50 ms delay:
2829 			 */
2830 			usbd_xfer_set_stall(xfer);
2831 			goto tr_setup;
2832 		}
2833 		break;
2834 	}
2835 }
2836 
2837 static void
2838 run_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2839 {
2840 	run_bulk_tx_callbackN(xfer, error, 0);
2841 }
2842 
2843 static void
2844 run_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2845 {
2846 	run_bulk_tx_callbackN(xfer, error, 1);
2847 }
2848 
2849 static void
2850 run_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2851 {
2852 	run_bulk_tx_callbackN(xfer, error, 2);
2853 }
2854 
2855 static void
2856 run_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2857 {
2858 	run_bulk_tx_callbackN(xfer, error, 3);
2859 }
2860 
2861 static void
2862 run_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2863 {
2864 	run_bulk_tx_callbackN(xfer, error, 4);
2865 }
2866 
2867 static void
2868 run_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2869 {
2870 	run_bulk_tx_callbackN(xfer, error, 5);
2871 }
2872 
2873 static void
2874 run_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2875 {
2876 	struct mbuf *m = data->m;
2877 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2878 	struct ieee80211vap *vap = data->ni->ni_vap;
2879 	struct ieee80211_frame *wh;
2880 	struct rt2870_txd *txd;
2881 	struct rt2860_txwi *txwi;
2882 	uint16_t xferlen;
2883 	uint16_t mcs;
2884 	uint8_t ridx = data->ridx;
2885 	uint8_t pad;
2886 
2887 	/* get MCS code from rate index */
2888 	mcs = rt2860_rates[ridx].mcs;
2889 
2890 	xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2891 
2892 	/* roundup to 32-bit alignment */
2893 	xferlen = (xferlen + 3) & ~3;
2894 
2895 	txd = (struct rt2870_txd *)&data->desc;
2896 	txd->len = htole16(xferlen);
2897 
2898 	wh = mtod(m, struct ieee80211_frame *);
2899 
2900 	/*
2901 	 * Ether both are true or both are false, the header
2902 	 * are nicely aligned to 32-bit. So, no L2 padding.
2903 	 */
2904 	if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2905 		pad = 0;
2906 	else
2907 		pad = 2;
2908 
2909 	/* setup TX Wireless Information */
2910 	txwi = (struct rt2860_txwi *)(txd + 1);
2911 	txwi->len = htole16(m->m_pkthdr.len - pad);
2912 	if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2913 		txwi->phy = htole16(RT2860_PHY_CCK);
2914 		if (ridx != RT2860_RIDX_CCK1 &&
2915 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2916 			mcs |= RT2860_PHY_SHPRE;
2917 	} else
2918 		txwi->phy = htole16(RT2860_PHY_OFDM);
2919 	txwi->phy |= htole16(mcs);
2920 
2921 	/* check if RTS/CTS or CTS-to-self protection is required */
2922 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2923 	    (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2924 	     ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2925 	      rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
2926 		txwi->txop |= RT2860_TX_TXOP_HT;
2927 	else
2928 		txwi->txop |= RT2860_TX_TXOP_BACKOFF;
2929 
2930 	if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
2931 		txwi->xflags |= RT2860_TX_NSEQ;
2932 }
2933 
2934 /* This function must be called locked */
2935 static int
2936 run_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2937 {
2938 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2939 	struct ieee80211vap *vap = ni->ni_vap;
2940 	struct ieee80211_frame *wh;
2941 	struct ieee80211_channel *chan;
2942 	const struct ieee80211_txparam *tp;
2943 	struct run_node *rn = (void *)ni;
2944 	struct run_tx_data *data;
2945 	struct rt2870_txd *txd;
2946 	struct rt2860_txwi *txwi;
2947 	uint16_t qos;
2948 	uint16_t dur;
2949 	uint16_t qid;
2950 	uint8_t type;
2951 	uint8_t tid;
2952 	uint8_t ridx;
2953 	uint8_t ctl_ridx;
2954 	uint8_t qflags;
2955 	uint8_t xflags = 0;
2956 	int hasqos;
2957 
2958 	RUN_LOCK_ASSERT(sc, MA_OWNED);
2959 
2960 	wh = mtod(m, struct ieee80211_frame *);
2961 
2962 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2963 
2964 	/*
2965 	 * There are 7 bulk endpoints: 1 for RX
2966 	 * and 6 for TX (4 EDCAs + HCCA + Prio).
2967 	 * Update 03-14-2009:  some devices like the Planex GW-US300MiniS
2968 	 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
2969 	 */
2970 	if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
2971 		uint8_t *frm;
2972 
2973 		if(IEEE80211_HAS_ADDR4(wh))
2974 			frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
2975 		else
2976 			frm =((struct ieee80211_qosframe *)wh)->i_qos;
2977 
2978 		qos = le16toh(*(const uint16_t *)frm);
2979 		tid = qos & IEEE80211_QOS_TID;
2980 		qid = TID_TO_WME_AC(tid);
2981 	} else {
2982 		qos = 0;
2983 		tid = 0;
2984 		qid = WME_AC_BE;
2985 	}
2986 	qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
2987 
2988 	DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
2989 	    qos, qid, tid, qflags);
2990 
2991 	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
2992 	tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2993 
2994 	/* pickup a rate index */
2995 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2996 	    type != IEEE80211_FC0_TYPE_DATA) {
2997 		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2998 		    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
2999 		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3000 	} else {
3001 		if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3002 			ridx = rn->fix_ridx;
3003 		else
3004 			ridx = rn->amrr_ridx;
3005 		ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3006 	}
3007 
3008 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3009 	    (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3010 	     IEEE80211_QOS_ACKPOLICY_NOACK)) {
3011 		xflags |= RT2860_TX_ACK;
3012 		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3013 			dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3014 		else
3015 			dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3016 		*(uint16_t *)wh->i_dur = htole16(dur);
3017 	}
3018 
3019 	/* reserve slots for mgmt packets, just in case */
3020 	if (sc->sc_epq[qid].tx_nfree < 3) {
3021 		DPRINTFN(10, "tx ring %d is full\n", qid);
3022 		return (-1);
3023 	}
3024 
3025 	data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3026 	STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3027 	sc->sc_epq[qid].tx_nfree--;
3028 
3029 	txd = (struct rt2870_txd *)&data->desc;
3030 	txd->flags = qflags;
3031 	txwi = (struct rt2860_txwi *)(txd + 1);
3032 	txwi->xflags = xflags;
3033 	txwi->wcid = IEEE80211_IS_MULTICAST(wh->i_addr1) ?
3034 	    0 : RUN_AID2WCID(ni->ni_associd);
3035 	/* clear leftover garbage bits */
3036 	txwi->flags = 0;
3037 	txwi->txop = 0;
3038 
3039 	data->m = m;
3040 	data->ni = ni;
3041 	data->ridx = ridx;
3042 
3043 	run_set_tx_desc(sc, data);
3044 
3045 	/*
3046 	 * The chip keeps track of 2 kind of Tx stats,
3047 	 *  * TX_STAT_FIFO, for per WCID stats, and
3048 	 *  * TX_STA_CNT0 for all-TX-in-one stats.
3049 	 *
3050 	 * To use FIFO stats, we need to store MCS into the driver-private
3051  	 * PacketID field. So that, we can tell whose stats when we read them.
3052  	 * We add 1 to the MCS because setting the PacketID field to 0 means
3053  	 * that we don't want feedback in TX_STAT_FIFO.
3054  	 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3055  	 *
3056  	 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3057  	 */
3058 	if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3059 	    vap->iv_opmode == IEEE80211_M_MBSS) {
3060 		uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3061 		txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3062 
3063 		/*
3064 		 * Unlike PCI based devices, we don't get any interrupt from
3065 		 * USB devices, so we simulate FIFO-is-full interrupt here.
3066 		 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3067 		 * quickly get fulled. To prevent overflow, increment a counter on
3068 		 * every FIFO stat request, so we know how many slots are left.
3069 		 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3070 		 * are used only in those modes.
3071 		 * We just drain stats. AMRR gets updated every 1 sec by
3072 		 * run_ratectl_cb() via callout.
3073 		 * Call it early. Otherwise overflow.
3074 		 */
3075 		if (sc->fifo_cnt++ == 10) {
3076 			/*
3077 			 * With multiple vaps or if_bridge, if_start() is called
3078 			 * with a non-sleepable lock, tcpinp. So, need to defer.
3079 			 */
3080 			uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3081 			DPRINTFN(6, "cmdq_store=%d\n", i);
3082 			sc->cmdq[i].func = run_drain_fifo;
3083 			sc->cmdq[i].arg0 = sc;
3084 			ieee80211_runtask(ic, &sc->cmdq_task);
3085 		}
3086 	}
3087 
3088         STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3089 
3090 	usbd_transfer_start(sc->sc_xfer[qid]);
3091 
3092 	DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3093 	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3094 	    rt2860_rates[ridx].rate, qid);
3095 
3096 	return (0);
3097 }
3098 
3099 static int
3100 run_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3101 {
3102 	struct ifnet *ifp = sc->sc_ifp;
3103 	struct ieee80211com *ic = ifp->if_l2com;
3104 	struct run_node *rn = (void *)ni;
3105 	struct run_tx_data *data;
3106 	struct ieee80211_frame *wh;
3107 	struct rt2870_txd *txd;
3108 	struct rt2860_txwi *txwi;
3109 	uint16_t dur;
3110 	uint8_t ridx = rn->mgt_ridx;
3111 	uint8_t type;
3112 	uint8_t xflags = 0;
3113 	uint8_t wflags = 0;
3114 
3115 	RUN_LOCK_ASSERT(sc, MA_OWNED);
3116 
3117 	wh = mtod(m, struct ieee80211_frame *);
3118 
3119 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3120 
3121 	/* tell hardware to add timestamp for probe responses */
3122 	if ((wh->i_fc[0] &
3123 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3124 	    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3125 		wflags |= RT2860_TX_TS;
3126 	else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3127 		xflags |= RT2860_TX_ACK;
3128 
3129 		dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3130 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3131 		*(uint16_t *)wh->i_dur = htole16(dur);
3132 	}
3133 
3134 	if (sc->sc_epq[0].tx_nfree == 0) {
3135 		/* let caller free mbuf */
3136 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3137 		return (EIO);
3138 	}
3139 	data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3140 	STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3141 	sc->sc_epq[0].tx_nfree--;
3142 
3143 	txd = (struct rt2870_txd *)&data->desc;
3144 	txd->flags = RT2860_TX_QSEL_EDCA;
3145 	txwi = (struct rt2860_txwi *)(txd + 1);
3146 	txwi->wcid = 0xff;
3147 	txwi->flags = wflags;
3148 	txwi->xflags = xflags;
3149 	txwi->txop = 0;	/* clear leftover garbage bits */
3150 
3151 	data->m = m;
3152 	data->ni = ni;
3153 	data->ridx = ridx;
3154 
3155 	run_set_tx_desc(sc, data);
3156 
3157 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3158 	    (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3159 	    rt2860_rates[ridx].rate);
3160 
3161 	STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3162 
3163 	usbd_transfer_start(sc->sc_xfer[0]);
3164 
3165 	return (0);
3166 }
3167 
3168 static int
3169 run_sendprot(struct run_softc *sc,
3170     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3171 {
3172 	struct ieee80211com *ic = ni->ni_ic;
3173 	struct ieee80211_frame *wh;
3174 	struct run_tx_data *data;
3175 	struct rt2870_txd *txd;
3176 	struct rt2860_txwi *txwi;
3177 	struct mbuf *mprot;
3178 	int ridx;
3179 	int protrate;
3180 	int ackrate;
3181 	int pktlen;
3182 	int isshort;
3183 	uint16_t dur;
3184 	uint8_t type;
3185 	uint8_t wflags = 0;
3186 	uint8_t xflags = 0;
3187 
3188 	RUN_LOCK_ASSERT(sc, MA_OWNED);
3189 
3190 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3191 	    ("protection %d", prot));
3192 
3193 	wh = mtod(m, struct ieee80211_frame *);
3194 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3195 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3196 
3197 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3198 	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3199 
3200 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3201 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3202 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3203 	wflags = RT2860_TX_FRAG;
3204 
3205 	/* check that there are free slots before allocating the mbuf */
3206 	if (sc->sc_epq[0].tx_nfree == 0) {
3207 		/* let caller free mbuf */
3208 		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3209 		return (ENOBUFS);
3210 	}
3211 
3212 	if (prot == IEEE80211_PROT_RTSCTS) {
3213 		/* NB: CTS is the same size as an ACK */
3214 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3215 		xflags |= RT2860_TX_ACK;
3216 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3217 	} else {
3218 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3219 	}
3220 	if (mprot == NULL) {
3221 		sc->sc_ifp->if_oerrors++;
3222 		DPRINTF("could not allocate mbuf\n");
3223 		return (ENOBUFS);
3224 	}
3225 
3226         data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3227         STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3228         sc->sc_epq[0].tx_nfree--;
3229 
3230 	txd = (struct rt2870_txd *)&data->desc;
3231 	txd->flags = RT2860_TX_QSEL_EDCA;
3232 	txwi = (struct rt2860_txwi *)(txd + 1);
3233 	txwi->wcid = 0xff;
3234 	txwi->flags = wflags;
3235 	txwi->xflags = xflags;
3236 	txwi->txop = 0;	/* clear leftover garbage bits */
3237 
3238 	data->m = mprot;
3239 	data->ni = ieee80211_ref_node(ni);
3240 
3241 	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3242 		if (rt2860_rates[ridx].rate == protrate)
3243 			break;
3244 	data->ridx = ridx;
3245 
3246 	run_set_tx_desc(sc, data);
3247 
3248         DPRINTFN(1, "sending prot len=%u rate=%u\n",
3249             m->m_pkthdr.len, rate);
3250 
3251         STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3252 
3253 	usbd_transfer_start(sc->sc_xfer[0]);
3254 
3255 	return (0);
3256 }
3257 
3258 static int
3259 run_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3260     const struct ieee80211_bpf_params *params)
3261 {
3262 	struct ieee80211com *ic = ni->ni_ic;
3263 	struct ieee80211_frame *wh;
3264 	struct run_tx_data *data;
3265 	struct rt2870_txd *txd;
3266 	struct rt2860_txwi *txwi;
3267 	uint8_t type;
3268 	uint8_t ridx;
3269 	uint8_t rate;
3270 	uint8_t opflags = 0;
3271 	uint8_t xflags = 0;
3272 	int error;
3273 
3274 	RUN_LOCK_ASSERT(sc, MA_OWNED);
3275 
3276 	KASSERT(params != NULL, ("no raw xmit params"));
3277 
3278 	wh = mtod(m, struct ieee80211_frame *);
3279 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3280 
3281 	rate = params->ibp_rate0;
3282 	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3283 		/* let caller free mbuf */
3284 		return (EINVAL);
3285 	}
3286 
3287 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3288 		xflags |= RT2860_TX_ACK;
3289 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3290 		error = run_sendprot(sc, m, ni,
3291 		    params->ibp_flags & IEEE80211_BPF_RTS ?
3292 			IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3293 		    rate);
3294 		if (error) {
3295 			/* let caller free mbuf */
3296 			return error;
3297 		}
3298 		opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3299 	}
3300 
3301 	if (sc->sc_epq[0].tx_nfree == 0) {
3302 		/* let caller free mbuf */
3303 		sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3304 		DPRINTF("sending raw frame, but tx ring is full\n");
3305 		return (EIO);
3306 	}
3307         data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3308         STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3309         sc->sc_epq[0].tx_nfree--;
3310 
3311 	txd = (struct rt2870_txd *)&data->desc;
3312 	txd->flags = RT2860_TX_QSEL_EDCA;
3313 	txwi = (struct rt2860_txwi *)(txd + 1);
3314 	txwi->wcid = 0xff;
3315 	txwi->xflags = xflags;
3316 	txwi->txop = opflags;
3317 	txwi->flags = 0;	/* clear leftover garbage bits */
3318 
3319         data->m = m;
3320         data->ni = ni;
3321 	for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3322 		if (rt2860_rates[ridx].rate == rate)
3323 			break;
3324 	data->ridx = ridx;
3325 
3326         run_set_tx_desc(sc, data);
3327 
3328         DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3329             m->m_pkthdr.len, rate);
3330 
3331         STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3332 
3333 	usbd_transfer_start(sc->sc_xfer[0]);
3334 
3335         return (0);
3336 }
3337 
3338 static int
3339 run_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3340     const struct ieee80211_bpf_params *params)
3341 {
3342 	struct ifnet *ifp = ni->ni_ic->ic_ifp;
3343 	struct run_softc *sc = ifp->if_softc;
3344 	int error = 0;
3345 
3346 	RUN_LOCK(sc);
3347 
3348 	/* prevent management frames from being sent if we're not ready */
3349 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3350 		error =  ENETDOWN;
3351 		goto done;
3352 	}
3353 
3354 	if (params == NULL) {
3355 		/* tx mgt packet */
3356 		if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3357 			ifp->if_oerrors++;
3358 			DPRINTF("mgt tx failed\n");
3359 			goto done;
3360 		}
3361 	} else {
3362 		/* tx raw packet with param */
3363 		if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3364 			ifp->if_oerrors++;
3365 			DPRINTF("tx with param failed\n");
3366 			goto done;
3367 		}
3368 	}
3369 
3370 	ifp->if_opackets++;
3371 
3372 done:
3373 	RUN_UNLOCK(sc);
3374 
3375 	if (error != 0) {
3376 		if(m != NULL)
3377 			m_freem(m);
3378 		ieee80211_free_node(ni);
3379 	}
3380 
3381 	return (error);
3382 }
3383 
3384 static void
3385 run_start(struct ifnet *ifp)
3386 {
3387 	struct run_softc *sc = ifp->if_softc;
3388 	struct ieee80211_node *ni;
3389 	struct mbuf *m;
3390 
3391 	RUN_LOCK(sc);
3392 
3393 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3394 		RUN_UNLOCK(sc);
3395 		return;
3396 	}
3397 
3398 	for (;;) {
3399 		/* send data frames */
3400 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3401 		if (m == NULL)
3402 			break;
3403 
3404 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3405 		if (run_tx(sc, m, ni) != 0) {
3406 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
3407 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3408 			break;
3409 		}
3410 	}
3411 
3412 	RUN_UNLOCK(sc);
3413 }
3414 
3415 static int
3416 run_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3417 {
3418 	struct run_softc *sc = ifp->if_softc;
3419 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3420 	struct ifreq *ifr = (struct ifreq *) data;
3421 	int startall = 0;
3422 	int error = 0;
3423 
3424 	switch (cmd) {
3425 	case SIOCSIFFLAGS:
3426 		RUN_LOCK(sc);
3427 		if (ifp->if_flags & IFF_UP) {
3428 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3429 				startall = 1;
3430 				run_init_locked(sc);
3431 			} else
3432 				run_update_promisc_locked(ifp);
3433 		} else {
3434 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3435 			    (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3436 					run_stop(sc);
3437 			}
3438 		}
3439 		RUN_UNLOCK(sc);
3440 		if (startall)
3441 			ieee80211_start_all(ic);
3442 		break;
3443 	case SIOCGIFMEDIA:
3444 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3445 		break;
3446 	case SIOCGIFADDR:
3447 		error = ether_ioctl(ifp, cmd, data);
3448 		break;
3449 	default:
3450 		error = EINVAL;
3451 		break;
3452 	}
3453 
3454 	return (error);
3455 }
3456 
3457 static void
3458 run_set_agc(struct run_softc *sc, uint8_t agc)
3459 {
3460 	uint8_t bbp;
3461 
3462 	if (sc->mac_ver == 0x3572) {
3463 		run_bbp_read(sc, 27, &bbp);
3464 		bbp &= ~(0x3 << 5);
3465 		run_bbp_write(sc, 27, bbp | 0 << 5);	/* select Rx0 */
3466 		run_bbp_write(sc, 66, agc);
3467 		run_bbp_write(sc, 27, bbp | 1 << 5);	/* select Rx1 */
3468 		run_bbp_write(sc, 66, agc);
3469 	} else
3470 		run_bbp_write(sc, 66, agc);
3471 }
3472 
3473 static void
3474 run_select_chan_group(struct run_softc *sc, int group)
3475 {
3476 	uint32_t tmp;
3477 	uint8_t agc;
3478 
3479 	run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3480 	run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3481 	run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3482 	run_bbp_write(sc, 86, 0x00);
3483 
3484 	if (group == 0) {
3485 		if (sc->ext_2ghz_lna) {
3486 			run_bbp_write(sc, 82, 0x62);
3487 			run_bbp_write(sc, 75, 0x46);
3488 		} else {
3489 			run_bbp_write(sc, 82, 0x84);
3490 			run_bbp_write(sc, 75, 0x50);
3491 		}
3492 	} else {
3493 		if (sc->mac_ver == 0x3572)
3494 			run_bbp_write(sc, 82, 0x94);
3495 		else
3496 			run_bbp_write(sc, 82, 0xf2);
3497 		if (sc->ext_5ghz_lna)
3498 			run_bbp_write(sc, 75, 0x46);
3499 		else
3500 			run_bbp_write(sc, 75, 0x50);
3501 	}
3502 
3503 	run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3504 	tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3505 	tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3506 	run_write(sc, RT2860_TX_BAND_CFG, tmp);
3507 
3508 	/* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3509 	tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3510 	if (sc->nrxchains > 1)
3511 		tmp |= RT2860_LNA_PE1_EN;
3512 	if (group == 0) {	/* 2GHz */
3513 		tmp |= RT2860_PA_PE_G0_EN;
3514 		if (sc->ntxchains > 1)
3515 			tmp |= RT2860_PA_PE_G1_EN;
3516 	} else {		/* 5GHz */
3517 		tmp |= RT2860_PA_PE_A0_EN;
3518 		if (sc->ntxchains > 1)
3519 			tmp |= RT2860_PA_PE_A1_EN;
3520 	}
3521 	if (sc->mac_ver == 0x3572) {
3522 		run_rt3070_rf_write(sc, 8, 0x00);
3523 		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3524 		run_rt3070_rf_write(sc, 8, 0x80);
3525 	} else
3526 		run_write(sc, RT2860_TX_PIN_CFG, tmp);
3527 
3528 	/* set initial AGC value */
3529 	if (group == 0) {	/* 2GHz band */
3530 		if (sc->mac_ver >= 0x3070)
3531 			agc = 0x1c + sc->lna[0] * 2;
3532 		else
3533 			agc = 0x2e + sc->lna[0];
3534 	} else {		/* 5GHz band */
3535 		if (sc->mac_ver == 0x3572)
3536 			agc = 0x22 + (sc->lna[group] * 5) / 3;
3537 		else
3538 			agc = 0x32 + (sc->lna[group] * 5) / 3;
3539 	}
3540 	run_set_agc(sc, agc);
3541 }
3542 
3543 static void
3544 run_rt2870_set_chan(struct run_softc *sc, uint32_t chan)
3545 {
3546 	const struct rfprog *rfprog = rt2860_rf2850;
3547 	uint32_t r2, r3, r4;
3548 	int8_t txpow1, txpow2;
3549 	int i;
3550 
3551 	/* find the settings for this channel (we know it exists) */
3552 	for (i = 0; rfprog[i].chan != chan; i++);
3553 
3554 	r2 = rfprog[i].r2;
3555 	if (sc->ntxchains == 1)
3556 		r2 |= 1 << 12;		/* 1T: disable Tx chain 2 */
3557 	if (sc->nrxchains == 1)
3558 		r2 |= 1 << 15 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3559 	else if (sc->nrxchains == 2)
3560 		r2 |= 1 << 4;		/* 2R: disable Rx chain 3 */
3561 
3562 	/* use Tx power values from EEPROM */
3563 	txpow1 = sc->txpow1[i];
3564 	txpow2 = sc->txpow2[i];
3565 	if (chan > 14) {
3566 		if (txpow1 >= 0)
3567 			txpow1 = txpow1 << 1 | 1;
3568 		else
3569 			txpow1 = (7 + txpow1) << 1;
3570 		if (txpow2 >= 0)
3571 			txpow2 = txpow2 << 1 | 1;
3572 		else
3573 			txpow2 = (7 + txpow2) << 1;
3574 	}
3575 	r3 = rfprog[i].r3 | txpow1 << 7;
3576 	r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3577 
3578 	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3579 	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3580 	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3581 	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3582 
3583 	run_delay(sc, 10);
3584 
3585 	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3586 	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3587 	run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3588 	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3589 
3590 	run_delay(sc, 10);
3591 
3592 	run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3593 	run_rt2870_rf_write(sc, RT2860_RF2, r2);
3594 	run_rt2870_rf_write(sc, RT2860_RF3, r3);
3595 	run_rt2870_rf_write(sc, RT2860_RF4, r4);
3596 }
3597 
3598 static void
3599 run_rt3070_set_chan(struct run_softc *sc, uint32_t chan)
3600 {
3601 	int8_t txpow1, txpow2;
3602 	uint8_t rf;
3603 	int i;
3604 
3605 	/* RT3070 is 2GHz only */
3606 	KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3607 
3608 	/* find the settings for this channel (we know it exists) */
3609 	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3610 
3611 	/* use Tx power values from EEPROM */
3612 	txpow1 = sc->txpow1[i];
3613 	txpow2 = sc->txpow2[i];
3614 
3615 	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3616 	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3617 	run_rt3070_rf_read(sc, 6, &rf);
3618 	rf = (rf & ~0x03) | rt3070_freqs[i].r;
3619 	run_rt3070_rf_write(sc, 6, rf);
3620 
3621 	/* set Tx0 power */
3622 	run_rt3070_rf_read(sc, 12, &rf);
3623 	rf = (rf & ~0x1f) | txpow1;
3624 	run_rt3070_rf_write(sc, 12, rf);
3625 
3626 	/* set Tx1 power */
3627 	run_rt3070_rf_read(sc, 13, &rf);
3628 	rf = (rf & ~0x1f) | txpow2;
3629 	run_rt3070_rf_write(sc, 13, rf);
3630 
3631 	run_rt3070_rf_read(sc, 1, &rf);
3632 	rf &= ~0xfc;
3633 	if (sc->ntxchains == 1)
3634 		rf |= 1 << 7 | 1 << 5;	/* 1T: disable Tx chains 2 & 3 */
3635 	else if (sc->ntxchains == 2)
3636 		rf |= 1 << 7;		/* 2T: disable Tx chain 3 */
3637 	if (sc->nrxchains == 1)
3638 		rf |= 1 << 6 | 1 << 4;	/* 1R: disable Rx chains 2 & 3 */
3639 	else if (sc->nrxchains == 2)
3640 		rf |= 1 << 6;		/* 2R: disable Rx chain 3 */
3641 	run_rt3070_rf_write(sc, 1, rf);
3642 
3643 	/* set RF offset */
3644 	run_rt3070_rf_read(sc, 23, &rf);
3645 	rf = (rf & ~0x7f) | sc->freq;
3646 	run_rt3070_rf_write(sc, 23, rf);
3647 
3648 	/* program RF filter */
3649 	run_rt3070_rf_read(sc, 24, &rf);	/* Tx */
3650 	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3651 	run_rt3070_rf_write(sc, 24, rf);
3652 	run_rt3070_rf_read(sc, 31, &rf);	/* Rx */
3653 	rf = (rf & ~0x3f) | sc->rf24_20mhz;
3654 	run_rt3070_rf_write(sc, 31, rf);
3655 
3656 	/* enable RF tuning */
3657 	run_rt3070_rf_read(sc, 7, &rf);
3658 	run_rt3070_rf_write(sc, 7, rf | 0x01);
3659 }
3660 
3661 static void
3662 run_rt3572_set_chan(struct run_softc *sc, u_int chan)
3663 {
3664 	int8_t txpow1, txpow2;
3665 	uint32_t tmp;
3666 	uint8_t rf;
3667 	int i;
3668 
3669 	/* find the settings for this channel (we know it exists) */
3670 	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3671 
3672 	/* use Tx power values from EEPROM */
3673 	txpow1 = sc->txpow1[i];
3674 	txpow2 = sc->txpow2[i];
3675 
3676 	if (chan <= 14) {
3677 		run_bbp_write(sc, 25, sc->bbp25);
3678 		run_bbp_write(sc, 26, sc->bbp26);
3679 	} else {
3680 		/* enable IQ phase correction */
3681 		run_bbp_write(sc, 25, 0x09);
3682 		run_bbp_write(sc, 26, 0xff);
3683 	}
3684 
3685 	run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3686 	run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3687 	run_rt3070_rf_read(sc, 6, &rf);
3688 	rf  = (rf & ~0x0f) | rt3070_freqs[i].r;
3689 	rf |= (chan <= 14) ? 0x08 : 0x04;
3690 	run_rt3070_rf_write(sc, 6, rf);
3691 
3692 	/* set PLL mode */
3693 	run_rt3070_rf_read(sc, 5, &rf);
3694 	rf &= ~(0x08 | 0x04);
3695 	rf |= (chan <= 14) ? 0x04 : 0x08;
3696 	run_rt3070_rf_write(sc, 5, rf);
3697 
3698 	/* set Tx power for chain 0 */
3699 	if (chan <= 14)
3700 		rf = 0x60 | txpow1;
3701 	else
3702 		rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3703 	run_rt3070_rf_write(sc, 12, rf);
3704 
3705 	/* set Tx power for chain 1 */
3706 	if (chan <= 14)
3707 		rf = 0x60 | txpow2;
3708 	else
3709 		rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3710 	run_rt3070_rf_write(sc, 13, rf);
3711 
3712 	/* set Tx/Rx streams */
3713 	run_rt3070_rf_read(sc, 1, &rf);
3714 	rf &= ~0xfc;
3715 	if (sc->ntxchains == 1)
3716 		rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3717 	else if (sc->ntxchains == 2)
3718 		rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3719 	if (sc->nrxchains == 1)
3720 		rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3721 	else if (sc->nrxchains == 2)
3722 		rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3723 	run_rt3070_rf_write(sc, 1, rf);
3724 
3725 	/* set RF offset */
3726 	run_rt3070_rf_read(sc, 23, &rf);
3727 	rf = (rf & ~0x7f) | sc->freq;
3728 	run_rt3070_rf_write(sc, 23, rf);
3729 
3730 	/* program RF filter */
3731 	rf = sc->rf24_20mhz;
3732 	run_rt3070_rf_write(sc, 24, rf);	/* Tx */
3733 	run_rt3070_rf_write(sc, 31, rf);	/* Rx */
3734 
3735 	/* enable RF tuning */
3736 	run_rt3070_rf_read(sc, 7, &rf);
3737 	rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3738 	run_rt3070_rf_write(sc, 7, rf);
3739 
3740 	/* TSSI */
3741 	rf = (chan <= 14) ? 0xc3 : 0xc0;
3742 	run_rt3070_rf_write(sc, 9, rf);
3743 
3744 	/* set loop filter 1 */
3745 	run_rt3070_rf_write(sc, 10, 0xf1);
3746 	/* set loop filter 2 */
3747 	run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3748 
3749 	/* set tx_mx2_ic */
3750 	run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3751 	/* set tx_mx1_ic */
3752 	if (chan <= 14)
3753 		rf = 0x48 | sc->txmixgain_2ghz;
3754 	else
3755 		rf = 0x78 | sc->txmixgain_5ghz;
3756 	run_rt3070_rf_write(sc, 16, rf);
3757 
3758 	/* set tx_lo1 */
3759 	run_rt3070_rf_write(sc, 17, 0x23);
3760 	/* set tx_lo2 */
3761 	if (chan <= 14)
3762 		rf = 0x93;
3763 	else if (chan <= 64)
3764 		rf = 0xb7;
3765 	else if (chan <= 128)
3766 		rf = 0x74;
3767 	else
3768 		rf = 0x72;
3769 	run_rt3070_rf_write(sc, 19, rf);
3770 
3771 	/* set rx_lo1 */
3772 	if (chan <= 14)
3773 		rf = 0xb3;
3774 	else if (chan <= 64)
3775 		rf = 0xf6;
3776 	else if (chan <= 128)
3777 		rf = 0xf4;
3778 	else
3779 		rf = 0xf3;
3780 	run_rt3070_rf_write(sc, 20, rf);
3781 
3782 	/* set pfd_delay */
3783 	if (chan <= 14)
3784 		rf = 0x15;
3785 	else if (chan <= 64)
3786 		rf = 0x3d;
3787 	else
3788 		rf = 0x01;
3789 	run_rt3070_rf_write(sc, 25, rf);
3790 
3791 	/* set rx_lo2 */
3792 	run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3793 	/* set ldo_rf_vc */
3794 	run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3795 	/* set drv_cc */
3796 	run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3797 
3798 	run_read(sc, RT2860_GPIO_CTRL, &tmp);
3799 	tmp &= ~0x8080;
3800 	if (chan <= 14)
3801 		tmp |= 0x80;
3802 	run_write(sc, RT2860_GPIO_CTRL, tmp);
3803 
3804 	/* enable RF tuning */
3805 	run_rt3070_rf_read(sc, 7, &rf);
3806 	run_rt3070_rf_write(sc, 7, rf | 0x01);
3807 
3808 	run_delay(sc, 2);
3809 }
3810 
3811 static void
3812 run_set_rx_antenna(struct run_softc *sc, int aux)
3813 {
3814 	uint32_t tmp;
3815 
3816 	if (aux) {
3817 		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
3818 		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3819 		run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
3820 	} else {
3821 		run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
3822 		run_read(sc, RT2860_GPIO_CTRL, &tmp);
3823 		run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
3824 	}
3825 }
3826 
3827 static int
3828 run_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
3829 {
3830 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3831 	uint32_t chan, group;
3832 
3833 	chan = ieee80211_chan2ieee(ic, c);
3834 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
3835 		return (EINVAL);
3836 
3837 	if (sc->mac_ver == 0x3572)
3838 		run_rt3572_set_chan(sc, chan);
3839 	else if (sc->mac_ver >= 0x3070)
3840 		run_rt3070_set_chan(sc, chan);
3841 	else
3842 		run_rt2870_set_chan(sc, chan);
3843 
3844 	/* determine channel group */
3845 	if (chan <= 14)
3846 		group = 0;
3847 	else if (chan <= 64)
3848 		group = 1;
3849 	else if (chan <= 128)
3850 		group = 2;
3851 	else
3852 		group = 3;
3853 
3854 	/* XXX necessary only when group has changed! */
3855 	run_select_chan_group(sc, group);
3856 
3857 	run_delay(sc, 10);
3858 
3859 	return (0);
3860 }
3861 
3862 static void
3863 run_set_channel(struct ieee80211com *ic)
3864 {
3865 	struct run_softc *sc = ic->ic_ifp->if_softc;
3866 
3867 	RUN_LOCK(sc);
3868 	run_set_chan(sc, ic->ic_curchan);
3869 	RUN_UNLOCK(sc);
3870 
3871 	return;
3872 }
3873 
3874 static void
3875 run_scan_start(struct ieee80211com *ic)
3876 {
3877 	struct run_softc *sc = ic->ic_ifp->if_softc;
3878 	uint32_t tmp;
3879 
3880 	RUN_LOCK(sc);
3881 
3882 	/* abort TSF synchronization */
3883 	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
3884 	run_write(sc, RT2860_BCN_TIME_CFG,
3885 	    tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
3886 	    RT2860_TBTT_TIMER_EN));
3887 	run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
3888 
3889 	RUN_UNLOCK(sc);
3890 
3891 	return;
3892 }
3893 
3894 static void
3895 run_scan_end(struct ieee80211com *ic)
3896 {
3897 	struct run_softc *sc = ic->ic_ifp->if_softc;
3898 
3899 	RUN_LOCK(sc);
3900 
3901 	run_enable_tsf_sync(sc);
3902 	/* XXX keep local copy */
3903 	run_set_bssid(sc, sc->sc_bssid);
3904 
3905 	RUN_UNLOCK(sc);
3906 
3907 	return;
3908 }
3909 
3910 /*
3911  * Could be called from ieee80211_node_timeout()
3912  * (non-sleepable thread)
3913  */
3914 static void
3915 run_update_beacon(struct ieee80211vap *vap, int item)
3916 {
3917 	struct ieee80211com *ic = vap->iv_ic;
3918 	struct run_softc *sc = ic->ic_ifp->if_softc;
3919 	struct run_vap *rvp = RUN_VAP(vap);
3920 	int mcast = 0;
3921 	uint32_t i;
3922 
3923 	KASSERT(vap != NULL, ("no beacon"));
3924 
3925 	switch (item) {
3926 	case IEEE80211_BEACON_ERP:
3927 		run_updateslot(ic->ic_ifp);
3928 		break;
3929 	case IEEE80211_BEACON_HTINFO:
3930 		run_updateprot(ic);
3931 		break;
3932 	case IEEE80211_BEACON_TIM:
3933 		mcast = 1;	/*TODO*/
3934 		break;
3935 	default:
3936 		break;
3937 	}
3938 
3939 	setbit(rvp->bo.bo_flags, item);
3940 	ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
3941 
3942 	i = RUN_CMDQ_GET(&sc->cmdq_store);
3943 	DPRINTF("cmdq_store=%d\n", i);
3944 	sc->cmdq[i].func = run_update_beacon_cb;
3945 	sc->cmdq[i].arg0 = vap;
3946 	ieee80211_runtask(ic, &sc->cmdq_task);
3947 
3948 	return;
3949 }
3950 
3951 static void
3952 run_update_beacon_cb(void *arg)
3953 {
3954 	struct ieee80211vap *vap = arg;
3955 	struct run_vap *rvp = RUN_VAP(vap);
3956 	struct ieee80211com *ic = vap->iv_ic;
3957 	struct run_softc *sc = ic->ic_ifp->if_softc;
3958 	struct rt2860_txwi txwi;
3959 	struct mbuf *m;
3960 	uint8_t ridx;
3961 
3962 	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
3963 		return;
3964 
3965 	/*
3966 	 * No need to call ieee80211_beacon_update(), run_update_beacon()
3967 	 * is taking care of apropriate calls.
3968 	 */
3969 	if (rvp->beacon_mbuf == NULL) {
3970 		rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
3971 		    &rvp->bo);
3972 		if (rvp->beacon_mbuf == NULL)
3973 			return;
3974 	}
3975 	m = rvp->beacon_mbuf;
3976 
3977 	memset(&txwi, 0, sizeof txwi);
3978 	txwi.wcid = 0xff;
3979 	txwi.len = htole16(m->m_pkthdr.len);
3980 	/* send beacons at the lowest available rate */
3981 	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3982 	    RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3983 	txwi.phy = htole16(rt2860_rates[ridx].mcs);
3984 	if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
3985 	        txwi.phy |= htole16(RT2860_PHY_OFDM);
3986 	txwi.txop = RT2860_TX_TXOP_HT;
3987 	txwi.flags = RT2860_TX_TS;
3988 	txwi.xflags = RT2860_TX_NSEQ;
3989 
3990 	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
3991 	    (uint8_t *)&txwi, sizeof txwi);
3992 	run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
3993 	    mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);	/* roundup len */
3994 
3995 	return;
3996 }
3997 
3998 static void
3999 run_updateprot(struct ieee80211com *ic)
4000 {
4001 	struct run_softc *sc = ic->ic_ifp->if_softc;
4002 	uint32_t i;
4003 
4004 	i = RUN_CMDQ_GET(&sc->cmdq_store);
4005 	DPRINTF("cmdq_store=%d\n", i);
4006 	sc->cmdq[i].func = run_updateprot_cb;
4007 	sc->cmdq[i].arg0 = ic;
4008 	ieee80211_runtask(ic, &sc->cmdq_task);
4009 }
4010 
4011 static void
4012 run_updateprot_cb(void *arg)
4013 {
4014 	struct ieee80211com *ic = arg;
4015 	struct run_softc *sc = ic->ic_ifp->if_softc;
4016 	uint32_t tmp;
4017 
4018 	tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4019 	/* setup protection frame rate (MCS code) */
4020 	tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4021 	    rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4022 	    rt2860_rates[RT2860_RIDX_CCK11].mcs;
4023 
4024 	/* CCK frames don't require protection */
4025 	run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4026 	if (ic->ic_flags & IEEE80211_F_USEPROT) {
4027 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4028 			tmp |= RT2860_PROT_CTRL_RTS_CTS;
4029 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4030 			tmp |= RT2860_PROT_CTRL_CTS;
4031 	}
4032 	run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4033 }
4034 
4035 static void
4036 run_usb_timeout_cb(void *arg)
4037 {
4038 	struct ieee80211vap *vap = arg;
4039 	struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4040 
4041 	RUN_LOCK_ASSERT(sc, MA_OWNED);
4042 
4043 	if(vap->iv_state == IEEE80211_S_RUN &&
4044 	    vap->iv_opmode != IEEE80211_M_STA)
4045 		run_reset_livelock(sc);
4046 	else if (vap->iv_state == IEEE80211_S_SCAN) {
4047 		DPRINTF("timeout caused by scan\n");
4048 		/* cancel bgscan */
4049 		ieee80211_cancel_scan(vap);
4050 	} else
4051 		DPRINTF("timeout by unknown cause\n");
4052 }
4053 
4054 static void
4055 run_reset_livelock(struct run_softc *sc)
4056 {
4057 	uint32_t tmp;
4058 
4059 	RUN_LOCK_ASSERT(sc, MA_OWNED);
4060 
4061 	/*
4062 	 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4063 	 * can run into a livelock and start sending CTS-to-self frames like
4064 	 * crazy if protection is enabled.  Reset MAC/BBP for a while
4065 	 */
4066 	run_read(sc, RT2860_DEBUG, &tmp);
4067 	DPRINTFN(3, "debug reg %08x\n", tmp);
4068 	if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4069 		DPRINTF("CTS-to-self livelock detected\n");
4070 		run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4071 		run_delay(sc, 1);
4072 		run_write(sc, RT2860_MAC_SYS_CTRL,
4073 		    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4074 	}
4075 }
4076 
4077 static void
4078 run_update_promisc_locked(struct ifnet *ifp)
4079 {
4080 	struct run_softc *sc = ifp->if_softc;
4081         uint32_t tmp;
4082 
4083 	run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4084 
4085 	tmp |= RT2860_DROP_UC_NOME;
4086         if (ifp->if_flags & IFF_PROMISC)
4087 		tmp &= ~RT2860_DROP_UC_NOME;
4088 
4089 	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4090 
4091         DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4092             "entering" : "leaving");
4093 }
4094 
4095 static void
4096 run_update_promisc(struct ifnet *ifp)
4097 {
4098 	struct run_softc *sc = ifp->if_softc;
4099 
4100 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4101 		return;
4102 
4103 	RUN_LOCK(sc);
4104 	run_update_promisc_locked(ifp);
4105 	RUN_UNLOCK(sc);
4106 }
4107 
4108 static void
4109 run_enable_tsf_sync(struct run_softc *sc)
4110 {
4111 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4112 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4113 	uint32_t tmp;
4114 
4115 	DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode);
4116 
4117 	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4118 	tmp &= ~0x1fffff;
4119 	tmp |= vap->iv_bss->ni_intval * 16;
4120 	tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4121 
4122 	if (ic->ic_opmode == IEEE80211_M_STA) {
4123 		/*
4124 		 * Local TSF is always updated with remote TSF on beacon
4125 		 * reception.
4126 		 */
4127 		tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4128 	} else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4129 	        tmp |= RT2860_BCN_TX_EN;
4130 	        /*
4131 	         * Local TSF is updated with remote TSF on beacon reception
4132 	         * only if the remote TSF is greater than local TSF.
4133 	         */
4134 	        tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4135 	} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4136 		    ic->ic_opmode == IEEE80211_M_MBSS) {
4137 	        tmp |= RT2860_BCN_TX_EN;
4138 	        /* SYNC with nobody */
4139 	        tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4140 	} else {
4141 		DPRINTF("Enabling TSF failed. undefined opmode\n");
4142 		return;
4143 	}
4144 
4145 	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4146 }
4147 
4148 static void
4149 run_enable_mrr(struct run_softc *sc)
4150 {
4151 #define CCK(mcs)	(mcs)
4152 #define OFDM(mcs)	(1 << 3 | (mcs))
4153 	run_write(sc, RT2860_LG_FBK_CFG0,
4154 	    OFDM(6) << 28 |	/* 54->48 */
4155 	    OFDM(5) << 24 |	/* 48->36 */
4156 	    OFDM(4) << 20 |	/* 36->24 */
4157 	    OFDM(3) << 16 |	/* 24->18 */
4158 	    OFDM(2) << 12 |	/* 18->12 */
4159 	    OFDM(1) <<  8 |	/* 12-> 9 */
4160 	    OFDM(0) <<  4 |	/*  9-> 6 */
4161 	    OFDM(0));		/*  6-> 6 */
4162 
4163 	run_write(sc, RT2860_LG_FBK_CFG1,
4164 	    CCK(2) << 12 |	/* 11->5.5 */
4165 	    CCK(1) <<  8 |	/* 5.5-> 2 */
4166 	    CCK(0) <<  4 |	/*   2-> 1 */
4167 	    CCK(0));		/*   1-> 1 */
4168 #undef OFDM
4169 #undef CCK
4170 }
4171 
4172 static void
4173 run_set_txpreamble(struct run_softc *sc)
4174 {
4175 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4176 	uint32_t tmp;
4177 
4178 	run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4179 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4180 		tmp |= RT2860_CCK_SHORT_EN;
4181 	else
4182 		tmp &= ~RT2860_CCK_SHORT_EN;
4183 	run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4184 }
4185 
4186 static void
4187 run_set_basicrates(struct run_softc *sc)
4188 {
4189 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4190 
4191 	/* set basic rates mask */
4192 	if (ic->ic_curmode == IEEE80211_MODE_11B)
4193 		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4194 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
4195 		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4196 	else	/* 11g */
4197 		run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4198 }
4199 
4200 static void
4201 run_set_leds(struct run_softc *sc, uint16_t which)
4202 {
4203 	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4204 	    which | (sc->leds & 0x7f));
4205 }
4206 
4207 static void
4208 run_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4209 {
4210 	run_write(sc, RT2860_MAC_BSSID_DW0,
4211 	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4212 	run_write(sc, RT2860_MAC_BSSID_DW1,
4213 	    bssid[4] | bssid[5] << 8);
4214 }
4215 
4216 static void
4217 run_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4218 {
4219 	run_write(sc, RT2860_MAC_ADDR_DW0,
4220 	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4221 	run_write(sc, RT2860_MAC_ADDR_DW1,
4222 	    addr[4] | addr[5] << 8 | 0xff << 16);
4223 }
4224 
4225 static void
4226 run_updateslot(struct ifnet *ifp)
4227 {
4228 	struct run_softc *sc = ifp->if_softc;
4229 	struct ieee80211com *ic = ifp->if_l2com;
4230 	uint32_t i;
4231 
4232 	i = RUN_CMDQ_GET(&sc->cmdq_store);
4233 	DPRINTF("cmdq_store=%d\n", i);
4234 	sc->cmdq[i].func = run_updateslot_cb;
4235 	sc->cmdq[i].arg0 = ifp;
4236 	ieee80211_runtask(ic, &sc->cmdq_task);
4237 
4238 	return;
4239 }
4240 
4241 /* ARGSUSED */
4242 static void
4243 run_updateslot_cb(void *arg)
4244 {
4245 	struct ifnet *ifp = arg;
4246 	struct run_softc *sc = ifp->if_softc;
4247 	struct ieee80211com *ic = ifp->if_l2com;
4248 	uint32_t tmp;
4249 
4250 	run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4251 	tmp &= ~0xff;
4252 	tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4253 	run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4254 }
4255 
4256 static void
4257 run_update_mcast(struct ifnet *ifp)
4258 {
4259 	/* h/w filter supports getting everything or nothing */
4260 	ifp->if_flags |= IFF_ALLMULTI;
4261 }
4262 
4263 static int8_t
4264 run_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4265 {
4266 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4267 	struct ieee80211_channel *c = ic->ic_curchan;
4268 	int delta;
4269 
4270 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
4271 		uint32_t chan = ieee80211_chan2ieee(ic, c);
4272 		delta = sc->rssi_5ghz[rxchain];
4273 
4274 		/* determine channel group */
4275 		if (chan <= 64)
4276 			delta -= sc->lna[1];
4277 		else if (chan <= 128)
4278 			delta -= sc->lna[2];
4279 		else
4280 			delta -= sc->lna[3];
4281 	} else
4282 		delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4283 
4284 	return (-12 - delta - rssi);
4285 }
4286 
4287 static int
4288 run_bbp_init(struct run_softc *sc)
4289 {
4290 	int i, error, ntries;
4291 	uint8_t bbp0;
4292 
4293 	/* wait for BBP to wake up */
4294 	for (ntries = 0; ntries < 20; ntries++) {
4295 		if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4296 			return error;
4297 		if (bbp0 != 0 && bbp0 != 0xff)
4298 			break;
4299 	}
4300 	if (ntries == 20)
4301 		return (ETIMEDOUT);
4302 
4303 	/* initialize BBP registers to default values */
4304 	for (i = 0; i < nitems(rt2860_def_bbp); i++) {
4305 		run_bbp_write(sc, rt2860_def_bbp[i].reg,
4306 		    rt2860_def_bbp[i].val);
4307 	}
4308 
4309 	/* fix BBP84 for RT2860E */
4310 	if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4311 		run_bbp_write(sc, 84, 0x19);
4312 
4313 	if (sc->mac_ver >= 0x3070) {
4314 		run_bbp_write(sc, 79, 0x13);
4315 		run_bbp_write(sc, 80, 0x05);
4316 		run_bbp_write(sc, 81, 0x33);
4317 	} else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4318 		run_bbp_write(sc, 69, 0x16);
4319 		run_bbp_write(sc, 73, 0x12);
4320 	}
4321 	return (0);
4322 }
4323 
4324 static int
4325 run_rt3070_rf_init(struct run_softc *sc)
4326 {
4327 	uint32_t tmp;
4328 	uint8_t rf, target, bbp4;
4329 	int i;
4330 
4331 	run_rt3070_rf_read(sc, 30, &rf);
4332 	/* toggle RF R30 bit 7 */
4333 	run_rt3070_rf_write(sc, 30, rf | 0x80);
4334 	run_delay(sc, 10);
4335 	run_rt3070_rf_write(sc, 30, rf & ~0x80);
4336 
4337 	/* initialize RF registers to default value */
4338 	if (sc->mac_ver == 0x3572) {
4339 		for (i = 0; i < nitems(rt3572_def_rf); i++) {
4340 			run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4341 			    rt3572_def_rf[i].val);
4342 		}
4343 	} else {
4344 		for (i = 0; i < nitems(rt3070_def_rf); i++) {
4345 			run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4346 			    rt3070_def_rf[i].val);
4347 		}
4348 	}
4349 
4350 	if (sc->mac_ver == 0x3070) {
4351 		/* change voltage from 1.2V to 1.35V for RT3070 */
4352 		run_read(sc, RT3070_LDO_CFG0, &tmp);
4353 		tmp = (tmp & ~0x0f000000) | 0x0d000000;
4354 		run_write(sc, RT3070_LDO_CFG0, tmp);
4355 
4356 	} else if (sc->mac_ver == 0x3071) {
4357 		run_rt3070_rf_read(sc, 6, &rf);
4358 		run_rt3070_rf_write(sc, 6, rf | 0x40);
4359 		run_rt3070_rf_write(sc, 31, 0x14);
4360 
4361 		run_read(sc, RT3070_LDO_CFG0, &tmp);
4362 		tmp &= ~0x1f000000;
4363 		if (sc->mac_rev < 0x0211)
4364 			tmp |= 0x0d000000;	/* 1.3V */
4365 		else
4366 			tmp |= 0x01000000;	/* 1.2V */
4367 		run_write(sc, RT3070_LDO_CFG0, tmp);
4368 
4369 		/* patch LNA_PE_G1 */
4370 		run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4371 		run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4372 
4373 	} else if (sc->mac_ver == 0x3572) {
4374 		run_rt3070_rf_read(sc, 6, &rf);
4375 		run_rt3070_rf_write(sc, 6, rf | 0x40);
4376 
4377 		/* increase voltage from 1.2V to 1.35V */
4378 		run_read(sc, RT3070_LDO_CFG0, &tmp);
4379 		tmp = (tmp & ~0x1f000000) | 0x0d000000;
4380 		run_write(sc, RT3070_LDO_CFG0, tmp);
4381 
4382 		if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4383 			run_delay(sc, 1);	/* wait for 1msec */
4384 			/* decrease voltage back to 1.2V */
4385 			tmp = (tmp & ~0x1f000000) | 0x01000000;
4386 			run_write(sc, RT3070_LDO_CFG0, tmp);
4387 		}
4388 	}
4389 
4390 	/* select 20MHz bandwidth */
4391 	run_rt3070_rf_read(sc, 31, &rf);
4392 	run_rt3070_rf_write(sc, 31, rf & ~0x20);
4393 
4394 	/* calibrate filter for 20MHz bandwidth */
4395 	sc->rf24_20mhz = 0x1f;	/* default value */
4396 	target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4397 	run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4398 
4399 	/* select 40MHz bandwidth */
4400 	run_bbp_read(sc, 4, &bbp4);
4401 	run_bbp_write(sc, 4, (bbp4 & ~0x08) | 0x10);
4402 	run_rt3070_rf_read(sc, 31, &rf);
4403 	run_rt3070_rf_write(sc, 31, rf | 0x20);
4404 
4405 	/* calibrate filter for 40MHz bandwidth */
4406 	sc->rf24_40mhz = 0x2f;	/* default value */
4407 	target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4408 	run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4409 
4410 	/* go back to 20MHz bandwidth */
4411 	run_bbp_read(sc, 4, &bbp4);
4412 	run_bbp_write(sc, 4, bbp4 & ~0x18);
4413 
4414 	if (sc->mac_ver == 0x3572) {
4415 		/* save default BBP registers 25 and 26 values */
4416 		run_bbp_read(sc, 25, &sc->bbp25);
4417 		run_bbp_read(sc, 26, &sc->bbp26);
4418 	} else if (sc->mac_rev < 0x0211)
4419 		run_rt3070_rf_write(sc, 27, 0x03);
4420 
4421 	run_read(sc, RT3070_OPT_14, &tmp);
4422 	run_write(sc, RT3070_OPT_14, tmp | 1);
4423 
4424 	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4425 		run_rt3070_rf_read(sc, 17, &rf);
4426 		rf &= ~RT3070_TX_LO1;
4427 		if ((sc->mac_ver == 0x3070 ||
4428 		     (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4429 		    !sc->ext_2ghz_lna)
4430 			rf |= 0x20;	/* fix for long range Rx issue */
4431 		if (sc->txmixgain_2ghz >= 1)
4432 			rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4433 		run_rt3070_rf_write(sc, 17, rf);
4434 	}
4435 
4436 	if (sc->mac_rev == 0x3071) {
4437 		run_rt3070_rf_read(sc, 1, &rf);
4438 		rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4439 		rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4440 		run_rt3070_rf_write(sc, 1, rf);
4441 
4442 		run_rt3070_rf_read(sc, 15, &rf);
4443 		run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4444 
4445 		run_rt3070_rf_read(sc, 20, &rf);
4446 		run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4447 
4448 		run_rt3070_rf_read(sc, 21, &rf);
4449 		run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4450 	}
4451 
4452 	if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4453 		/* fix Tx to Rx IQ glitch by raising RF voltage */
4454 		run_rt3070_rf_read(sc, 27, &rf);
4455 		rf &= ~0x77;
4456 		if (sc->mac_rev < 0x0211)
4457 			rf |= 0x03;
4458 		run_rt3070_rf_write(sc, 27, rf);
4459 	}
4460 	return (0);
4461 }
4462 
4463 static int
4464 run_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4465     uint8_t *val)
4466 {
4467 	uint8_t rf22, rf24;
4468 	uint8_t bbp55_pb, bbp55_sb, delta;
4469 	int ntries;
4470 
4471 	/* program filter */
4472 	run_rt3070_rf_read(sc, 24, &rf24);
4473 	rf24 = (rf24 & 0xc0) | init;	/* initial filter value */
4474 	run_rt3070_rf_write(sc, 24, rf24);
4475 
4476 	/* enable baseband loopback mode */
4477 	run_rt3070_rf_read(sc, 22, &rf22);
4478 	run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4479 
4480 	/* set power and frequency of passband test tone */
4481 	run_bbp_write(sc, 24, 0x00);
4482 	for (ntries = 0; ntries < 100; ntries++) {
4483 		/* transmit test tone */
4484 		run_bbp_write(sc, 25, 0x90);
4485 		run_delay(sc, 10);
4486 		/* read received power */
4487 		run_bbp_read(sc, 55, &bbp55_pb);
4488 		if (bbp55_pb != 0)
4489 			break;
4490 	}
4491 	if (ntries == 100)
4492 		return ETIMEDOUT;
4493 
4494 	/* set power and frequency of stopband test tone */
4495 	run_bbp_write(sc, 24, 0x06);
4496 	for (ntries = 0; ntries < 100; ntries++) {
4497 		/* transmit test tone */
4498 		run_bbp_write(sc, 25, 0x90);
4499 		run_delay(sc, 10);
4500 		/* read received power */
4501 		run_bbp_read(sc, 55, &bbp55_sb);
4502 
4503 		delta = bbp55_pb - bbp55_sb;
4504 		if (delta > target)
4505 			break;
4506 
4507 		/* reprogram filter */
4508 		rf24++;
4509 		run_rt3070_rf_write(sc, 24, rf24);
4510 	}
4511 	if (ntries < 100) {
4512 		if (rf24 != init)
4513 			rf24--;	/* backtrack */
4514 		*val = rf24;
4515 		run_rt3070_rf_write(sc, 24, rf24);
4516 	}
4517 
4518 	/* restore initial state */
4519 	run_bbp_write(sc, 24, 0x00);
4520 
4521 	/* disable baseband loopback mode */
4522 	run_rt3070_rf_read(sc, 22, &rf22);
4523 	run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4524 
4525 	return (0);
4526 }
4527 
4528 static void
4529 run_rt3070_rf_setup(struct run_softc *sc)
4530 {
4531 	uint8_t bbp, rf;
4532 	int i;
4533 
4534 	if (sc->mac_ver == 0x3572) {
4535 		/* enable DC filter */
4536 		if (sc->mac_rev >= 0x0201)
4537 			run_bbp_write(sc, 103, 0xc0);
4538 
4539 		run_bbp_read(sc, 138, &bbp);
4540 		if (sc->ntxchains == 1)
4541 			bbp |= 0x20;	/* turn off DAC1 */
4542 		if (sc->nrxchains == 1)
4543 			bbp &= ~0x02;	/* turn off ADC1 */
4544 		run_bbp_write(sc, 138, bbp);
4545 
4546 		if (sc->mac_rev >= 0x0211) {
4547 			/* improve power consumption */
4548 			run_bbp_read(sc, 31, &bbp);
4549 			run_bbp_write(sc, 31, bbp & ~0x03);
4550 		}
4551 
4552 		run_rt3070_rf_read(sc, 16, &rf);
4553 		rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4554 		run_rt3070_rf_write(sc, 16, rf);
4555 
4556 	} else if (sc->mac_ver == 0x3071) {
4557 		/* enable DC filter */
4558 		if (sc->mac_rev >= 0x0201)
4559 			run_bbp_write(sc, 103, 0xc0);
4560 
4561 		run_bbp_read(sc, 138, &bbp);
4562 		if (sc->ntxchains == 1)
4563 			bbp |= 0x20;	/* turn off DAC1 */
4564 		if (sc->nrxchains == 1)
4565 			bbp &= ~0x02;	/* turn off ADC1 */
4566 		run_bbp_write(sc, 138, bbp);
4567 
4568 		if (sc->mac_rev >= 0x0211) {
4569 			/* improve power consumption */
4570 			run_bbp_read(sc, 31, &bbp);
4571 			run_bbp_write(sc, 31, bbp & ~0x03);
4572 		}
4573 
4574 		run_write(sc, RT2860_TX_SW_CFG1, 0);
4575 		if (sc->mac_rev < 0x0211) {
4576 			run_write(sc, RT2860_TX_SW_CFG2,
4577 			    sc->patch_dac ? 0x2c : 0x0f);
4578 		} else
4579 			run_write(sc, RT2860_TX_SW_CFG2, 0);
4580 
4581 	} else if (sc->mac_ver == 0x3070) {
4582 		if (sc->mac_rev >= 0x0201) {
4583 			/* enable DC filter */
4584 			run_bbp_write(sc, 103, 0xc0);
4585 
4586 			/* improve power consumption */
4587 			run_bbp_read(sc, 31, &bbp);
4588 			run_bbp_write(sc, 31, bbp & ~0x03);
4589 		}
4590 
4591 		if (sc->mac_rev < 0x0211) {
4592 			run_write(sc, RT2860_TX_SW_CFG1, 0);
4593 			run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4594 		} else
4595 			run_write(sc, RT2860_TX_SW_CFG2, 0);
4596 	}
4597 
4598 	/* initialize RF registers from ROM for >=RT3071*/
4599 	if (sc->mac_ver >= 0x3071) {
4600 		for (i = 0; i < 10; i++) {
4601 			if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4602 				continue;
4603 			run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4604 		}
4605 	}
4606 }
4607 
4608 static int
4609 run_txrx_enable(struct run_softc *sc)
4610 {
4611 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4612 	uint32_t tmp;
4613 	int error, ntries;
4614 
4615 	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4616 	for (ntries = 0; ntries < 200; ntries++) {
4617 		if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4618 			return error;
4619 		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4620 			break;
4621 		run_delay(sc, 50);
4622 	}
4623 	if (ntries == 200)
4624 		return ETIMEDOUT;
4625 
4626 	run_delay(sc, 50);
4627 
4628 	tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4629 	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4630 
4631 	/* enable Rx bulk aggregation (set timeout and limit) */
4632 	tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
4633 	    RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
4634 	run_write(sc, RT2860_USB_DMA_CFG, tmp);
4635 
4636 	/* set Rx filter */
4637 	tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
4638 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
4639 		tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
4640 		    RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
4641 		    RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
4642 		    RT2860_DROP_CFACK | RT2860_DROP_CFEND;
4643 		if (ic->ic_opmode == IEEE80211_M_STA)
4644 			tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
4645 	}
4646 	run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4647 
4648 	run_write(sc, RT2860_MAC_SYS_CTRL,
4649 	    RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4650 
4651 	return (0);
4652 }
4653 
4654 static void
4655 run_init_locked(struct run_softc *sc)
4656 {
4657 	struct ifnet *ifp = sc->sc_ifp;
4658 	struct ieee80211com *ic = ifp->if_l2com;
4659 	uint32_t tmp;
4660 	uint8_t bbp1, bbp3;
4661 	int i;
4662 	int ridx;
4663 	int ntries;
4664 
4665 	if (ic->ic_nrunning > 1)
4666 		return;
4667 
4668 	run_stop(sc);
4669 
4670 	for (ntries = 0; ntries < 100; ntries++) {
4671 		if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
4672 			goto fail;
4673 		if (tmp != 0 && tmp != 0xffffffff)
4674 			break;
4675 		run_delay(sc, 10);
4676 	}
4677 	if (ntries == 100)
4678 		goto fail;
4679 
4680 	for (i = 0; i != RUN_EP_QUEUES; i++)
4681 		run_setup_tx_list(sc, &sc->sc_epq[i]);
4682 
4683 	run_set_macaddr(sc, IF_LLADDR(ifp));
4684 
4685 	for (ntries = 0; ntries < 100; ntries++) {
4686 		if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
4687 			goto fail;
4688 		if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4689 			break;
4690 		run_delay(sc, 10);
4691 	}
4692 	if (ntries == 100) {
4693 		device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
4694 		goto fail;
4695 	}
4696 	tmp &= 0xff0;
4697 	tmp |= RT2860_TX_WB_DDONE;
4698 	run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4699 
4700 	/* turn off PME_OEN to solve high-current issue */
4701 	run_read(sc, RT2860_SYS_CTRL, &tmp);
4702 	run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
4703 
4704 	run_write(sc, RT2860_MAC_SYS_CTRL,
4705 	    RT2860_BBP_HRST | RT2860_MAC_SRST);
4706 	run_write(sc, RT2860_USB_DMA_CFG, 0);
4707 
4708 	if (run_reset(sc) != 0) {
4709 		device_printf(sc->sc_dev, "could not reset chipset\n");
4710 		goto fail;
4711 	}
4712 
4713 	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4714 
4715 	/* init Tx power for all Tx rates (from EEPROM) */
4716 	for (ridx = 0; ridx < 5; ridx++) {
4717 		if (sc->txpow20mhz[ridx] == 0xffffffff)
4718 			continue;
4719 		run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
4720 	}
4721 
4722 	for (i = 0; i < nitems(rt2870_def_mac); i++)
4723 		run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
4724 	run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
4725 	run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
4726 	run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
4727 
4728 	if (sc->mac_ver >= 0x3070) {
4729 		/* set delay of PA_PE assertion to 1us (unit of 0.25us) */
4730 		run_write(sc, RT2860_TX_SW_CFG0,
4731 		    4 << RT2860_DLY_PAPE_EN_SHIFT);
4732 	}
4733 
4734 	/* wait while MAC is busy */
4735 	for (ntries = 0; ntries < 100; ntries++) {
4736 		if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
4737 			goto fail;
4738 		if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
4739 			break;
4740 		run_delay(sc, 10);
4741 	}
4742 	if (ntries == 100)
4743 		goto fail;
4744 
4745 	/* clear Host to MCU mailbox */
4746 	run_write(sc, RT2860_H2M_BBPAGENT, 0);
4747 	run_write(sc, RT2860_H2M_MAILBOX, 0);
4748 	run_delay(sc, 10);
4749 
4750 	if (run_bbp_init(sc) != 0) {
4751 		device_printf(sc->sc_dev, "could not initialize BBP\n");
4752 		goto fail;
4753 	}
4754 
4755 	/* abort TSF synchronization */
4756 	run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4757 	tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4758 	    RT2860_TBTT_TIMER_EN);
4759 	run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4760 
4761 	/* clear RX WCID search table */
4762 	run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
4763 	/* clear WCID attribute table */
4764 	run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
4765 
4766 	/* hostapd sets a key before init. So, don't clear it. */
4767 	if (sc->cmdq_key_set != RUN_CMDQ_GO) {
4768 		/* clear shared key table */
4769 		run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
4770 		/* clear shared key mode */
4771 		run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
4772 	}
4773 
4774 	run_read(sc, RT2860_US_CYC_CNT, &tmp);
4775 	tmp = (tmp & ~0xff) | 0x1e;
4776 	run_write(sc, RT2860_US_CYC_CNT, tmp);
4777 
4778 	if (sc->mac_rev != 0x0101)
4779 		run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
4780 
4781 	run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
4782 	run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
4783 
4784 	/* write vendor-specific BBP values (from EEPROM) */
4785 	for (i = 0; i < 10; i++) {
4786 		if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
4787 			continue;
4788 		run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
4789 	}
4790 
4791 	/* select Main antenna for 1T1R devices */
4792 	if (sc->rf_rev == RT3070_RF_3020)
4793 		run_set_rx_antenna(sc, 0);
4794 
4795 	/* send LEDs operating mode to microcontroller */
4796 	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
4797 	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
4798 	(void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
4799 
4800 	if (sc->mac_ver >= 0x3070)
4801 		run_rt3070_rf_init(sc);
4802 
4803 	/* disable non-existing Rx chains */
4804 	run_bbp_read(sc, 3, &bbp3);
4805 	bbp3 &= ~(1 << 3 | 1 << 4);
4806 	if (sc->nrxchains == 2)
4807 		bbp3 |= 1 << 3;
4808 	else if (sc->nrxchains == 3)
4809 		bbp3 |= 1 << 4;
4810 	run_bbp_write(sc, 3, bbp3);
4811 
4812 	/* disable non-existing Tx chains */
4813 	run_bbp_read(sc, 1, &bbp1);
4814 	if (sc->ntxchains == 1)
4815 		bbp1 &= ~(1 << 3 | 1 << 4);
4816 	run_bbp_write(sc, 1, bbp1);
4817 
4818 	if (sc->mac_ver >= 0x3070)
4819 		run_rt3070_rf_setup(sc);
4820 
4821 	/* select default channel */
4822 	run_set_chan(sc, ic->ic_curchan);
4823 
4824 	/* setup initial protection mode */
4825 	run_updateprot_cb(ic);
4826 
4827 	/* turn radio LED on */
4828 	run_set_leds(sc, RT2860_LED_RADIO);
4829 
4830 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4831 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
4832 	sc->cmdq_run = RUN_CMDQ_GO;
4833 
4834 	for (i = 0; i != RUN_N_XFER; i++)
4835 		usbd_xfer_set_stall(sc->sc_xfer[i]);
4836 
4837 	usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
4838 
4839 	if (run_txrx_enable(sc) != 0)
4840 		goto fail;
4841 
4842 	return;
4843 
4844 fail:
4845 	run_stop(sc);
4846 }
4847 
4848 static void
4849 run_init(void *arg)
4850 {
4851 	struct run_softc *sc = arg;
4852 	struct ifnet *ifp = sc->sc_ifp;
4853 	struct ieee80211com *ic = ifp->if_l2com;
4854 
4855 	RUN_LOCK(sc);
4856 	run_init_locked(sc);
4857 	RUN_UNLOCK(sc);
4858 
4859 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4860 		ieee80211_start_all(ic);
4861 }
4862 
4863 static void
4864 run_stop(void *arg)
4865 {
4866 	struct run_softc *sc = (struct run_softc *)arg;
4867 	struct ifnet *ifp = sc->sc_ifp;
4868 	uint32_t tmp;
4869 	int i;
4870 	int ntries;
4871 
4872 	RUN_LOCK_ASSERT(sc, MA_OWNED);
4873 
4874 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4875 		run_set_leds(sc, 0);	/* turn all LEDs off */
4876 
4877 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4878 
4879 	sc->ratectl_run = RUN_RATECTL_OFF;
4880 	sc->cmdq_run = sc->cmdq_key_set;
4881 
4882 	RUN_UNLOCK(sc);
4883 
4884 	for(i = 0; i < RUN_N_XFER; i++)
4885 		usbd_transfer_drain(sc->sc_xfer[i]);
4886 
4887 	RUN_LOCK(sc);
4888 
4889 	if (sc->rx_m != NULL) {
4890 		m_free(sc->rx_m);
4891 		sc->rx_m = NULL;
4892 	}
4893 
4894 	/* disable Tx/Rx */
4895 	run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
4896 	tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4897 	run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
4898 
4899 	/* wait for pending Tx to complete */
4900 	for (ntries = 0; ntries < 100; ntries++) {
4901 		if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
4902 			DPRINTF("Cannot read Tx queue count\n");
4903 			break;
4904 		}
4905 		if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
4906 			DPRINTF("All Tx cleared\n");
4907 			break;
4908 		}
4909 		run_delay(sc, 10);
4910 	}
4911 	if (ntries >= 100)
4912 		DPRINTF("There are still pending Tx\n");
4913 	run_delay(sc, 10);
4914 	run_write(sc, RT2860_USB_DMA_CFG, 0);
4915 
4916 	run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
4917 	run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4918 
4919 	for (i = 0; i != RUN_EP_QUEUES; i++)
4920 		run_unsetup_tx_list(sc, &sc->sc_epq[i]);
4921 
4922 	return;
4923 }
4924 
4925 static void
4926 run_delay(struct run_softc *sc, unsigned int ms)
4927 {
4928 	usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
4929 	    &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
4930 }
4931 
4932 static device_method_t run_methods[] = {
4933 	/* Device interface */
4934 	DEVMETHOD(device_probe,		run_match),
4935 	DEVMETHOD(device_attach,	run_attach),
4936 	DEVMETHOD(device_detach,	run_detach),
4937 
4938 	{ 0, 0 }
4939 };
4940 
4941 static driver_t run_driver = {
4942 	"run",
4943 	run_methods,
4944 	sizeof(struct run_softc)
4945 };
4946 
4947 static devclass_t run_devclass;
4948 
4949 DRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0);
4950 MODULE_DEPEND(run, wlan, 1, 1, 1);
4951 MODULE_DEPEND(run, usb, 1, 1, 1);
4952 MODULE_DEPEND(run, firmware, 1, 1, 1);
4953 MODULE_VERSION(run, 1);
4954