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