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