xref: /dragonfly/sys/bus/u4b/wlan/if_rum.c (revision 066b6da2)
1 /*	$FreeBSD: src/sys/dev/usb/wlan/if_rum.c,v 1.51 2013/03/22 02:25:33 svnexp Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6  * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 /*-
22  * Ralink Technology RT2501USB/RT2601USB chipset driver
23  * http://www.ralinktech.com.tw/
24  */
25 
26 #include "opt_inet.h"
27 
28 #include <sys/param.h>
29 #include <sys/sockio.h>
30 #include <sys/sysctl.h>
31 #include <sys/lock.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 
41 #include <sys/rman.h>
42 
43 #include <net/bpf.h>
44 #include <net/if.h>
45 #include <net/if_arp.h>
46 #include <net/ethernet.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/if_types.h>
50 #include <net/ifq_var.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip.h>
58 #endif
59 
60 #include <netproto/802_11/ieee80211_var.h>
61 #include <netproto/802_11/ieee80211_regdomain.h>
62 #include <netproto/802_11/ieee80211_radiotap.h>
63 #include <netproto/802_11/ieee80211_ratectl.h>
64 
65 #include <bus/u4b/usb.h>
66 #include <bus/u4b/usbdi.h>
67 #include "usbdevs.h"
68 
69 #define	USB_DEBUG_VAR rum_debug
70 #include <bus/u4b/usb_debug.h>
71 
72 #include <bus/u4b/wlan/if_rumreg.h>
73 #include <bus/u4b/wlan/if_rumvar.h>
74 #include <bus/u4b/wlan/if_rumfw.h>
75 
76 #ifdef USB_DEBUG
77 static int rum_debug = 0;
78 
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
80 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0,
81     "Debug level");
82 #endif
83 
84 #define N(a)	((int)(sizeof (a) / sizeof ((a)[0])))
85 
86 static const STRUCT_USB_HOST_ID rum_devs[] = {
87 #define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
88     RUM_DEV(ABOCOM, HWU54DM),
89     RUM_DEV(ABOCOM, RT2573_2),
90     RUM_DEV(ABOCOM, RT2573_3),
91     RUM_DEV(ABOCOM, RT2573_4),
92     RUM_DEV(ABOCOM, WUG2700),
93     RUM_DEV(AMIT, CGWLUSB2GO),
94     RUM_DEV(ASUS, RT2573_1),
95     RUM_DEV(ASUS, RT2573_2),
96     RUM_DEV(BELKIN, F5D7050A),
97     RUM_DEV(BELKIN, F5D9050V3),
98     RUM_DEV(CISCOLINKSYS, WUSB54GC),
99     RUM_DEV(CISCOLINKSYS, WUSB54GR),
100     RUM_DEV(CONCEPTRONIC2, C54RU2),
101     RUM_DEV(COREGA, CGWLUSB2GL),
102     RUM_DEV(COREGA, CGWLUSB2GPX),
103     RUM_DEV(DICKSMITH, CWD854F),
104     RUM_DEV(DICKSMITH, RT2573),
105     RUM_DEV(EDIMAX, EW7318USG),
106     RUM_DEV(DLINK2, DWLG122C1),
107     RUM_DEV(DLINK2, WUA1340),
108     RUM_DEV(DLINK2, DWA111),
109     RUM_DEV(DLINK2, DWA110),
110     RUM_DEV(GIGABYTE, GNWB01GS),
111     RUM_DEV(GIGABYTE, GNWI05GS),
112     RUM_DEV(GIGASET, RT2573),
113     RUM_DEV(GOODWAY, RT2573),
114     RUM_DEV(GUILLEMOT, HWGUSB254LB),
115     RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
116     RUM_DEV(HUAWEI3COM, WUB320G),
117     RUM_DEV(MELCO, G54HP),
118     RUM_DEV(MELCO, SG54HP),
119     RUM_DEV(MELCO, SG54HG),
120     RUM_DEV(MELCO, WLIUCG),
121     RUM_DEV(MELCO, WLRUCG),
122     RUM_DEV(MELCO, WLRUCGAOSS),
123     RUM_DEV(MSI, RT2573_1),
124     RUM_DEV(MSI, RT2573_2),
125     RUM_DEV(MSI, RT2573_3),
126     RUM_DEV(MSI, RT2573_4),
127     RUM_DEV(NOVATECH, RT2573),
128     RUM_DEV(PLANEX2, GWUS54HP),
129     RUM_DEV(PLANEX2, GWUS54MINI2),
130     RUM_DEV(PLANEX2, GWUSMM),
131     RUM_DEV(QCOM, RT2573),
132     RUM_DEV(QCOM, RT2573_2),
133     RUM_DEV(QCOM, RT2573_3),
134     RUM_DEV(RALINK, RT2573),
135     RUM_DEV(RALINK, RT2573_2),
136     RUM_DEV(RALINK, RT2671),
137     RUM_DEV(SITECOMEU, WL113R2),
138     RUM_DEV(SITECOMEU, WL172),
139     RUM_DEV(SPARKLAN, RT2573),
140     RUM_DEV(SURECOM, RT2573),
141 #undef RUM_DEV
142 };
143 
144 static device_probe_t rum_match;
145 static device_attach_t rum_attach;
146 static device_detach_t rum_detach;
147 
148 static usb_callback_t rum_bulk_read_callback;
149 static usb_callback_t rum_bulk_write_callback;
150 
151 static usb_error_t	rum_do_request(struct rum_softc *sc,
152 			    struct usb_device_request *req, void *data);
153 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
154 			    const char [IFNAMSIZ], int,
155 			    enum ieee80211_opmode,
156 			    int, const uint8_t [IEEE80211_ADDR_LEN],
157 			    const uint8_t [IEEE80211_ADDR_LEN]);
158 static void		rum_vap_delete(struct ieee80211vap *);
159 static void		rum_tx_free(struct rum_tx_data *, int);
160 static void		rum_setup_tx_list(struct rum_softc *);
161 static void		rum_unsetup_tx_list(struct rum_softc *);
162 static int		rum_newstate(struct ieee80211vap *,
163 			    enum ieee80211_state, int);
164 static void		rum_setup_tx_desc(struct rum_softc *,
165 			    struct rum_tx_desc *, uint32_t, uint16_t, int,
166 			    int);
167 static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
168 			    struct ieee80211_node *);
169 static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
170 			    struct ieee80211_node *,
171 			    const struct ieee80211_bpf_params *);
172 static int		rum_tx_data(struct rum_softc *, struct mbuf *,
173 			    struct ieee80211_node *);
174 static void		rum_start_locked(struct ifnet *);
175 static void		rum_start(struct ifnet *, struct ifaltq_subque *);
176 static int		rum_ioctl(struct ifnet *, u_long, caddr_t,
177 			    struct ucred *);
178 static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
179 			    int);
180 static uint32_t		rum_read(struct rum_softc *, uint16_t);
181 static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
182 			    int);
183 static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
184 static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
185 			    size_t);
186 static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
187 static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
188 static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
189 static void		rum_select_antenna(struct rum_softc *);
190 static void		rum_enable_mrr(struct rum_softc *);
191 static void		rum_set_txpreamble(struct rum_softc *);
192 static void		rum_set_basicrates(struct rum_softc *);
193 static void		rum_select_band(struct rum_softc *,
194 			    struct ieee80211_channel *);
195 static void		rum_set_chan(struct rum_softc *,
196 			    struct ieee80211_channel *);
197 static void		rum_enable_tsf_sync(struct rum_softc *);
198 static void		rum_enable_tsf(struct rum_softc *);
199 static void		rum_update_slot(struct ifnet *);
200 static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
201 static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
202 static void		rum_update_mcast(struct ifnet *);
203 static void		rum_update_promisc(struct ifnet *);
204 static void		rum_setpromisc(struct rum_softc *);
205 static const char	*rum_get_rf(int);
206 static void		rum_read_eeprom(struct rum_softc *);
207 static int		rum_bbp_init(struct rum_softc *);
208 static void		rum_init_locked(struct rum_softc *);
209 static void		rum_init(void *);
210 static void		rum_stop(struct rum_softc *);
211 static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
212 			    size_t);
213 static void		rum_prepare_beacon(struct rum_softc *,
214 			    struct ieee80211vap *);
215 static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
216 			    const struct ieee80211_bpf_params *);
217 static void		rum_scan_start(struct ieee80211com *);
218 static void		rum_scan_end(struct ieee80211com *);
219 static void		rum_set_channel(struct ieee80211com *);
220 static int		rum_get_rssi(struct rum_softc *, uint8_t);
221 static void		rum_ratectl_start(struct rum_softc *,
222 			    struct ieee80211_node *);
223 static void		rum_ratectl_timeout(void *);
224 static void		rum_ratectl_task(void *, int);
225 static int		rum_pause(struct rum_softc *, int);
226 
227 static const struct {
228 	uint32_t	reg;
229 	uint32_t	val;
230 } rum_def_mac[] = {
231 	{ RT2573_TXRX_CSR0,  0x025fb032 },
232 	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
233 	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
234 	{ RT2573_TXRX_CSR3,  0x00858687 },
235 	{ RT2573_TXRX_CSR7,  0x2e31353b },
236 	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
237 	{ RT2573_TXRX_CSR15, 0x0000000f },
238 	{ RT2573_MAC_CSR6,   0x00000fff },
239 	{ RT2573_MAC_CSR8,   0x016c030a },
240 	{ RT2573_MAC_CSR10,  0x00000718 },
241 	{ RT2573_MAC_CSR12,  0x00000004 },
242 	{ RT2573_MAC_CSR13,  0x00007f00 },
243 	{ RT2573_SEC_CSR0,   0x00000000 },
244 	{ RT2573_SEC_CSR1,   0x00000000 },
245 	{ RT2573_SEC_CSR5,   0x00000000 },
246 	{ RT2573_PHY_CSR1,   0x000023b0 },
247 	{ RT2573_PHY_CSR5,   0x00040a06 },
248 	{ RT2573_PHY_CSR6,   0x00080606 },
249 	{ RT2573_PHY_CSR7,   0x00000408 },
250 	{ RT2573_AIFSN_CSR,  0x00002273 },
251 	{ RT2573_CWMIN_CSR,  0x00002344 },
252 	{ RT2573_CWMAX_CSR,  0x000034aa }
253 };
254 
255 static const struct {
256 	uint8_t	reg;
257 	uint8_t	val;
258 } rum_def_bbp[] = {
259 	{   3, 0x80 },
260 	{  15, 0x30 },
261 	{  17, 0x20 },
262 	{  21, 0xc8 },
263 	{  22, 0x38 },
264 	{  23, 0x06 },
265 	{  24, 0xfe },
266 	{  25, 0x0a },
267 	{  26, 0x0d },
268 	{  32, 0x0b },
269 	{  34, 0x12 },
270 	{  37, 0x07 },
271 	{  39, 0xf8 },
272 	{  41, 0x60 },
273 	{  53, 0x10 },
274 	{  54, 0x18 },
275 	{  60, 0x10 },
276 	{  61, 0x04 },
277 	{  62, 0x04 },
278 	{  75, 0xfe },
279 	{  86, 0xfe },
280 	{  88, 0xfe },
281 	{  90, 0x0f },
282 	{  99, 0x00 },
283 	{ 102, 0x16 },
284 	{ 107, 0x04 }
285 };
286 
287 static const struct rfprog {
288 	uint8_t		chan;
289 	uint32_t	r1, r2, r3, r4;
290 }  rum_rf5226[] = {
291 	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
292 	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
293 	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
294 	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
295 	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
296 	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
297 	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
298 	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
299 	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
300 	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
301 	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
302 	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
303 	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
304 	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
305 
306 	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
307 	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
308 	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
309 	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
310 
311 	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
312 	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
313 	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
314 	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
315 	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
316 	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
317 	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
318 	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
319 
320 	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
321 	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
322 	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
323 	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
324 	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
325 	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
326 	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
327 	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
328 	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
329 	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
330 	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
331 
332 	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
333 	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
334 	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
335 	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
336 	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
337 }, rum_rf5225[] = {
338 	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
339 	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
340 	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
341 	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
342 	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
343 	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
344 	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
345 	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
346 	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
347 	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
348 	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
349 	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
350 	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
351 	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
352 
353 	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
354 	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
355 	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
356 	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
357 
358 	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
359 	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
360 	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
361 	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
362 	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
363 	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
364 	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
365 	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
366 
367 	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
368 	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
369 	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
370 	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
371 	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
372 	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
373 	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
374 	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
375 	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
376 	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
377 	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
378 
379 	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
380 	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
381 	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
382 	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
383 	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
384 };
385 
386 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
387 	[RUM_BULK_WR] = {
388 		.type = UE_BULK,
389 		.endpoint = UE_ADDR_ANY,
390 		.direction = UE_DIR_OUT,
391 		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
392 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
393 		.callback = rum_bulk_write_callback,
394 		.timeout = 5000,	/* ms */
395 	},
396 	[RUM_BULK_RD] = {
397 		.type = UE_BULK,
398 		.endpoint = UE_ADDR_ANY,
399 		.direction = UE_DIR_IN,
400 		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
401 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
402 		.callback = rum_bulk_read_callback,
403 	},
404 };
405 
406 static int
407 rum_match(device_t self)
408 {
409 	struct usb_attach_arg *uaa = device_get_ivars(self);
410 
411 	if (uaa->usb_mode != USB_MODE_HOST)
412 		return (ENXIO);
413 	if (uaa->info.bConfigIndex != 0)
414 		return (ENXIO);
415 	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
416 		return (ENXIO);
417 
418 	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
419 }
420 
421 static int
422 rum_attach(device_t self)
423 {
424 	struct usb_attach_arg *uaa = device_get_ivars(self);
425 	struct rum_softc *sc = device_get_softc(self);
426 	struct ieee80211com *ic;
427 	struct ifnet *ifp;
428 	uint8_t iface_index, bands;
429 	uint32_t tmp;
430 	int error, ntries;
431 
432 	wlan_serialize_enter();
433 	device_set_usb_desc(self);
434 	sc->sc_udev = uaa->device;
435 	sc->sc_dev = self;
436 
437 	lockinit(&sc->sc_lock, device_get_nameunit(self), 0, LK_CANRECURSE);
438 
439 	iface_index = RT2573_IFACE_INDEX;
440 	error = usbd_transfer_setup(uaa->device, &iface_index,
441 	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_lock);
442 	if (error) {
443 		device_printf(self, "could not allocate USB transfers, "
444 		    "err=%s\n", usbd_errstr(error));
445 		goto detach;
446 	}
447 
448 	RUM_LOCK(sc);
449 	/* retrieve RT2573 rev. no */
450 	for (ntries = 0; ntries < 100; ntries++) {
451 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
452 			break;
453 		if (rum_pause(sc, hz / 100))
454 			break;
455 	}
456 	if (ntries == 100) {
457 		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
458 		RUM_UNLOCK(sc);
459 		goto detach;
460 	}
461 
462 	/* retrieve MAC address and various other things from EEPROM */
463 	rum_read_eeprom(sc);
464 
465 	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
466 	    tmp, rum_get_rf(sc->rf_rev));
467 
468 	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
469 	RUM_UNLOCK(sc);
470 
471 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
472 	if (ifp == NULL) {
473 		device_printf(sc->sc_dev, "can not if_alloc()\n");
474 		goto detach;
475 	}
476 	ic = ifp->if_l2com;
477 
478 	ifp->if_softc = sc;
479 	if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
480 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
481 	ifp->if_init = rum_init;
482 	ifp->if_ioctl = rum_ioctl;
483 	ifp->if_start = rum_start;
484 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
485 #if 0 /* XXX swildner: see c3d4131842e47b168d93a0650d58d425ebeef789 */
486 	ifq_set_ready(&ifp->if_snd);
487 #endif
488 
489 	ic->ic_ifp = ifp;
490 	ic->ic_opmode = IEEE80211_M_STA;
491 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
492 
493 	/* set device capabilities */
494 	ic->ic_caps =
495 	      IEEE80211_C_STA		/* station mode supported */
496 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
497 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
498 	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
499 	    | IEEE80211_C_TXPMGT	/* tx power management */
500 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
501 	    | IEEE80211_C_SHSLOT	/* short slot time supported */
502 	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
503 	    | IEEE80211_C_WPA		/* 802.11i */
504 	    ;
505 
506 	bands = 0;
507 	setbit(&bands, IEEE80211_MODE_11B);
508 	setbit(&bands, IEEE80211_MODE_11G);
509 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
510 		setbit(&bands, IEEE80211_MODE_11A);
511 	ieee80211_init_channels(ic, NULL, &bands);
512 
513 	ieee80211_ifattach(ic, sc->sc_bssid);
514 	ic->ic_update_promisc = rum_update_promisc;
515 	ic->ic_raw_xmit = rum_raw_xmit;
516 	ic->ic_scan_start = rum_scan_start;
517 	ic->ic_scan_end = rum_scan_end;
518 	ic->ic_set_channel = rum_set_channel;
519 
520 	ic->ic_vap_create = rum_vap_create;
521 	ic->ic_vap_delete = rum_vap_delete;
522 	ic->ic_update_mcast = rum_update_mcast;
523 
524 	ieee80211_radiotap_attach(ic,
525 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
526 		RT2573_TX_RADIOTAP_PRESENT,
527 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
528 		RT2573_RX_RADIOTAP_PRESENT);
529 
530 	if (bootverbose)
531 		ieee80211_announce(ic);
532 
533 	wlan_serialize_exit();
534 	return (0);
535 
536 detach:
537 	wlan_serialize_exit();
538 	rum_detach(self);
539 	return (ENXIO);			/* failure */
540 }
541 
542 static int
543 rum_detach(device_t self)
544 {
545 	struct rum_softc *sc = device_get_softc(self);
546 	struct ifnet *ifp = sc->sc_ifp;
547 	struct ieee80211com *ic;
548 
549 	wlan_serialize_enter();
550 	/* Prevent further ioctls */
551 	sc->sc_detached = 1;
552 
553 	/* stop all USB transfers */
554 	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
555 
556 	/* free TX list, if any */
557 	RUM_LOCK(sc);
558 	rum_unsetup_tx_list(sc);
559 	RUM_UNLOCK(sc);
560 
561 	if (ifp) {
562 		ic = ifp->if_l2com;
563 		ieee80211_ifdetach(ic);
564 		if_free(ifp);
565 	}
566 	lockuninit(&sc->sc_lock);
567 	wlan_serialize_exit();
568 	return (0);
569 }
570 
571 static usb_error_t
572 rum_do_request(struct rum_softc *sc,
573     struct usb_device_request *req, void *data)
574 {
575 	usb_error_t err;
576 	int ntries = 10;
577 
578 	while (ntries--) {
579 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
580 		    req, data, 0, NULL, 250 /* ms */);
581 		if (err == 0)
582 			break;
583 
584 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
585 		    usbd_errstr(err));
586 		if (rum_pause(sc, hz / 100))
587 			break;
588 	}
589 	return (err);
590 }
591 
592 static struct ieee80211vap *
593 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
594     enum ieee80211_opmode opmode, int flags,
595     const uint8_t bssid[IEEE80211_ADDR_LEN],
596     const uint8_t mac[IEEE80211_ADDR_LEN])
597 {
598 	struct rum_softc *sc = ic->ic_ifp->if_softc;
599 	struct rum_vap *rvp;
600 	struct ieee80211vap *vap;
601 
602 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
603 		return NULL;
604 	rvp = (struct rum_vap *) kmalloc(sizeof(struct rum_vap),
605 	    M_80211_VAP, M_INTWAIT | M_ZERO);
606 	vap = &rvp->vap;
607 	/* enable s/w bmiss handling for sta mode */
608 	ieee80211_vap_setup(ic, vap, name, unit, opmode,
609 	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
610 
611 	/* override state transition machine */
612 	rvp->newstate = vap->iv_newstate;
613 	vap->iv_newstate = rum_newstate;
614 
615 	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_lock, 0);
616 	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
617 	ieee80211_ratectl_init(vap);
618 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
619 	/* complete setup */
620 	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
621 	ic->ic_opmode = opmode;
622 	return vap;
623 }
624 
625 static void
626 rum_vap_delete(struct ieee80211vap *vap)
627 {
628 	struct rum_vap *rvp = RUM_VAP(vap);
629 	struct ieee80211com *ic = vap->iv_ic;
630 
631 	usb_callout_drain(&rvp->ratectl_ch);
632 	ieee80211_draintask(ic, &rvp->ratectl_task);
633 	ieee80211_ratectl_deinit(vap);
634 	ieee80211_vap_detach(vap);
635 	kfree(rvp, M_80211_VAP);
636 }
637 
638 static void
639 rum_tx_free(struct rum_tx_data *data, int txerr)
640 {
641 	struct rum_softc *sc = data->sc;
642 
643 	if (data->m != NULL) {
644 		if (data->m->m_flags & M_TXCB)
645 			ieee80211_process_callback(data->ni, data->m,
646 			    txerr ? ETIMEDOUT : 0);
647 		m_freem(data->m);
648 		data->m = NULL;
649 
650 		ieee80211_free_node(data->ni);
651 		data->ni = NULL;
652 	}
653 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
654 	sc->tx_nfree++;
655 }
656 
657 static void
658 rum_setup_tx_list(struct rum_softc *sc)
659 {
660 	struct rum_tx_data *data;
661 	int i;
662 
663 	sc->tx_nfree = 0;
664 	STAILQ_INIT(&sc->tx_q);
665 	STAILQ_INIT(&sc->tx_free);
666 
667 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
668 		data = &sc->tx_data[i];
669 
670 		data->sc = sc;
671 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
672 		sc->tx_nfree++;
673 	}
674 }
675 
676 static void
677 rum_unsetup_tx_list(struct rum_softc *sc)
678 {
679 	struct rum_tx_data *data;
680 	int i;
681 
682 	/* make sure any subsequent use of the queues will fail */
683 	sc->tx_nfree = 0;
684 	STAILQ_INIT(&sc->tx_q);
685 	STAILQ_INIT(&sc->tx_free);
686 
687 	/* free up all node references and mbufs */
688 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
689 		data = &sc->tx_data[i];
690 
691 		if (data->m != NULL) {
692 			m_freem(data->m);
693 			data->m = NULL;
694 		}
695 		if (data->ni != NULL) {
696 			ieee80211_free_node(data->ni);
697 			data->ni = NULL;
698 		}
699 	}
700 }
701 
702 static int
703 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
704 {
705 	struct rum_vap *rvp = RUM_VAP(vap);
706 	struct ieee80211com *ic = vap->iv_ic;
707 	struct rum_softc *sc = ic->ic_ifp->if_softc;
708 	const struct ieee80211_txparam *tp;
709 	enum ieee80211_state ostate;
710 	struct ieee80211_node *ni;
711 	uint32_t tmp;
712 
713 	ostate = vap->iv_state;
714 	DPRINTF("%s -> %s\n",
715 		ieee80211_state_name[ostate],
716 		ieee80211_state_name[nstate]);
717 
718 #if 0 /* XXX swildner: needed? */
719 	IEEE80211_UNLOCK(ic);
720 #endif
721 	RUM_LOCK(sc);
722 	usb_callout_stop(&rvp->ratectl_ch);
723 
724 	switch (nstate) {
725 	case IEEE80211_S_INIT:
726 		if (ostate == IEEE80211_S_RUN) {
727 			/* abort TSF synchronization */
728 			tmp = rum_read(sc, RT2573_TXRX_CSR9);
729 			rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
730 		}
731 		break;
732 
733 	case IEEE80211_S_RUN:
734 		ni = ieee80211_ref_node(vap->iv_bss);
735 
736 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
737 			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
738 				RUM_UNLOCK(sc);
739 #if 0 /* XXX swildner: needed? */
740 				IEEE80211_LOCK(ic);
741 #endif
742 				ieee80211_free_node(ni);
743 				return (-1);
744 			}
745 			rum_update_slot(ic->ic_ifp);
746 			rum_enable_mrr(sc);
747 			rum_set_txpreamble(sc);
748 			rum_set_basicrates(sc);
749 			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
750 			rum_set_bssid(sc, sc->sc_bssid);
751 		}
752 
753 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
754 		    vap->iv_opmode == IEEE80211_M_IBSS)
755 			rum_prepare_beacon(sc, vap);
756 
757 		if (vap->iv_opmode != IEEE80211_M_MONITOR)
758 			rum_enable_tsf_sync(sc);
759 		else
760 			rum_enable_tsf(sc);
761 
762 		/* enable automatic rate adaptation */
763 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
764 		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
765 			rum_ratectl_start(sc, ni);
766 		ieee80211_free_node(ni);
767 		break;
768 	default:
769 		break;
770 	}
771 	RUM_UNLOCK(sc);
772 #if 0 /* XXX swildner: needed? */
773 	IEEE80211_LOCK(ic);
774 #endif
775 	return (rvp->newstate(vap, nstate, arg));
776 }
777 
778 static void
779 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
780 {
781 	struct rum_softc *sc = usbd_xfer_softc(xfer);
782 	struct ifnet *ifp = sc->sc_ifp;
783 	struct ieee80211vap *vap;
784 	struct rum_tx_data *data;
785 	struct mbuf *m;
786 	struct usb_page_cache *pc;
787 	unsigned int len;
788 	int actlen, sumlen;
789 
790 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
791 
792 	switch (USB_GET_STATE(xfer)) {
793 	case USB_ST_TRANSFERRED:
794 		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
795 
796 		/* free resources */
797 		data = usbd_xfer_get_priv(xfer);
798 		rum_tx_free(data, 0);
799 		usbd_xfer_set_priv(xfer, NULL);
800 
801 		ifp->if_opackets++;
802 		ifq_clr_oactive(&ifp->if_snd);
803 
804 		/* FALLTHROUGH */
805 	case USB_ST_SETUP:
806 tr_setup:
807 		data = STAILQ_FIRST(&sc->tx_q);
808 		if (data) {
809 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
810 			m = data->m;
811 
812 			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
813 				DPRINTFN(0, "data overflow, %u bytes\n",
814 				    m->m_pkthdr.len);
815 				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
816 			}
817 			pc = usbd_xfer_get_frame(xfer, 0);
818 			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
819 			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
820 			    m->m_pkthdr.len);
821 
822 			vap = data->ni->ni_vap;
823 			if (ieee80211_radiotap_active_vap(vap)) {
824 				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
825 
826 				tap->wt_flags = 0;
827 				tap->wt_rate = data->rate;
828 				tap->wt_antenna = sc->tx_ant;
829 
830 				ieee80211_radiotap_tx(vap, m);
831 			}
832 
833 			/* align end on a 4-bytes boundary */
834 			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
835 			if ((len % 64) == 0)
836 				len += 4;
837 
838 			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
839 			    m->m_pkthdr.len, len);
840 
841 			usbd_xfer_set_frame_len(xfer, 0, len);
842 			usbd_xfer_set_priv(xfer, data);
843 
844 			usbd_transfer_submit(xfer);
845 		}
846 		rum_start_locked(ifp);
847 		break;
848 
849 	default:			/* Error */
850 		DPRINTFN(11, "transfer error, %s\n",
851 		    usbd_errstr(error));
852 
853 		ifp->if_oerrors++;
854 		data = usbd_xfer_get_priv(xfer);
855 		if (data != NULL) {
856 			rum_tx_free(data, error);
857 			usbd_xfer_set_priv(xfer, NULL);
858 		}
859 
860 		if (error != USB_ERR_CANCELLED) {
861 			if (error == USB_ERR_TIMEOUT)
862 				device_printf(sc->sc_dev, "device timeout\n");
863 
864 			/*
865 			 * Try to clear stall first, also if other
866 			 * errors occur, hence clearing stall
867 			 * introduces a 50 ms delay:
868 			 */
869 			usbd_xfer_set_stall(xfer);
870 			goto tr_setup;
871 		}
872 		break;
873 	}
874 }
875 
876 static void
877 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
878 {
879 	struct rum_softc *sc = usbd_xfer_softc(xfer);
880 	struct ifnet *ifp = sc->sc_ifp;
881 	struct ieee80211com *ic = ifp->if_l2com;
882 	struct ieee80211_node *ni;
883 	struct mbuf *m = NULL;
884 	struct usb_page_cache *pc;
885 	uint32_t flags;
886 	uint8_t rssi = 0;
887 	int len;
888 
889 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
890 
891 	switch (USB_GET_STATE(xfer)) {
892 	case USB_ST_TRANSFERRED:
893 
894 		DPRINTFN(15, "rx done, actlen=%d\n", len);
895 
896 		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
897 			DPRINTF("%s: xfer too short %d\n",
898 			    device_get_nameunit(sc->sc_dev), len);
899 			ifp->if_ierrors++;
900 			goto tr_setup;
901 		}
902 
903 		len -= RT2573_RX_DESC_SIZE;
904 		pc = usbd_xfer_get_frame(xfer, 0);
905 		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
906 
907 		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
908 		flags = le32toh(sc->sc_rx_desc.flags);
909 		if (flags & RT2573_RX_CRC_ERROR) {
910 			/*
911 		         * This should not happen since we did not
912 		         * request to receive those frames when we
913 		         * filled RUM_TXRX_CSR2:
914 		         */
915 			DPRINTFN(5, "PHY or CRC error\n");
916 			ifp->if_ierrors++;
917 			goto tr_setup;
918 		}
919 
920 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
921 		if (m == NULL) {
922 			DPRINTF("could not allocate mbuf\n");
923 			ifp->if_ierrors++;
924 			goto tr_setup;
925 		}
926 		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
927 		    mtod(m, uint8_t *), len);
928 
929 		/* finalize mbuf */
930 		m->m_pkthdr.rcvif = ifp;
931 		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
932 
933 		if (ieee80211_radiotap_active(ic)) {
934 			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
935 
936 			/* XXX read tsf */
937 			tap->wr_flags = 0;
938 			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
939 			    (flags & RT2573_RX_OFDM) ?
940 			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
941 			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
942 			tap->wr_antnoise = RT2573_NOISE_FLOOR;
943 			tap->wr_antenna = sc->rx_ant;
944 		}
945 		/* FALLTHROUGH */
946 	case USB_ST_SETUP:
947 tr_setup:
948 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
949 		usbd_transfer_submit(xfer);
950 
951 		/*
952 		 * At the end of a USB callback it is always safe to unlock
953 		 * the private mutex of a device! That is why we do the
954 		 * "ieee80211_input" here, and not some lines up!
955 		 */
956 		RUM_UNLOCK(sc);
957 		if (m) {
958 			ni = ieee80211_find_rxnode(ic,
959 			    mtod(m, struct ieee80211_frame_min *));
960 			if (ni != NULL) {
961 				(void) ieee80211_input(ni, m, rssi,
962 				    RT2573_NOISE_FLOOR);
963 				ieee80211_free_node(ni);
964 			} else
965 				(void) ieee80211_input_all(ic, m, rssi,
966 				    RT2573_NOISE_FLOOR);
967 		}
968 		if (!ifq_is_oactive(&ifp->if_snd) &&
969 		    !ifq_is_empty(&ifp->if_snd))
970 			rum_start_locked(ifp);
971 		RUM_LOCK(sc);
972 		return;
973 
974 	default:			/* Error */
975 		if (error != USB_ERR_CANCELLED) {
976 			/* try to clear stall first */
977 			usbd_xfer_set_stall(xfer);
978 			goto tr_setup;
979 		}
980 		return;
981 	}
982 }
983 
984 static uint8_t
985 rum_plcp_signal(int rate)
986 {
987 	switch (rate) {
988 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
989 	case 12:	return 0xb;
990 	case 18:	return 0xf;
991 	case 24:	return 0xa;
992 	case 36:	return 0xe;
993 	case 48:	return 0x9;
994 	case 72:	return 0xd;
995 	case 96:	return 0x8;
996 	case 108:	return 0xc;
997 
998 	/* CCK rates (NB: not IEEE std, device-specific) */
999 	case 2:		return 0x0;
1000 	case 4:		return 0x1;
1001 	case 11:	return 0x2;
1002 	case 22:	return 0x3;
1003 	}
1004 	return 0xff;		/* XXX unsupported/unknown rate */
1005 }
1006 
1007 static void
1008 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1009     uint32_t flags, uint16_t xflags, int len, int rate)
1010 {
1011 	struct ifnet *ifp = sc->sc_ifp;
1012 	struct ieee80211com *ic = ifp->if_l2com;
1013 	uint16_t plcp_length;
1014 	int remainder;
1015 
1016 	desc->flags = htole32(flags);
1017 	desc->flags |= htole32(RT2573_TX_VALID);
1018 	desc->flags |= htole32(len << 16);
1019 
1020 	desc->xflags = htole16(xflags);
1021 
1022 	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1023 	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1024 
1025 	/* setup PLCP fields */
1026 	desc->plcp_signal  = rum_plcp_signal(rate);
1027 	desc->plcp_service = 4;
1028 
1029 	len += IEEE80211_CRC_LEN;
1030 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1031 		desc->flags |= htole32(RT2573_TX_OFDM);
1032 
1033 		plcp_length = len & 0xfff;
1034 		desc->plcp_length_hi = plcp_length >> 6;
1035 		desc->plcp_length_lo = plcp_length & 0x3f;
1036 	} else {
1037 		plcp_length = (16 * len + rate - 1) / rate;
1038 		if (rate == 22) {
1039 			remainder = (16 * len) % 22;
1040 			if (remainder != 0 && remainder < 7)
1041 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1042 		}
1043 		desc->plcp_length_hi = plcp_length >> 8;
1044 		desc->plcp_length_lo = plcp_length & 0xff;
1045 
1046 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1047 			desc->plcp_signal |= 0x08;
1048 	}
1049 }
1050 
1051 static int
1052 rum_sendprot(struct rum_softc *sc,
1053     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1054 {
1055 	struct ieee80211com *ic = ni->ni_ic;
1056 	const struct ieee80211_frame *wh;
1057 	struct rum_tx_data *data;
1058 	struct mbuf *mprot;
1059 	int protrate, ackrate, pktlen, flags, isshort;
1060 	uint16_t dur;
1061 
1062 	RUM_LOCK_ASSERT(sc, MA_OWNED);
1063 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1064 	    ("protection %d", prot));
1065 
1066 	wh = mtod(m, const struct ieee80211_frame *);
1067 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1068 
1069 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1070 	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1071 
1072 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1073 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1074 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1075 	flags = RT2573_TX_MORE_FRAG;
1076 	if (prot == IEEE80211_PROT_RTSCTS) {
1077 		/* NB: CTS is the same size as an ACK */
1078 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1079 		flags |= RT2573_TX_NEED_ACK;
1080 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1081 	} else {
1082 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1083 	}
1084 	if (mprot == NULL) {
1085 		/* XXX stat + msg */
1086 		return (ENOBUFS);
1087 	}
1088 	data = STAILQ_FIRST(&sc->tx_free);
1089 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1090 	sc->tx_nfree--;
1091 
1092 	data->m = mprot;
1093 	data->ni = ieee80211_ref_node(ni);
1094 	data->rate = protrate;
1095 	rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1096 
1097 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1098 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1099 
1100 	return 0;
1101 }
1102 
1103 static int
1104 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1105 {
1106 	struct ieee80211vap *vap = ni->ni_vap;
1107 	struct ifnet *ifp = sc->sc_ifp;
1108 	struct ieee80211com *ic = ifp->if_l2com;
1109 	struct rum_tx_data *data;
1110 	struct ieee80211_frame *wh;
1111 	const struct ieee80211_txparam *tp;
1112 	struct ieee80211_key *k;
1113 	uint32_t flags = 0;
1114 	uint16_t dur;
1115 
1116 	RUM_LOCK_ASSERT(sc, MA_OWNED);
1117 
1118 	data = STAILQ_FIRST(&sc->tx_free);
1119 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1120 	sc->tx_nfree--;
1121 
1122 	wh = mtod(m0, struct ieee80211_frame *);
1123 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1124 		k = ieee80211_crypto_encap(ni, m0);
1125 		if (k == NULL) {
1126 			m_freem(m0);
1127 			return ENOBUFS;
1128 		}
1129 		wh = mtod(m0, struct ieee80211_frame *);
1130 	}
1131 
1132 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1133 
1134 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1135 		flags |= RT2573_TX_NEED_ACK;
1136 
1137 		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1138 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1139 		*(uint16_t *)wh->i_dur = htole16(dur);
1140 
1141 		/* tell hardware to add timestamp for probe responses */
1142 		if ((wh->i_fc[0] &
1143 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1144 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1145 			flags |= RT2573_TX_TIMESTAMP;
1146 	}
1147 
1148 	data->m = m0;
1149 	data->ni = ni;
1150 	data->rate = tp->mgmtrate;
1151 
1152 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1153 
1154 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1155 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1156 
1157 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1158 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1159 
1160 	return (0);
1161 }
1162 
1163 static int
1164 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1165     const struct ieee80211_bpf_params *params)
1166 {
1167 	struct ieee80211com *ic = ni->ni_ic;
1168 	struct rum_tx_data *data;
1169 	uint32_t flags;
1170 	int rate, error;
1171 
1172 	RUM_LOCK_ASSERT(sc, MA_OWNED);
1173 	KASSERT(params != NULL, ("no raw xmit params"));
1174 
1175 	rate = params->ibp_rate0;
1176 	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1177 		m_freem(m0);
1178 		return EINVAL;
1179 	}
1180 	flags = 0;
1181 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1182 		flags |= RT2573_TX_NEED_ACK;
1183 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1184 		error = rum_sendprot(sc, m0, ni,
1185 		    params->ibp_flags & IEEE80211_BPF_RTS ?
1186 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1187 		    rate);
1188 		if (error || sc->tx_nfree == 0) {
1189 			m_freem(m0);
1190 			return ENOBUFS;
1191 		}
1192 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1193 	}
1194 
1195 	data = STAILQ_FIRST(&sc->tx_free);
1196 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1197 	sc->tx_nfree--;
1198 
1199 	data->m = m0;
1200 	data->ni = ni;
1201 	data->rate = rate;
1202 
1203 	/* XXX need to setup descriptor ourself */
1204 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1205 
1206 	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1207 	    m0->m_pkthdr.len, rate);
1208 
1209 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1210 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1211 
1212 	return 0;
1213 }
1214 
1215 static int
1216 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1217 {
1218 	struct ieee80211vap *vap = ni->ni_vap;
1219 	struct ifnet *ifp = sc->sc_ifp;
1220 	struct ieee80211com *ic = ifp->if_l2com;
1221 	struct rum_tx_data *data;
1222 	struct ieee80211_frame *wh;
1223 	const struct ieee80211_txparam *tp;
1224 	struct ieee80211_key *k;
1225 	uint32_t flags = 0;
1226 	uint16_t dur;
1227 	int error, rate;
1228 
1229 #if 0 /* XXX swildner: lock needed? */
1230 	RUM_LOCK_ASSERT(sc, MA_OWNED);
1231 #endif
1232 
1233 	wh = mtod(m0, struct ieee80211_frame *);
1234 
1235 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1236 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1237 		rate = tp->mcastrate;
1238 	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1239 		rate = tp->ucastrate;
1240 	else
1241 		rate = ni->ni_txrate;
1242 
1243 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1244 		k = ieee80211_crypto_encap(ni, m0);
1245 		if (k == NULL) {
1246 			m_freem(m0);
1247 			return ENOBUFS;
1248 		}
1249 
1250 		/* packet header may have moved, reset our local pointer */
1251 		wh = mtod(m0, struct ieee80211_frame *);
1252 	}
1253 
1254 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1255 		int prot = IEEE80211_PROT_NONE;
1256 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1257 			prot = IEEE80211_PROT_RTSCTS;
1258 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1259 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1260 			prot = ic->ic_protmode;
1261 		if (prot != IEEE80211_PROT_NONE) {
1262 			error = rum_sendprot(sc, m0, ni, prot, rate);
1263 			if (error || sc->tx_nfree == 0) {
1264 				m_freem(m0);
1265 				return ENOBUFS;
1266 			}
1267 			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1268 		}
1269 	}
1270 
1271 	data = STAILQ_FIRST(&sc->tx_free);
1272 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1273 	sc->tx_nfree--;
1274 
1275 	data->m = m0;
1276 	data->ni = ni;
1277 	data->rate = rate;
1278 
1279 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1280 		flags |= RT2573_TX_NEED_ACK;
1281 		flags |= RT2573_TX_MORE_FRAG;
1282 
1283 		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1284 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1285 		*(uint16_t *)wh->i_dur = htole16(dur);
1286 	}
1287 
1288 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1289 
1290 	DPRINTFN(10, "sending frame len=%d rate=%d\n",
1291 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1292 
1293 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1294 	RUM_LOCK(sc);
1295 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1296 	RUM_UNLOCK(sc);
1297 
1298 	return 0;
1299 }
1300 
1301 static void
1302 rum_start_locked(struct ifnet *ifp)
1303 {
1304 	struct rum_softc *sc = ifp->if_softc;
1305 	struct ieee80211_node *ni;
1306 	struct mbuf *m;
1307 
1308 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
1309 		return;
1310 	}
1311 	for (;;) {
1312 		m = ifq_dequeue(&ifp->if_snd);
1313 		if (m == NULL)
1314 			break;
1315 		if (sc->tx_nfree < RUM_TX_MINFREE) {
1316 			ifq_prepend(&ifp->if_snd, m);
1317 			ifq_set_oactive(&ifp->if_snd);
1318 			break;
1319 		}
1320 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1321 		if (rum_tx_data(sc, m, ni) != 0) {
1322 			ieee80211_free_node(ni);
1323 			ifp->if_oerrors++;
1324 			break;
1325 		}
1326 	}
1327 }
1328 
1329 static void
1330 rum_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1331 {
1332 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1333 	rum_start_locked(ifp);
1334 }
1335 
1336 static int
1337 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
1338 {
1339 	struct rum_softc *sc = ifp->if_softc;
1340 	struct ieee80211com *ic = ifp->if_l2com;
1341 	struct ifreq *ifr = (struct ifreq *) data;
1342 	int error;
1343 	int startall = 0;
1344 
1345 	RUM_LOCK(sc);
1346 	error = sc->sc_detached ? ENXIO : 0;
1347 	RUM_UNLOCK(sc);
1348 	if (error)
1349 		return (error);
1350 
1351 	switch (cmd) {
1352 	case SIOCSIFFLAGS:
1353 		RUM_LOCK(sc);
1354 		if (ifp->if_flags & IFF_UP) {
1355 			if ((ifp->if_flags & IFF_RUNNING) == 0) {
1356 				rum_init_locked(sc);
1357 				startall = 1;
1358 			} else
1359 				rum_setpromisc(sc);
1360 		} else {
1361 			if (ifp->if_flags & IFF_RUNNING)
1362 				rum_stop(sc);
1363 		}
1364 		RUM_UNLOCK(sc);
1365 		if (startall)
1366 			ieee80211_start_all(ic);
1367 		break;
1368 	case SIOCGIFMEDIA:
1369 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1370 		break;
1371 	case SIOCGIFADDR:
1372 		error = ether_ioctl(ifp, cmd, data);
1373 		break;
1374 	default:
1375 		error = EINVAL;
1376 		break;
1377 	}
1378 	return error;
1379 }
1380 
1381 static void
1382 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1383 {
1384 	struct usb_device_request req;
1385 	usb_error_t error;
1386 
1387 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1388 	req.bRequest = RT2573_READ_EEPROM;
1389 	USETW(req.wValue, 0);
1390 	USETW(req.wIndex, addr);
1391 	USETW(req.wLength, len);
1392 
1393 	error = rum_do_request(sc, &req, buf);
1394 	if (error != 0) {
1395 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1396 		    usbd_errstr(error));
1397 	}
1398 }
1399 
1400 static uint32_t
1401 rum_read(struct rum_softc *sc, uint16_t reg)
1402 {
1403 	uint32_t val;
1404 
1405 	rum_read_multi(sc, reg, &val, sizeof val);
1406 
1407 	return le32toh(val);
1408 }
1409 
1410 static void
1411 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1412 {
1413 	struct usb_device_request req;
1414 	usb_error_t error;
1415 
1416 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1417 	req.bRequest = RT2573_READ_MULTI_MAC;
1418 	USETW(req.wValue, 0);
1419 	USETW(req.wIndex, reg);
1420 	USETW(req.wLength, len);
1421 
1422 	error = rum_do_request(sc, &req, buf);
1423 	if (error != 0) {
1424 		device_printf(sc->sc_dev,
1425 		    "could not multi read MAC register: %s\n",
1426 		    usbd_errstr(error));
1427 	}
1428 }
1429 
1430 static usb_error_t
1431 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1432 {
1433 	uint32_t tmp = htole32(val);
1434 
1435 	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1436 }
1437 
1438 static usb_error_t
1439 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1440 {
1441 	struct usb_device_request req;
1442 	usb_error_t error;
1443 	size_t offset;
1444 
1445 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1446 	req.bRequest = RT2573_WRITE_MULTI_MAC;
1447 	USETW(req.wValue, 0);
1448 
1449 	/* write at most 64 bytes at a time */
1450 	for (offset = 0; offset < len; offset += 64) {
1451 		USETW(req.wIndex, reg + offset);
1452 		USETW(req.wLength, MIN(len - offset, 64));
1453 
1454 		error = rum_do_request(sc, &req, (char *)buf + offset);
1455 		if (error != 0) {
1456 			device_printf(sc->sc_dev,
1457 			    "could not multi write MAC register: %s\n",
1458 			    usbd_errstr(error));
1459 			return (error);
1460 		}
1461 	}
1462 
1463 	return (USB_ERR_NORMAL_COMPLETION);
1464 }
1465 
1466 static void
1467 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1468 {
1469 	uint32_t tmp;
1470 	int ntries;
1471 
1472 	DPRINTFN(2, "reg=0x%08x\n", reg);
1473 
1474 	for (ntries = 0; ntries < 100; ntries++) {
1475 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1476 			break;
1477 		if (rum_pause(sc, hz / 100))
1478 			break;
1479 	}
1480 	if (ntries == 100) {
1481 		device_printf(sc->sc_dev, "could not write to BBP\n");
1482 		return;
1483 	}
1484 
1485 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1486 	rum_write(sc, RT2573_PHY_CSR3, tmp);
1487 }
1488 
1489 static uint8_t
1490 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1491 {
1492 	uint32_t val;
1493 	int ntries;
1494 
1495 	DPRINTFN(2, "reg=0x%08x\n", reg);
1496 
1497 	for (ntries = 0; ntries < 100; ntries++) {
1498 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1499 			break;
1500 		if (rum_pause(sc, hz / 100))
1501 			break;
1502 	}
1503 	if (ntries == 100) {
1504 		device_printf(sc->sc_dev, "could not read BBP\n");
1505 		return 0;
1506 	}
1507 
1508 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1509 	rum_write(sc, RT2573_PHY_CSR3, val);
1510 
1511 	for (ntries = 0; ntries < 100; ntries++) {
1512 		val = rum_read(sc, RT2573_PHY_CSR3);
1513 		if (!(val & RT2573_BBP_BUSY))
1514 			return val & 0xff;
1515 		if (rum_pause(sc, hz / 100))
1516 			break;
1517 	}
1518 
1519 	device_printf(sc->sc_dev, "could not read BBP\n");
1520 	return 0;
1521 }
1522 
1523 static void
1524 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1525 {
1526 	uint32_t tmp;
1527 	int ntries;
1528 
1529 	for (ntries = 0; ntries < 100; ntries++) {
1530 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1531 			break;
1532 		if (rum_pause(sc, hz / 100))
1533 			break;
1534 	}
1535 	if (ntries == 100) {
1536 		device_printf(sc->sc_dev, "could not write to RF\n");
1537 		return;
1538 	}
1539 
1540 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1541 	    (reg & 3);
1542 	rum_write(sc, RT2573_PHY_CSR4, tmp);
1543 
1544 	/* remember last written value in sc */
1545 	sc->rf_regs[reg] = val;
1546 
1547 	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1548 }
1549 
1550 static void
1551 rum_select_antenna(struct rum_softc *sc)
1552 {
1553 	uint8_t bbp4, bbp77;
1554 	uint32_t tmp;
1555 
1556 	bbp4  = rum_bbp_read(sc, 4);
1557 	bbp77 = rum_bbp_read(sc, 77);
1558 
1559 	/* TBD */
1560 
1561 	/* make sure Rx is disabled before switching antenna */
1562 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1563 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1564 
1565 	rum_bbp_write(sc,  4, bbp4);
1566 	rum_bbp_write(sc, 77, bbp77);
1567 
1568 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1569 }
1570 
1571 /*
1572  * Enable multi-rate retries for frames sent at OFDM rates.
1573  * In 802.11b/g mode, allow fallback to CCK rates.
1574  */
1575 static void
1576 rum_enable_mrr(struct rum_softc *sc)
1577 {
1578 	struct ifnet *ifp = sc->sc_ifp;
1579 	struct ieee80211com *ic = ifp->if_l2com;
1580 	uint32_t tmp;
1581 
1582 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1583 
1584 	tmp &= ~RT2573_MRR_CCK_FALLBACK;
1585 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1586 		tmp |= RT2573_MRR_CCK_FALLBACK;
1587 	tmp |= RT2573_MRR_ENABLED;
1588 
1589 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1590 }
1591 
1592 static void
1593 rum_set_txpreamble(struct rum_softc *sc)
1594 {
1595 	struct ifnet *ifp = sc->sc_ifp;
1596 	struct ieee80211com *ic = ifp->if_l2com;
1597 	uint32_t tmp;
1598 
1599 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1600 
1601 	tmp &= ~RT2573_SHORT_PREAMBLE;
1602 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1603 		tmp |= RT2573_SHORT_PREAMBLE;
1604 
1605 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1606 }
1607 
1608 static void
1609 rum_set_basicrates(struct rum_softc *sc)
1610 {
1611 	struct ifnet *ifp = sc->sc_ifp;
1612 	struct ieee80211com *ic = ifp->if_l2com;
1613 
1614 	/* update basic rate set */
1615 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1616 		/* 11b basic rates: 1, 2Mbps */
1617 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1618 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1619 		/* 11a basic rates: 6, 12, 24Mbps */
1620 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1621 	} else {
1622 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1623 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1624 	}
1625 }
1626 
1627 /*
1628  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1629  * driver.
1630  */
1631 static void
1632 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1633 {
1634 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1635 	uint32_t tmp;
1636 
1637 	/* update all BBP registers that depend on the band */
1638 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1639 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1640 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1641 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1642 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1643 	}
1644 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1645 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1646 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1647 	}
1648 
1649 	sc->bbp17 = bbp17;
1650 	rum_bbp_write(sc,  17, bbp17);
1651 	rum_bbp_write(sc,  96, bbp96);
1652 	rum_bbp_write(sc, 104, bbp104);
1653 
1654 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1655 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1656 		rum_bbp_write(sc, 75, 0x80);
1657 		rum_bbp_write(sc, 86, 0x80);
1658 		rum_bbp_write(sc, 88, 0x80);
1659 	}
1660 
1661 	rum_bbp_write(sc, 35, bbp35);
1662 	rum_bbp_write(sc, 97, bbp97);
1663 	rum_bbp_write(sc, 98, bbp98);
1664 
1665 	tmp = rum_read(sc, RT2573_PHY_CSR0);
1666 	tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1667 	if (IEEE80211_IS_CHAN_2GHZ(c))
1668 		tmp |= RT2573_PA_PE_2GHZ;
1669 	else
1670 		tmp |= RT2573_PA_PE_5GHZ;
1671 	rum_write(sc, RT2573_PHY_CSR0, tmp);
1672 }
1673 
1674 static void
1675 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1676 {
1677 	struct ifnet *ifp = sc->sc_ifp;
1678 	struct ieee80211com *ic = ifp->if_l2com;
1679 	const struct rfprog *rfprog;
1680 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1681 	int8_t power;
1682 	int i, chan;
1683 
1684 	chan = ieee80211_chan2ieee(ic, c);
1685 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1686 		return;
1687 
1688 	/* select the appropriate RF settings based on what EEPROM says */
1689 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1690 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1691 
1692 	/* find the settings for this channel (we know it exists) */
1693 	for (i = 0; rfprog[i].chan != chan; i++);
1694 
1695 	power = sc->txpow[i];
1696 	if (power < 0) {
1697 		bbp94 += power;
1698 		power = 0;
1699 	} else if (power > 31) {
1700 		bbp94 += power - 31;
1701 		power = 31;
1702 	}
1703 
1704 	/*
1705 	 * If we are switching from the 2GHz band to the 5GHz band or
1706 	 * vice-versa, BBP registers need to be reprogrammed.
1707 	 */
1708 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
1709 		rum_select_band(sc, c);
1710 		rum_select_antenna(sc);
1711 	}
1712 	ic->ic_curchan = c;
1713 
1714 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1715 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1716 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1717 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1718 
1719 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1720 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1721 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1722 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1723 
1724 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1725 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1726 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1727 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1728 
1729 	rum_pause(sc, hz / 100);
1730 
1731 	/* enable smart mode for MIMO-capable RFs */
1732 	bbp3 = rum_bbp_read(sc, 3);
1733 
1734 	bbp3 &= ~RT2573_SMART_MODE;
1735 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1736 		bbp3 |= RT2573_SMART_MODE;
1737 
1738 	rum_bbp_write(sc, 3, bbp3);
1739 
1740 	if (bbp94 != RT2573_BBPR94_DEFAULT)
1741 		rum_bbp_write(sc, 94, bbp94);
1742 
1743 	/* give the chip some extra time to do the switchover */
1744 	rum_pause(sc, hz / 100);
1745 }
1746 
1747 /*
1748  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1749  * and HostAP operating modes.
1750  */
1751 static void
1752 rum_enable_tsf_sync(struct rum_softc *sc)
1753 {
1754 	struct ifnet *ifp = sc->sc_ifp;
1755 	struct ieee80211com *ic = ifp->if_l2com;
1756 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1757 	uint32_t tmp;
1758 
1759 	if (vap->iv_opmode != IEEE80211_M_STA) {
1760 		/*
1761 		 * Change default 16ms TBTT adjustment to 8ms.
1762 		 * Must be done before enabling beacon generation.
1763 		 */
1764 		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1765 	}
1766 
1767 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1768 
1769 	/* set beacon interval (in 1/16ms unit) */
1770 	tmp |= vap->iv_bss->ni_intval * 16;
1771 
1772 	tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1773 	if (vap->iv_opmode == IEEE80211_M_STA)
1774 		tmp |= RT2573_TSF_MODE(1);
1775 	else
1776 		tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1777 
1778 	rum_write(sc, RT2573_TXRX_CSR9, tmp);
1779 }
1780 
1781 static void
1782 rum_enable_tsf(struct rum_softc *sc)
1783 {
1784 	rum_write(sc, RT2573_TXRX_CSR9,
1785 	    (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) |
1786 	    RT2573_TSF_TICKING | RT2573_TSF_MODE(2));
1787 }
1788 
1789 static void
1790 rum_update_slot(struct ifnet *ifp)
1791 {
1792 	struct rum_softc *sc = ifp->if_softc;
1793 	struct ieee80211com *ic = ifp->if_l2com;
1794 	uint8_t slottime;
1795 	uint32_t tmp;
1796 
1797 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1798 
1799 	tmp = rum_read(sc, RT2573_MAC_CSR9);
1800 	tmp = (tmp & ~0xff) | slottime;
1801 	rum_write(sc, RT2573_MAC_CSR9, tmp);
1802 
1803 	DPRINTF("setting slot time to %uus\n", slottime);
1804 }
1805 
1806 static void
1807 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1808 {
1809 	uint32_t tmp;
1810 
1811 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1812 	rum_write(sc, RT2573_MAC_CSR4, tmp);
1813 
1814 	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1815 	rum_write(sc, RT2573_MAC_CSR5, tmp);
1816 }
1817 
1818 static void
1819 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1820 {
1821 	uint32_t tmp;
1822 
1823 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1824 	rum_write(sc, RT2573_MAC_CSR2, tmp);
1825 
1826 	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1827 	rum_write(sc, RT2573_MAC_CSR3, tmp);
1828 }
1829 
1830 static void
1831 rum_setpromisc(struct rum_softc *sc)
1832 {
1833 	struct ifnet *ifp = sc->sc_ifp;
1834 	uint32_t tmp;
1835 
1836 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1837 
1838 	tmp &= ~RT2573_DROP_NOT_TO_ME;
1839 	if (!(ifp->if_flags & IFF_PROMISC))
1840 		tmp |= RT2573_DROP_NOT_TO_ME;
1841 
1842 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1843 
1844 	DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1845 	    "entering" : "leaving");
1846 }
1847 
1848 static void
1849 rum_update_promisc(struct ifnet *ifp)
1850 {
1851 	struct rum_softc *sc = ifp->if_softc;
1852 
1853 	if ((ifp->if_flags & IFF_RUNNING) == 0)
1854 		return;
1855 
1856 	RUM_LOCK(sc);
1857 	rum_setpromisc(sc);
1858 	RUM_UNLOCK(sc);
1859 }
1860 
1861 static void
1862 rum_update_mcast(struct ifnet *ifp)
1863 {
1864 	static int warning_printed;
1865 
1866 	if (warning_printed == 0) {
1867 		if_printf(ifp, "need to implement %s\n", __func__);
1868 		warning_printed = 1;
1869 	}
1870 }
1871 
1872 static const char *
1873 rum_get_rf(int rev)
1874 {
1875 	switch (rev) {
1876 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
1877 	case RT2573_RF_2528:	return "RT2528";
1878 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
1879 	case RT2573_RF_5226:	return "RT5226";
1880 	default:		return "unknown";
1881 	}
1882 }
1883 
1884 static void
1885 rum_read_eeprom(struct rum_softc *sc)
1886 {
1887 	uint16_t val;
1888 #ifdef RUM_DEBUG
1889 	int i;
1890 #endif
1891 
1892 	/* read MAC address */
1893 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6);
1894 
1895 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1896 	val = le16toh(val);
1897 	sc->rf_rev =   (val >> 11) & 0x1f;
1898 	sc->hw_radio = (val >> 10) & 0x1;
1899 	sc->rx_ant =   (val >> 4)  & 0x3;
1900 	sc->tx_ant =   (val >> 2)  & 0x3;
1901 	sc->nb_ant =   val & 0x3;
1902 
1903 	DPRINTF("RF revision=%d\n", sc->rf_rev);
1904 
1905 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1906 	val = le16toh(val);
1907 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
1908 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
1909 
1910 	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1911 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1912 
1913 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1914 	val = le16toh(val);
1915 	if ((val & 0xff) != 0xff)
1916 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
1917 
1918 	/* Only [-10, 10] is valid */
1919 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1920 		sc->rssi_2ghz_corr = 0;
1921 
1922 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1923 	val = le16toh(val);
1924 	if ((val & 0xff) != 0xff)
1925 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
1926 
1927 	/* Only [-10, 10] is valid */
1928 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1929 		sc->rssi_5ghz_corr = 0;
1930 
1931 	if (sc->ext_2ghz_lna)
1932 		sc->rssi_2ghz_corr -= 14;
1933 	if (sc->ext_5ghz_lna)
1934 		sc->rssi_5ghz_corr -= 14;
1935 
1936 	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1937 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1938 
1939 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1940 	val = le16toh(val);
1941 	if ((val & 0xff) != 0xff)
1942 		sc->rffreq = val & 0xff;
1943 
1944 	DPRINTF("RF freq=%d\n", sc->rffreq);
1945 
1946 	/* read Tx power for all a/b/g channels */
1947 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1948 	/* XXX default Tx power for 802.11a channels */
1949 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1950 #ifdef RUM_DEBUG
1951 	for (i = 0; i < 14; i++)
1952 		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
1953 #endif
1954 
1955 	/* read default values for BBP registers */
1956 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1957 #ifdef RUM_DEBUG
1958 	for (i = 0; i < 14; i++) {
1959 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1960 			continue;
1961 		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1962 		    sc->bbp_prom[i].val);
1963 	}
1964 #endif
1965 }
1966 
1967 static int
1968 rum_bbp_init(struct rum_softc *sc)
1969 {
1970 	int i, ntries;
1971 
1972 	/* wait for BBP to be ready */
1973 	for (ntries = 0; ntries < 100; ntries++) {
1974 		const uint8_t val = rum_bbp_read(sc, 0);
1975 		if (val != 0 && val != 0xff)
1976 			break;
1977 		if (rum_pause(sc, hz / 100))
1978 			break;
1979 	}
1980 	if (ntries == 100) {
1981 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1982 		return EIO;
1983 	}
1984 
1985 	/* initialize BBP registers to default values */
1986 	for (i = 0; i < N(rum_def_bbp); i++)
1987 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1988 
1989 	/* write vendor-specific BBP values (from EEPROM) */
1990 	for (i = 0; i < 16; i++) {
1991 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1992 			continue;
1993 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1994 	}
1995 
1996 	return 0;
1997 }
1998 
1999 static void
2000 rum_init_locked(struct rum_softc *sc)
2001 {
2002 	struct ifnet *ifp = sc->sc_ifp;
2003 	struct ieee80211com *ic = ifp->if_l2com;
2004 	uint32_t tmp;
2005 	usb_error_t error;
2006 	int i, ntries;
2007 
2008 	RUM_LOCK_ASSERT(sc, MA_OWNED);
2009 
2010 	rum_stop(sc);
2011 
2012 	/* initialize MAC registers to default values */
2013 	for (i = 0; i < N(rum_def_mac); i++)
2014 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2015 
2016 	/* set host ready */
2017 	rum_write(sc, RT2573_MAC_CSR1, 3);
2018 	rum_write(sc, RT2573_MAC_CSR1, 0);
2019 
2020 	/* wait for BBP/RF to wakeup */
2021 	for (ntries = 0; ntries < 100; ntries++) {
2022 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2023 			break;
2024 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
2025 		if (rum_pause(sc, hz / 100))
2026 			break;
2027 	}
2028 	if (ntries == 100) {
2029 		device_printf(sc->sc_dev,
2030 		    "timeout waiting for BBP/RF to wakeup\n");
2031 		goto fail;
2032 	}
2033 
2034 	if ((error = rum_bbp_init(sc)) != 0)
2035 		goto fail;
2036 
2037 	/* select default channel */
2038 	rum_select_band(sc, ic->ic_curchan);
2039 	rum_select_antenna(sc);
2040 	rum_set_chan(sc, ic->ic_curchan);
2041 
2042 	/* clear STA registers */
2043 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2044 
2045 	rum_set_macaddr(sc, IF_LLADDR(ifp));
2046 
2047 	/* initialize ASIC */
2048 	rum_write(sc, RT2573_MAC_CSR1, 4);
2049 
2050 	/*
2051 	 * Allocate Tx and Rx xfer queues.
2052 	 */
2053 	rum_setup_tx_list(sc);
2054 
2055 	/* update Rx filter */
2056 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2057 
2058 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2059 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2060 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2061 		       RT2573_DROP_ACKCTS;
2062 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2063 			tmp |= RT2573_DROP_TODS;
2064 		if (!(ifp->if_flags & IFF_PROMISC))
2065 			tmp |= RT2573_DROP_NOT_TO_ME;
2066 	}
2067 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2068 
2069 	ifq_clr_oactive(&ifp->if_snd);
2070 	ifp->if_flags |= IFF_RUNNING;
2071 	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2072 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2073 	return;
2074 
2075 fail:	rum_stop(sc);
2076 #undef N
2077 }
2078 
2079 static void
2080 rum_init(void *priv)
2081 {
2082 	struct rum_softc *sc = priv;
2083 	struct ifnet *ifp = sc->sc_ifp;
2084 	struct ieee80211com *ic = ifp->if_l2com;
2085 
2086 	RUM_LOCK(sc);
2087 	rum_init_locked(sc);
2088 	RUM_UNLOCK(sc);
2089 
2090 	if (ifp->if_flags & IFF_RUNNING)
2091 		ieee80211_start_all(ic);		/* start all vap's */
2092 }
2093 
2094 static void
2095 rum_stop(struct rum_softc *sc)
2096 {
2097 	struct ifnet *ifp = sc->sc_ifp;
2098 	uint32_t tmp;
2099 
2100 	RUM_LOCK_ASSERT(sc, MA_OWNED);
2101 
2102 	ifp->if_flags &= ~IFF_RUNNING;
2103 	ifq_clr_oactive(&ifp->if_snd);
2104 
2105 	RUM_UNLOCK(sc);
2106 
2107 	/*
2108 	 * Drain the USB transfers, if not already drained:
2109 	 */
2110 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2111 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2112 
2113 	RUM_LOCK(sc);
2114 
2115 	rum_unsetup_tx_list(sc);
2116 
2117 	/* disable Rx */
2118 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
2119 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2120 
2121 	/* reset ASIC */
2122 	rum_write(sc, RT2573_MAC_CSR1, 3);
2123 	rum_write(sc, RT2573_MAC_CSR1, 0);
2124 }
2125 
2126 static void
2127 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2128 {
2129 	struct usb_device_request req;
2130 	uint16_t reg = RT2573_MCU_CODE_BASE;
2131 	usb_error_t err;
2132 
2133 	/* copy firmware image into NIC */
2134 	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2135 		err = rum_write(sc, reg, UGETDW(ucode));
2136 		if (err) {
2137 			/* firmware already loaded ? */
2138 			device_printf(sc->sc_dev, "Firmware load "
2139 			    "failure! (ignored)\n");
2140 			break;
2141 		}
2142 	}
2143 
2144 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2145 	req.bRequest = RT2573_MCU_CNTL;
2146 	USETW(req.wValue, RT2573_MCU_RUN);
2147 	USETW(req.wIndex, 0);
2148 	USETW(req.wLength, 0);
2149 
2150 	err = rum_do_request(sc, &req, NULL);
2151 	if (err != 0) {
2152 		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2153 		    usbd_errstr(err));
2154 	}
2155 
2156 	/* give the chip some time to boot */
2157 	rum_pause(sc, hz / 8);
2158 }
2159 
2160 static void
2161 rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2162 {
2163 	struct ieee80211com *ic = vap->iv_ic;
2164 	const struct ieee80211_txparam *tp;
2165 	struct rum_tx_desc desc;
2166 	struct mbuf *m0;
2167 
2168 	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
2169 		return;
2170 	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2171 		return;
2172 
2173 	m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2174 	if (m0 == NULL)
2175 		return;
2176 
2177 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2178 	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2179 	    m0->m_pkthdr.len, tp->mgmtrate);
2180 
2181 	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2182 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2183 
2184 	/* copy beacon header and payload into NIC memory */
2185 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2186 	    m0->m_pkthdr.len);
2187 
2188 	m_freem(m0);
2189 }
2190 
2191 static int
2192 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2193     const struct ieee80211_bpf_params *params)
2194 {
2195 	struct ifnet *ifp = ni->ni_ic->ic_ifp;
2196 	struct rum_softc *sc = ifp->if_softc;
2197 
2198 	RUM_LOCK(sc);
2199 	/* prevent management frames from being sent if we're not ready */
2200 	if (!(ifp->if_flags & IFF_RUNNING)) {
2201 		RUM_UNLOCK(sc);
2202 		m_freem(m);
2203 		ieee80211_free_node(ni);
2204 		return ENETDOWN;
2205 	}
2206 	if (sc->tx_nfree < RUM_TX_MINFREE) {
2207 		ifq_set_oactive(&ifp->if_snd);
2208 		RUM_UNLOCK(sc);
2209 		m_freem(m);
2210 		ieee80211_free_node(ni);
2211 		return EIO;
2212 	}
2213 
2214 	ifp->if_opackets++;
2215 
2216 	if (params == NULL) {
2217 		/*
2218 		 * Legacy path; interpret frame contents to decide
2219 		 * precisely how to send the frame.
2220 		 */
2221 		if (rum_tx_mgt(sc, m, ni) != 0)
2222 			goto bad;
2223 	} else {
2224 		/*
2225 		 * Caller supplied explicit parameters to use in
2226 		 * sending the frame.
2227 		 */
2228 		if (rum_tx_raw(sc, m, ni, params) != 0)
2229 			goto bad;
2230 	}
2231 	RUM_UNLOCK(sc);
2232 
2233 	return 0;
2234 bad:
2235 	ifp->if_oerrors++;
2236 	RUM_UNLOCK(sc);
2237 	ieee80211_free_node(ni);
2238 	return EIO;
2239 }
2240 
2241 static void
2242 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2243 {
2244 	struct ieee80211vap *vap = ni->ni_vap;
2245 	struct rum_vap *rvp = RUM_VAP(vap);
2246 
2247 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2248 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2249 
2250 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2251 }
2252 
2253 static void
2254 rum_ratectl_timeout(void *arg)
2255 {
2256 	struct rum_vap *rvp = arg;
2257 	struct ieee80211vap *vap = &rvp->vap;
2258 	struct ieee80211com *ic = vap->iv_ic;
2259 
2260 	ieee80211_runtask(ic, &rvp->ratectl_task);
2261 }
2262 
2263 static void
2264 rum_ratectl_task(void *arg, int pending)
2265 {
2266 	struct rum_vap *rvp = arg;
2267 	struct ieee80211vap *vap = &rvp->vap;
2268 	struct ieee80211com *ic = vap->iv_ic;
2269 	struct ifnet *ifp = ic->ic_ifp;
2270 	struct rum_softc *sc = ifp->if_softc;
2271 	struct ieee80211_node *ni;
2272 	int ok, fail;
2273 	int sum, retrycnt;
2274 
2275 	RUM_LOCK(sc);
2276 	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2277 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2278 
2279 	ok = (le32toh(sc->sta[4]) & 0xffff) +	/* TX no-retry ok count */
2280 	    (le32toh(sc->sta[4]) >> 16) +	/* TX one-retry ok count */
2281 	    (le32toh(sc->sta[5]) & 0xffff);	/* TX more-retry ok count */
2282 	fail = (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2283 	sum = ok+fail;
2284 	retrycnt = (le32toh(sc->sta[4]) >> 16) +
2285 	    (le32toh(sc->sta[5]) & 0xffff) + fail;
2286 
2287 	ni = ieee80211_ref_node(vap->iv_bss);
2288 	ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2289 	(void) ieee80211_ratectl_rate(ni, NULL, 0);
2290 	ieee80211_free_node(ni);
2291 
2292 	ifp->if_oerrors += fail;	/* count TX retry-fail as Tx errors */
2293 
2294 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2295 	RUM_UNLOCK(sc);
2296 }
2297 
2298 static void
2299 rum_scan_start(struct ieee80211com *ic)
2300 {
2301 	struct ifnet *ifp = ic->ic_ifp;
2302 	struct rum_softc *sc = ifp->if_softc;
2303 	uint32_t tmp;
2304 
2305 	RUM_LOCK(sc);
2306 	/* abort TSF synchronization */
2307 	tmp = rum_read(sc, RT2573_TXRX_CSR9);
2308 	rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2309 	rum_set_bssid(sc, ifp->if_broadcastaddr);
2310 	RUM_UNLOCK(sc);
2311 
2312 }
2313 
2314 static void
2315 rum_scan_end(struct ieee80211com *ic)
2316 {
2317 	struct rum_softc *sc = ic->ic_ifp->if_softc;
2318 
2319 	RUM_LOCK(sc);
2320 	rum_enable_tsf_sync(sc);
2321 	rum_set_bssid(sc, sc->sc_bssid);
2322 	RUM_UNLOCK(sc);
2323 
2324 }
2325 
2326 static void
2327 rum_set_channel(struct ieee80211com *ic)
2328 {
2329 	struct rum_softc *sc = ic->ic_ifp->if_softc;
2330 
2331 	RUM_LOCK(sc);
2332 	rum_set_chan(sc, ic->ic_curchan);
2333 	RUM_UNLOCK(sc);
2334 }
2335 
2336 static int
2337 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2338 {
2339 	struct ifnet *ifp = sc->sc_ifp;
2340 	struct ieee80211com *ic = ifp->if_l2com;
2341 	int lna, agc, rssi;
2342 
2343 	lna = (raw >> 5) & 0x3;
2344 	agc = raw & 0x1f;
2345 
2346 	if (lna == 0) {
2347 		/*
2348 		 * No RSSI mapping
2349 		 *
2350 		 * NB: Since RSSI is relative to noise floor, -1 is
2351 		 *     adequate for caller to know error happened.
2352 		 */
2353 		return -1;
2354 	}
2355 
2356 	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2357 
2358 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2359 		rssi += sc->rssi_2ghz_corr;
2360 
2361 		if (lna == 1)
2362 			rssi -= 64;
2363 		else if (lna == 2)
2364 			rssi -= 74;
2365 		else if (lna == 3)
2366 			rssi -= 90;
2367 	} else {
2368 		rssi += sc->rssi_5ghz_corr;
2369 
2370 		if (!sc->ext_5ghz_lna && lna != 1)
2371 			rssi += 4;
2372 
2373 		if (lna == 1)
2374 			rssi -= 64;
2375 		else if (lna == 2)
2376 			rssi -= 86;
2377 		else if (lna == 3)
2378 			rssi -= 100;
2379 	}
2380 	return rssi;
2381 }
2382 
2383 static int
2384 rum_pause(struct rum_softc *sc, int timeout)
2385 {
2386 	usb_pause_ls(lockowned(&sc->sc_lock) ? &sc->sc_lock : NULL,
2387 	    &wlan_global_serializer, timeout + 1);
2388 	return (0);
2389 }
2390 
2391 static device_method_t rum_methods[] = {
2392 	/* Device interface */
2393 	DEVMETHOD(device_probe,		rum_match),
2394 	DEVMETHOD(device_attach,	rum_attach),
2395 	DEVMETHOD(device_detach,	rum_detach),
2396 	DEVMETHOD_END
2397 };
2398 
2399 static driver_t rum_driver = {
2400 	.name = "rum",
2401 	.methods = rum_methods,
2402 	.size = sizeof(struct rum_softc),
2403 };
2404 
2405 static devclass_t rum_devclass;
2406 
2407 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2408 MODULE_DEPEND(rum, wlan, 1, 1, 1);
2409 MODULE_DEPEND(rum, usb, 1, 1, 1);
2410 MODULE_VERSION(rum, 1);
2411