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