xref: /freebsd/sys/dev/usb/wlan/if_rum.c (revision 2284664ef9fcb0baaf59f1ef7df877c0b0f2b187)
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  * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
24 
25 /*-
26  * Ralink Technology RT2501USB/RT2601USB chipset driver
27  * http://www.ralinktech.com.tw/
28  */
29 
30 #include "opt_wlan.h"
31 
32 #include <sys/param.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/mbuf.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/bus.h>
44 #include <sys/endian.h>
45 #include <sys/kdb.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/rman.h>
50 
51 #include <net/bpf.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
55 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59 
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/if_ether.h>
65 #include <netinet/ip.h>
66 #endif
67 
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_regdomain.h>
70 #include <net80211/ieee80211_radiotap.h>
71 #include <net80211/ieee80211_ratectl.h>
72 
73 #include <dev/usb/usb.h>
74 #include <dev/usb/usbdi.h>
75 #include "usbdevs.h"
76 
77 #define	USB_DEBUG_VAR rum_debug
78 #include <dev/usb/usb_debug.h>
79 
80 #include <dev/usb/wlan/if_rumreg.h>
81 #include <dev/usb/wlan/if_rumvar.h>
82 #include <dev/usb/wlan/if_rumfw.h>
83 
84 #ifdef USB_DEBUG
85 static int rum_debug = 0;
86 
87 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
88 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
89     "Debug level");
90 #endif
91 
92 static const STRUCT_USB_HOST_ID rum_devs[] = {
93 #define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
94     RUM_DEV(ABOCOM, HWU54DM),
95     RUM_DEV(ABOCOM, RT2573_2),
96     RUM_DEV(ABOCOM, RT2573_3),
97     RUM_DEV(ABOCOM, RT2573_4),
98     RUM_DEV(ABOCOM, WUG2700),
99     RUM_DEV(AMIT, CGWLUSB2GO),
100     RUM_DEV(ASUS, RT2573_1),
101     RUM_DEV(ASUS, RT2573_2),
102     RUM_DEV(BELKIN, F5D7050A),
103     RUM_DEV(BELKIN, F5D9050V3),
104     RUM_DEV(CISCOLINKSYS, WUSB54GC),
105     RUM_DEV(CISCOLINKSYS, WUSB54GR),
106     RUM_DEV(CONCEPTRONIC2, C54RU2),
107     RUM_DEV(COREGA, CGWLUSB2GL),
108     RUM_DEV(COREGA, CGWLUSB2GPX),
109     RUM_DEV(DICKSMITH, CWD854F),
110     RUM_DEV(DICKSMITH, RT2573),
111     RUM_DEV(EDIMAX, EW7318USG),
112     RUM_DEV(DLINK2, DWLG122C1),
113     RUM_DEV(DLINK2, WUA1340),
114     RUM_DEV(DLINK2, DWA111),
115     RUM_DEV(DLINK2, DWA110),
116     RUM_DEV(GIGABYTE, GNWB01GS),
117     RUM_DEV(GIGABYTE, GNWI05GS),
118     RUM_DEV(GIGASET, RT2573),
119     RUM_DEV(GOODWAY, RT2573),
120     RUM_DEV(GUILLEMOT, HWGUSB254LB),
121     RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
122     RUM_DEV(HUAWEI3COM, WUB320G),
123     RUM_DEV(MELCO, G54HP),
124     RUM_DEV(MELCO, SG54HP),
125     RUM_DEV(MELCO, SG54HG),
126     RUM_DEV(MELCO, WLIUCG),
127     RUM_DEV(MELCO, WLRUCG),
128     RUM_DEV(MELCO, WLRUCGAOSS),
129     RUM_DEV(MSI, RT2573_1),
130     RUM_DEV(MSI, RT2573_2),
131     RUM_DEV(MSI, RT2573_3),
132     RUM_DEV(MSI, RT2573_4),
133     RUM_DEV(NOVATECH, RT2573),
134     RUM_DEV(PLANEX2, GWUS54HP),
135     RUM_DEV(PLANEX2, GWUS54MINI2),
136     RUM_DEV(PLANEX2, GWUSMM),
137     RUM_DEV(QCOM, RT2573),
138     RUM_DEV(QCOM, RT2573_2),
139     RUM_DEV(QCOM, RT2573_3),
140     RUM_DEV(RALINK, RT2573),
141     RUM_DEV(RALINK, RT2573_2),
142     RUM_DEV(RALINK, RT2671),
143     RUM_DEV(SITECOMEU, WL113R2),
144     RUM_DEV(SITECOMEU, WL172),
145     RUM_DEV(SPARKLAN, RT2573),
146     RUM_DEV(SURECOM, RT2573),
147 #undef RUM_DEV
148 };
149 
150 static device_probe_t rum_match;
151 static device_attach_t rum_attach;
152 static device_detach_t rum_detach;
153 
154 static usb_callback_t rum_bulk_read_callback;
155 static usb_callback_t rum_bulk_write_callback;
156 
157 static usb_error_t	rum_do_request(struct rum_softc *sc,
158 			    struct usb_device_request *req, void *data);
159 static usb_error_t	rum_do_mcu_request(struct rum_softc *sc, int);
160 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
161 			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
162 			    int, const uint8_t [IEEE80211_ADDR_LEN],
163 			    const uint8_t [IEEE80211_ADDR_LEN]);
164 static void		rum_vap_delete(struct ieee80211vap *);
165 static void		rum_cmdq_cb(void *, int);
166 static int		rum_cmd_sleepable(struct rum_softc *, const void *,
167 			    size_t, uint8_t, CMD_FUNC_PROTO);
168 static void		rum_tx_free(struct rum_tx_data *, int);
169 static void		rum_setup_tx_list(struct rum_softc *);
170 static void		rum_reset_tx_list(struct rum_softc *,
171 			    struct ieee80211vap *);
172 static void		rum_unsetup_tx_list(struct rum_softc *);
173 static void		rum_beacon_miss(struct ieee80211vap *);
174 static void		rum_sta_recv_mgmt(struct ieee80211_node *,
175 			    struct mbuf *, int,
176 			    const struct ieee80211_rx_stats *, int, int);
177 static int		rum_set_power_state(struct rum_softc *, int);
178 static int		rum_newstate(struct ieee80211vap *,
179 			    enum ieee80211_state, int);
180 static uint8_t		rum_crypto_mode(struct rum_softc *, u_int, int);
181 static void		rum_setup_tx_desc(struct rum_softc *,
182 			    struct rum_tx_desc *, struct ieee80211_key *,
183 			    uint32_t, uint8_t, uint8_t, int, int, int);
184 static uint32_t		rum_tx_crypto_flags(struct rum_softc *,
185 			    struct ieee80211_node *,
186 			    const struct ieee80211_key *);
187 static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
188 			    struct ieee80211_node *);
189 static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
190 			    struct ieee80211_node *,
191 			    const struct ieee80211_bpf_params *);
192 static int		rum_tx_data(struct rum_softc *, struct mbuf *,
193 			    struct ieee80211_node *);
194 static int		rum_transmit(struct ieee80211com *, struct mbuf *);
195 static void		rum_start(struct rum_softc *);
196 static void		rum_parent(struct ieee80211com *);
197 static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
198 			    int);
199 static uint32_t		rum_read(struct rum_softc *, uint16_t);
200 static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
201 			    int);
202 static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
203 static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
204 			    size_t);
205 static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
206 static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
207 static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
208 			    uint32_t);
209 static int		rum_bbp_busy(struct rum_softc *);
210 static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
211 static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
212 static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
213 static void		rum_select_antenna(struct rum_softc *);
214 static void		rum_enable_mrr(struct rum_softc *);
215 static void		rum_set_txpreamble(struct rum_softc *);
216 static void		rum_set_basicrates(struct rum_softc *);
217 static void		rum_select_band(struct rum_softc *,
218 			    struct ieee80211_channel *);
219 static void		rum_set_chan(struct rum_softc *,
220 			    struct ieee80211_channel *);
221 static void		rum_set_maxretry(struct rum_softc *,
222 			    struct ieee80211vap *);
223 static int		rum_enable_tsf_sync(struct rum_softc *);
224 static void		rum_enable_tsf(struct rum_softc *);
225 static void		rum_abort_tsf_sync(struct rum_softc *);
226 static void		rum_get_tsf(struct rum_softc *, uint64_t *);
227 static void		rum_update_slot_cb(struct rum_softc *,
228 			    union sec_param *, uint8_t);
229 static void		rum_update_slot(struct ieee80211com *);
230 static int		rum_wme_update(struct ieee80211com *);
231 static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
232 static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
233 static void		rum_update_mcast(struct ieee80211com *);
234 static void		rum_update_promisc(struct ieee80211com *);
235 static void		rum_setpromisc(struct rum_softc *);
236 static const char	*rum_get_rf(int);
237 static void		rum_read_eeprom(struct rum_softc *);
238 static int		rum_bbp_wakeup(struct rum_softc *);
239 static int		rum_bbp_init(struct rum_softc *);
240 static void		rum_clr_shkey_regs(struct rum_softc *);
241 static int		rum_init(struct rum_softc *);
242 static void		rum_stop(struct rum_softc *);
243 static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
244 			    size_t);
245 static int		rum_set_sleep_time(struct rum_softc *, uint16_t);
246 static int		rum_reset(struct ieee80211vap *, u_long);
247 static int		rum_set_beacon(struct rum_softc *,
248 			    struct ieee80211vap *);
249 static int		rum_alloc_beacon(struct rum_softc *,
250 			    struct ieee80211vap *);
251 static void		rum_update_beacon_cb(struct rum_softc *,
252 			    union sec_param *, uint8_t);
253 static void		rum_update_beacon(struct ieee80211vap *, int);
254 static int		rum_common_key_set(struct rum_softc *,
255 			    struct ieee80211_key *, uint16_t);
256 static void		rum_group_key_set_cb(struct rum_softc *,
257 			    union sec_param *, uint8_t);
258 static void		rum_group_key_del_cb(struct rum_softc *,
259 			    union sec_param *, uint8_t);
260 static void		rum_pair_key_set_cb(struct rum_softc *,
261 			    union sec_param *, uint8_t);
262 static void		rum_pair_key_del_cb(struct rum_softc *,
263 			    union sec_param *, uint8_t);
264 static int		rum_key_alloc(struct ieee80211vap *,
265 			    struct ieee80211_key *, ieee80211_keyix *,
266 			    ieee80211_keyix *);
267 static int		rum_key_set(struct ieee80211vap *,
268 			    const struct ieee80211_key *);
269 static int		rum_key_delete(struct ieee80211vap *,
270 			    const struct ieee80211_key *);
271 static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
272 			    const struct ieee80211_bpf_params *);
273 static void		rum_scan_start(struct ieee80211com *);
274 static void		rum_scan_end(struct ieee80211com *);
275 static void		rum_set_channel(struct ieee80211com *);
276 static void		rum_getradiocaps(struct ieee80211com *, int, int *,
277 			    struct ieee80211_channel[]);
278 static int		rum_get_rssi(struct rum_softc *, uint8_t);
279 static void		rum_ratectl_start(struct rum_softc *,
280 			    struct ieee80211_node *);
281 static void		rum_ratectl_timeout(void *);
282 static void		rum_ratectl_task(void *, int);
283 static int		rum_pause(struct rum_softc *, int);
284 
285 static const struct {
286 	uint32_t	reg;
287 	uint32_t	val;
288 } rum_def_mac[] = {
289 	{ RT2573_TXRX_CSR0,  0x025fb032 },
290 	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
291 	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
292 	{ RT2573_TXRX_CSR3,  0x00858687 },
293 	{ RT2573_TXRX_CSR7,  0x2e31353b },
294 	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
295 	{ RT2573_TXRX_CSR15, 0x0000000f },
296 	{ RT2573_MAC_CSR6,   0x00000fff },
297 	{ RT2573_MAC_CSR8,   0x016c030a },
298 	{ RT2573_MAC_CSR10,  0x00000718 },
299 	{ RT2573_MAC_CSR12,  0x00000004 },
300 	{ RT2573_MAC_CSR13,  0x00007f00 },
301 	{ RT2573_SEC_CSR2,   0x00000000 },
302 	{ RT2573_SEC_CSR3,   0x00000000 },
303 	{ RT2573_SEC_CSR4,   0x00000000 },
304 	{ RT2573_PHY_CSR1,   0x000023b0 },
305 	{ RT2573_PHY_CSR5,   0x00040a06 },
306 	{ RT2573_PHY_CSR6,   0x00080606 },
307 	{ RT2573_PHY_CSR7,   0x00000408 },
308 	{ RT2573_AIFSN_CSR,  0x00002273 },
309 	{ RT2573_CWMIN_CSR,  0x00002344 },
310 	{ RT2573_CWMAX_CSR,  0x000034aa }
311 };
312 
313 static const struct {
314 	uint8_t	reg;
315 	uint8_t	val;
316 } rum_def_bbp[] = {
317 	{   3, 0x80 },
318 	{  15, 0x30 },
319 	{  17, 0x20 },
320 	{  21, 0xc8 },
321 	{  22, 0x38 },
322 	{  23, 0x06 },
323 	{  24, 0xfe },
324 	{  25, 0x0a },
325 	{  26, 0x0d },
326 	{  32, 0x0b },
327 	{  34, 0x12 },
328 	{  37, 0x07 },
329 	{  39, 0xf8 },
330 	{  41, 0x60 },
331 	{  53, 0x10 },
332 	{  54, 0x18 },
333 	{  60, 0x10 },
334 	{  61, 0x04 },
335 	{  62, 0x04 },
336 	{  75, 0xfe },
337 	{  86, 0xfe },
338 	{  88, 0xfe },
339 	{  90, 0x0f },
340 	{  99, 0x00 },
341 	{ 102, 0x16 },
342 	{ 107, 0x04 }
343 };
344 
345 static const uint8_t rum_chan_2ghz[] =
346 	{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
347 
348 static const uint8_t rum_chan_5ghz[] =
349 	{ 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
350 	  100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
351 	  149, 153, 157, 161, 165 };
352 
353 static const struct rfprog {
354 	uint8_t		chan;
355 	uint32_t	r1, r2, r3, r4;
356 }  rum_rf5226[] = {
357 	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
358 	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
359 	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
360 	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
361 	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
362 	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
363 	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
364 	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
365 	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
366 	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
367 	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
368 	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
369 	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
370 	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
371 
372 	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
373 	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
374 	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
375 	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
376 
377 	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
378 	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
379 	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
380 	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
381 	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
382 	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
383 	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
384 	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
385 
386 	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
387 	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
388 	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
389 	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
390 	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
391 	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
392 	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
393 	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
394 	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
395 	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
396 	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
397 
398 	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
399 	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
400 	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
401 	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
402 	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
403 }, rum_rf5225[] = {
404 	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
405 	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
406 	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
407 	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
408 	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
409 	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
410 	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
411 	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
412 	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
413 	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
414 	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
415 	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
416 	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
417 	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
418 
419 	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
420 	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
421 	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
422 	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
423 
424 	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
425 	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
426 	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
427 	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
428 	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
429 	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
430 	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
431 	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
432 
433 	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
434 	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
435 	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
436 	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
437 	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
438 	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
439 	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
440 	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
441 	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
442 	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
443 	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
444 
445 	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
446 	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
447 	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
448 	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
449 	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
450 };
451 
452 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
453 	[RUM_BULK_WR] = {
454 		.type = UE_BULK,
455 		.endpoint = UE_ADDR_ANY,
456 		.direction = UE_DIR_OUT,
457 		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
458 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
459 		.callback = rum_bulk_write_callback,
460 		.timeout = 5000,	/* ms */
461 	},
462 	[RUM_BULK_RD] = {
463 		.type = UE_BULK,
464 		.endpoint = UE_ADDR_ANY,
465 		.direction = UE_DIR_IN,
466 		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
467 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
468 		.callback = rum_bulk_read_callback,
469 	},
470 };
471 
472 static int
473 rum_match(device_t self)
474 {
475 	struct usb_attach_arg *uaa = device_get_ivars(self);
476 
477 	if (uaa->usb_mode != USB_MODE_HOST)
478 		return (ENXIO);
479 	if (uaa->info.bConfigIndex != 0)
480 		return (ENXIO);
481 	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
482 		return (ENXIO);
483 
484 	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
485 }
486 
487 static int
488 rum_attach(device_t self)
489 {
490 	struct usb_attach_arg *uaa = device_get_ivars(self);
491 	struct rum_softc *sc = device_get_softc(self);
492 	struct ieee80211com *ic = &sc->sc_ic;
493 	uint32_t tmp;
494 	uint8_t iface_index;
495 	int error, ntries;
496 
497 	device_set_usb_desc(self);
498 	sc->sc_udev = uaa->device;
499 	sc->sc_dev = self;
500 
501 	RUM_LOCK_INIT(sc);
502 	RUM_CMDQ_LOCK_INIT(sc);
503 	mbufq_init(&sc->sc_snd, ifqmaxlen);
504 
505 	iface_index = RT2573_IFACE_INDEX;
506 	error = usbd_transfer_setup(uaa->device, &iface_index,
507 	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
508 	if (error) {
509 		device_printf(self, "could not allocate USB transfers, "
510 		    "err=%s\n", usbd_errstr(error));
511 		goto detach;
512 	}
513 
514 	RUM_LOCK(sc);
515 	/* retrieve RT2573 rev. no */
516 	for (ntries = 0; ntries < 100; ntries++) {
517 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
518 			break;
519 		if (rum_pause(sc, hz / 100))
520 			break;
521 	}
522 	if (ntries == 100) {
523 		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
524 		RUM_UNLOCK(sc);
525 		goto detach;
526 	}
527 
528 	/* retrieve MAC address and various other things from EEPROM */
529 	rum_read_eeprom(sc);
530 
531 	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
532 	    tmp, rum_get_rf(sc->rf_rev));
533 
534 	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
535 	RUM_UNLOCK(sc);
536 
537 	ic->ic_softc = sc;
538 	ic->ic_name = device_get_nameunit(self);
539 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
540 
541 	/* set device capabilities */
542 	ic->ic_caps =
543 	      IEEE80211_C_STA		/* station mode supported */
544 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
545 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
546 	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
547 	    | IEEE80211_C_AHDEMO	/* adhoc demo mode */
548 	    | IEEE80211_C_TXPMGT	/* tx power management */
549 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
550 	    | IEEE80211_C_SHSLOT	/* short slot time supported */
551 	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
552 	    | IEEE80211_C_WPA		/* 802.11i */
553 	    | IEEE80211_C_WME		/* 802.11e */
554 	    | IEEE80211_C_PMGT		/* Station-side power mgmt */
555 	    | IEEE80211_C_SWSLEEP	/* net80211 managed power mgmt */
556 	    ;
557 
558 	ic->ic_cryptocaps =
559 	    IEEE80211_CRYPTO_WEP |
560 	    IEEE80211_CRYPTO_AES_CCM |
561 	    IEEE80211_CRYPTO_TKIPMIC |
562 	    IEEE80211_CRYPTO_TKIP;
563 
564 	rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
565 	    ic->ic_channels);
566 
567 	ieee80211_ifattach(ic);
568 	ic->ic_update_promisc = rum_update_promisc;
569 	ic->ic_raw_xmit = rum_raw_xmit;
570 	ic->ic_scan_start = rum_scan_start;
571 	ic->ic_scan_end = rum_scan_end;
572 	ic->ic_set_channel = rum_set_channel;
573 	ic->ic_getradiocaps = rum_getradiocaps;
574 	ic->ic_transmit = rum_transmit;
575 	ic->ic_parent = rum_parent;
576 	ic->ic_vap_create = rum_vap_create;
577 	ic->ic_vap_delete = rum_vap_delete;
578 	ic->ic_updateslot = rum_update_slot;
579 	ic->ic_wme.wme_update = rum_wme_update;
580 	ic->ic_update_mcast = rum_update_mcast;
581 
582 	ieee80211_radiotap_attach(ic,
583 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
584 		RT2573_TX_RADIOTAP_PRESENT,
585 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
586 		RT2573_RX_RADIOTAP_PRESENT);
587 
588 	TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
589 
590 	if (bootverbose)
591 		ieee80211_announce(ic);
592 
593 	return (0);
594 
595 detach:
596 	rum_detach(self);
597 	return (ENXIO);			/* failure */
598 }
599 
600 static int
601 rum_detach(device_t self)
602 {
603 	struct rum_softc *sc = device_get_softc(self);
604 	struct ieee80211com *ic = &sc->sc_ic;
605 
606 	/* Prevent further ioctls */
607 	RUM_LOCK(sc);
608 	sc->sc_detached = 1;
609 	RUM_UNLOCK(sc);
610 
611 	/* stop all USB transfers */
612 	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
613 
614 	/* free TX list, if any */
615 	RUM_LOCK(sc);
616 	rum_unsetup_tx_list(sc);
617 	RUM_UNLOCK(sc);
618 
619 	if (ic->ic_softc == sc) {
620 		ieee80211_draintask(ic, &sc->cmdq_task);
621 		ieee80211_ifdetach(ic);
622 	}
623 
624 	mbufq_drain(&sc->sc_snd);
625 	RUM_CMDQ_LOCK_DESTROY(sc);
626 	RUM_LOCK_DESTROY(sc);
627 
628 	return (0);
629 }
630 
631 static usb_error_t
632 rum_do_request(struct rum_softc *sc,
633     struct usb_device_request *req, void *data)
634 {
635 	usb_error_t err;
636 	int ntries = 10;
637 
638 	while (ntries--) {
639 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
640 		    req, data, 0, NULL, 250 /* ms */);
641 		if (err == 0)
642 			break;
643 
644 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
645 		    usbd_errstr(err));
646 		if (rum_pause(sc, hz / 100))
647 			break;
648 	}
649 	return (err);
650 }
651 
652 static usb_error_t
653 rum_do_mcu_request(struct rum_softc *sc, int request)
654 {
655 	struct usb_device_request req;
656 
657 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
658 	req.bRequest = RT2573_MCU_CNTL;
659 	USETW(req.wValue, request);
660 	USETW(req.wIndex, 0);
661 	USETW(req.wLength, 0);
662 
663 	return (rum_do_request(sc, &req, NULL));
664 }
665 
666 static struct ieee80211vap *
667 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
668     enum ieee80211_opmode opmode, int flags,
669     const uint8_t bssid[IEEE80211_ADDR_LEN],
670     const uint8_t mac[IEEE80211_ADDR_LEN])
671 {
672 	struct rum_softc *sc = ic->ic_softc;
673 	struct rum_vap *rvp;
674 	struct ieee80211vap *vap;
675 
676 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
677 		return NULL;
678 	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
679 	vap = &rvp->vap;
680 	/* enable s/w bmiss handling for sta mode */
681 
682 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
683 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
684 		/* out of memory */
685 		free(rvp, M_80211_VAP);
686 		return (NULL);
687 	}
688 
689 	/* override state transition machine */
690 	rvp->newstate = vap->iv_newstate;
691 	vap->iv_newstate = rum_newstate;
692 	vap->iv_key_alloc = rum_key_alloc;
693 	vap->iv_key_set = rum_key_set;
694 	vap->iv_key_delete = rum_key_delete;
695 	vap->iv_update_beacon = rum_update_beacon;
696 	vap->iv_reset = rum_reset;
697 	vap->iv_max_aid = RT2573_ADDR_MAX;
698 
699 	if (opmode == IEEE80211_M_STA) {
700 		/*
701 		 * Move device to the sleep state when
702 		 * beacon is received and there is no data for us.
703 		 *
704 		 * Used only for IEEE80211_S_SLEEP state.
705 		 */
706 		rvp->recv_mgmt = vap->iv_recv_mgmt;
707 		vap->iv_recv_mgmt = rum_sta_recv_mgmt;
708 
709 		/* Ignored while sleeping. */
710 		rvp->bmiss = vap->iv_bmiss;
711 		vap->iv_bmiss = rum_beacon_miss;
712 	}
713 
714 	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
715 	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
716 	ieee80211_ratectl_init(vap);
717 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
718 	/* complete setup */
719 	ieee80211_vap_attach(vap, ieee80211_media_change,
720 	    ieee80211_media_status, mac);
721 	ic->ic_opmode = opmode;
722 	return vap;
723 }
724 
725 static void
726 rum_vap_delete(struct ieee80211vap *vap)
727 {
728 	struct rum_vap *rvp = RUM_VAP(vap);
729 	struct ieee80211com *ic = vap->iv_ic;
730 	struct rum_softc *sc = ic->ic_softc;
731 
732 	/* Put vap into INIT state. */
733 	ieee80211_new_state(vap, IEEE80211_S_INIT, -1);
734 	ieee80211_draintask(ic, &vap->iv_nstate_task);
735 
736 	RUM_LOCK(sc);
737 	/* Cancel any unfinished Tx. */
738 	rum_reset_tx_list(sc, vap);
739 	RUM_UNLOCK(sc);
740 
741 	usb_callout_drain(&rvp->ratectl_ch);
742 	ieee80211_draintask(ic, &rvp->ratectl_task);
743 	ieee80211_ratectl_deinit(vap);
744 	ieee80211_vap_detach(vap);
745 	m_freem(rvp->bcn_mbuf);
746 	free(rvp, M_80211_VAP);
747 }
748 
749 static void
750 rum_cmdq_cb(void *arg, int pending)
751 {
752 	struct rum_softc *sc = arg;
753 	struct rum_cmdq *rc;
754 
755 	RUM_CMDQ_LOCK(sc);
756 	while (sc->cmdq[sc->cmdq_first].func != NULL) {
757 		rc = &sc->cmdq[sc->cmdq_first];
758 		RUM_CMDQ_UNLOCK(sc);
759 
760 		RUM_LOCK(sc);
761 		rc->func(sc, &rc->data, rc->rvp_id);
762 		RUM_UNLOCK(sc);
763 
764 		RUM_CMDQ_LOCK(sc);
765 		memset(rc, 0, sizeof (*rc));
766 		sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
767 	}
768 	RUM_CMDQ_UNLOCK(sc);
769 }
770 
771 static int
772 rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
773     uint8_t rvp_id, CMD_FUNC_PROTO)
774 {
775 	struct ieee80211com *ic = &sc->sc_ic;
776 
777 	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
778 
779 	RUM_CMDQ_LOCK(sc);
780 	if (sc->cmdq[sc->cmdq_last].func != NULL) {
781 		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
782 		RUM_CMDQ_UNLOCK(sc);
783 
784 		return EAGAIN;
785 	}
786 
787 	if (ptr != NULL)
788 		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
789 	sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
790 	sc->cmdq[sc->cmdq_last].func = func;
791 	sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
792 	RUM_CMDQ_UNLOCK(sc);
793 
794 	ieee80211_runtask(ic, &sc->cmdq_task);
795 
796 	return 0;
797 }
798 
799 static void
800 rum_tx_free(struct rum_tx_data *data, int txerr)
801 {
802 	struct rum_softc *sc = data->sc;
803 
804 	if (data->m != NULL) {
805 		ieee80211_tx_complete(data->ni, data->m, txerr);
806 		data->m = NULL;
807 		data->ni = NULL;
808 	}
809 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
810 	sc->tx_nfree++;
811 }
812 
813 static void
814 rum_setup_tx_list(struct rum_softc *sc)
815 {
816 	struct rum_tx_data *data;
817 	int i;
818 
819 	sc->tx_nfree = 0;
820 	STAILQ_INIT(&sc->tx_q);
821 	STAILQ_INIT(&sc->tx_free);
822 
823 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
824 		data = &sc->tx_data[i];
825 
826 		data->sc = sc;
827 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
828 		sc->tx_nfree++;
829 	}
830 }
831 
832 static void
833 rum_reset_tx_list(struct rum_softc *sc, struct ieee80211vap *vap)
834 {
835 	struct rum_tx_data *data, *tmp;
836 
837 	KASSERT(vap != NULL, ("%s: vap is NULL\n", __func__));
838 
839 	STAILQ_FOREACH_SAFE(data, &sc->tx_q, next, tmp) {
840 		if (data->ni != NULL && data->ni->ni_vap == vap) {
841 			ieee80211_free_node(data->ni);
842 			data->ni = NULL;
843 
844 			KASSERT(data->m != NULL, ("%s: m is NULL\n",
845 			    __func__));
846 			m_freem(data->m);
847 			data->m = NULL;
848 
849 			STAILQ_REMOVE(&sc->tx_q, data, rum_tx_data, next);
850 			STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
851 			sc->tx_nfree++;
852 		}
853 	}
854 }
855 
856 static void
857 rum_unsetup_tx_list(struct rum_softc *sc)
858 {
859 	struct rum_tx_data *data;
860 	int i;
861 
862 	/* make sure any subsequent use of the queues will fail */
863 	sc->tx_nfree = 0;
864 	STAILQ_INIT(&sc->tx_q);
865 	STAILQ_INIT(&sc->tx_free);
866 
867 	/* free up all node references and mbufs */
868 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
869 		data = &sc->tx_data[i];
870 
871 		if (data->m != NULL) {
872 			m_freem(data->m);
873 			data->m = NULL;
874 		}
875 		if (data->ni != NULL) {
876 			ieee80211_free_node(data->ni);
877 			data->ni = NULL;
878 		}
879 	}
880 }
881 
882 static void
883 rum_beacon_miss(struct ieee80211vap *vap)
884 {
885 	struct ieee80211com *ic = vap->iv_ic;
886 	struct rum_softc *sc = ic->ic_softc;
887 	struct rum_vap *rvp = RUM_VAP(vap);
888 	int sleep;
889 
890 	RUM_LOCK(sc);
891 	if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
892 		DPRINTFN(12, "dropping 'sleeping' bit, "
893 		    "device must be awake now\n");
894 
895 		sc->sc_sleeping = 0;
896 	}
897 
898 	sleep = sc->sc_sleeping;
899 	RUM_UNLOCK(sc);
900 
901 	if (!sleep)
902 		rvp->bmiss(vap);
903 #ifdef USB_DEBUG
904 	else
905 		DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
906 #endif
907 }
908 
909 static void
910 rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
911     const struct ieee80211_rx_stats *rxs,
912     int rssi, int nf)
913 {
914 	struct ieee80211vap *vap = ni->ni_vap;
915 	struct rum_softc *sc = vap->iv_ic->ic_softc;
916 	struct rum_vap *rvp = RUM_VAP(vap);
917 
918 	if (vap->iv_state == IEEE80211_S_SLEEP &&
919 	    subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
920 		RUM_LOCK(sc);
921 		DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
922 		    !!(sc->last_rx_flags & RT2573_RX_MYBSS),
923 		    sc->last_rx_flags);
924 
925 		if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
926 		    (RT2573_RX_MYBSS | RT2573_RX_BC)) {
927 			/*
928 			 * Put it to sleep here; in case if there is a data
929 			 * for us, iv_recv_mgmt() will wakeup the device via
930 			 * SLEEP -> RUN state transition.
931 			 */
932 			rum_set_power_state(sc, 1);
933 		}
934 		RUM_UNLOCK(sc);
935 	}
936 
937 	rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
938 }
939 
940 static int
941 rum_set_power_state(struct rum_softc *sc, int sleep)
942 {
943 	usb_error_t uerror;
944 
945 	RUM_LOCK_ASSERT(sc);
946 
947 	DPRINTFN(12, "moving to %s state (sleep time %u)\n",
948 	    sleep ? "sleep" : "awake", sc->sc_sleep_time);
949 
950 	uerror = rum_do_mcu_request(sc,
951 	    sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
952 	if (uerror != USB_ERR_NORMAL_COMPLETION) {
953 		device_printf(sc->sc_dev,
954 		    "%s: could not change power state: %s\n",
955 		    __func__, usbd_errstr(uerror));
956 		return (EIO);
957 	}
958 
959 	sc->sc_sleeping = !!sleep;
960 	sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
961 
962 	return (0);
963 }
964 
965 static int
966 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
967 {
968 	struct rum_vap *rvp = RUM_VAP(vap);
969 	struct ieee80211com *ic = vap->iv_ic;
970 	struct rum_softc *sc = ic->ic_softc;
971 	const struct ieee80211_txparam *tp;
972 	enum ieee80211_state ostate;
973 	struct ieee80211_node *ni;
974 	usb_error_t uerror;
975 	int ret = 0;
976 
977 	ostate = vap->iv_state;
978 	DPRINTF("%s -> %s\n",
979 		ieee80211_state_name[ostate],
980 		ieee80211_state_name[nstate]);
981 
982 	IEEE80211_UNLOCK(ic);
983 	RUM_LOCK(sc);
984 	usb_callout_stop(&rvp->ratectl_ch);
985 
986 	if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
987 		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
988 		rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
989 
990 		/*
991 		 * Ignore any errors;
992 		 * any subsequent TX will wakeup it anyway
993 		 */
994 		(void) rum_set_power_state(sc, 0);
995 	}
996 
997 	switch (nstate) {
998 	case IEEE80211_S_INIT:
999 		if (ostate == IEEE80211_S_RUN)
1000 			rum_abort_tsf_sync(sc);
1001 
1002 		break;
1003 
1004 	case IEEE80211_S_RUN:
1005 		if (ostate == IEEE80211_S_SLEEP)
1006 			break;		/* already handled */
1007 
1008 		ni = ieee80211_ref_node(vap->iv_bss);
1009 
1010 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1011 			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1012 			    ni->ni_chan == IEEE80211_CHAN_ANYC) {
1013 				ret = EINVAL;
1014 				goto run_fail;
1015 			}
1016 			rum_update_slot_cb(sc, NULL, 0);
1017 			rum_enable_mrr(sc);
1018 			rum_set_txpreamble(sc);
1019 			rum_set_basicrates(sc);
1020 			rum_set_maxretry(sc, vap);
1021 			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1022 			rum_set_bssid(sc, sc->sc_bssid);
1023 		}
1024 
1025 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1026 		    vap->iv_opmode == IEEE80211_M_IBSS) {
1027 			if ((ret = rum_alloc_beacon(sc, vap)) != 0)
1028 				goto run_fail;
1029 		}
1030 
1031 		if (vap->iv_opmode != IEEE80211_M_MONITOR &&
1032 		    vap->iv_opmode != IEEE80211_M_AHDEMO) {
1033 			if ((ret = rum_enable_tsf_sync(sc)) != 0)
1034 				goto run_fail;
1035 		} else
1036 			rum_enable_tsf(sc);
1037 
1038 		/* enable automatic rate adaptation */
1039 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1040 		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1041 			rum_ratectl_start(sc, ni);
1042 run_fail:
1043 		ieee80211_free_node(ni);
1044 		break;
1045 	case IEEE80211_S_SLEEP:
1046 		/* Implemented for STA mode only. */
1047 		if (vap->iv_opmode != IEEE80211_M_STA)
1048 			break;
1049 
1050 		uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1051 		if (uerror != USB_ERR_NORMAL_COMPLETION) {
1052 			ret = EIO;
1053 			break;
1054 		}
1055 
1056 		uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1057 		if (uerror != USB_ERR_NORMAL_COMPLETION) {
1058 			ret = EIO;
1059 			break;
1060 		}
1061 
1062 		ret = rum_set_power_state(sc, 1);
1063 		if (ret != 0) {
1064 			device_printf(sc->sc_dev,
1065 			    "%s: could not move to the SLEEP state: %s\n",
1066 			    __func__, usbd_errstr(uerror));
1067 		}
1068 		break;
1069 	default:
1070 		break;
1071 	}
1072 	RUM_UNLOCK(sc);
1073 	IEEE80211_LOCK(ic);
1074 	return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
1075 }
1076 
1077 static void
1078 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1079 {
1080 	struct rum_softc *sc = usbd_xfer_softc(xfer);
1081 	struct ieee80211vap *vap;
1082 	struct rum_tx_data *data;
1083 	struct mbuf *m;
1084 	struct usb_page_cache *pc;
1085 	unsigned int len;
1086 	int actlen, sumlen;
1087 
1088 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1089 
1090 	switch (USB_GET_STATE(xfer)) {
1091 	case USB_ST_TRANSFERRED:
1092 		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
1093 
1094 		/* free resources */
1095 		data = usbd_xfer_get_priv(xfer);
1096 		rum_tx_free(data, 0);
1097 		usbd_xfer_set_priv(xfer, NULL);
1098 
1099 		/* FALLTHROUGH */
1100 	case USB_ST_SETUP:
1101 tr_setup:
1102 		data = STAILQ_FIRST(&sc->tx_q);
1103 		if (data) {
1104 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
1105 			m = data->m;
1106 
1107 			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
1108 				DPRINTFN(0, "data overflow, %u bytes\n",
1109 				    m->m_pkthdr.len);
1110 				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
1111 			}
1112 			pc = usbd_xfer_get_frame(xfer, 0);
1113 			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1114 			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1115 			    m->m_pkthdr.len);
1116 
1117 			vap = data->ni->ni_vap;
1118 			if (ieee80211_radiotap_active_vap(vap)) {
1119 				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1120 
1121 				tap->wt_flags = 0;
1122 				tap->wt_rate = data->rate;
1123 				tap->wt_antenna = sc->tx_ant;
1124 
1125 				ieee80211_radiotap_tx(vap, m);
1126 			}
1127 
1128 			/* align end on a 4-bytes boundary */
1129 			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
1130 			if ((len % 64) == 0)
1131 				len += 4;
1132 
1133 			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
1134 			    m->m_pkthdr.len, len);
1135 
1136 			usbd_xfer_set_frame_len(xfer, 0, len);
1137 			usbd_xfer_set_priv(xfer, data);
1138 
1139 			usbd_transfer_submit(xfer);
1140 		}
1141 		rum_start(sc);
1142 		break;
1143 
1144 	default:			/* Error */
1145 		DPRINTFN(11, "transfer error, %s\n",
1146 		    usbd_errstr(error));
1147 
1148 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1149 		data = usbd_xfer_get_priv(xfer);
1150 		if (data != NULL) {
1151 			rum_tx_free(data, error);
1152 			usbd_xfer_set_priv(xfer, NULL);
1153 		}
1154 
1155 		if (error != USB_ERR_CANCELLED) {
1156 			if (error == USB_ERR_TIMEOUT)
1157 				device_printf(sc->sc_dev, "device timeout\n");
1158 
1159 			/*
1160 			 * Try to clear stall first, also if other
1161 			 * errors occur, hence clearing stall
1162 			 * introduces a 50 ms delay:
1163 			 */
1164 			usbd_xfer_set_stall(xfer);
1165 			goto tr_setup;
1166 		}
1167 		break;
1168 	}
1169 }
1170 
1171 static void
1172 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1173 {
1174 	struct rum_softc *sc = usbd_xfer_softc(xfer);
1175 	struct ieee80211com *ic = &sc->sc_ic;
1176 	struct ieee80211_frame_min *wh;
1177 	struct ieee80211_node *ni;
1178 	struct mbuf *m = NULL;
1179 	struct usb_page_cache *pc;
1180 	uint32_t flags;
1181 	uint8_t rssi = 0;
1182 	int len;
1183 
1184 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1185 
1186 	switch (USB_GET_STATE(xfer)) {
1187 	case USB_ST_TRANSFERRED:
1188 
1189 		DPRINTFN(15, "rx done, actlen=%d\n", len);
1190 
1191 		if (len < RT2573_RX_DESC_SIZE) {
1192 			DPRINTF("%s: xfer too short %d\n",
1193 			    device_get_nameunit(sc->sc_dev), len);
1194 			counter_u64_add(ic->ic_ierrors, 1);
1195 			goto tr_setup;
1196 		}
1197 
1198 		len -= RT2573_RX_DESC_SIZE;
1199 		pc = usbd_xfer_get_frame(xfer, 0);
1200 		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1201 
1202 		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1203 		flags = le32toh(sc->sc_rx_desc.flags);
1204 		sc->last_rx_flags = flags;
1205 		if (len < ((flags >> 16) & 0xfff)) {
1206 			DPRINTFN(5, "%s: frame is truncated from %d to %d "
1207 			    "bytes\n", device_get_nameunit(sc->sc_dev),
1208 			    (flags >> 16) & 0xfff, len);
1209 			counter_u64_add(ic->ic_ierrors, 1);
1210 			goto tr_setup;
1211 		}
1212 		len = (flags >> 16) & 0xfff;
1213 		if (len < sizeof(struct ieee80211_frame_ack)) {
1214 			DPRINTFN(5, "%s: frame too short %d\n",
1215 			    device_get_nameunit(sc->sc_dev), len);
1216 			counter_u64_add(ic->ic_ierrors, 1);
1217 			goto tr_setup;
1218 		}
1219 		if (flags & RT2573_RX_CRC_ERROR) {
1220 			/*
1221 		         * This should not happen since we did not
1222 		         * request to receive those frames when we
1223 		         * filled RUM_TXRX_CSR2:
1224 		         */
1225 			DPRINTFN(5, "PHY or CRC error\n");
1226 			counter_u64_add(ic->ic_ierrors, 1);
1227 			goto tr_setup;
1228 		}
1229 		if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1230 			switch (flags & RT2573_RX_DEC_MASK) {
1231 			case RT2573_RX_IV_ERROR:
1232 				DPRINTFN(5, "IV/EIV error\n");
1233 				break;
1234 			case RT2573_RX_MIC_ERROR:
1235 				DPRINTFN(5, "MIC error\n");
1236 				break;
1237 			case RT2573_RX_KEY_ERROR:
1238 				DPRINTFN(5, "Key error\n");
1239 				break;
1240 			}
1241 			counter_u64_add(ic->ic_ierrors, 1);
1242 			goto tr_setup;
1243 		}
1244 
1245 		m = m_get2(len, M_NOWAIT, MT_DATA, M_PKTHDR);
1246 		if (m == NULL) {
1247 			DPRINTF("could not allocate mbuf\n");
1248 			counter_u64_add(ic->ic_ierrors, 1);
1249 			goto tr_setup;
1250 		}
1251 		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1252 		    mtod(m, uint8_t *), len);
1253 
1254 		wh = mtod(m, struct ieee80211_frame_min *);
1255 
1256 		if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1257 		    (flags & RT2573_RX_CIP_MASK) !=
1258 		     RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1259 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1260 			m->m_flags |= M_WEP;
1261 		}
1262 
1263 		/* finalize mbuf */
1264 		m->m_pkthdr.len = m->m_len = len;
1265 
1266 		if (ieee80211_radiotap_active(ic)) {
1267 			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1268 
1269 			tap->wr_flags = 0;
1270 			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1271 			    (flags & RT2573_RX_OFDM) ?
1272 			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
1273 			rum_get_tsf(sc, &tap->wr_tsf);
1274 			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1275 			tap->wr_antnoise = RT2573_NOISE_FLOOR;
1276 			tap->wr_antenna = sc->rx_ant;
1277 		}
1278 		/* FALLTHROUGH */
1279 	case USB_ST_SETUP:
1280 tr_setup:
1281 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1282 		usbd_transfer_submit(xfer);
1283 
1284 		/*
1285 		 * At the end of a USB callback it is always safe to unlock
1286 		 * the private mutex of a device! That is why we do the
1287 		 * "ieee80211_input" here, and not some lines up!
1288 		 */
1289 		RUM_UNLOCK(sc);
1290 		if (m) {
1291 			if (m->m_len >= sizeof(struct ieee80211_frame_min))
1292 				ni = ieee80211_find_rxnode(ic, wh);
1293 			else
1294 				ni = NULL;
1295 
1296 			if (ni != NULL) {
1297 				(void) ieee80211_input(ni, m, rssi,
1298 				    RT2573_NOISE_FLOOR);
1299 				ieee80211_free_node(ni);
1300 			} else
1301 				(void) ieee80211_input_all(ic, m, rssi,
1302 				    RT2573_NOISE_FLOOR);
1303 		}
1304 		RUM_LOCK(sc);
1305 		rum_start(sc);
1306 		return;
1307 
1308 	default:			/* Error */
1309 		if (error != USB_ERR_CANCELLED) {
1310 			/* try to clear stall first */
1311 			usbd_xfer_set_stall(xfer);
1312 			goto tr_setup;
1313 		}
1314 		return;
1315 	}
1316 }
1317 
1318 static uint8_t
1319 rum_plcp_signal(int rate)
1320 {
1321 	switch (rate) {
1322 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1323 	case 12:	return 0xb;
1324 	case 18:	return 0xf;
1325 	case 24:	return 0xa;
1326 	case 36:	return 0xe;
1327 	case 48:	return 0x9;
1328 	case 72:	return 0xd;
1329 	case 96:	return 0x8;
1330 	case 108:	return 0xc;
1331 
1332 	/* CCK rates (NB: not IEEE std, device-specific) */
1333 	case 2:		return 0x0;
1334 	case 4:		return 0x1;
1335 	case 11:	return 0x2;
1336 	case 22:	return 0x3;
1337 	}
1338 	return 0xff;		/* XXX unsupported/unknown rate */
1339 }
1340 
1341 /*
1342  * Map net80211 cipher to RT2573 security mode.
1343  */
1344 static uint8_t
1345 rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1346 {
1347 	switch (cipher) {
1348 	case IEEE80211_CIPHER_WEP:
1349 		return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1350 	case IEEE80211_CIPHER_TKIP:
1351 		return RT2573_MODE_TKIP;
1352 	case IEEE80211_CIPHER_AES_CCM:
1353 		return RT2573_MODE_AES_CCMP;
1354 	default:
1355 		device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1356 		return 0;
1357 	}
1358 }
1359 
1360 static void
1361 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1362     struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1363     int hdrlen, int len, int rate)
1364 {
1365 	struct ieee80211com *ic = &sc->sc_ic;
1366 	struct wmeParams *wmep = &sc->wme_params[qid];
1367 	uint16_t plcp_length;
1368 	int remainder;
1369 
1370 	flags |= RT2573_TX_VALID;
1371 	flags |= len << 16;
1372 
1373 	if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1374 		const struct ieee80211_cipher *cip = k->wk_cipher;
1375 
1376 		len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1377 
1378 		desc->eiv = 0;		/* for WEP */
1379 		cip->ic_setiv(k, (uint8_t *)&desc->iv);
1380 	}
1381 
1382 	/* setup PLCP fields */
1383 	desc->plcp_signal  = rum_plcp_signal(rate);
1384 	desc->plcp_service = 4;
1385 
1386 	len += IEEE80211_CRC_LEN;
1387 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1388 		flags |= RT2573_TX_OFDM;
1389 
1390 		plcp_length = len & 0xfff;
1391 		desc->plcp_length_hi = plcp_length >> 6;
1392 		desc->plcp_length_lo = plcp_length & 0x3f;
1393 	} else {
1394 		if (rate == 0)
1395 			rate = 2;	/* avoid division by zero */
1396 		plcp_length = howmany(16 * len, rate);
1397 		if (rate == 22) {
1398 			remainder = (16 * len) % 22;
1399 			if (remainder != 0 && remainder < 7)
1400 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1401 		}
1402 		desc->plcp_length_hi = plcp_length >> 8;
1403 		desc->plcp_length_lo = plcp_length & 0xff;
1404 
1405 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1406 			desc->plcp_signal |= 0x08;
1407 	}
1408 
1409 	desc->flags = htole32(flags);
1410 	desc->hdrlen = hdrlen;
1411 	desc->xflags = xflags;
1412 
1413 	desc->wme = htole16(RT2573_QID(qid) |
1414 	    RT2573_AIFSN(wmep->wmep_aifsn) |
1415 	    RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1416 	    RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1417 }
1418 
1419 static int
1420 rum_sendprot(struct rum_softc *sc,
1421     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1422 {
1423 	struct ieee80211com *ic = ni->ni_ic;
1424 	struct rum_tx_data *data;
1425 	struct mbuf *mprot;
1426 	int protrate, flags;
1427 
1428 	RUM_LOCK_ASSERT(sc);
1429 
1430 	mprot = ieee80211_alloc_prot(ni, m, rate, prot);
1431 	if (mprot == NULL) {
1432 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
1433 		device_printf(sc->sc_dev,
1434 		    "could not allocate mbuf for protection mode %d\n", prot);
1435 		return (ENOBUFS);
1436 	}
1437 
1438 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1439 	flags = 0;
1440 	if (prot == IEEE80211_PROT_RTSCTS)
1441 		flags |= RT2573_TX_NEED_ACK;
1442 
1443 	data = STAILQ_FIRST(&sc->tx_free);
1444 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1445 	sc->tx_nfree--;
1446 
1447 	data->m = mprot;
1448 	data->ni = ieee80211_ref_node(ni);
1449 	data->rate = protrate;
1450 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1451 	    mprot->m_pkthdr.len, protrate);
1452 
1453 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1454 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1455 
1456 	return 0;
1457 }
1458 
1459 static uint32_t
1460 rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1461     const struct ieee80211_key *k)
1462 {
1463 	struct ieee80211vap *vap = ni->ni_vap;
1464 	u_int cipher;
1465 	uint32_t flags = 0;
1466 	uint8_t mode, pos;
1467 
1468 	if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1469 		cipher = k->wk_cipher->ic_cipher;
1470 		pos = k->wk_keyix;
1471 		mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1472 		if (mode == 0)
1473 			return 0;
1474 
1475 		flags |= RT2573_TX_CIP_MODE(mode);
1476 
1477 		/* Do not trust GROUP flag */
1478 		if (!(k >= &vap->iv_nw_keys[0] &&
1479 		      k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1480 			flags |= RT2573_TX_KEY_PAIR;
1481 		else
1482 			pos += 0 * RT2573_SKEY_MAX;	/* vap id */
1483 
1484 		flags |= RT2573_TX_KEY_ID(pos);
1485 
1486 		if (cipher == IEEE80211_CIPHER_TKIP)
1487 			flags |= RT2573_TX_TKIPMIC;
1488 	}
1489 
1490 	return flags;
1491 }
1492 
1493 static int
1494 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1495 {
1496 	const struct ieee80211_txparam *tp = ni->ni_txparms;
1497 	struct ieee80211com *ic = &sc->sc_ic;
1498 	struct rum_tx_data *data;
1499 	struct ieee80211_frame *wh;
1500 	struct ieee80211_key *k = NULL;
1501 	uint32_t flags = 0;
1502 	uint16_t dur;
1503 	uint8_t ac, type, xflags = 0;
1504 	int hdrlen;
1505 
1506 	RUM_LOCK_ASSERT(sc);
1507 
1508 	data = STAILQ_FIRST(&sc->tx_free);
1509 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1510 	sc->tx_nfree--;
1511 
1512 	wh = mtod(m0, struct ieee80211_frame *);
1513 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1514 	hdrlen = ieee80211_anyhdrsize(wh);
1515 	ac = M_WME_GETAC(m0);
1516 
1517 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1518 		k = ieee80211_crypto_get_txkey(ni, m0);
1519 		if (k == NULL)
1520 			return (ENOENT);
1521 
1522 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1523 		    !k->wk_cipher->ic_encap(k, m0))
1524 			return (ENOBUFS);
1525 
1526 		wh = mtod(m0, struct ieee80211_frame *);
1527 	}
1528 
1529 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1530 		flags |= RT2573_TX_NEED_ACK;
1531 
1532 		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1533 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1534 		USETW(wh->i_dur, dur);
1535 
1536 		/* tell hardware to add timestamp for probe responses */
1537 		if (type == IEEE80211_FC0_TYPE_MGT &&
1538 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1539 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1540 			flags |= RT2573_TX_TIMESTAMP;
1541 	}
1542 
1543 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1544 		xflags |= RT2573_TX_HWSEQ;
1545 
1546 	if (k != NULL)
1547 		flags |= rum_tx_crypto_flags(sc, ni, k);
1548 
1549 	data->m = m0;
1550 	data->ni = ni;
1551 	data->rate = tp->mgmtrate;
1552 
1553 	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1554 	    m0->m_pkthdr.len, tp->mgmtrate);
1555 
1556 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1557 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1558 
1559 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1560 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1561 
1562 	return (0);
1563 }
1564 
1565 static int
1566 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1567     const struct ieee80211_bpf_params *params)
1568 {
1569 	struct ieee80211com *ic = ni->ni_ic;
1570 	struct ieee80211_frame *wh;
1571 	struct rum_tx_data *data;
1572 	uint32_t flags;
1573 	uint8_t ac, type, xflags = 0;
1574 	int rate, error;
1575 
1576 	RUM_LOCK_ASSERT(sc);
1577 
1578 	wh = mtod(m0, struct ieee80211_frame *);
1579 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1580 
1581 	ac = params->ibp_pri & 3;
1582 
1583 	rate = params->ibp_rate0;
1584 	if (!ieee80211_isratevalid(ic->ic_rt, rate))
1585 		return (EINVAL);
1586 
1587 	flags = 0;
1588 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1589 		flags |= RT2573_TX_NEED_ACK;
1590 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1591 		error = rum_sendprot(sc, m0, ni,
1592 		    params->ibp_flags & IEEE80211_BPF_RTS ?
1593 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1594 		    rate);
1595 		if (error || sc->tx_nfree == 0)
1596 			return (ENOBUFS);
1597 
1598 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1599 	}
1600 
1601 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1602 		xflags |= RT2573_TX_HWSEQ;
1603 
1604 	data = STAILQ_FIRST(&sc->tx_free);
1605 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1606 	sc->tx_nfree--;
1607 
1608 	data->m = m0;
1609 	data->ni = ni;
1610 	data->rate = rate;
1611 
1612 	/* XXX need to setup descriptor ourself */
1613 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1614 	    m0->m_pkthdr.len, rate);
1615 
1616 	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1617 	    m0->m_pkthdr.len, rate);
1618 
1619 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1620 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1621 
1622 	return 0;
1623 }
1624 
1625 static int
1626 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1627 {
1628 	struct ieee80211vap *vap = ni->ni_vap;
1629 	struct ieee80211com *ic = &sc->sc_ic;
1630 	struct rum_tx_data *data;
1631 	struct ieee80211_frame *wh;
1632 	const struct ieee80211_txparam *tp = ni->ni_txparms;
1633 	struct ieee80211_key *k = NULL;
1634 	uint32_t flags = 0;
1635 	uint16_t dur;
1636 	uint8_t ac, type, qos, xflags = 0;
1637 	int error, hdrlen, rate;
1638 
1639 	RUM_LOCK_ASSERT(sc);
1640 
1641 	wh = mtod(m0, struct ieee80211_frame *);
1642 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1643 	hdrlen = ieee80211_anyhdrsize(wh);
1644 
1645 	if (IEEE80211_QOS_HAS_SEQ(wh))
1646 		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1647 	else
1648 		qos = 0;
1649 	ac = M_WME_GETAC(m0);
1650 
1651 	if (m0->m_flags & M_EAPOL)
1652 		rate = tp->mgmtrate;
1653 	else if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1654 		rate = tp->mcastrate;
1655 	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1656 		rate = tp->ucastrate;
1657 	else {
1658 		(void) ieee80211_ratectl_rate(ni, NULL, 0);
1659 		rate = ni->ni_txrate;
1660 	}
1661 
1662 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1663 		k = ieee80211_crypto_get_txkey(ni, m0);
1664 		if (k == NULL) {
1665 			m_freem(m0);
1666 			return (ENOENT);
1667 		}
1668 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1669 		    !k->wk_cipher->ic_encap(k, m0)) {
1670 			m_freem(m0);
1671 			return (ENOBUFS);
1672 		}
1673 
1674 		/* packet header may have moved, reset our local pointer */
1675 		wh = mtod(m0, struct ieee80211_frame *);
1676 	}
1677 
1678 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1679 		xflags |= RT2573_TX_HWSEQ;
1680 
1681 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1682 		int prot = IEEE80211_PROT_NONE;
1683 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1684 			prot = IEEE80211_PROT_RTSCTS;
1685 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1686 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1687 			prot = ic->ic_protmode;
1688 		if (prot != IEEE80211_PROT_NONE) {
1689 			error = rum_sendprot(sc, m0, ni, prot, rate);
1690 			if (error || sc->tx_nfree == 0) {
1691 				m_freem(m0);
1692 				return ENOBUFS;
1693 			}
1694 			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1695 		}
1696 	}
1697 
1698 	if (k != NULL)
1699 		flags |= rum_tx_crypto_flags(sc, ni, k);
1700 
1701 	data = STAILQ_FIRST(&sc->tx_free);
1702 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1703 	sc->tx_nfree--;
1704 
1705 	data->m = m0;
1706 	data->ni = ni;
1707 	data->rate = rate;
1708 
1709 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1710 		/* Unicast frame, check if an ACK is expected. */
1711 		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1712 		    IEEE80211_QOS_ACKPOLICY_NOACK)
1713 			flags |= RT2573_TX_NEED_ACK;
1714 
1715 		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1716 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1717 		USETW(wh->i_dur, dur);
1718 	}
1719 
1720 	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1721 	    m0->m_pkthdr.len, rate);
1722 
1723 	DPRINTFN(10, "sending frame len=%d rate=%d\n",
1724 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1725 
1726 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1727 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1728 
1729 	return 0;
1730 }
1731 
1732 static int
1733 rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1734 {
1735 	struct rum_softc *sc = ic->ic_softc;
1736 	int error;
1737 
1738 	RUM_LOCK(sc);
1739 	if (!sc->sc_running) {
1740 		RUM_UNLOCK(sc);
1741 		return (ENXIO);
1742 	}
1743 	error = mbufq_enqueue(&sc->sc_snd, m);
1744 	if (error) {
1745 		RUM_UNLOCK(sc);
1746 		return (error);
1747 	}
1748 	rum_start(sc);
1749 	RUM_UNLOCK(sc);
1750 
1751 	return (0);
1752 }
1753 
1754 static void
1755 rum_start(struct rum_softc *sc)
1756 {
1757 	struct ieee80211_node *ni;
1758 	struct mbuf *m;
1759 
1760 	RUM_LOCK_ASSERT(sc);
1761 
1762 	if (!sc->sc_running)
1763 		return;
1764 
1765 	while (sc->tx_nfree >= RUM_TX_MINFREE &&
1766 	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1767 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1768 		if (rum_tx_data(sc, m, ni) != 0) {
1769 			if_inc_counter(ni->ni_vap->iv_ifp,
1770 			    IFCOUNTER_OERRORS, 1);
1771 			ieee80211_free_node(ni);
1772 			break;
1773 		}
1774 	}
1775 }
1776 
1777 static void
1778 rum_parent(struct ieee80211com *ic)
1779 {
1780 	struct rum_softc *sc = ic->ic_softc;
1781 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1782 
1783 	RUM_LOCK(sc);
1784 	if (sc->sc_detached) {
1785 		RUM_UNLOCK(sc);
1786 		return;
1787 	}
1788 	RUM_UNLOCK(sc);
1789 
1790 	if (ic->ic_nrunning > 0) {
1791 		if (rum_init(sc) == 0)
1792 			ieee80211_start_all(ic);
1793 		else
1794 			ieee80211_stop(vap);
1795 	} else
1796 		rum_stop(sc);
1797 }
1798 
1799 static void
1800 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1801 {
1802 	struct usb_device_request req;
1803 	usb_error_t error;
1804 
1805 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1806 	req.bRequest = RT2573_READ_EEPROM;
1807 	USETW(req.wValue, 0);
1808 	USETW(req.wIndex, addr);
1809 	USETW(req.wLength, len);
1810 
1811 	error = rum_do_request(sc, &req, buf);
1812 	if (error != 0) {
1813 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1814 		    usbd_errstr(error));
1815 	}
1816 }
1817 
1818 static uint32_t
1819 rum_read(struct rum_softc *sc, uint16_t reg)
1820 {
1821 	uint32_t val;
1822 
1823 	rum_read_multi(sc, reg, &val, sizeof val);
1824 
1825 	return le32toh(val);
1826 }
1827 
1828 static void
1829 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1830 {
1831 	struct usb_device_request req;
1832 	usb_error_t error;
1833 
1834 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1835 	req.bRequest = RT2573_READ_MULTI_MAC;
1836 	USETW(req.wValue, 0);
1837 	USETW(req.wIndex, reg);
1838 	USETW(req.wLength, len);
1839 
1840 	error = rum_do_request(sc, &req, buf);
1841 	if (error != 0) {
1842 		device_printf(sc->sc_dev,
1843 		    "could not multi read MAC register: %s\n",
1844 		    usbd_errstr(error));
1845 	}
1846 }
1847 
1848 static usb_error_t
1849 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1850 {
1851 	uint32_t tmp = htole32(val);
1852 
1853 	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1854 }
1855 
1856 static usb_error_t
1857 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1858 {
1859 	struct usb_device_request req;
1860 	usb_error_t error;
1861 	size_t offset;
1862 
1863 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1864 	req.bRequest = RT2573_WRITE_MULTI_MAC;
1865 	USETW(req.wValue, 0);
1866 
1867 	/* write at most 64 bytes at a time */
1868 	for (offset = 0; offset < len; offset += 64) {
1869 		USETW(req.wIndex, reg + offset);
1870 		USETW(req.wLength, MIN(len - offset, 64));
1871 
1872 		error = rum_do_request(sc, &req, (char *)buf + offset);
1873 		if (error != 0) {
1874 			device_printf(sc->sc_dev,
1875 			    "could not multi write MAC register: %s\n",
1876 			    usbd_errstr(error));
1877 			return (error);
1878 		}
1879 	}
1880 
1881 	return (USB_ERR_NORMAL_COMPLETION);
1882 }
1883 
1884 static usb_error_t
1885 rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1886 {
1887 	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1888 }
1889 
1890 static usb_error_t
1891 rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1892 {
1893 	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1894 }
1895 
1896 static usb_error_t
1897 rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1898 {
1899 	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1900 }
1901 
1902 static int
1903 rum_bbp_busy(struct rum_softc *sc)
1904 {
1905 	int ntries;
1906 
1907 	for (ntries = 0; ntries < 100; ntries++) {
1908 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1909 			break;
1910 		if (rum_pause(sc, hz / 100))
1911 			break;
1912 	}
1913 	if (ntries == 100)
1914 		return (ETIMEDOUT);
1915 
1916 	return (0);
1917 }
1918 
1919 static void
1920 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1921 {
1922 	uint32_t tmp;
1923 
1924 	DPRINTFN(2, "reg=0x%08x\n", reg);
1925 
1926 	if (rum_bbp_busy(sc) != 0) {
1927 		device_printf(sc->sc_dev, "could not write to BBP\n");
1928 		return;
1929 	}
1930 
1931 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1932 	rum_write(sc, RT2573_PHY_CSR3, tmp);
1933 }
1934 
1935 static uint8_t
1936 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1937 {
1938 	uint32_t val;
1939 	int ntries;
1940 
1941 	DPRINTFN(2, "reg=0x%08x\n", reg);
1942 
1943 	if (rum_bbp_busy(sc) != 0) {
1944 		device_printf(sc->sc_dev, "could not read BBP\n");
1945 		return 0;
1946 	}
1947 
1948 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1949 	rum_write(sc, RT2573_PHY_CSR3, val);
1950 
1951 	for (ntries = 0; ntries < 100; ntries++) {
1952 		val = rum_read(sc, RT2573_PHY_CSR3);
1953 		if (!(val & RT2573_BBP_BUSY))
1954 			return val & 0xff;
1955 		if (rum_pause(sc, hz / 100))
1956 			break;
1957 	}
1958 
1959 	device_printf(sc->sc_dev, "could not read BBP\n");
1960 	return 0;
1961 }
1962 
1963 static void
1964 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1965 {
1966 	uint32_t tmp;
1967 	int ntries;
1968 
1969 	for (ntries = 0; ntries < 100; ntries++) {
1970 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1971 			break;
1972 		if (rum_pause(sc, hz / 100))
1973 			break;
1974 	}
1975 	if (ntries == 100) {
1976 		device_printf(sc->sc_dev, "could not write to RF\n");
1977 		return;
1978 	}
1979 
1980 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1981 	    (reg & 3);
1982 	rum_write(sc, RT2573_PHY_CSR4, tmp);
1983 
1984 	/* remember last written value in sc */
1985 	sc->rf_regs[reg] = val;
1986 
1987 	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1988 }
1989 
1990 static void
1991 rum_select_antenna(struct rum_softc *sc)
1992 {
1993 	uint8_t bbp4, bbp77;
1994 	uint32_t tmp;
1995 
1996 	bbp4  = rum_bbp_read(sc, 4);
1997 	bbp77 = rum_bbp_read(sc, 77);
1998 
1999 	/* TBD */
2000 
2001 	/* make sure Rx is disabled before switching antenna */
2002 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
2003 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2004 
2005 	rum_bbp_write(sc,  4, bbp4);
2006 	rum_bbp_write(sc, 77, bbp77);
2007 
2008 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2009 }
2010 
2011 /*
2012  * Enable multi-rate retries for frames sent at OFDM rates.
2013  * In 802.11b/g mode, allow fallback to CCK rates.
2014  */
2015 static void
2016 rum_enable_mrr(struct rum_softc *sc)
2017 {
2018 	struct ieee80211com *ic = &sc->sc_ic;
2019 
2020 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2021 		rum_setbits(sc, RT2573_TXRX_CSR4,
2022 		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
2023 	} else {
2024 		rum_modbits(sc, RT2573_TXRX_CSR4,
2025 		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
2026 	}
2027 }
2028 
2029 static void
2030 rum_set_txpreamble(struct rum_softc *sc)
2031 {
2032 	struct ieee80211com *ic = &sc->sc_ic;
2033 
2034 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2035 		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2036 	else
2037 		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2038 }
2039 
2040 static void
2041 rum_set_basicrates(struct rum_softc *sc)
2042 {
2043 	struct ieee80211com *ic = &sc->sc_ic;
2044 
2045 	/* update basic rate set */
2046 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
2047 		/* 11b basic rates: 1, 2Mbps */
2048 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
2049 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2050 		/* 11a basic rates: 6, 12, 24Mbps */
2051 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
2052 	} else {
2053 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
2054 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
2055 	}
2056 }
2057 
2058 /*
2059  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
2060  * driver.
2061  */
2062 static void
2063 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
2064 {
2065 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2066 
2067 	/* update all BBP registers that depend on the band */
2068 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2069 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
2070 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
2071 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2072 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
2073 	}
2074 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2075 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2076 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2077 	}
2078 
2079 	sc->bbp17 = bbp17;
2080 	rum_bbp_write(sc,  17, bbp17);
2081 	rum_bbp_write(sc,  96, bbp96);
2082 	rum_bbp_write(sc, 104, bbp104);
2083 
2084 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2085 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2086 		rum_bbp_write(sc, 75, 0x80);
2087 		rum_bbp_write(sc, 86, 0x80);
2088 		rum_bbp_write(sc, 88, 0x80);
2089 	}
2090 
2091 	rum_bbp_write(sc, 35, bbp35);
2092 	rum_bbp_write(sc, 97, bbp97);
2093 	rum_bbp_write(sc, 98, bbp98);
2094 
2095 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
2096 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2097 		    RT2573_PA_PE_5GHZ);
2098 	} else {
2099 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2100 		    RT2573_PA_PE_2GHZ);
2101 	}
2102 }
2103 
2104 static void
2105 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
2106 {
2107 	struct ieee80211com *ic = &sc->sc_ic;
2108 	const struct rfprog *rfprog;
2109 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
2110 	int8_t power;
2111 	int i, chan;
2112 
2113 	chan = ieee80211_chan2ieee(ic, c);
2114 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2115 		return;
2116 
2117 	/* select the appropriate RF settings based on what EEPROM says */
2118 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
2119 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
2120 
2121 	/* find the settings for this channel (we know it exists) */
2122 	for (i = 0; rfprog[i].chan != chan; i++);
2123 
2124 	power = sc->txpow[i];
2125 	if (power < 0) {
2126 		bbp94 += power;
2127 		power = 0;
2128 	} else if (power > 31) {
2129 		bbp94 += power - 31;
2130 		power = 31;
2131 	}
2132 
2133 	/*
2134 	 * If we are switching from the 2GHz band to the 5GHz band or
2135 	 * vice-versa, BBP registers need to be reprogrammed.
2136 	 */
2137 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
2138 		rum_select_band(sc, c);
2139 		rum_select_antenna(sc);
2140 	}
2141 	ic->ic_curchan = c;
2142 
2143 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2144 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2145 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2146 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2147 
2148 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2149 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2150 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
2151 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2152 
2153 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2154 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2155 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2156 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2157 
2158 	rum_pause(sc, hz / 100);
2159 
2160 	/* enable smart mode for MIMO-capable RFs */
2161 	bbp3 = rum_bbp_read(sc, 3);
2162 
2163 	bbp3 &= ~RT2573_SMART_MODE;
2164 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
2165 		bbp3 |= RT2573_SMART_MODE;
2166 
2167 	rum_bbp_write(sc, 3, bbp3);
2168 
2169 	if (bbp94 != RT2573_BBPR94_DEFAULT)
2170 		rum_bbp_write(sc, 94, bbp94);
2171 
2172 	/* give the chip some extra time to do the switchover */
2173 	rum_pause(sc, hz / 100);
2174 }
2175 
2176 static void
2177 rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2178 {
2179 	struct ieee80211_node *ni = vap->iv_bss;
2180 	const struct ieee80211_txparam *tp = ni->ni_txparms;
2181 	struct rum_vap *rvp = RUM_VAP(vap);
2182 
2183 	rvp->maxretry = MIN(tp->maxretry, 0xf);
2184 
2185 	rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2186 	    RT2573_LONG_RETRY(rvp->maxretry),
2187 	    RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2188 }
2189 
2190 /*
2191  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2192  * and HostAP operating modes.
2193  */
2194 static int
2195 rum_enable_tsf_sync(struct rum_softc *sc)
2196 {
2197 	struct ieee80211com *ic = &sc->sc_ic;
2198 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2199 	uint32_t tmp;
2200 	uint16_t bintval;
2201 
2202 	if (vap->iv_opmode != IEEE80211_M_STA) {
2203 		/*
2204 		 * Change default 16ms TBTT adjustment to 8ms.
2205 		 * Must be done before enabling beacon generation.
2206 		 */
2207 		if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2208 			return EIO;
2209 	}
2210 
2211 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2212 
2213 	/* set beacon interval (in 1/16ms unit) */
2214 	bintval = vap->iv_bss->ni_intval;
2215 	tmp |= bintval * 16;
2216 	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2217 
2218 	switch (vap->iv_opmode) {
2219 	case IEEE80211_M_STA:
2220 		/*
2221 		 * Local TSF is always updated with remote TSF on beacon
2222 		 * reception.
2223 		 */
2224 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2225 		break;
2226 	case IEEE80211_M_IBSS:
2227 		/*
2228 		 * Local TSF is updated with remote TSF on beacon reception
2229 		 * only if the remote TSF is greater than local TSF.
2230 		 */
2231 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2232 		tmp |= RT2573_BCN_TX_EN;
2233 		break;
2234 	case IEEE80211_M_HOSTAP:
2235 		/* SYNC with nobody */
2236 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2237 		tmp |= RT2573_BCN_TX_EN;
2238 		break;
2239 	default:
2240 		device_printf(sc->sc_dev,
2241 		    "Enabling TSF failed. undefined opmode %d\n",
2242 		    vap->iv_opmode);
2243 		return EINVAL;
2244 	}
2245 
2246 	if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2247 		return EIO;
2248 
2249 	/* refresh current sleep time */
2250 	return (rum_set_sleep_time(sc, bintval));
2251 }
2252 
2253 static void
2254 rum_enable_tsf(struct rum_softc *sc)
2255 {
2256 	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2257 	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2258 }
2259 
2260 static void
2261 rum_abort_tsf_sync(struct rum_softc *sc)
2262 {
2263 	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2264 }
2265 
2266 static void
2267 rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2268 {
2269 	rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2270 }
2271 
2272 static void
2273 rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2274 {
2275 	struct ieee80211com *ic = &sc->sc_ic;
2276 	uint8_t slottime;
2277 
2278 	slottime = IEEE80211_GET_SLOTTIME(ic);
2279 
2280 	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2281 
2282 	DPRINTF("setting slot time to %uus\n", slottime);
2283 }
2284 
2285 static void
2286 rum_update_slot(struct ieee80211com *ic)
2287 {
2288 	rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2289 }
2290 
2291 static int
2292 rum_wme_update(struct ieee80211com *ic)
2293 {
2294 	struct chanAccParams chp;
2295 	const struct wmeParams *chanp;
2296 	struct rum_softc *sc = ic->ic_softc;
2297 	int error = 0;
2298 
2299 	ieee80211_wme_ic_getparams(ic, &chp);
2300 	chanp = chp.cap_wmeParams;
2301 
2302 	RUM_LOCK(sc);
2303 	error = rum_write(sc, RT2573_AIFSN_CSR,
2304 	    chanp[WME_AC_VO].wmep_aifsn  << 12 |
2305 	    chanp[WME_AC_VI].wmep_aifsn  <<  8 |
2306 	    chanp[WME_AC_BK].wmep_aifsn  <<  4 |
2307 	    chanp[WME_AC_BE].wmep_aifsn);
2308 	if (error)
2309 		goto print_err;
2310 	error = rum_write(sc, RT2573_CWMIN_CSR,
2311 	    chanp[WME_AC_VO].wmep_logcwmin << 12 |
2312 	    chanp[WME_AC_VI].wmep_logcwmin <<  8 |
2313 	    chanp[WME_AC_BK].wmep_logcwmin <<  4 |
2314 	    chanp[WME_AC_BE].wmep_logcwmin);
2315 	if (error)
2316 		goto print_err;
2317 	error = rum_write(sc, RT2573_CWMAX_CSR,
2318 	    chanp[WME_AC_VO].wmep_logcwmax << 12 |
2319 	    chanp[WME_AC_VI].wmep_logcwmax <<  8 |
2320 	    chanp[WME_AC_BK].wmep_logcwmax <<  4 |
2321 	    chanp[WME_AC_BE].wmep_logcwmax);
2322 	if (error)
2323 		goto print_err;
2324 	error = rum_write(sc, RT2573_TXOP01_CSR,
2325 	    chanp[WME_AC_BK].wmep_txopLimit << 16 |
2326 	    chanp[WME_AC_BE].wmep_txopLimit);
2327 	if (error)
2328 		goto print_err;
2329 	error = rum_write(sc, RT2573_TXOP23_CSR,
2330 	    chanp[WME_AC_VO].wmep_txopLimit << 16 |
2331 	    chanp[WME_AC_VI].wmep_txopLimit);
2332 	if (error)
2333 		goto print_err;
2334 
2335 	memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2336 
2337 print_err:
2338 	RUM_UNLOCK(sc);
2339 	if (error != 0) {
2340 		device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2341 		    __func__, error);
2342 	}
2343 
2344 	return (error);
2345 }
2346 
2347 static void
2348 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2349 {
2350 
2351 	rum_write(sc, RT2573_MAC_CSR4,
2352 	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2353 	rum_write(sc, RT2573_MAC_CSR5,
2354 	    bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2355 }
2356 
2357 static void
2358 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2359 {
2360 
2361 	rum_write(sc, RT2573_MAC_CSR2,
2362 	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2363 	rum_write(sc, RT2573_MAC_CSR3,
2364 	    addr[4] | addr[5] << 8 | 0xff << 16);
2365 }
2366 
2367 static void
2368 rum_setpromisc(struct rum_softc *sc)
2369 {
2370 	struct ieee80211com *ic = &sc->sc_ic;
2371 
2372 	if (ic->ic_promisc == 0)
2373 		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2374 	else
2375 		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2376 
2377 	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2378 	    "entering" : "leaving");
2379 }
2380 
2381 static void
2382 rum_update_promisc(struct ieee80211com *ic)
2383 {
2384 	struct rum_softc *sc = ic->ic_softc;
2385 
2386 	RUM_LOCK(sc);
2387 	if (sc->sc_running)
2388 		rum_setpromisc(sc);
2389 	RUM_UNLOCK(sc);
2390 }
2391 
2392 static void
2393 rum_update_mcast(struct ieee80211com *ic)
2394 {
2395 	/* Ignore. */
2396 }
2397 
2398 static const char *
2399 rum_get_rf(int rev)
2400 {
2401 	switch (rev) {
2402 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
2403 	case RT2573_RF_2528:	return "RT2528";
2404 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
2405 	case RT2573_RF_5226:	return "RT5226";
2406 	default:		return "unknown";
2407 	}
2408 }
2409 
2410 static void
2411 rum_read_eeprom(struct rum_softc *sc)
2412 {
2413 	uint16_t val;
2414 #ifdef RUM_DEBUG
2415 	int i;
2416 #endif
2417 
2418 	/* read MAC address */
2419 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2420 
2421 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2422 	val = le16toh(val);
2423 	sc->rf_rev =   (val >> 11) & 0x1f;
2424 	sc->hw_radio = (val >> 10) & 0x1;
2425 	sc->rx_ant =   (val >> 4)  & 0x3;
2426 	sc->tx_ant =   (val >> 2)  & 0x3;
2427 	sc->nb_ant =   val & 0x3;
2428 
2429 	DPRINTF("RF revision=%d\n", sc->rf_rev);
2430 
2431 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2432 	val = le16toh(val);
2433 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
2434 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
2435 
2436 	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2437 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2438 
2439 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2440 	val = le16toh(val);
2441 	if ((val & 0xff) != 0xff)
2442 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
2443 
2444 	/* Only [-10, 10] is valid */
2445 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2446 		sc->rssi_2ghz_corr = 0;
2447 
2448 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2449 	val = le16toh(val);
2450 	if ((val & 0xff) != 0xff)
2451 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
2452 
2453 	/* Only [-10, 10] is valid */
2454 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2455 		sc->rssi_5ghz_corr = 0;
2456 
2457 	if (sc->ext_2ghz_lna)
2458 		sc->rssi_2ghz_corr -= 14;
2459 	if (sc->ext_5ghz_lna)
2460 		sc->rssi_5ghz_corr -= 14;
2461 
2462 	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2463 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2464 
2465 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2466 	val = le16toh(val);
2467 	if ((val & 0xff) != 0xff)
2468 		sc->rffreq = val & 0xff;
2469 
2470 	DPRINTF("RF freq=%d\n", sc->rffreq);
2471 
2472 	/* read Tx power for all a/b/g channels */
2473 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2474 	/* XXX default Tx power for 802.11a channels */
2475 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2476 #ifdef RUM_DEBUG
2477 	for (i = 0; i < 14; i++)
2478 		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
2479 #endif
2480 
2481 	/* read default values for BBP registers */
2482 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2483 #ifdef RUM_DEBUG
2484 	for (i = 0; i < 14; i++) {
2485 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2486 			continue;
2487 		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2488 		    sc->bbp_prom[i].val);
2489 	}
2490 #endif
2491 }
2492 
2493 static int
2494 rum_bbp_wakeup(struct rum_softc *sc)
2495 {
2496 	unsigned int ntries;
2497 
2498 	for (ntries = 0; ntries < 100; ntries++) {
2499 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2500 			break;
2501 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
2502 		if (rum_pause(sc, hz / 100))
2503 			break;
2504 	}
2505 	if (ntries == 100) {
2506 		device_printf(sc->sc_dev,
2507 		    "timeout waiting for BBP/RF to wakeup\n");
2508 		return (ETIMEDOUT);
2509 	}
2510 
2511 	return (0);
2512 }
2513 
2514 static int
2515 rum_bbp_init(struct rum_softc *sc)
2516 {
2517 	int i, ntries;
2518 
2519 	/* wait for BBP to be ready */
2520 	for (ntries = 0; ntries < 100; ntries++) {
2521 		const uint8_t val = rum_bbp_read(sc, 0);
2522 		if (val != 0 && val != 0xff)
2523 			break;
2524 		if (rum_pause(sc, hz / 100))
2525 			break;
2526 	}
2527 	if (ntries == 100) {
2528 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2529 		return EIO;
2530 	}
2531 
2532 	/* initialize BBP registers to default values */
2533 	for (i = 0; i < nitems(rum_def_bbp); i++)
2534 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2535 
2536 	/* write vendor-specific BBP values (from EEPROM) */
2537 	for (i = 0; i < 16; i++) {
2538 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2539 			continue;
2540 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2541 	}
2542 
2543 	return 0;
2544 }
2545 
2546 static void
2547 rum_clr_shkey_regs(struct rum_softc *sc)
2548 {
2549 	rum_write(sc, RT2573_SEC_CSR0, 0);
2550 	rum_write(sc, RT2573_SEC_CSR1, 0);
2551 	rum_write(sc, RT2573_SEC_CSR5, 0);
2552 }
2553 
2554 static int
2555 rum_init(struct rum_softc *sc)
2556 {
2557 	struct ieee80211com *ic = &sc->sc_ic;
2558 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2559 	uint32_t tmp;
2560 	int i, ret;
2561 
2562 	RUM_LOCK(sc);
2563 	if (sc->sc_running) {
2564 		ret = 0;
2565 		goto end;
2566 	}
2567 
2568 	/* initialize MAC registers to default values */
2569 	for (i = 0; i < nitems(rum_def_mac); i++)
2570 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2571 
2572 	/* reset some WME parameters to default values */
2573 	sc->wme_params[0].wmep_aifsn = 2;
2574 	sc->wme_params[0].wmep_logcwmin = 4;
2575 	sc->wme_params[0].wmep_logcwmax = 10;
2576 
2577 	/* set host ready */
2578 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2579 	rum_write(sc, RT2573_MAC_CSR1, 0);
2580 
2581 	/* wait for BBP/RF to wakeup */
2582 	if ((ret = rum_bbp_wakeup(sc)) != 0)
2583 		goto end;
2584 
2585 	if ((ret = rum_bbp_init(sc)) != 0)
2586 		goto end;
2587 
2588 	/* select default channel */
2589 	rum_select_band(sc, ic->ic_curchan);
2590 	rum_select_antenna(sc);
2591 	rum_set_chan(sc, ic->ic_curchan);
2592 
2593 	/* clear STA registers */
2594 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2595 
2596 	/* clear security registers (if required) */
2597 	if (sc->sc_clr_shkeys == 0) {
2598 		rum_clr_shkey_regs(sc);
2599 		sc->sc_clr_shkeys = 1;
2600 	}
2601 
2602 	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2603 
2604 	/* initialize ASIC */
2605 	rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2606 
2607 	/*
2608 	 * Allocate Tx and Rx xfer queues.
2609 	 */
2610 	rum_setup_tx_list(sc);
2611 
2612 	/* update Rx filter */
2613 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2614 
2615 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2616 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2617 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2618 		       RT2573_DROP_ACKCTS;
2619 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2620 			tmp |= RT2573_DROP_TODS;
2621 		if (ic->ic_promisc == 0)
2622 			tmp |= RT2573_DROP_NOT_TO_ME;
2623 	}
2624 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2625 
2626 	sc->sc_running = 1;
2627 	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2628 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2629 
2630 end:	RUM_UNLOCK(sc);
2631 
2632 	if (ret != 0)
2633 		rum_stop(sc);
2634 
2635 	return ret;
2636 }
2637 
2638 static void
2639 rum_stop(struct rum_softc *sc)
2640 {
2641 
2642 	RUM_LOCK(sc);
2643 	if (!sc->sc_running) {
2644 		RUM_UNLOCK(sc);
2645 		return;
2646 	}
2647 	sc->sc_running = 0;
2648 	RUM_UNLOCK(sc);
2649 
2650 	/*
2651 	 * Drain the USB transfers, if not already drained:
2652 	 */
2653 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2654 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2655 
2656 	RUM_LOCK(sc);
2657 	rum_unsetup_tx_list(sc);
2658 
2659 	/* disable Rx */
2660 	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2661 
2662 	/* reset ASIC */
2663 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2664 	rum_write(sc, RT2573_MAC_CSR1, 0);
2665 	RUM_UNLOCK(sc);
2666 }
2667 
2668 static void
2669 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2670 {
2671 	uint16_t reg = RT2573_MCU_CODE_BASE;
2672 	usb_error_t err;
2673 
2674 	/* copy firmware image into NIC */
2675 	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2676 		err = rum_write(sc, reg, UGETDW(ucode));
2677 		if (err) {
2678 			/* firmware already loaded ? */
2679 			device_printf(sc->sc_dev, "Firmware load "
2680 			    "failure! (ignored)\n");
2681 			break;
2682 		}
2683 	}
2684 
2685 	err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2686 	if (err != USB_ERR_NORMAL_COMPLETION) {
2687 		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2688 		    usbd_errstr(err));
2689 	}
2690 
2691 	/* give the chip some time to boot */
2692 	rum_pause(sc, hz / 8);
2693 }
2694 
2695 static int
2696 rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2697 {
2698 	struct ieee80211com *ic = &sc->sc_ic;
2699 	usb_error_t uerror;
2700 	int exp, delay;
2701 
2702 	RUM_LOCK_ASSERT(sc);
2703 
2704 	exp = ic->ic_lintval / bintval;
2705 	delay = ic->ic_lintval % bintval;
2706 
2707 	if (exp > RT2573_TBCN_EXP_MAX)
2708 		exp = RT2573_TBCN_EXP_MAX;
2709 	if (delay > RT2573_TBCN_DELAY_MAX)
2710 		delay = RT2573_TBCN_DELAY_MAX;
2711 
2712 	uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2713 	    RT2573_TBCN_EXP(exp) |
2714 	    RT2573_TBCN_DELAY(delay),
2715 	    RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2716 	    RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2717 
2718 	if (uerror != USB_ERR_NORMAL_COMPLETION)
2719 		return (EIO);
2720 
2721 	sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2722 
2723 	return (0);
2724 }
2725 
2726 static int
2727 rum_reset(struct ieee80211vap *vap, u_long cmd)
2728 {
2729 	struct ieee80211com *ic = vap->iv_ic;
2730 	struct ieee80211_node *ni;
2731 	struct rum_softc *sc = ic->ic_softc;
2732 	int error;
2733 
2734 	switch (cmd) {
2735 	case IEEE80211_IOC_POWERSAVE:
2736 	case IEEE80211_IOC_PROTMODE:
2737 	case IEEE80211_IOC_RTSTHRESHOLD:
2738 		error = 0;
2739 		break;
2740 	case IEEE80211_IOC_POWERSAVESLEEP:
2741 		ni = ieee80211_ref_node(vap->iv_bss);
2742 
2743 		RUM_LOCK(sc);
2744 		error = rum_set_sleep_time(sc, ni->ni_intval);
2745 		if (vap->iv_state == IEEE80211_S_SLEEP) {
2746 			/* Use new values for wakeup timer. */
2747 			rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2748 			rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2749 		}
2750 		/* XXX send reassoc */
2751 		RUM_UNLOCK(sc);
2752 
2753 		ieee80211_free_node(ni);
2754 		break;
2755 	default:
2756 		error = ENETRESET;
2757 		break;
2758 	}
2759 
2760 	return (error);
2761 }
2762 
2763 static int
2764 rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2765 {
2766 	struct ieee80211com *ic = vap->iv_ic;
2767 	struct rum_vap *rvp = RUM_VAP(vap);
2768 	struct mbuf *m = rvp->bcn_mbuf;
2769 	const struct ieee80211_txparam *tp;
2770 	struct rum_tx_desc desc;
2771 
2772 	RUM_LOCK_ASSERT(sc);
2773 
2774 	if (m == NULL)
2775 		return EINVAL;
2776 	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2777 		return EINVAL;
2778 
2779 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2780 	rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2781 	    RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2782 
2783 	/* copy the Tx descriptor into NIC memory */
2784 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2785 	    RT2573_TX_DESC_SIZE) != 0)
2786 		return EIO;
2787 
2788 	/* copy beacon header and payload into NIC memory */
2789 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2790 	    mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2791 		return EIO;
2792 
2793 	return 0;
2794 }
2795 
2796 static int
2797 rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2798 {
2799 	struct rum_vap *rvp = RUM_VAP(vap);
2800 	struct ieee80211_node *ni = vap->iv_bss;
2801 	struct mbuf *m;
2802 
2803 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2804 		return EINVAL;
2805 
2806 	m = ieee80211_beacon_alloc(ni);
2807 	if (m == NULL)
2808 		return ENOMEM;
2809 
2810 	if (rvp->bcn_mbuf != NULL)
2811 		m_freem(rvp->bcn_mbuf);
2812 
2813 	rvp->bcn_mbuf = m;
2814 
2815 	return (rum_set_beacon(sc, vap));
2816 }
2817 
2818 static void
2819 rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2820     uint8_t rvp_id)
2821 {
2822 	struct ieee80211vap *vap = data->vap;
2823 
2824 	rum_set_beacon(sc, vap);
2825 }
2826 
2827 static void
2828 rum_update_beacon(struct ieee80211vap *vap, int item)
2829 {
2830 	struct ieee80211com *ic = vap->iv_ic;
2831 	struct rum_softc *sc = ic->ic_softc;
2832 	struct rum_vap *rvp = RUM_VAP(vap);
2833 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2834 	struct ieee80211_node *ni = vap->iv_bss;
2835 	struct mbuf *m = rvp->bcn_mbuf;
2836 	int mcast = 0;
2837 
2838 	RUM_LOCK(sc);
2839 	if (m == NULL) {
2840 		m = ieee80211_beacon_alloc(ni);
2841 		if (m == NULL) {
2842 			device_printf(sc->sc_dev,
2843 			    "%s: could not allocate beacon frame\n", __func__);
2844 			RUM_UNLOCK(sc);
2845 			return;
2846 		}
2847 		rvp->bcn_mbuf = m;
2848 	}
2849 
2850 	switch (item) {
2851 	case IEEE80211_BEACON_ERP:
2852 		rum_update_slot(ic);
2853 		break;
2854 	case IEEE80211_BEACON_TIM:
2855 		mcast = 1;	/*TODO*/
2856 		break;
2857 	default:
2858 		break;
2859 	}
2860 	RUM_UNLOCK(sc);
2861 
2862 	setbit(bo->bo_flags, item);
2863 	ieee80211_beacon_update(ni, m, mcast);
2864 
2865 	rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2866 }
2867 
2868 static int
2869 rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2870     uint16_t base)
2871 {
2872 
2873 	if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2874 		return EIO;
2875 
2876 	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2877 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2878 		    k->wk_txmic, 8))
2879 			return EIO;
2880 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2881 		    k->wk_rxmic, 8))
2882 			return EIO;
2883 	}
2884 
2885 	return 0;
2886 }
2887 
2888 static void
2889 rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2890     uint8_t rvp_id)
2891 {
2892 	struct ieee80211_key *k = &data->key;
2893 	uint8_t mode;
2894 
2895 	if (sc->sc_clr_shkeys == 0) {
2896 		rum_clr_shkey_regs(sc);
2897 		sc->sc_clr_shkeys = 1;
2898 	}
2899 
2900 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2901 	if (mode == 0)
2902 		goto print_err;
2903 
2904 	DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2905 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2906 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2907 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2908 
2909 	/* Install the key. */
2910 	if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2911 		goto print_err;
2912 
2913 	/* Set cipher mode. */
2914 	if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2915 	      mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2916 	      RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2917 	    != 0)
2918 		goto print_err;
2919 
2920 	/* Mark this key as valid. */
2921 	if (rum_setbits(sc, RT2573_SEC_CSR0,
2922 	      1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2923 		goto print_err;
2924 
2925 	return;
2926 
2927 print_err:
2928 	device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2929 	    __func__, k->wk_keyix, rvp_id);
2930 }
2931 
2932 static void
2933 rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2934     uint8_t rvp_id)
2935 {
2936 	struct ieee80211_key *k = &data->key;
2937 
2938 	DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2939 	    k->wk_keyix, rvp_id);
2940 	rum_clrbits(sc,
2941 	    rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2942 	    RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2943 	rum_clrbits(sc, RT2573_SEC_CSR0,
2944 	    rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2945 }
2946 
2947 static void
2948 rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2949     uint8_t rvp_id)
2950 {
2951 	struct ieee80211_key *k = &data->key;
2952 	uint8_t buf[IEEE80211_ADDR_LEN + 1];
2953 	uint8_t mode;
2954 
2955 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2956 	if (mode == 0)
2957 		goto print_err;
2958 
2959 	DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2960 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2961 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2962 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2963 
2964 	/* Install the key. */
2965 	if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2966 		goto print_err;
2967 
2968 	IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2969 	buf[IEEE80211_ADDR_LEN] = mode;
2970 
2971 	/* Set transmitter address and cipher mode. */
2972 	if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2973 	      buf, sizeof buf) != 0)
2974 		goto print_err;
2975 
2976 	/* Enable key table lookup for this vap. */
2977 	if (sc->vap_key_count[rvp_id]++ == 0)
2978 		if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2979 			goto print_err;
2980 
2981 	/* Mark this key as valid. */
2982 	if (rum_setbits(sc,
2983 	      k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2984 	      1 << (k->wk_keyix % 32)) != 0)
2985 		goto print_err;
2986 
2987 	return;
2988 
2989 print_err:
2990 	device_printf(sc->sc_dev,
2991 	    "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2992 	    rvp_id);
2993 }
2994 
2995 static void
2996 rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2997     uint8_t rvp_id)
2998 {
2999 	struct ieee80211_key *k = &data->key;
3000 
3001 	DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
3002 	rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
3003 	    1 << (k->wk_keyix % 32));
3004 	sc->keys_bmap &= ~(1ULL << k->wk_keyix);
3005 	if (--sc->vap_key_count[rvp_id] == 0)
3006 		rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
3007 }
3008 
3009 static int
3010 rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
3011     ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
3012 {
3013 	struct rum_softc *sc = vap->iv_ic->ic_softc;
3014 	uint8_t i;
3015 
3016 	if (!(&vap->iv_nw_keys[0] <= k &&
3017 	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
3018 		if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
3019 			RUM_LOCK(sc);
3020 			for (i = 0; i < RT2573_ADDR_MAX; i++) {
3021 				if ((sc->keys_bmap & (1ULL << i)) == 0) {
3022 					sc->keys_bmap |= (1ULL << i);
3023 					*keyix = i;
3024 					break;
3025 				}
3026 			}
3027 			RUM_UNLOCK(sc);
3028 			if (i == RT2573_ADDR_MAX) {
3029 				device_printf(sc->sc_dev,
3030 				    "%s: no free space in the key table\n",
3031 				    __func__);
3032 				return 0;
3033 			}
3034 		} else
3035 			*keyix = 0;
3036 	} else {
3037 		*keyix = ieee80211_crypto_get_key_wepidx(vap, k);
3038 	}
3039 	*rxkeyix = *keyix;
3040 	return 1;
3041 }
3042 
3043 static int
3044 rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
3045 {
3046 	struct rum_softc *sc = vap->iv_ic->ic_softc;
3047 	int group;
3048 
3049 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3050 		/* Not for us. */
3051 		return 1;
3052 	}
3053 
3054 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3055 
3056 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3057 		   group ? rum_group_key_set_cb : rum_pair_key_set_cb);
3058 }
3059 
3060 static int
3061 rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
3062 {
3063 	struct rum_softc *sc = vap->iv_ic->ic_softc;
3064 	int group;
3065 
3066 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3067 		/* Not for us. */
3068 		return 1;
3069 	}
3070 
3071 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3072 
3073 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3074 		   group ? rum_group_key_del_cb : rum_pair_key_del_cb);
3075 }
3076 
3077 static int
3078 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3079     const struct ieee80211_bpf_params *params)
3080 {
3081 	struct rum_softc *sc = ni->ni_ic->ic_softc;
3082 	int ret;
3083 
3084 	RUM_LOCK(sc);
3085 	/* prevent management frames from being sent if we're not ready */
3086 	if (!sc->sc_running) {
3087 		ret = ENETDOWN;
3088 		goto bad;
3089 	}
3090 	if (sc->tx_nfree < RUM_TX_MINFREE) {
3091 		ret = EIO;
3092 		goto bad;
3093 	}
3094 
3095 	if (params == NULL) {
3096 		/*
3097 		 * Legacy path; interpret frame contents to decide
3098 		 * precisely how to send the frame.
3099 		 */
3100 		if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
3101 			goto bad;
3102 	} else {
3103 		/*
3104 		 * Caller supplied explicit parameters to use in
3105 		 * sending the frame.
3106 		 */
3107 		if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
3108 			goto bad;
3109 	}
3110 	RUM_UNLOCK(sc);
3111 
3112 	return 0;
3113 bad:
3114 	RUM_UNLOCK(sc);
3115 	m_freem(m);
3116 	return ret;
3117 }
3118 
3119 static void
3120 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
3121 {
3122 	struct ieee80211vap *vap = ni->ni_vap;
3123 	struct rum_vap *rvp = RUM_VAP(vap);
3124 
3125 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
3126 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
3127 
3128 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3129 }
3130 
3131 static void
3132 rum_ratectl_timeout(void *arg)
3133 {
3134 	struct rum_vap *rvp = arg;
3135 	struct ieee80211vap *vap = &rvp->vap;
3136 	struct ieee80211com *ic = vap->iv_ic;
3137 
3138 	ieee80211_runtask(ic, &rvp->ratectl_task);
3139 }
3140 
3141 static void
3142 rum_ratectl_task(void *arg, int pending)
3143 {
3144 	struct rum_vap *rvp = arg;
3145 	struct ieee80211vap *vap = &rvp->vap;
3146 	struct rum_softc *sc = vap->iv_ic->ic_softc;
3147 	struct ieee80211_ratectl_tx_stats *txs = &sc->sc_txs;
3148 	int ok[3], fail;
3149 
3150 	RUM_LOCK(sc);
3151 	/* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
3152 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
3153 
3154 	ok[0] = (le32toh(sc->sta[4]) & 0xffff);	/* TX ok w/o retry */
3155 	ok[1] = (le32toh(sc->sta[4]) >> 16);	/* TX ok w/ one retry */
3156 	ok[2] = (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ multiple retries */
3157 	fail =  (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
3158 
3159 	txs->flags = IEEE80211_RATECTL_TX_STATS_RETRIES;
3160 	txs->nframes = ok[0] + ok[1] + ok[2] + fail;
3161 	txs->nsuccess = txs->nframes - fail;
3162 	/* XXX at least */
3163 	txs->nretries = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3164 
3165 	if (txs->nframes != 0)
3166 		ieee80211_ratectl_tx_update(vap, txs);
3167 
3168 	/* count TX retry-fail as Tx errors */
3169 	if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
3170 
3171 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3172 	RUM_UNLOCK(sc);
3173 }
3174 
3175 static void
3176 rum_scan_start(struct ieee80211com *ic)
3177 {
3178 	struct rum_softc *sc = ic->ic_softc;
3179 
3180 	RUM_LOCK(sc);
3181 	rum_abort_tsf_sync(sc);
3182 	rum_set_bssid(sc, ieee80211broadcastaddr);
3183 	RUM_UNLOCK(sc);
3184 
3185 }
3186 
3187 static void
3188 rum_scan_end(struct ieee80211com *ic)
3189 {
3190 	struct rum_softc *sc = ic->ic_softc;
3191 
3192 	if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
3193 		RUM_LOCK(sc);
3194 		if (ic->ic_opmode != IEEE80211_M_AHDEMO)
3195 			rum_enable_tsf_sync(sc);
3196 		else
3197 			rum_enable_tsf(sc);
3198 		rum_set_bssid(sc, sc->sc_bssid);
3199 		RUM_UNLOCK(sc);
3200 	}
3201 }
3202 
3203 static void
3204 rum_set_channel(struct ieee80211com *ic)
3205 {
3206 	struct rum_softc *sc = ic->ic_softc;
3207 
3208 	RUM_LOCK(sc);
3209 	rum_set_chan(sc, ic->ic_curchan);
3210 	RUM_UNLOCK(sc);
3211 }
3212 
3213 static void
3214 rum_getradiocaps(struct ieee80211com *ic,
3215     int maxchans, int *nchans, struct ieee80211_channel chans[])
3216 {
3217 	struct rum_softc *sc = ic->ic_softc;
3218 	uint8_t bands[IEEE80211_MODE_BYTES];
3219 
3220 	memset(bands, 0, sizeof(bands));
3221 	setbit(bands, IEEE80211_MODE_11B);
3222 	setbit(bands, IEEE80211_MODE_11G);
3223 	ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
3224 	    rum_chan_2ghz, nitems(rum_chan_2ghz), bands, 0);
3225 
3226 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
3227 		setbit(bands, IEEE80211_MODE_11A);
3228 		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3229 		    rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
3230 	}
3231 }
3232 
3233 static int
3234 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
3235 {
3236 	struct ieee80211com *ic = &sc->sc_ic;
3237 	int lna, agc, rssi;
3238 
3239 	lna = (raw >> 5) & 0x3;
3240 	agc = raw & 0x1f;
3241 
3242 	if (lna == 0) {
3243 		/*
3244 		 * No RSSI mapping
3245 		 *
3246 		 * NB: Since RSSI is relative to noise floor, -1 is
3247 		 *     adequate for caller to know error happened.
3248 		 */
3249 		return -1;
3250 	}
3251 
3252 	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
3253 
3254 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
3255 		rssi += sc->rssi_2ghz_corr;
3256 
3257 		if (lna == 1)
3258 			rssi -= 64;
3259 		else if (lna == 2)
3260 			rssi -= 74;
3261 		else if (lna == 3)
3262 			rssi -= 90;
3263 	} else {
3264 		rssi += sc->rssi_5ghz_corr;
3265 
3266 		if (!sc->ext_5ghz_lna && lna != 1)
3267 			rssi += 4;
3268 
3269 		if (lna == 1)
3270 			rssi -= 64;
3271 		else if (lna == 2)
3272 			rssi -= 86;
3273 		else if (lna == 3)
3274 			rssi -= 100;
3275 	}
3276 	return rssi;
3277 }
3278 
3279 static int
3280 rum_pause(struct rum_softc *sc, int timeout)
3281 {
3282 
3283 	usb_pause_mtx(&sc->sc_mtx, timeout);
3284 	return (0);
3285 }
3286 
3287 static device_method_t rum_methods[] = {
3288 	/* Device interface */
3289 	DEVMETHOD(device_probe,		rum_match),
3290 	DEVMETHOD(device_attach,	rum_attach),
3291 	DEVMETHOD(device_detach,	rum_detach),
3292 	DEVMETHOD_END
3293 };
3294 
3295 static driver_t rum_driver = {
3296 	.name = "rum",
3297 	.methods = rum_methods,
3298 	.size = sizeof(struct rum_softc),
3299 };
3300 
3301 static devclass_t rum_devclass;
3302 
3303 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3304 MODULE_DEPEND(rum, wlan, 1, 1, 1);
3305 MODULE_DEPEND(rum, usb, 1, 1, 1);
3306 MODULE_VERSION(rum, 1);
3307 USB_PNP_HOST_INFO(rum_devs);
3308