xref: /openbsd-src/sys/dev/usb/if_rum.c (revision 7ebc5b51da5611c78123bf2a163bb4cee0fee6d5)
1 /*	$OpenBSD: if_rum.c,v 1.111 2015/06/12 15:47:31 mpi Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*-
21  * Ralink Technology RT2501USB/RT2601USB chipset driver
22  * http://www.ralinktech.com.tw/
23  */
24 
25 #include "bpfilter.h"
26 
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/timeout.h>
34 #include <sys/conf.h>
35 #include <sys/device.h>
36 #include <sys/endian.h>
37 
38 #include <machine/intr.h>
39 
40 #if NBPFILTER > 0
41 #include <net/bpf.h>
42 #endif
43 #include <net/if.h>
44 #include <net/if_arp.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/if_types.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51 
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_amrr.h>
54 #include <net80211/ieee80211_radiotap.h>
55 
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbdevs.h>
60 
61 #include <dev/usb/if_rumreg.h>
62 #include <dev/usb/if_rumvar.h>
63 
64 #ifdef RUM_DEBUG
65 #define DPRINTF(x)	do { if (rum_debug) printf x; } while (0)
66 #define DPRINTFN(n, x)	do { if (rum_debug >= (n)) printf x; } while (0)
67 int rum_debug = 0;
68 #else
69 #define DPRINTF(x)
70 #define DPRINTFN(n, x)
71 #endif
72 
73 /* various supported device vendors/products */
74 static const struct usb_devno rum_devs[] = {
75 	{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_HWU54DM },
76 	{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_RT2573_2 },
77 	{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_RT2573_3 },
78 	{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_RT2573_4 },
79 	{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_WUG2700 },
80 	{ USB_VENDOR_AMIT,		USB_PRODUCT_AMIT_CGWLUSB2GO },
81 	{ USB_VENDOR_ASUS,		USB_PRODUCT_ASUS_RT2573_1 },
82 	{ USB_VENDOR_ASUS,		USB_PRODUCT_ASUS_RT2573_2 },
83 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D7050A },
84 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D9050V3 },
85 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D9050C },
86 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB200 },
87 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
88 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
89 	{ USB_VENDOR_CONCEPTRONIC2,	USB_PRODUCT_CONCEPTRONIC2_C54RU2 },
90 	{ USB_VENDOR_CONCEPTRONIC2,	USB_PRODUCT_CONCEPTRONIC2_RT2573 },
91 	{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_CGWLUSB2GL },
92 	{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_CGWLUSB2GPX },
93 	{ USB_VENDOR_DICKSMITH,		USB_PRODUCT_DICKSMITH_CWD854F },
94 	{ USB_VENDOR_DICKSMITH,		USB_PRODUCT_DICKSMITH_RT2573 },
95 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA111 },
96 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA110 },
97 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWLG122C1 },
98 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_WUA1340 },
99 	{ USB_VENDOR_EDIMAX,		USB_PRODUCT_EDIMAX_EW7318 },
100 	{ USB_VENDOR_EDIMAX,		USB_PRODUCT_EDIMAX_EW7618 },
101 	{ USB_VENDOR_GIGABYTE,		USB_PRODUCT_GIGABYTE_GNWB01GS },
102 	{ USB_VENDOR_GIGABYTE,		USB_PRODUCT_GIGABYTE_GNWI05GS },
103 	{ USB_VENDOR_GIGASET,		USB_PRODUCT_GIGASET_RT2573 },
104 	{ USB_VENDOR_GOODWAY,		USB_PRODUCT_GOODWAY_RT2573 },
105 	{ USB_VENDOR_GUILLEMOT,		USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
106 	{ USB_VENDOR_GUILLEMOT,		USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
107 	{ USB_VENDOR_HUAWEI3COM,	USB_PRODUCT_HUAWEI3COM_WUB320G },
108 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_G54HP },
109 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_SG54HP },
110 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_SG54HG },
111 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_1 },
112 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_2 },
113 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_3 },
114 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_4 },
115 	{ USB_VENDOR_NOVATECH,		USB_PRODUCT_NOVATECH_RT2573 },
116 	{ USB_VENDOR_PLANEX2,		USB_PRODUCT_PLANEX2_GWUS54HP },
117 	{ USB_VENDOR_PLANEX2,		USB_PRODUCT_PLANEX2_GWUS54MINI2 },
118 	{ USB_VENDOR_PLANEX2,		USB_PRODUCT_PLANEX2_GWUSMM },
119 	{ USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573 },
120 	{ USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573_2 },
121 	{ USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573_3 },
122 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2573 },
123 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2573_2 },
124 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2671 },
125 	{ USB_VENDOR_SITECOMEU,		USB_PRODUCT_SITECOMEU_WL113R2 },
126 	{ USB_VENDOR_SITECOMEU,		USB_PRODUCT_SITECOMEU_WL172 },
127 	{ USB_VENDOR_SURECOM,		USB_PRODUCT_SURECOM_RT2573 },
128 	{ USB_VENDOR_SPARKLAN,		USB_PRODUCT_SPARKLAN_RT2573 },
129 	{ USB_VENDOR_ZYXEL,		USB_PRODUCT_ZYXEL_RT2573 }
130 };
131 
132 void		rum_attachhook(void *);
133 int		rum_alloc_tx_list(struct rum_softc *);
134 void		rum_free_tx_list(struct rum_softc *);
135 int		rum_alloc_rx_list(struct rum_softc *);
136 void		rum_free_rx_list(struct rum_softc *);
137 int		rum_media_change(struct ifnet *);
138 void		rum_next_scan(void *);
139 void		rum_task(void *);
140 int		rum_newstate(struct ieee80211com *, enum ieee80211_state, int);
141 void		rum_txeof(struct usbd_xfer *, void *, usbd_status);
142 void		rum_rxeof(struct usbd_xfer *, void *, usbd_status);
143 #if NBPFILTER > 0
144 uint8_t		rum_rxrate(const struct rum_rx_desc *);
145 #endif
146 int		rum_ack_rate(struct ieee80211com *, int);
147 uint16_t	rum_txtime(int, int, uint32_t);
148 uint8_t		rum_plcp_signal(int);
149 void		rum_setup_tx_desc(struct rum_softc *, struct rum_tx_desc *,
150 		    uint32_t, uint16_t, int, int);
151 int		rum_tx_data(struct rum_softc *, struct mbuf *,
152 		    struct ieee80211_node *);
153 void		rum_start(struct ifnet *);
154 void		rum_watchdog(struct ifnet *);
155 int		rum_ioctl(struct ifnet *, u_long, caddr_t);
156 void		rum_eeprom_read(struct rum_softc *, uint16_t, void *, int);
157 uint32_t	rum_read(struct rum_softc *, uint16_t);
158 void		rum_read_multi(struct rum_softc *, uint16_t, void *, int);
159 void		rum_write(struct rum_softc *, uint16_t, uint32_t);
160 void		rum_write_multi(struct rum_softc *, uint16_t, void *, size_t);
161 void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
162 uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
163 void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
164 void		rum_select_antenna(struct rum_softc *);
165 void		rum_enable_mrr(struct rum_softc *);
166 void		rum_set_txpreamble(struct rum_softc *);
167 void		rum_set_basicrates(struct rum_softc *);
168 void		rum_select_band(struct rum_softc *,
169 		    struct ieee80211_channel *);
170 void		rum_set_chan(struct rum_softc *, struct ieee80211_channel *);
171 void		rum_enable_tsf_sync(struct rum_softc *);
172 void		rum_update_slot(struct rum_softc *);
173 void		rum_set_bssid(struct rum_softc *, const uint8_t *);
174 void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
175 void		rum_update_promisc(struct rum_softc *);
176 const char	*rum_get_rf(int);
177 void		rum_read_eeprom(struct rum_softc *);
178 int		rum_bbp_init(struct rum_softc *);
179 int		rum_init(struct ifnet *);
180 void		rum_stop(struct ifnet *, int);
181 int		rum_load_microcode(struct rum_softc *, const u_char *, size_t);
182 #ifndef IEEE80211_STA_ONLY
183 int		rum_prepare_beacon(struct rum_softc *);
184 #endif
185 void		rum_newassoc(struct ieee80211com *, struct ieee80211_node *,
186 		    int);
187 void		rum_amrr_start(struct rum_softc *, struct ieee80211_node *);
188 void		rum_amrr_timeout(void *);
189 void		rum_amrr_update(struct usbd_xfer *, void *,
190 		    usbd_status status);
191 
192 static const struct {
193 	uint32_t	reg;
194 	uint32_t	val;
195 } rum_def_mac[] = {
196 	RT2573_DEF_MAC
197 };
198 
199 static const struct {
200 	uint8_t	reg;
201 	uint8_t	val;
202 } rum_def_bbp[] = {
203 	RT2573_DEF_BBP
204 };
205 
206 static const struct rfprog {
207 	uint8_t		chan;
208 	uint32_t	r1, r2, r3, r4;
209 }  rum_rf5226[] = {
210 	RT2573_RF5226
211 }, rum_rf5225[] = {
212 	RT2573_RF5225
213 };
214 
215 int rum_match(struct device *, void *, void *);
216 void rum_attach(struct device *, struct device *, void *);
217 int rum_detach(struct device *, int);
218 
219 struct cfdriver rum_cd = {
220 	NULL, "rum", DV_IFNET
221 };
222 
223 const struct cfattach rum_ca = {
224 	sizeof(struct rum_softc), rum_match, rum_attach, rum_detach
225 };
226 
227 int
228 rum_match(struct device *parent, void *match, void *aux)
229 {
230 	struct usb_attach_arg *uaa = aux;
231 
232 	if (uaa->iface == NULL || uaa->configno != 1)
233 		return UMATCH_NONE;
234 
235 	return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
236 	    UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
237 }
238 
239 void
240 rum_attachhook(void *xsc)
241 {
242 	struct rum_softc *sc = xsc;
243 	const char *name = "rum-rt2573";
244 	u_char *ucode;
245 	size_t size;
246 	int error;
247 
248 	if ((error = loadfirmware(name, &ucode, &size)) != 0) {
249 		printf("%s: failed loadfirmware of file %s (error %d)\n",
250 		    sc->sc_dev.dv_xname, name, error);
251 		return;
252 	}
253 
254 	if (rum_load_microcode(sc, ucode, size) != 0) {
255 		printf("%s: could not load 8051 microcode\n",
256 		    sc->sc_dev.dv_xname);
257 	}
258 
259 	free(ucode, M_DEVBUF, 0);
260 }
261 
262 void
263 rum_attach(struct device *parent, struct device *self, void *aux)
264 {
265 	struct rum_softc *sc = (struct rum_softc *)self;
266 	struct usb_attach_arg *uaa = aux;
267 	struct ieee80211com *ic = &sc->sc_ic;
268 	struct ifnet *ifp = &ic->ic_if;
269 	usb_interface_descriptor_t *id;
270 	usb_endpoint_descriptor_t *ed;
271 	int i, ntries;
272 	uint32_t tmp;
273 
274 	sc->sc_udev = uaa->device;
275 	sc->sc_iface = uaa->iface;
276 
277 	/*
278 	 * Find endpoints.
279 	 */
280 	id = usbd_get_interface_descriptor(sc->sc_iface);
281 
282 	sc->sc_rx_no = sc->sc_tx_no = -1;
283 	for (i = 0; i < id->bNumEndpoints; i++) {
284 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
285 		if (ed == NULL) {
286 			printf("%s: no endpoint descriptor for iface %d\n",
287 			    sc->sc_dev.dv_xname, i);
288 			return;
289 		}
290 
291 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
292 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
293 			sc->sc_rx_no = ed->bEndpointAddress;
294 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
295 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
296 			sc->sc_tx_no = ed->bEndpointAddress;
297 	}
298 	if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
299 		printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
300 		return;
301 	}
302 
303 	usb_init_task(&sc->sc_task, rum_task, sc, USB_TASK_TYPE_GENERIC);
304 	timeout_set(&sc->scan_to, rum_next_scan, sc);
305 
306 	sc->amrr.amrr_min_success_threshold =  1;
307 	sc->amrr.amrr_max_success_threshold = 10;
308 	timeout_set(&sc->amrr_to, rum_amrr_timeout, sc);
309 
310 	/* retrieve RT2573 rev. no */
311 	for (ntries = 0; ntries < 1000; ntries++) {
312 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
313 			break;
314 		DELAY(1000);
315 	}
316 	if (ntries == 1000) {
317 		printf("%s: timeout waiting for chip to settle\n",
318 		    sc->sc_dev.dv_xname);
319 		return;
320 	}
321 
322 	/* retrieve MAC address and various other things from EEPROM */
323 	rum_read_eeprom(sc);
324 
325 	printf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %s\n",
326 	    sc->sc_dev.dv_xname, sc->macbbp_rev, tmp,
327 	    rum_get_rf(sc->rf_rev), ether_sprintf(ic->ic_myaddr));
328 
329 	if (rootvp == NULL)
330 		mountroothook_establish(rum_attachhook, sc);
331 	else
332 		rum_attachhook(sc);
333 
334 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
335 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
336 	ic->ic_state = IEEE80211_S_INIT;
337 
338 	/* set device capabilities */
339 	ic->ic_caps =
340 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
341 #ifndef IEEE80211_STA_ONLY
342 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
343 	    IEEE80211_C_HOSTAP |	/* HostAp mode supported */
344 #endif
345 	    IEEE80211_C_TXPMGT |	/* tx power management */
346 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
347 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
348 	    IEEE80211_C_WEP |		/* s/w WEP */
349 	    IEEE80211_C_RSN;		/* WPA/RSN */
350 
351 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
352 		/* set supported .11a rates */
353 		ic->ic_sup_rates[IEEE80211_MODE_11A] =
354 		    ieee80211_std_rateset_11a;
355 
356 		/* set supported .11a channels */
357 		for (i = 34; i <= 46; i += 4) {
358 			ic->ic_channels[i].ic_freq =
359 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
360 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
361 		}
362 		for (i = 36; i <= 64; i += 4) {
363 			ic->ic_channels[i].ic_freq =
364 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
365 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
366 		}
367 		for (i = 100; i <= 140; i += 4) {
368 			ic->ic_channels[i].ic_freq =
369 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
370 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
371 		}
372 		for (i = 149; i <= 165; i += 4) {
373 			ic->ic_channels[i].ic_freq =
374 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
375 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
376 		}
377 	}
378 
379 	/* set supported .11b and .11g rates */
380 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
381 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
382 
383 	/* set supported .11b and .11g channels (1 through 14) */
384 	for (i = 1; i <= 14; i++) {
385 		ic->ic_channels[i].ic_freq =
386 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
387 		ic->ic_channels[i].ic_flags =
388 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
389 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
390 	}
391 
392 	ifp->if_softc = sc;
393 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
394 	ifp->if_ioctl = rum_ioctl;
395 	ifp->if_start = rum_start;
396 	ifp->if_watchdog = rum_watchdog;
397 	IFQ_SET_READY(&ifp->if_snd);
398 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
399 
400 	if_attach(ifp);
401 	ieee80211_ifattach(ifp);
402 	ic->ic_newassoc = rum_newassoc;
403 
404 	/* override state transition machine */
405 	sc->sc_newstate = ic->ic_newstate;
406 	ic->ic_newstate = rum_newstate;
407 	ieee80211_media_init(ifp, rum_media_change, ieee80211_media_status);
408 
409 #if NBPFILTER > 0
410 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
411 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
412 
413 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
414 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
415 	sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
416 
417 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
418 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
419 	sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
420 #endif
421 }
422 
423 int
424 rum_detach(struct device *self, int flags)
425 {
426 	struct rum_softc *sc = (struct rum_softc *)self;
427 	struct ifnet *ifp = &sc->sc_ic.ic_if;
428 	int s;
429 
430 	s = splusb();
431 
432 	if (timeout_initialized(&sc->scan_to))
433 		timeout_del(&sc->scan_to);
434 	if (timeout_initialized(&sc->amrr_to))
435 		timeout_del(&sc->amrr_to);
436 
437 	usb_rem_wait_task(sc->sc_udev, &sc->sc_task);
438 
439 	usbd_ref_wait(sc->sc_udev);
440 
441 	if (ifp->if_softc != NULL) {
442 		ieee80211_ifdetach(ifp);	/* free all nodes */
443 		if_detach(ifp);
444 	}
445 
446 	if (sc->amrr_xfer != NULL) {
447 		usbd_free_xfer(sc->amrr_xfer);
448 		sc->amrr_xfer = NULL;
449 	}
450 	if (sc->sc_rx_pipeh != NULL) {
451 		usbd_abort_pipe(sc->sc_rx_pipeh);
452 		usbd_close_pipe(sc->sc_rx_pipeh);
453 	}
454 	if (sc->sc_tx_pipeh != NULL) {
455 		usbd_abort_pipe(sc->sc_tx_pipeh);
456 		usbd_close_pipe(sc->sc_tx_pipeh);
457 	}
458 
459 	rum_free_rx_list(sc);
460 	rum_free_tx_list(sc);
461 
462 	splx(s);
463 
464 	return 0;
465 }
466 
467 int
468 rum_alloc_tx_list(struct rum_softc *sc)
469 {
470 	int i, error;
471 
472 	sc->tx_cur = sc->tx_queued = 0;
473 
474 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
475 		struct rum_tx_data *data = &sc->tx_data[i];
476 
477 		data->sc = sc;
478 
479 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
480 		if (data->xfer == NULL) {
481 			printf("%s: could not allocate tx xfer\n",
482 			    sc->sc_dev.dv_xname);
483 			error = ENOMEM;
484 			goto fail;
485 		}
486 		data->buf = usbd_alloc_buffer(data->xfer,
487 		    RT2573_TX_DESC_SIZE + IEEE80211_MAX_LEN);
488 		if (data->buf == NULL) {
489 			printf("%s: could not allocate tx buffer\n",
490 			    sc->sc_dev.dv_xname);
491 			error = ENOMEM;
492 			goto fail;
493 		}
494 		/* clean Tx descriptor */
495 		bzero(data->buf, RT2573_TX_DESC_SIZE);
496 	}
497 
498 	return 0;
499 
500 fail:	rum_free_tx_list(sc);
501 	return error;
502 }
503 
504 void
505 rum_free_tx_list(struct rum_softc *sc)
506 {
507 	int i;
508 
509 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
510 		struct rum_tx_data *data = &sc->tx_data[i];
511 
512 		if (data->xfer != NULL) {
513 			usbd_free_xfer(data->xfer);
514 			data->xfer = NULL;
515 		}
516 		/*
517 		 * The node has already been freed at that point so don't call
518 		 * ieee80211_release_node() here.
519 		 */
520 		data->ni = NULL;
521 	}
522 }
523 
524 int
525 rum_alloc_rx_list(struct rum_softc *sc)
526 {
527 	int i, error;
528 
529 	for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
530 		struct rum_rx_data *data = &sc->rx_data[i];
531 
532 		data->sc = sc;
533 
534 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
535 		if (data->xfer == NULL) {
536 			printf("%s: could not allocate rx xfer\n",
537 			    sc->sc_dev.dv_xname);
538 			error = ENOMEM;
539 			goto fail;
540 		}
541 		if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
542 			printf("%s: could not allocate rx buffer\n",
543 			    sc->sc_dev.dv_xname);
544 			error = ENOMEM;
545 			goto fail;
546 		}
547 
548 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
549 		if (data->m == NULL) {
550 			printf("%s: could not allocate rx mbuf\n",
551 			    sc->sc_dev.dv_xname);
552 			error = ENOMEM;
553 			goto fail;
554 		}
555 		MCLGET(data->m, M_DONTWAIT);
556 		if (!(data->m->m_flags & M_EXT)) {
557 			printf("%s: could not allocate rx mbuf cluster\n",
558 			    sc->sc_dev.dv_xname);
559 			error = ENOMEM;
560 			goto fail;
561 		}
562 		data->buf = mtod(data->m, uint8_t *);
563 	}
564 
565 	return 0;
566 
567 fail:	rum_free_rx_list(sc);
568 	return error;
569 }
570 
571 void
572 rum_free_rx_list(struct rum_softc *sc)
573 {
574 	int i;
575 
576 	for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
577 		struct rum_rx_data *data = &sc->rx_data[i];
578 
579 		if (data->xfer != NULL) {
580 			usbd_free_xfer(data->xfer);
581 			data->xfer = NULL;
582 		}
583 		if (data->m != NULL) {
584 			m_freem(data->m);
585 			data->m = NULL;
586 		}
587 	}
588 }
589 
590 int
591 rum_media_change(struct ifnet *ifp)
592 {
593 	int error;
594 
595 	error = ieee80211_media_change(ifp);
596 	if (error != ENETRESET)
597 		return error;
598 
599 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
600 		rum_init(ifp);
601 
602 	return 0;
603 }
604 
605 /*
606  * This function is called periodically (every 200ms) during scanning to
607  * switch from one channel to another.
608  */
609 void
610 rum_next_scan(void *arg)
611 {
612 	struct rum_softc *sc = arg;
613 	struct ieee80211com *ic = &sc->sc_ic;
614 	struct ifnet *ifp = &ic->ic_if;
615 
616 	if (usbd_is_dying(sc->sc_udev))
617 		return;
618 
619 	usbd_ref_incr(sc->sc_udev);
620 
621 	if (ic->ic_state == IEEE80211_S_SCAN)
622 		ieee80211_next_scan(ifp);
623 
624 	usbd_ref_decr(sc->sc_udev);
625 }
626 
627 void
628 rum_task(void *arg)
629 {
630 	struct rum_softc *sc = arg;
631 	struct ieee80211com *ic = &sc->sc_ic;
632 	enum ieee80211_state ostate;
633 	struct ieee80211_node *ni;
634 	uint32_t tmp;
635 
636 	if (usbd_is_dying(sc->sc_udev))
637 		return;
638 
639 	ostate = ic->ic_state;
640 
641 	switch (sc->sc_state) {
642 	case IEEE80211_S_INIT:
643 		if (ostate == IEEE80211_S_RUN) {
644 			/* abort TSF synchronization */
645 			tmp = rum_read(sc, RT2573_TXRX_CSR9);
646 			rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
647 		}
648 		break;
649 
650 	case IEEE80211_S_SCAN:
651 		rum_set_chan(sc, ic->ic_bss->ni_chan);
652 		if (!usbd_is_dying(sc->sc_udev))
653 			timeout_add_msec(&sc->scan_to, 200);
654 		break;
655 
656 	case IEEE80211_S_AUTH:
657 		rum_set_chan(sc, ic->ic_bss->ni_chan);
658 		break;
659 
660 	case IEEE80211_S_ASSOC:
661 		rum_set_chan(sc, ic->ic_bss->ni_chan);
662 		break;
663 
664 	case IEEE80211_S_RUN:
665 		rum_set_chan(sc, ic->ic_bss->ni_chan);
666 
667 		ni = ic->ic_bss;
668 
669 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
670 			rum_update_slot(sc);
671 			rum_enable_mrr(sc);
672 			rum_set_txpreamble(sc);
673 			rum_set_basicrates(sc);
674 			rum_set_bssid(sc, ni->ni_bssid);
675 		}
676 
677 #ifndef IEEE80211_STA_ONLY
678 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
679 		    ic->ic_opmode == IEEE80211_M_IBSS)
680 			rum_prepare_beacon(sc);
681 #endif
682 
683 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
684 			rum_enable_tsf_sync(sc);
685 
686 		if (ic->ic_opmode == IEEE80211_M_STA) {
687 			/* fake a join to init the tx rate */
688 			rum_newassoc(ic, ic->ic_bss, 1);
689 
690 			/* enable automatic rate control in STA mode */
691 			if (ic->ic_fixed_rate == -1)
692 				rum_amrr_start(sc, ni);
693 		}
694 		break;
695 	}
696 
697 	sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
698 }
699 
700 int
701 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
702 {
703 	struct rum_softc *sc = ic->ic_if.if_softc;
704 
705 	usb_rem_task(sc->sc_udev, &sc->sc_task);
706 	timeout_del(&sc->scan_to);
707 	timeout_del(&sc->amrr_to);
708 
709 	/* do it in a process context */
710 	sc->sc_state = nstate;
711 	sc->sc_arg = arg;
712 	usb_add_task(sc->sc_udev, &sc->sc_task);
713 	return 0;
714 }
715 
716 /* quickly determine if a given rate is CCK or OFDM */
717 #define RUM_RATE_IS_OFDM(rate)	((rate) >= 12 && (rate) != 22)
718 
719 #define RUM_ACK_SIZE	14	/* 10 + 4(FCS) */
720 #define RUM_CTS_SIZE	14	/* 10 + 4(FCS) */
721 
722 void
723 rum_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
724 {
725 	struct rum_tx_data *data = priv;
726 	struct rum_softc *sc = data->sc;
727 	struct ieee80211com *ic = &sc->sc_ic;
728 	struct ifnet *ifp = &ic->ic_if;
729 	int s;
730 
731 	if (status != USBD_NORMAL_COMPLETION) {
732 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
733 			return;
734 
735 		printf("%s: could not transmit buffer: %s\n",
736 		    sc->sc_dev.dv_xname, usbd_errstr(status));
737 
738 		if (status == USBD_STALLED)
739 			usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
740 
741 		ifp->if_oerrors++;
742 		return;
743 	}
744 
745 	s = splnet();
746 
747 	ieee80211_release_node(ic, data->ni);
748 	data->ni = NULL;
749 
750 	sc->tx_queued--;
751 	ifp->if_opackets++;
752 
753 	DPRINTFN(10, ("tx done\n"));
754 
755 	sc->sc_tx_timer = 0;
756 	ifp->if_flags &= ~IFF_OACTIVE;
757 	rum_start(ifp);
758 
759 	splx(s);
760 }
761 
762 void
763 rum_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
764 {
765 	struct rum_rx_data *data = priv;
766 	struct rum_softc *sc = data->sc;
767 	struct ieee80211com *ic = &sc->sc_ic;
768 	struct ifnet *ifp = &ic->ic_if;
769 	const struct rum_rx_desc *desc;
770 	struct ieee80211_frame *wh;
771 	struct ieee80211_rxinfo rxi;
772 	struct ieee80211_node *ni;
773 	struct mbuf *mnew, *m;
774 	int s, len;
775 
776 	if (status != USBD_NORMAL_COMPLETION) {
777 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
778 			return;
779 
780 		if (status == USBD_STALLED)
781 			usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
782 		goto skip;
783 	}
784 
785 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
786 
787 	if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
788 		DPRINTF(("%s: xfer too short %d\n", sc->sc_dev.dv_xname,
789 		    len));
790 		ifp->if_ierrors++;
791 		goto skip;
792 	}
793 
794 	desc = (const struct rum_rx_desc *)data->buf;
795 
796 	if (letoh32(desc->flags) & RT2573_RX_CRC_ERROR) {
797 		/*
798 		 * This should not happen since we did not request to receive
799 		 * those frames when we filled RT2573_TXRX_CSR0.
800 		 */
801 		DPRINTFN(5, ("CRC error\n"));
802 		ifp->if_ierrors++;
803 		goto skip;
804 	}
805 
806 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
807 	if (mnew == NULL) {
808 		printf("%s: could not allocate rx mbuf\n",
809 		    sc->sc_dev.dv_xname);
810 		ifp->if_ierrors++;
811 		goto skip;
812 	}
813 	MCLGET(mnew, M_DONTWAIT);
814 	if (!(mnew->m_flags & M_EXT)) {
815 		printf("%s: could not allocate rx mbuf cluster\n",
816 		    sc->sc_dev.dv_xname);
817 		m_freem(mnew);
818 		ifp->if_ierrors++;
819 		goto skip;
820 	}
821 	m = data->m;
822 	data->m = mnew;
823 	data->buf = mtod(data->m, uint8_t *);
824 
825 	/* finalize mbuf */
826 	m->m_data = (caddr_t)(desc + 1);
827 	m->m_pkthdr.len = m->m_len = (letoh32(desc->flags) >> 16) & 0xfff;
828 
829 	s = splnet();
830 
831 #if NBPFILTER > 0
832 	if (sc->sc_drvbpf != NULL) {
833 		struct mbuf mb;
834 		struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
835 
836 		tap->wr_flags = 0;
837 		tap->wr_rate = rum_rxrate(desc);
838 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
839 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
840 		tap->wr_antenna = sc->rx_ant;
841 		tap->wr_antsignal = desc->rssi;
842 
843 		mb.m_data = (caddr_t)tap;
844 		mb.m_len = sc->sc_rxtap_len;
845 		mb.m_next = m;
846 		mb.m_nextpkt = NULL;
847 		mb.m_type = 0;
848 		mb.m_flags = 0;
849 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
850 	}
851 #endif
852 
853 	wh = mtod(m, struct ieee80211_frame *);
854 	ni = ieee80211_find_rxnode(ic, wh);
855 
856 	/* send the frame to the 802.11 layer */
857 	rxi.rxi_flags = 0;
858 	rxi.rxi_rssi = desc->rssi;
859 	rxi.rxi_tstamp = 0;	/* unused */
860 	ieee80211_input(ifp, m, ni, &rxi);
861 
862 	/* node is no longer needed */
863 	ieee80211_release_node(ic, ni);
864 
865 	splx(s);
866 
867 	DPRINTFN(15, ("rx done\n"));
868 
869 skip:	/* setup a new transfer */
870 	usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
871 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
872 	(void)usbd_transfer(xfer);
873 }
874 
875 /*
876  * This function is only used by the Rx radiotap code. It returns the rate at
877  * which a given frame was received.
878  */
879 #if NBPFILTER > 0
880 uint8_t
881 rum_rxrate(const struct rum_rx_desc *desc)
882 {
883 	if (letoh32(desc->flags) & RT2573_RX_OFDM) {
884 		/* reverse function of rum_plcp_signal */
885 		switch (desc->rate) {
886 		case 0xb:	return 12;
887 		case 0xf:	return 18;
888 		case 0xa:	return 24;
889 		case 0xe:	return 36;
890 		case 0x9:	return 48;
891 		case 0xd:	return 72;
892 		case 0x8:	return 96;
893 		case 0xc:	return 108;
894 		}
895 	} else {
896 		if (desc->rate == 10)
897 			return 2;
898 		if (desc->rate == 20)
899 			return 4;
900 		if (desc->rate == 55)
901 			return 11;
902 		if (desc->rate == 110)
903 			return 22;
904 	}
905 	return 2;	/* should not get there */
906 }
907 #endif
908 
909 /*
910  * Return the expected ack rate for a frame transmitted at rate `rate'.
911  */
912 int
913 rum_ack_rate(struct ieee80211com *ic, int rate)
914 {
915 	switch (rate) {
916 	/* CCK rates */
917 	case 2:
918 		return 2;
919 	case 4:
920 	case 11:
921 	case 22:
922 		return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
923 
924 	/* OFDM rates */
925 	case 12:
926 	case 18:
927 		return 12;
928 	case 24:
929 	case 36:
930 		return 24;
931 	case 48:
932 	case 72:
933 	case 96:
934 	case 108:
935 		return 48;
936 	}
937 
938 	/* default to 1Mbps */
939 	return 2;
940 }
941 
942 /*
943  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
944  * The function automatically determines the operating mode depending on the
945  * given rate. `flags' indicates whether short preamble is in use or not.
946  */
947 uint16_t
948 rum_txtime(int len, int rate, uint32_t flags)
949 {
950 	uint16_t txtime;
951 
952 	if (RUM_RATE_IS_OFDM(rate)) {
953 		/* IEEE Std 802.11a-1999, pp. 37 */
954 		txtime = (8 + 4 * len + 3 + rate - 1) / rate;
955 		txtime = 16 + 4 + 4 * txtime + 6;
956 	} else {
957 		/* IEEE Std 802.11b-1999, pp. 28 */
958 		txtime = (16 * len + rate - 1) / rate;
959 		if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
960 			txtime +=  72 + 24;
961 		else
962 			txtime += 144 + 48;
963 	}
964 	return txtime;
965 }
966 
967 uint8_t
968 rum_plcp_signal(int rate)
969 {
970 	switch (rate) {
971 	/* CCK rates (returned values are device-dependent) */
972 	case 2:		return 0x0;
973 	case 4:		return 0x1;
974 	case 11:	return 0x2;
975 	case 22:	return 0x3;
976 
977 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
978 	case 12:	return 0xb;
979 	case 18:	return 0xf;
980 	case 24:	return 0xa;
981 	case 36:	return 0xe;
982 	case 48:	return 0x9;
983 	case 72:	return 0xd;
984 	case 96:	return 0x8;
985 	case 108:	return 0xc;
986 
987 	/* unsupported rates (should not get there) */
988 	default:	return 0xff;
989 	}
990 }
991 
992 void
993 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
994     uint32_t flags, uint16_t xflags, int len, int rate)
995 {
996 	struct ieee80211com *ic = &sc->sc_ic;
997 	uint16_t plcp_length;
998 	int remainder;
999 
1000 	desc->flags = htole32(flags);
1001 	desc->flags |= htole32(RT2573_TX_VALID);
1002 	desc->flags |= htole32(len << 16);
1003 
1004 	desc->xflags = htole16(xflags);
1005 
1006 	desc->wme = htole16(
1007 	    RT2573_QID(0) |
1008 	    RT2573_AIFSN(2) |
1009 	    RT2573_LOGCWMIN(4) |
1010 	    RT2573_LOGCWMAX(10));
1011 
1012 	/* setup PLCP fields */
1013 	desc->plcp_signal  = rum_plcp_signal(rate);
1014 	desc->plcp_service = 4;
1015 
1016 	len += IEEE80211_CRC_LEN;
1017 	if (RUM_RATE_IS_OFDM(rate)) {
1018 		desc->flags |= htole32(RT2573_TX_OFDM);
1019 
1020 		plcp_length = len & 0xfff;
1021 		desc->plcp_length_hi = plcp_length >> 6;
1022 		desc->plcp_length_lo = plcp_length & 0x3f;
1023 	} else {
1024 		plcp_length = (16 * len + rate - 1) / rate;
1025 		if (rate == 22) {
1026 			remainder = (16 * len) % 22;
1027 			if (remainder != 0 && remainder < 7)
1028 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1029 		}
1030 		desc->plcp_length_hi = plcp_length >> 8;
1031 		desc->plcp_length_lo = plcp_length & 0xff;
1032 
1033 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1034 			desc->plcp_signal |= 0x08;
1035 	}
1036 }
1037 
1038 #define RUM_TX_TIMEOUT	5000
1039 
1040 int
1041 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1042 {
1043 	struct ieee80211com *ic = &sc->sc_ic;
1044 	struct rum_tx_desc *desc;
1045 	struct rum_tx_data *data;
1046 	struct ieee80211_frame *wh;
1047 	struct ieee80211_key *k;
1048 	uint32_t flags = 0;
1049 	uint16_t dur;
1050 	usbd_status error;
1051 	int rate, xferlen, pktlen, needrts = 0, needcts = 0;
1052 
1053 	wh = mtod(m0, struct ieee80211_frame *);
1054 
1055 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1056 		k = ieee80211_get_txkey(ic, wh, ni);
1057 
1058 		if ((m0 = ieee80211_encrypt(ic, m0, k)) == NULL)
1059 			return ENOBUFS;
1060 
1061 		/* packet header may have moved, reset our local pointer */
1062 		wh = mtod(m0, struct ieee80211_frame *);
1063 	}
1064 
1065 	/* compute actual packet length (including CRC and crypto overhead) */
1066 	pktlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
1067 
1068 	/* pickup a rate */
1069 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1070 	    ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1071 	     IEEE80211_FC0_TYPE_MGT)) {
1072 		/* mgmt/multicast frames are sent at the lowest avail. rate */
1073 		rate = ni->ni_rates.rs_rates[0];
1074 	} else if (ic->ic_fixed_rate != -1) {
1075 		rate = ic->ic_sup_rates[ic->ic_curmode].
1076 		    rs_rates[ic->ic_fixed_rate];
1077 	} else
1078 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1079 	if (rate == 0)
1080 		rate = 2;	/* XXX should not happen */
1081 	rate &= IEEE80211_RATE_VAL;
1082 
1083 	/* check if RTS/CTS or CTS-to-self protection must be used */
1084 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1085 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
1086 		if (pktlen > ic->ic_rtsthreshold) {
1087 			needrts = 1;	/* RTS/CTS based on frame length */
1088 		} else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1089 		    RUM_RATE_IS_OFDM(rate)) {
1090 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
1091 				needcts = 1;	/* CTS-to-self */
1092 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
1093 				needrts = 1;	/* RTS/CTS */
1094 		}
1095 	}
1096 	if (needrts || needcts) {
1097 		struct mbuf *mprot;
1098 		int protrate, ackrate;
1099 		uint16_t dur;
1100 
1101 		protrate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1102 		ackrate  = rum_ack_rate(ic, rate);
1103 
1104 		dur = rum_txtime(pktlen, rate, ic->ic_flags) +
1105 		      rum_txtime(RUM_ACK_SIZE, ackrate, ic->ic_flags) +
1106 		      2 * sc->sifs;
1107 		if (needrts) {
1108 			dur += rum_txtime(RUM_CTS_SIZE, rum_ack_rate(ic,
1109 			    protrate), ic->ic_flags) + sc->sifs;
1110 			mprot = ieee80211_get_rts(ic, wh, dur);
1111 		} else {
1112 			mprot = ieee80211_get_cts_to_self(ic, dur);
1113 		}
1114 		if (mprot == NULL) {
1115 			printf("%s: could not allocate protection frame\n",
1116 			    sc->sc_dev.dv_xname);
1117 			m_freem(m0);
1118 			return ENOBUFS;
1119 		}
1120 
1121 		data = &sc->tx_data[sc->tx_cur];
1122 		desc = (struct rum_tx_desc *)data->buf;
1123 
1124 		/* avoid multiple free() of the same node for each fragment */
1125 		data->ni = ieee80211_ref_node(ni);
1126 
1127 		m_copydata(mprot, 0, mprot->m_pkthdr.len,
1128 		    data->buf + RT2573_TX_DESC_SIZE);
1129 		rum_setup_tx_desc(sc, desc,
1130 		    (needrts ? RT2573_TX_NEED_ACK : 0) | RT2573_TX_MORE_FRAG,
1131 		    0, mprot->m_pkthdr.len, protrate);
1132 
1133 		/* no roundup necessary here */
1134 		xferlen = RT2573_TX_DESC_SIZE + mprot->m_pkthdr.len;
1135 
1136 		/* XXX may want to pass the protection frame to BPF */
1137 
1138 		/* mbuf is no longer needed */
1139 		m_freem(mprot);
1140 
1141 		usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1142 		    xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1143 		    RUM_TX_TIMEOUT, rum_txeof);
1144 		error = usbd_transfer(data->xfer);
1145 		if (error != 0 && error != USBD_IN_PROGRESS) {
1146 			m_freem(m0);
1147 			return error;
1148 		}
1149 
1150 		sc->tx_queued++;
1151 		sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1152 
1153 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1154 	}
1155 
1156 	data = &sc->tx_data[sc->tx_cur];
1157 	desc = (struct rum_tx_desc *)data->buf;
1158 
1159 	data->ni = ni;
1160 
1161 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1162 		flags |= RT2573_TX_NEED_ACK;
1163 
1164 		dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1165 		    ic->ic_flags) + sc->sifs;
1166 		*(uint16_t *)wh->i_dur = htole16(dur);
1167 
1168 #ifndef IEEE80211_STA_ONLY
1169 		/* tell hardware to set timestamp in probe responses */
1170 		if ((wh->i_fc[0] &
1171 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1172 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1173 			flags |= RT2573_TX_TIMESTAMP;
1174 #endif
1175 	}
1176 
1177 #if NBPFILTER > 0
1178 	if (sc->sc_drvbpf != NULL) {
1179 		struct mbuf mb;
1180 		struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1181 
1182 		tap->wt_flags = 0;
1183 		tap->wt_rate = rate;
1184 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1185 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1186 		tap->wt_antenna = sc->tx_ant;
1187 
1188 		mb.m_data = (caddr_t)tap;
1189 		mb.m_len = sc->sc_txtap_len;
1190 		mb.m_next = m0;
1191 		mb.m_nextpkt = NULL;
1192 		mb.m_type = 0;
1193 		mb.m_flags = 0;
1194 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1195 	}
1196 #endif
1197 
1198 	m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1199 	rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1200 
1201 	/* align end on a 4-bytes boundary */
1202 	xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1203 
1204 	/*
1205 	 * No space left in the last URB to store the extra 4 bytes, force
1206 	 * sending of another URB.
1207 	 */
1208 	if ((xferlen % 64) == 0)
1209 		xferlen += 4;
1210 
1211 	DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1212 	    m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1213 
1214 	/* mbuf is no longer needed */
1215 	m_freem(m0);
1216 
1217 	usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1218 	    USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1219 	error = usbd_transfer(data->xfer);
1220 	if (error != 0 && error != USBD_IN_PROGRESS)
1221 		return error;
1222 
1223 	sc->tx_queued++;
1224 	sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1225 
1226 	return 0;
1227 }
1228 
1229 void
1230 rum_start(struct ifnet *ifp)
1231 {
1232 	struct rum_softc *sc = ifp->if_softc;
1233 	struct ieee80211com *ic = &sc->sc_ic;
1234 	struct ieee80211_node *ni;
1235 	struct mbuf *m0;
1236 
1237 	/*
1238 	 * net80211 may still try to send management frames even if the
1239 	 * IFF_RUNNING flag is not set...
1240 	 */
1241 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1242 		return;
1243 
1244 	for (;;) {
1245 		IF_POLL(&ic->ic_mgtq, m0);
1246 		if (m0 != NULL) {
1247 			if (sc->tx_queued >= RUM_TX_LIST_COUNT - 1) {
1248 				ifp->if_flags |= IFF_OACTIVE;
1249 				break;
1250 			}
1251 			IF_DEQUEUE(&ic->ic_mgtq, m0);
1252 
1253 			ni = m0->m_pkthdr.ph_cookie;
1254 #if NBPFILTER > 0
1255 			if (ic->ic_rawbpf != NULL)
1256 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1257 #endif
1258 			if (rum_tx_data(sc, m0, ni) != 0)
1259 				break;
1260 
1261 		} else {
1262 			if (ic->ic_state != IEEE80211_S_RUN)
1263 				break;
1264 			IFQ_POLL(&ifp->if_snd, m0);
1265 			if (m0 == NULL)
1266 				break;
1267 			if (sc->tx_queued >= RUM_TX_LIST_COUNT - 1) {
1268 				ifp->if_flags |= IFF_OACTIVE;
1269 				break;
1270 			}
1271 			IFQ_DEQUEUE(&ifp->if_snd, m0);
1272 #if NBPFILTER > 0
1273 			if (ifp->if_bpf != NULL)
1274 				bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
1275 #endif
1276 			m0 = ieee80211_encap(ifp, m0, &ni);
1277 			if (m0 == NULL)
1278 				continue;
1279 #if NBPFILTER > 0
1280 			if (ic->ic_rawbpf != NULL)
1281 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1282 #endif
1283 			if (rum_tx_data(sc, m0, ni) != 0) {
1284 				if (ni != NULL)
1285 					ieee80211_release_node(ic, ni);
1286 				ifp->if_oerrors++;
1287 				break;
1288 			}
1289 		}
1290 
1291 		sc->sc_tx_timer = 5;
1292 		ifp->if_timer = 1;
1293 	}
1294 }
1295 
1296 void
1297 rum_watchdog(struct ifnet *ifp)
1298 {
1299 	struct rum_softc *sc = ifp->if_softc;
1300 
1301 	ifp->if_timer = 0;
1302 
1303 	if (sc->sc_tx_timer > 0) {
1304 		if (--sc->sc_tx_timer == 0) {
1305 			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1306 			/*rum_init(ifp); XXX needs a process context! */
1307 			ifp->if_oerrors++;
1308 			return;
1309 		}
1310 		ifp->if_timer = 1;
1311 	}
1312 
1313 	ieee80211_watchdog(ifp);
1314 }
1315 
1316 int
1317 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1318 {
1319 	struct rum_softc *sc = ifp->if_softc;
1320 	struct ieee80211com *ic = &sc->sc_ic;
1321 	struct ifaddr *ifa;
1322 	struct ifreq *ifr;
1323 	int s, error = 0;
1324 
1325 	if (usbd_is_dying(sc->sc_udev))
1326 		return ENXIO;
1327 
1328 	usbd_ref_incr(sc->sc_udev);
1329 
1330 	s = splnet();
1331 
1332 	switch (cmd) {
1333 	case SIOCSIFADDR:
1334 		ifa = (struct ifaddr *)data;
1335 		ifp->if_flags |= IFF_UP;
1336 		if (ifa->ifa_addr->sa_family == AF_INET)
1337 			arp_ifinit(&ic->ic_ac, ifa);
1338 		/* FALLTHROUGH */
1339 	case SIOCSIFFLAGS:
1340 		if (ifp->if_flags & IFF_UP) {
1341 			if (ifp->if_flags & IFF_RUNNING)
1342 				rum_update_promisc(sc);
1343 			else
1344 				rum_init(ifp);
1345 		} else {
1346 			if (ifp->if_flags & IFF_RUNNING)
1347 				rum_stop(ifp, 1);
1348 		}
1349 		break;
1350 
1351 	case SIOCADDMULTI:
1352 	case SIOCDELMULTI:
1353 		ifr = (struct ifreq *)data;
1354 		error = (cmd == SIOCADDMULTI) ?
1355 		    ether_addmulti(ifr, &ic->ic_ac) :
1356 		    ether_delmulti(ifr, &ic->ic_ac);
1357 
1358 		if (error == ENETRESET)
1359 			error = 0;
1360 		break;
1361 
1362 	case SIOCS80211CHANNEL:
1363 		/*
1364 		 * This allows for fast channel switching in monitor mode
1365 		 * (used by kismet). In IBSS mode, we must explicitly reset
1366 		 * the interface to generate a new beacon frame.
1367 		 */
1368 		error = ieee80211_ioctl(ifp, cmd, data);
1369 		if (error == ENETRESET &&
1370 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
1371 			if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1372 			    (IFF_UP | IFF_RUNNING))
1373 				rum_set_chan(sc, ic->ic_ibss_chan);
1374 			error = 0;
1375 		}
1376 		break;
1377 
1378 	default:
1379 		error = ieee80211_ioctl(ifp, cmd, data);
1380 	}
1381 
1382 	if (error == ENETRESET) {
1383 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1384 		    (IFF_UP | IFF_RUNNING))
1385 			rum_init(ifp);
1386 		error = 0;
1387 	}
1388 
1389 	splx(s);
1390 
1391 	usbd_ref_decr(sc->sc_udev);
1392 
1393 	return error;
1394 }
1395 
1396 void
1397 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1398 {
1399 	usb_device_request_t req;
1400 	usbd_status error;
1401 
1402 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1403 	req.bRequest = RT2573_READ_EEPROM;
1404 	USETW(req.wValue, 0);
1405 	USETW(req.wIndex, addr);
1406 	USETW(req.wLength, len);
1407 
1408 	error = usbd_do_request(sc->sc_udev, &req, buf);
1409 	if (error != 0) {
1410 		printf("%s: could not read EEPROM: %s\n",
1411 		    sc->sc_dev.dv_xname, usbd_errstr(error));
1412 	}
1413 }
1414 
1415 uint32_t
1416 rum_read(struct rum_softc *sc, uint16_t reg)
1417 {
1418 	uint32_t val;
1419 
1420 	rum_read_multi(sc, reg, &val, sizeof val);
1421 
1422 	return letoh32(val);
1423 }
1424 
1425 void
1426 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1427 {
1428 	usb_device_request_t req;
1429 	usbd_status error;
1430 
1431 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1432 	req.bRequest = RT2573_READ_MULTI_MAC;
1433 	USETW(req.wValue, 0);
1434 	USETW(req.wIndex, reg);
1435 	USETW(req.wLength, len);
1436 
1437 	error = usbd_do_request(sc->sc_udev, &req, buf);
1438 	if (error != 0) {
1439 		printf("%s: could not multi read MAC register: %s\n",
1440 		    sc->sc_dev.dv_xname, usbd_errstr(error));
1441 	}
1442 }
1443 
1444 void
1445 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1446 {
1447 	uint32_t tmp = htole32(val);
1448 
1449 	rum_write_multi(sc, reg, &tmp, sizeof tmp);
1450 }
1451 
1452 void
1453 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1454 {
1455 	usb_device_request_t req;
1456 	usbd_status error;
1457 	int offset;
1458 
1459 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1460 	req.bRequest = RT2573_WRITE_MULTI_MAC;
1461 	USETW(req.wValue, 0);
1462 
1463 	/* write at most 64 bytes at a time */
1464 	for (offset = 0; offset < len; offset += 64) {
1465 		USETW(req.wIndex, reg + offset);
1466 		USETW(req.wLength, MIN(len - offset, 64));
1467 
1468 		error = usbd_do_request(sc->sc_udev, &req, buf + offset);
1469 		if (error != 0) {
1470 			printf("%s: could not multi write MAC register: %s\n",
1471 			    sc->sc_dev.dv_xname, usbd_errstr(error));
1472 		}
1473 	}
1474 }
1475 
1476 void
1477 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1478 {
1479 	uint32_t tmp;
1480 	int ntries;
1481 
1482 	for (ntries = 0; ntries < 5; ntries++) {
1483 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1484 			break;
1485 	}
1486 	if (ntries == 5) {
1487 		printf("%s: could not write to BBP\n", sc->sc_dev.dv_xname);
1488 		return;
1489 	}
1490 
1491 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1492 	rum_write(sc, RT2573_PHY_CSR3, tmp);
1493 }
1494 
1495 uint8_t
1496 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1497 {
1498 	uint32_t val;
1499 	int ntries;
1500 
1501 	for (ntries = 0; ntries < 5; ntries++) {
1502 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1503 			break;
1504 	}
1505 	if (ntries == 5) {
1506 		printf("%s: could not read BBP\n", sc->sc_dev.dv_xname);
1507 		return 0;
1508 	}
1509 
1510 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1511 	rum_write(sc, RT2573_PHY_CSR3, val);
1512 
1513 	for (ntries = 0; ntries < 100; ntries++) {
1514 		val = rum_read(sc, RT2573_PHY_CSR3);
1515 		if (!(val & RT2573_BBP_BUSY))
1516 			return val & 0xff;
1517 		DELAY(1);
1518 	}
1519 
1520 	printf("%s: could not read BBP\n", sc->sc_dev.dv_xname);
1521 	return 0;
1522 }
1523 
1524 void
1525 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1526 {
1527 	uint32_t tmp;
1528 	int ntries;
1529 
1530 	for (ntries = 0; ntries < 5; ntries++) {
1531 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1532 			break;
1533 	}
1534 	if (ntries == 5) {
1535 		printf("%s: could not write to RF\n", sc->sc_dev.dv_xname);
1536 		return;
1537 	}
1538 
1539 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1540 	    (reg & 3);
1541 	rum_write(sc, RT2573_PHY_CSR4, tmp);
1542 
1543 	/* remember last written value in sc */
1544 	sc->rf_regs[reg] = val;
1545 
1546 	DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1547 }
1548 
1549 void
1550 rum_select_antenna(struct rum_softc *sc)
1551 {
1552 	uint8_t bbp4, bbp77;
1553 	uint32_t tmp;
1554 
1555 	bbp4  = rum_bbp_read(sc, 4);
1556 	bbp77 = rum_bbp_read(sc, 77);
1557 
1558 	/* TBD */
1559 
1560 	/* make sure Rx is disabled before switching antenna */
1561 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1562 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1563 
1564 	rum_bbp_write(sc,  4, bbp4);
1565 	rum_bbp_write(sc, 77, bbp77);
1566 
1567 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1568 }
1569 
1570 /*
1571  * Enable multi-rate retries for frames sent at OFDM rates.
1572  * In 802.11b/g mode, allow fallback to CCK rates.
1573  */
1574 void
1575 rum_enable_mrr(struct rum_softc *sc)
1576 {
1577 	struct ieee80211com *ic = &sc->sc_ic;
1578 	uint32_t tmp;
1579 
1580 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1581 
1582 	tmp &= ~RT2573_MRR_CCK_FALLBACK;
1583 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan))
1584 		tmp |= RT2573_MRR_CCK_FALLBACK;
1585 	tmp |= RT2573_MRR_ENABLED;
1586 
1587 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1588 }
1589 
1590 void
1591 rum_set_txpreamble(struct rum_softc *sc)
1592 {
1593 	uint32_t tmp;
1594 
1595 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1596 
1597 	tmp &= ~RT2573_SHORT_PREAMBLE;
1598 	if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1599 		tmp |= RT2573_SHORT_PREAMBLE;
1600 
1601 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1602 }
1603 
1604 void
1605 rum_set_basicrates(struct rum_softc *sc)
1606 {
1607 	struct ieee80211com *ic = &sc->sc_ic;
1608 
1609 	/* update basic rate set */
1610 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1611 		/* 11b basic rates: 1, 2Mbps */
1612 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1613 	} else if (ic->ic_curmode == IEEE80211_MODE_11A) {
1614 		/* 11a basic rates: 6, 12, 24Mbps */
1615 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1616 	} else {
1617 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1618 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1619 	}
1620 }
1621 
1622 /*
1623  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1624  * driver.
1625  */
1626 void
1627 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1628 {
1629 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1630 	uint32_t tmp;
1631 
1632 	/* update all BBP registers that depend on the band */
1633 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1634 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1635 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1636 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1637 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1638 	}
1639 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1640 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1641 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1642 	}
1643 
1644 	sc->bbp17 = bbp17;
1645 	rum_bbp_write(sc,  17, bbp17);
1646 	rum_bbp_write(sc,  96, bbp96);
1647 	rum_bbp_write(sc, 104, bbp104);
1648 
1649 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1650 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1651 		rum_bbp_write(sc, 75, 0x80);
1652 		rum_bbp_write(sc, 86, 0x80);
1653 		rum_bbp_write(sc, 88, 0x80);
1654 	}
1655 
1656 	rum_bbp_write(sc, 35, bbp35);
1657 	rum_bbp_write(sc, 97, bbp97);
1658 	rum_bbp_write(sc, 98, bbp98);
1659 
1660 	tmp = rum_read(sc, RT2573_PHY_CSR0);
1661 	tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1662 	if (IEEE80211_IS_CHAN_2GHZ(c))
1663 		tmp |= RT2573_PA_PE_2GHZ;
1664 	else
1665 		tmp |= RT2573_PA_PE_5GHZ;
1666 	rum_write(sc, RT2573_PHY_CSR0, tmp);
1667 
1668 	/* 802.11a uses a 16 microseconds short interframe space */
1669 	sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1670 }
1671 
1672 void
1673 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1674 {
1675 	struct ieee80211com *ic = &sc->sc_ic;
1676 	const struct rfprog *rfprog;
1677 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1678 	int8_t power;
1679 	u_int i, chan;
1680 
1681 	chan = ieee80211_chan2ieee(ic, c);
1682 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1683 		return;
1684 
1685 	/* select the appropriate RF settings based on what EEPROM says */
1686 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1687 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1688 
1689 	/* find the settings for this channel (we know it exists) */
1690 	for (i = 0; rfprog[i].chan != chan; i++);
1691 
1692 	power = sc->txpow[i];
1693 	if (power < 0) {
1694 		bbp94 += power;
1695 		power = 0;
1696 	} else if (power > 31) {
1697 		bbp94 += power - 31;
1698 		power = 31;
1699 	}
1700 
1701 	/*
1702 	 * If we are switching from the 2GHz band to the 5GHz band or
1703 	 * vice-versa, BBP registers need to be reprogrammed.
1704 	 */
1705 	if (c->ic_flags != sc->sc_curchan->ic_flags) {
1706 		rum_select_band(sc, c);
1707 		rum_select_antenna(sc);
1708 	}
1709 	sc->sc_curchan = c;
1710 
1711 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1712 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1713 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1714 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1715 
1716 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1717 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1718 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1719 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1720 
1721 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1722 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1723 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1724 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1725 
1726 	DELAY(10);
1727 
1728 	/* enable smart mode for MIMO-capable RFs */
1729 	bbp3 = rum_bbp_read(sc, 3);
1730 
1731 	bbp3 &= ~RT2573_SMART_MODE;
1732 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1733 		bbp3 |= RT2573_SMART_MODE;
1734 
1735 	rum_bbp_write(sc, 3, bbp3);
1736 
1737 	if (bbp94 != RT2573_BBPR94_DEFAULT)
1738 		rum_bbp_write(sc, 94, bbp94);
1739 }
1740 
1741 /*
1742  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1743  * and HostAP operating modes.
1744  */
1745 void
1746 rum_enable_tsf_sync(struct rum_softc *sc)
1747 {
1748 	struct ieee80211com *ic = &sc->sc_ic;
1749 	uint32_t tmp;
1750 
1751 #ifndef IEEE80211_STA_ONLY
1752 	if (ic->ic_opmode != IEEE80211_M_STA) {
1753 		/*
1754 		 * Change default 16ms TBTT adjustment to 8ms.
1755 		 * Must be done before enabling beacon generation.
1756 		 */
1757 		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1758 	}
1759 #endif
1760 
1761 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1762 
1763 	/* set beacon interval (in 1/16ms unit) */
1764 	tmp |= ic->ic_bss->ni_intval * 16;
1765 
1766 	tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1767 	if (ic->ic_opmode == IEEE80211_M_STA)
1768 		tmp |= RT2573_TSF_MODE(1);
1769 #ifndef IEEE80211_STA_ONLY
1770 	else
1771 		tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1772 #endif
1773 	rum_write(sc, RT2573_TXRX_CSR9, tmp);
1774 }
1775 
1776 void
1777 rum_update_slot(struct rum_softc *sc)
1778 {
1779 	struct ieee80211com *ic = &sc->sc_ic;
1780 	uint8_t slottime;
1781 	uint32_t tmp;
1782 
1783 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1784 
1785 	tmp = rum_read(sc, RT2573_MAC_CSR9);
1786 	tmp = (tmp & ~0xff) | slottime;
1787 	rum_write(sc, RT2573_MAC_CSR9, tmp);
1788 
1789 	DPRINTF(("setting slot time to %uus\n", slottime));
1790 }
1791 
1792 void
1793 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1794 {
1795 	uint32_t tmp;
1796 
1797 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1798 	rum_write(sc, RT2573_MAC_CSR4, tmp);
1799 
1800 	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1801 	rum_write(sc, RT2573_MAC_CSR5, tmp);
1802 }
1803 
1804 void
1805 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1806 {
1807 	uint32_t tmp;
1808 
1809 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1810 	rum_write(sc, RT2573_MAC_CSR2, tmp);
1811 
1812 	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1813 	rum_write(sc, RT2573_MAC_CSR3, tmp);
1814 }
1815 
1816 void
1817 rum_update_promisc(struct rum_softc *sc)
1818 {
1819 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1820 	uint32_t tmp;
1821 
1822 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1823 
1824 	tmp &= ~RT2573_DROP_NOT_TO_ME;
1825 	if (!(ifp->if_flags & IFF_PROMISC))
1826 		tmp |= RT2573_DROP_NOT_TO_ME;
1827 
1828 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1829 
1830 	DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1831 	    "entering" : "leaving"));
1832 }
1833 
1834 const char *
1835 rum_get_rf(int rev)
1836 {
1837 	switch (rev) {
1838 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
1839 	case RT2573_RF_2528:	return "RT2528";
1840 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
1841 	case RT2573_RF_5226:	return "RT5226";
1842 	default:		return "unknown";
1843 	}
1844 }
1845 
1846 void
1847 rum_read_eeprom(struct rum_softc *sc)
1848 {
1849 	struct ieee80211com *ic = &sc->sc_ic;
1850 	uint16_t val;
1851 #ifdef RUM_DEBUG
1852 	int i;
1853 #endif
1854 
1855 	/* read MAC/BBP type */
1856 	rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1857 	sc->macbbp_rev = letoh16(val);
1858 
1859 	/* read MAC address */
1860 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1861 
1862 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1863 	val = letoh16(val);
1864 	sc->rf_rev =   (val >> 11) & 0x1f;
1865 	sc->hw_radio = (val >> 10) & 0x1;
1866 	sc->rx_ant =   (val >> 4)  & 0x3;
1867 	sc->tx_ant =   (val >> 2)  & 0x3;
1868 	sc->nb_ant =   val & 0x3;
1869 
1870 	DPRINTF(("RF revision=%d\n", sc->rf_rev));
1871 
1872 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1873 	val = letoh16(val);
1874 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
1875 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
1876 
1877 	DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1878 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1879 
1880 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1881 	val = letoh16(val);
1882 	if ((val & 0xff) != 0xff)
1883 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
1884 
1885 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1886 	val = letoh16(val);
1887 	if ((val & 0xff) != 0xff)
1888 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
1889 
1890 	DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1891 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1892 
1893 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1894 	val = letoh16(val);
1895 	if ((val & 0xff) != 0xff)
1896 		sc->rffreq = val & 0xff;
1897 
1898 	DPRINTF(("RF freq=%d\n", sc->rffreq));
1899 
1900 	/* read Tx power for all a/b/g channels */
1901 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1902 	/* XXX default Tx power for 802.11a channels */
1903 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1904 #ifdef RUM_DEBUG
1905 	for (i = 0; i < 14; i++)
1906 		DPRINTF(("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]));
1907 #endif
1908 
1909 	/* read default values for BBP registers */
1910 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1911 #ifdef RUM_DEBUG
1912 	for (i = 0; i < 14; i++) {
1913 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1914 			continue;
1915 		DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1916 		    sc->bbp_prom[i].val));
1917 	}
1918 #endif
1919 }
1920 
1921 int
1922 rum_bbp_init(struct rum_softc *sc)
1923 {
1924 	int i, ntries;
1925 
1926 	/* wait for BBP to be ready */
1927 	for (ntries = 0; ntries < 100; ntries++) {
1928 		const uint8_t val = rum_bbp_read(sc, 0);
1929 		if (val != 0 && val != 0xff)
1930 			break;
1931 		DELAY(1000);
1932 	}
1933 	if (ntries == 100) {
1934 		printf("%s: timeout waiting for BBP\n",
1935 		    sc->sc_dev.dv_xname);
1936 		return EIO;
1937 	}
1938 
1939 	/* initialize BBP registers to default values */
1940 	for (i = 0; i < nitems(rum_def_bbp); i++)
1941 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1942 
1943 	/* write vendor-specific BBP values (from EEPROM) */
1944 	for (i = 0; i < 16; i++) {
1945 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1946 			continue;
1947 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1948 	}
1949 
1950 	return 0;
1951 }
1952 
1953 int
1954 rum_init(struct ifnet *ifp)
1955 {
1956 	struct rum_softc *sc = ifp->if_softc;
1957 	struct ieee80211com *ic = &sc->sc_ic;
1958 	uint32_t tmp;
1959 	usbd_status error;
1960 	int i, ntries;
1961 
1962 	rum_stop(ifp, 0);
1963 
1964 	/* initialize MAC registers to default values */
1965 	for (i = 0; i < nitems(rum_def_mac); i++)
1966 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1967 
1968 	/* set host ready */
1969 	rum_write(sc, RT2573_MAC_CSR1, 3);
1970 	rum_write(sc, RT2573_MAC_CSR1, 0);
1971 
1972 	/* wait for BBP/RF to wakeup */
1973 	for (ntries = 0; ntries < 1000; ntries++) {
1974 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1975 			break;
1976 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
1977 		DELAY(1000);
1978 	}
1979 	if (ntries == 1000) {
1980 		printf("%s: timeout waiting for BBP/RF to wakeup\n",
1981 		    sc->sc_dev.dv_xname);
1982 		error = ENODEV;
1983 		goto fail;
1984 	}
1985 
1986 	if ((error = rum_bbp_init(sc)) != 0)
1987 		goto fail;
1988 
1989 	/* select default channel */
1990 	sc->sc_curchan = ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1991 	rum_select_band(sc, sc->sc_curchan);
1992 	rum_select_antenna(sc);
1993 	rum_set_chan(sc, sc->sc_curchan);
1994 
1995 	/* clear STA registers */
1996 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1997 
1998 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1999 	rum_set_macaddr(sc, ic->ic_myaddr);
2000 
2001 	/* initialize ASIC */
2002 	rum_write(sc, RT2573_MAC_CSR1, 4);
2003 
2004 	/*
2005 	 * Allocate xfer for AMRR statistics requests.
2006 	 */
2007 	sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2008 	if (sc->amrr_xfer == NULL) {
2009 		printf("%s: could not allocate AMRR xfer\n",
2010 		    sc->sc_dev.dv_xname);
2011 		goto fail;
2012 	}
2013 
2014 	/*
2015 	 * Open Tx and Rx USB bulk pipes.
2016 	 */
2017 	error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2018 	    &sc->sc_tx_pipeh);
2019 	if (error != 0) {
2020 		printf("%s: could not open Tx pipe: %s\n",
2021 		    sc->sc_dev.dv_xname, usbd_errstr(error));
2022 		goto fail;
2023 	}
2024 	error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2025 	    &sc->sc_rx_pipeh);
2026 	if (error != 0) {
2027 		printf("%s: could not open Rx pipe: %s\n",
2028 		    sc->sc_dev.dv_xname, usbd_errstr(error));
2029 		goto fail;
2030 	}
2031 
2032 	/*
2033 	 * Allocate Tx and Rx xfer queues.
2034 	 */
2035 	error = rum_alloc_tx_list(sc);
2036 	if (error != 0) {
2037 		printf("%s: could not allocate Tx list\n",
2038 		    sc->sc_dev.dv_xname);
2039 		goto fail;
2040 	}
2041 	error = rum_alloc_rx_list(sc);
2042 	if (error != 0) {
2043 		printf("%s: could not allocate Rx list\n",
2044 		    sc->sc_dev.dv_xname);
2045 		goto fail;
2046 	}
2047 
2048 	/*
2049 	 * Start up the receive pipe.
2050 	 */
2051 	for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2052 		struct rum_rx_data *data = &sc->rx_data[i];
2053 
2054 		usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2055 		    MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2056 		error = usbd_transfer(data->xfer);
2057 		if (error != 0 && error != USBD_IN_PROGRESS) {
2058 			printf("%s: could not queue Rx transfer\n",
2059 			    sc->sc_dev.dv_xname);
2060 			goto fail;
2061 		}
2062 	}
2063 
2064 	/* update Rx filter */
2065 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2066 
2067 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2068 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2069 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2070 		       RT2573_DROP_ACKCTS;
2071 #ifndef IEEE80211_STA_ONLY
2072 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2073 #endif
2074 			tmp |= RT2573_DROP_TODS;
2075 		if (!(ifp->if_flags & IFF_PROMISC))
2076 			tmp |= RT2573_DROP_NOT_TO_ME;
2077 	}
2078 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2079 
2080 	ifp->if_flags &= ~IFF_OACTIVE;
2081 	ifp->if_flags |= IFF_RUNNING;
2082 
2083 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2084 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2085 	else
2086 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2087 
2088 	return 0;
2089 
2090 fail:	rum_stop(ifp, 1);
2091 	return error;
2092 }
2093 
2094 void
2095 rum_stop(struct ifnet *ifp, int disable)
2096 {
2097 	struct rum_softc *sc = ifp->if_softc;
2098 	struct ieee80211com *ic = &sc->sc_ic;
2099 	uint32_t tmp;
2100 
2101 	sc->sc_tx_timer = 0;
2102 	ifp->if_timer = 0;
2103 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2104 
2105 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
2106 
2107 	/* disable Rx */
2108 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
2109 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2110 
2111 	/* reset ASIC */
2112 	rum_write(sc, RT2573_MAC_CSR1, 3);
2113 	rum_write(sc, RT2573_MAC_CSR1, 0);
2114 
2115 	if (sc->amrr_xfer != NULL) {
2116 		usbd_free_xfer(sc->amrr_xfer);
2117 		sc->amrr_xfer = NULL;
2118 	}
2119 	if (sc->sc_rx_pipeh != NULL) {
2120 		usbd_abort_pipe(sc->sc_rx_pipeh);
2121 		usbd_close_pipe(sc->sc_rx_pipeh);
2122 		sc->sc_rx_pipeh = NULL;
2123 	}
2124 	if (sc->sc_tx_pipeh != NULL) {
2125 		usbd_abort_pipe(sc->sc_tx_pipeh);
2126 		usbd_close_pipe(sc->sc_tx_pipeh);
2127 		sc->sc_tx_pipeh = NULL;
2128 	}
2129 
2130 	rum_free_rx_list(sc);
2131 	rum_free_tx_list(sc);
2132 }
2133 
2134 int
2135 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2136 {
2137 	usb_device_request_t req;
2138 	uint16_t reg = RT2573_MCU_CODE_BASE;
2139 	usbd_status error;
2140 
2141 	/* copy firmware image into NIC */
2142 	for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2143 		rum_write(sc, reg, UGETDW(ucode));
2144 
2145 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2146 	req.bRequest = RT2573_MCU_CNTL;
2147 	USETW(req.wValue, RT2573_MCU_RUN);
2148 	USETW(req.wIndex, 0);
2149 	USETW(req.wLength, 0);
2150 
2151 	error = usbd_do_request(sc->sc_udev, &req, NULL);
2152 	if (error != 0) {
2153 		printf("%s: could not run firmware: %s\n",
2154 		    sc->sc_dev.dv_xname, usbd_errstr(error));
2155 	}
2156 	return error;
2157 }
2158 
2159 #ifndef IEEE80211_STA_ONLY
2160 int
2161 rum_prepare_beacon(struct rum_softc *sc)
2162 {
2163 	struct ieee80211com *ic = &sc->sc_ic;
2164 	struct rum_tx_desc desc;
2165 	struct mbuf *m0;
2166 	int rate;
2167 
2168 	m0 = ieee80211_beacon_alloc(ic, ic->ic_bss);
2169 	if (m0 == NULL) {
2170 		printf("%s: could not allocate beacon frame\n",
2171 		    sc->sc_dev.dv_xname);
2172 		return ENOBUFS;
2173 	}
2174 
2175 	/* send beacons at the lowest available rate */
2176 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2177 
2178 	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2179 	    m0->m_pkthdr.len, rate);
2180 
2181 	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2182 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2183 
2184 	/* copy beacon header and payload into NIC memory */
2185 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2186 	    m0->m_pkthdr.len);
2187 
2188 	m_freem(m0);
2189 
2190 	return 0;
2191 }
2192 #endif
2193 
2194 void
2195 rum_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
2196 {
2197 	/* start with lowest Tx rate */
2198 	ni->ni_txrate = 0;
2199 }
2200 
2201 void
2202 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2203 {
2204 	int i;
2205 
2206 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2207 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2208 
2209 	ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2210 
2211 	/* set rate to some reasonable initial value */
2212 	for (i = ni->ni_rates.rs_nrates - 1;
2213 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2214 	     i--);
2215 	ni->ni_txrate = i;
2216 
2217 	if (!usbd_is_dying(sc->sc_udev))
2218 		timeout_add_sec(&sc->amrr_to, 1);
2219 }
2220 
2221 void
2222 rum_amrr_timeout(void *arg)
2223 {
2224 	struct rum_softc *sc = arg;
2225 	usb_device_request_t req;
2226 
2227 	if (usbd_is_dying(sc->sc_udev))
2228 		return;
2229 
2230 	/*
2231 	 * Asynchronously read statistic registers (cleared by read).
2232 	 */
2233 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2234 	req.bRequest = RT2573_READ_MULTI_MAC;
2235 	USETW(req.wValue, 0);
2236 	USETW(req.wIndex, RT2573_STA_CSR0);
2237 	USETW(req.wLength, sizeof sc->sta);
2238 
2239 	usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2240 	    USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2241 	    rum_amrr_update);
2242 	(void)usbd_transfer(sc->amrr_xfer);
2243 }
2244 
2245 void
2246 rum_amrr_update(struct usbd_xfer *xfer, void *priv,
2247     usbd_status status)
2248 {
2249 	struct rum_softc *sc = (struct rum_softc *)priv;
2250 	struct ifnet *ifp = &sc->sc_ic.ic_if;
2251 
2252 	if (status != USBD_NORMAL_COMPLETION) {
2253 		printf("%s: could not retrieve Tx statistics - cancelling "
2254 		    "automatic rate control\n", sc->sc_dev.dv_xname);
2255 		return;
2256 	}
2257 
2258 	/* count TX retry-fail as Tx errors */
2259 	ifp->if_oerrors += letoh32(sc->sta[5]) >> 16;
2260 
2261 	sc->amn.amn_retrycnt =
2262 	    (letoh32(sc->sta[4]) >> 16) +	/* TX one-retry ok count */
2263 	    (letoh32(sc->sta[5]) & 0xffff) +	/* TX more-retry ok count */
2264 	    (letoh32(sc->sta[5]) >> 16);	/* TX retry-fail count */
2265 
2266 	sc->amn.amn_txcnt =
2267 	    sc->amn.amn_retrycnt +
2268 	    (letoh32(sc->sta[4]) & 0xffff);	/* TX no-retry ok count */
2269 
2270 	ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2271 
2272 	if (!usbd_is_dying(sc->sc_udev))
2273 		timeout_add_sec(&sc->amrr_to, 1);
2274 }
2275