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