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