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