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