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