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