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