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