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