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