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