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