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