The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/if_rum.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: releng/7.3/sys/dev/usb/if_rum.c 181863 2008-08-19 01:51:37Z kevlo $   */
    2 
    3 /*-
    4  * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
    5  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
    6  *
    7  * Permission to use, copy, modify, and distribute this software for any
    8  * purpose with or without fee is hereby granted, provided that the above
    9  * copyright notice and this permission notice appear in all copies.
   10  *
   11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   18  */
   19 
   20 #include <sys/cdefs.h>
   21 __FBSDID("$FreeBSD: releng/7.3/sys/dev/usb/if_rum.c 181863 2008-08-19 01:51:37Z kevlo $");
   22 
   23 /*-
   24  * Ralink Technology RT2501USB/RT2601USB chipset driver
   25  * http://www.ralinktech.com.tw/
   26  */
   27 
   28 #include <sys/param.h>
   29 #include <sys/sysctl.h>
   30 #include <sys/sockio.h>
   31 #include <sys/mbuf.h>
   32 #include <sys/kernel.h>
   33 #include <sys/socket.h>
   34 #include <sys/systm.h>
   35 #include <sys/malloc.h>
   36 #include <sys/module.h>
   37 #include <sys/bus.h>
   38 #include <sys/endian.h>
   39 
   40 #include <machine/bus.h>
   41 #include <machine/resource.h>
   42 #include <sys/rman.h>
   43 
   44 #include <net/bpf.h>
   45 #include <net/if.h>
   46 #include <net/if_arp.h>
   47 #include <net/ethernet.h>
   48 #include <net/if_dl.h>
   49 #include <net/if_media.h>
   50 #include <net/if_types.h>
   51 
   52 #include <net80211/ieee80211_var.h>
   53 #include <net80211/ieee80211_amrr.h>
   54 #include <net80211/ieee80211_radiotap.h>
   55 #include <net80211/ieee80211_regdomain.h>
   56 
   57 #include <dev/usb/usb.h>
   58 #include <dev/usb/usbdi.h>
   59 #include <dev/usb/usbdi_util.h>
   60 #include "usbdevs.h"
   61 
   62 #include <dev/usb/if_rumreg.h>
   63 #include <dev/usb/if_rumvar.h>
   64 #include <dev/usb/rt2573_ucode.h>
   65 
   66 #ifdef USB_DEBUG
   67 #define DPRINTF(x)      do { if (rumdebug > 0) printf x; } while (0)
   68 #define DPRINTFN(n, x)  do { if (rumdebug >= (n)) printf x; } while (0)
   69 int rumdebug = 0;
   70 SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
   71 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rumdebug, 0,
   72     "rum debug level");
   73 #else
   74 #define DPRINTF(x)
   75 #define DPRINTFN(n, x)
   76 #endif
   77 
   78 /* various supported device vendors/products */
   79 static const struct usb_devno rum_devs[] = {
   80         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_HWU54DM },
   81         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_2 },
   82         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_3 },
   83         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_4 },
   84         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_WUG2700 },
   85         { USB_VENDOR_AMIT,              USB_PRODUCT_AMIT_CGWLUSB2GO },
   86         { USB_VENDOR_ASUS,              USB_PRODUCT_ASUS_RT2573_1 },
   87         { USB_VENDOR_ASUS,              USB_PRODUCT_ASUS_RT2573_2 },
   88         { USB_VENDOR_BELKIN,            USB_PRODUCT_BELKIN_F5D7050A },
   89         { USB_VENDOR_BELKIN,            USB_PRODUCT_BELKIN_F5D9050V3 },
   90         { USB_VENDOR_CISCOLINKSYS,      USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
   91         { USB_VENDOR_CISCOLINKSYS,      USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
   92         { USB_VENDOR_CONCEPTRONIC2,     USB_PRODUCT_CONCEPTRONIC2_C54RU2 },
   93         { USB_VENDOR_DICKSMITH,         USB_PRODUCT_DICKSMITH_CWD854F },
   94         { USB_VENDOR_DICKSMITH,         USB_PRODUCT_DICKSMITH_RT2573 },
   95         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_DWLG122C1 },
   96         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_WUA1340 },
   97         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_DWA111 },
   98         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_DWA110 },
   99         { USB_VENDOR_GIGABYTE,          USB_PRODUCT_GIGABYTE_GNWB01GS },
  100         { USB_VENDOR_GIGABYTE,          USB_PRODUCT_GIGABYTE_GNWI05GS },
  101         { USB_VENDOR_GIGASET,           USB_PRODUCT_GIGASET_RT2573 },
  102         { USB_VENDOR_GOODWAY,           USB_PRODUCT_GOODWAY_RT2573 },
  103         { USB_VENDOR_GUILLEMOT,         USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
  104         { USB_VENDOR_GUILLEMOT,         USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
  105         { USB_VENDOR_HUAWEI3COM,        USB_PRODUCT_HUAWEI3COM_WUB320G },
  106         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_G54HP },
  107         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_SG54HP },
  108         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_1 },
  109         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_2 },
  110         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_3 },
  111         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_4 },
  112         { USB_VENDOR_NOVATECH,          USB_PRODUCT_NOVATECH_RT2573 },
  113         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUS54HP },
  114         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUS54MINI2 },
  115         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUSMM },
  116         { USB_VENDOR_QCOM,              USB_PRODUCT_QCOM_RT2573 },
  117         { USB_VENDOR_QCOM,              USB_PRODUCT_QCOM_RT2573_2 },
  118         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2573 },
  119         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2573_2 },
  120         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2671 },
  121         { USB_VENDOR_SITECOMEU,         USB_PRODUCT_SITECOMEU_WL113R2 },
  122         { USB_VENDOR_SITECOMEU,         USB_PRODUCT_SITECOMEU_WL172 },
  123         { USB_VENDOR_SPARKLAN,          USB_PRODUCT_SPARKLAN_RT2573 },
  124         { USB_VENDOR_SURECOM,           USB_PRODUCT_SURECOM_RT2573 }
  125 };
  126 
  127 MODULE_DEPEND(rum, wlan, 1, 1, 1);
  128 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
  129 MODULE_DEPEND(rum, usb, 1, 1, 1);
  130 
  131 static int              rum_alloc_tx_list(struct rum_softc *);
  132 static void             rum_free_tx_list(struct rum_softc *);
  133 static int              rum_alloc_rx_list(struct rum_softc *);
  134 static void             rum_free_rx_list(struct rum_softc *);
  135 static int              rum_media_change(struct ifnet *);
  136 static void             rum_task(void *);
  137 static void             rum_scantask(void *);
  138 static int              rum_newstate(struct ieee80211com *,
  139                             enum ieee80211_state, int);
  140 static void             rum_txeof(usbd_xfer_handle, usbd_private_handle,
  141                             usbd_status);
  142 static void             rum_rxeof(usbd_xfer_handle, usbd_private_handle,
  143                             usbd_status);
  144 static int              rum_rxrate(struct rum_rx_desc *);
  145 static int              rum_ack_rate(struct ieee80211com *, int);
  146 static uint16_t         rum_txtime(int, int, uint32_t);
  147 static uint8_t          rum_plcp_signal(int);
  148 static void             rum_setup_tx_desc(struct rum_softc *,
  149                             struct rum_tx_desc *, uint32_t, uint16_t, int,
  150                             int);
  151 static int              rum_tx_mgt(struct rum_softc *, struct mbuf *,
  152                             struct ieee80211_node *);
  153 static int              rum_tx_raw(struct rum_softc *, struct mbuf *,
  154                             struct ieee80211_node *, 
  155                             const struct ieee80211_bpf_params *);
  156 static int              rum_tx_data(struct rum_softc *, struct mbuf *,
  157                             struct ieee80211_node *);
  158 static void             rum_start(struct ifnet *);
  159 static void             rum_watchdog(void *);
  160 static int              rum_ioctl(struct ifnet *, u_long, caddr_t);
  161 static void             rum_eeprom_read(struct rum_softc *, uint16_t, void *,
  162                             int);
  163 static uint32_t         rum_read(struct rum_softc *, uint16_t);
  164 static void             rum_read_multi(struct rum_softc *, uint16_t, void *,
  165                             int);
  166 static void             rum_write(struct rum_softc *, uint16_t, uint32_t);
  167 static void             rum_write_multi(struct rum_softc *, uint16_t, void *,
  168                             size_t);
  169 static void             rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
  170 static uint8_t          rum_bbp_read(struct rum_softc *, uint8_t);
  171 static void             rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
  172 static void             rum_select_antenna(struct rum_softc *);
  173 static void             rum_enable_mrr(struct rum_softc *);
  174 static void             rum_set_txpreamble(struct rum_softc *);
  175 static void             rum_set_basicrates(struct rum_softc *);
  176 static void             rum_select_band(struct rum_softc *,
  177                             struct ieee80211_channel *);
  178 static void             rum_set_chan(struct rum_softc *,
  179                             struct ieee80211_channel *);
  180 static void             rum_enable_tsf_sync(struct rum_softc *);
  181 static void             rum_update_slot(struct ifnet *);
  182 static void             rum_set_bssid(struct rum_softc *, const uint8_t *);
  183 static void             rum_set_macaddr(struct rum_softc *, const uint8_t *);
  184 static void             rum_update_promisc(struct rum_softc *);
  185 static const char       *rum_get_rf(int);
  186 static void             rum_read_eeprom(struct rum_softc *);
  187 static int              rum_bbp_init(struct rum_softc *);
  188 static void             rum_init(void *);
  189 static void             rum_stop(void *);
  190 static int              rum_load_microcode(struct rum_softc *, const u_char *,
  191                             size_t);
  192 static int              rum_prepare_beacon(struct rum_softc *);
  193 static int              rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
  194                             const struct ieee80211_bpf_params *);
  195 static void             rum_scan_start(struct ieee80211com *);
  196 static void             rum_scan_end(struct ieee80211com *);
  197 static void             rum_set_channel(struct ieee80211com *);
  198 static int              rum_get_rssi(struct rum_softc *, uint8_t);
  199 static void             rum_amrr_start(struct rum_softc *,
  200                             struct ieee80211_node *);
  201 static void             rum_amrr_timeout(void *);
  202 static void             rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
  203                             usbd_status);
  204 
  205 static const struct {
  206         uint32_t        reg;
  207         uint32_t        val;
  208 } rum_def_mac[] = {
  209         { RT2573_TXRX_CSR0,  0x025fb032 },
  210         { RT2573_TXRX_CSR1,  0x9eaa9eaf },
  211         { RT2573_TXRX_CSR2,  0x8a8b8c8d }, 
  212         { RT2573_TXRX_CSR3,  0x00858687 },
  213         { RT2573_TXRX_CSR7,  0x2e31353b },
  214         { RT2573_TXRX_CSR8,  0x2a2a2a2c },
  215         { RT2573_TXRX_CSR15, 0x0000000f },
  216         { RT2573_MAC_CSR6,   0x00000fff },
  217         { RT2573_MAC_CSR8,   0x016c030a },
  218         { RT2573_MAC_CSR10,  0x00000718 },
  219         { RT2573_MAC_CSR12,  0x00000004 },
  220         { RT2573_MAC_CSR13,  0x00007f00 },
  221         { RT2573_SEC_CSR0,   0x00000000 },
  222         { RT2573_SEC_CSR1,   0x00000000 },
  223         { RT2573_SEC_CSR5,   0x00000000 },
  224         { RT2573_PHY_CSR1,   0x000023b0 },
  225         { RT2573_PHY_CSR5,   0x00040a06 },
  226         { RT2573_PHY_CSR6,   0x00080606 },
  227         { RT2573_PHY_CSR7,   0x00000408 },
  228         { RT2573_AIFSN_CSR,  0x00002273 },
  229         { RT2573_CWMIN_CSR,  0x00002344 },
  230         { RT2573_CWMAX_CSR,  0x000034aa }
  231 };
  232 
  233 static const struct {
  234         uint8_t reg;
  235         uint8_t val;
  236 } rum_def_bbp[] = {
  237         {   3, 0x80 },
  238         {  15, 0x30 },
  239         {  17, 0x20 },
  240         {  21, 0xc8 },
  241         {  22, 0x38 },
  242         {  23, 0x06 },
  243         {  24, 0xfe },
  244         {  25, 0x0a },
  245         {  26, 0x0d },
  246         {  32, 0x0b },
  247         {  34, 0x12 },
  248         {  37, 0x07 },
  249         {  39, 0xf8 },
  250         {  41, 0x60 },
  251         {  53, 0x10 },
  252         {  54, 0x18 },
  253         {  60, 0x10 },
  254         {  61, 0x04 },
  255         {  62, 0x04 },
  256         {  75, 0xfe },
  257         {  86, 0xfe },
  258         {  88, 0xfe },
  259         {  90, 0x0f },
  260         {  99, 0x00 },
  261         { 102, 0x16 },
  262         { 107, 0x04 }
  263 };
  264 
  265 static const struct rfprog {
  266         uint8_t         chan;
  267         uint32_t        r1, r2, r3, r4;
  268 }  rum_rf5226[] = {
  269         {   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
  270         {   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
  271         {   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
  272         {   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
  273         {   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
  274         {   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
  275         {   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
  276         {   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
  277         {   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
  278         {  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
  279         {  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
  280         {  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
  281         {  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
  282         {  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
  283 
  284         {  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
  285         {  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
  286         {  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
  287         {  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
  288 
  289         {  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
  290         {  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
  291         {  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
  292         {  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
  293         {  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
  294         {  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
  295         {  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
  296         {  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
  297 
  298         { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
  299         { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
  300         { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
  301         { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
  302         { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
  303         { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
  304         { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
  305         { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
  306         { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
  307         { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
  308         { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
  309 
  310         { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
  311         { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
  312         { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
  313         { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
  314         { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
  315 }, rum_rf5225[] = {
  316         {   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
  317         {   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
  318         {   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
  319         {   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
  320         {   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
  321         {   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
  322         {   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
  323         {   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
  324         {   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
  325         {  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
  326         {  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
  327         {  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
  328         {  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
  329         {  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
  330 
  331         {  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
  332         {  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
  333         {  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
  334         {  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
  335 
  336         {  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
  337         {  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
  338         {  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
  339         {  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
  340         {  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
  341         {  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
  342         {  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
  343         {  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
  344 
  345         { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
  346         { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
  347         { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
  348         { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
  349         { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
  350         { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
  351         { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
  352         { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
  353         { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
  354         { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
  355         { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
  356 
  357         { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
  358         { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
  359         { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
  360         { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
  361         { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
  362 };
  363 
  364 static int
  365 rum_match(device_t self)
  366 {
  367         struct usb_attach_arg *uaa = device_get_ivars(self);
  368 
  369         if (uaa->iface != NULL)
  370                 return UMATCH_NONE;
  371 
  372         return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
  373             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
  374 }
  375 
  376 static int
  377 rum_attach(device_t self)
  378 {
  379         struct rum_softc *sc = device_get_softc(self);
  380         struct usb_attach_arg *uaa = device_get_ivars(self);
  381         struct ieee80211com *ic = &sc->sc_ic;
  382         struct ifnet *ifp;
  383         const uint8_t *ucode = NULL;
  384         usb_interface_descriptor_t *id;
  385         usb_endpoint_descriptor_t *ed;
  386         usbd_status error;
  387         int i, ntries, size, bands;
  388         uint32_t tmp;
  389 
  390         sc->sc_udev = uaa->device;
  391         sc->sc_dev = self;
  392 
  393         if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
  394                 printf("%s: could not set configuration no\n",
  395                     device_get_nameunit(sc->sc_dev));
  396                 return ENXIO;
  397         }
  398 
  399         /* get the first interface handle */
  400         error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
  401             &sc->sc_iface);
  402         if (error != 0) {
  403                 printf("%s: could not get interface handle\n",
  404                     device_get_nameunit(sc->sc_dev));
  405                 return ENXIO;
  406         }
  407 
  408         /*
  409          * Find endpoints.
  410          */
  411         id = usbd_get_interface_descriptor(sc->sc_iface);
  412 
  413         sc->sc_rx_no = sc->sc_tx_no = -1;
  414         for (i = 0; i < id->bNumEndpoints; i++) {
  415                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
  416                 if (ed == NULL) {
  417                         printf("%s: no endpoint descriptor for iface %d\n",
  418                             device_get_nameunit(sc->sc_dev), i);
  419                         return ENXIO;
  420                 }
  421 
  422                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  423                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
  424                         sc->sc_rx_no = ed->bEndpointAddress;
  425                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
  426                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
  427                         sc->sc_tx_no = ed->bEndpointAddress;
  428         }
  429         if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
  430                 printf("%s: missing endpoint\n", 
  431                     device_get_nameunit(sc->sc_dev));
  432                 return ENXIO;
  433         }
  434 
  435         mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
  436             MTX_DEF | MTX_RECURSE);
  437 
  438         usb_init_task(&sc->sc_task, rum_task, sc);
  439         usb_init_task(&sc->sc_scantask, rum_scantask, sc);
  440         callout_init(&sc->watchdog_ch, 0);
  441         callout_init(&sc->amrr_ch, 0);
  442 
  443         /* retrieve RT2573 rev. no */
  444         for (ntries = 0; ntries < 1000; ntries++) {
  445                 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
  446                         break;
  447                 DELAY(1000);
  448         }
  449         if (ntries == 1000) {
  450                 printf("%s: timeout waiting for chip to settle\n",
  451                     device_get_nameunit(sc->sc_dev));
  452                 return ENXIO;
  453         }
  454 
  455         /* retrieve MAC address and various other things from EEPROM */
  456         rum_read_eeprom(sc);
  457 
  458         printf("%s: MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
  459             device_get_nameunit(sc->sc_dev), tmp, rum_get_rf(sc->rf_rev));
  460 
  461         ucode = rt2573_ucode;
  462         size = sizeof rt2573_ucode;
  463         error = rum_load_microcode(sc, ucode, size);
  464         if (error != 0) {
  465                 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
  466                 mtx_destroy(&sc->sc_mtx);
  467                 return ENXIO;
  468         }
  469 
  470         ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
  471         if (ifp == NULL) {
  472                 printf("%s: can not if_alloc()\n", 
  473                     device_get_nameunit(sc->sc_dev));
  474                 mtx_destroy(&sc->sc_mtx);
  475                 return ENXIO;
  476         }
  477 
  478         ifp->if_softc = sc;
  479         if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
  480         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
  481             IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
  482         ifp->if_init = rum_init;
  483         ifp->if_ioctl = rum_ioctl;
  484         ifp->if_start = rum_start;
  485         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
  486         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
  487         IFQ_SET_READY(&ifp->if_snd);
  488 
  489         ic->ic_ifp = ifp;
  490         ic->ic_phytype = IEEE80211_T_OFDM;      /* not only, but not used */
  491         ic->ic_opmode = IEEE80211_M_STA;        /* default to BSS mode */
  492         ic->ic_state = IEEE80211_S_INIT;
  493 
  494         /* set device capabilities */
  495         ic->ic_caps =
  496             IEEE80211_C_IBSS |          /* IBSS mode supported */
  497             IEEE80211_C_MONITOR |       /* monitor mode supported */
  498             IEEE80211_C_HOSTAP |        /* HostAp mode supported */
  499             IEEE80211_C_TXPMGT |        /* tx power management */
  500             IEEE80211_C_SHPREAMBLE |    /* short preamble supported */
  501             IEEE80211_C_SHSLOT |        /* short slot time supported */
  502             IEEE80211_C_BGSCAN |        /* bg scanning supported */
  503             IEEE80211_C_WPA;            /* 802.11i */
  504 
  505         bands = 0;
  506         setbit(&bands, IEEE80211_MODE_11B);
  507         setbit(&bands, IEEE80211_MODE_11G);
  508         ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
  509 
  510         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
  511                 struct ieee80211_channel *c;
  512 
  513                 /* set supported .11a channels */
  514                 for (i = 34; i <= 46; i += 4) {
  515                         c = &ic->ic_channels[ic->ic_nchans++];
  516                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
  517                         c->ic_flags = IEEE80211_CHAN_A;
  518                         c->ic_ieee = i;
  519                 }
  520                 for (i = 36; i <= 64; i += 4) {
  521                         c = &ic->ic_channels[ic->ic_nchans++];
  522                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
  523                         c->ic_flags = IEEE80211_CHAN_A;
  524                         c->ic_ieee = i;
  525                 }
  526                 for (i = 100; i <= 140; i += 4) {
  527                         c = &ic->ic_channels[ic->ic_nchans++];
  528                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
  529                         c->ic_flags = IEEE80211_CHAN_A;
  530                         c->ic_ieee = i;
  531                 }
  532                 for (i = 149; i <= 165; i += 4) {
  533                         c = &ic->ic_channels[ic->ic_nchans++];
  534                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
  535                         c->ic_flags = IEEE80211_CHAN_A;
  536                         c->ic_ieee = i;
  537                 }
  538         }
  539 
  540         ieee80211_ifattach(ic);
  541         ic->ic_scan_start = rum_scan_start;
  542         ic->ic_scan_end = rum_scan_end;
  543         ic->ic_set_channel = rum_set_channel;
  544 
  545         /* enable s/w bmiss handling in sta mode */
  546         ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
  547 
  548         /* override state transition machine */
  549         sc->sc_newstate = ic->ic_newstate;
  550         ic->ic_newstate = rum_newstate;
  551         ic->ic_raw_xmit = rum_raw_xmit;
  552         ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
  553 
  554         ieee80211_amrr_init(&sc->amrr, ic,
  555             IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
  556             IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD);
  557 
  558         bpfattach2(ifp, DLT_IEEE802_11_RADIO,
  559             sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, 
  560             &sc->sc_drvbpf);
  561 
  562         sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
  563         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
  564         sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
  565 
  566         sc->sc_txtap_len = sizeof sc->sc_txtapu;
  567         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
  568         sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
  569 
  570         if (bootverbose)
  571                 ieee80211_announce(ic);
  572 
  573         return 0;
  574 }
  575 
  576 static int
  577 rum_detach(device_t self)
  578 {
  579         struct rum_softc *sc = device_get_softc(self);
  580         struct ieee80211com *ic = &sc->sc_ic;
  581         struct ifnet *ifp = ic->ic_ifp;
  582 
  583         rum_stop(sc);
  584         usb_rem_task(sc->sc_udev, &sc->sc_task);
  585         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
  586         callout_stop(&sc->watchdog_ch);
  587         callout_stop(&sc->amrr_ch);
  588 
  589         if (sc->amrr_xfer != NULL) {
  590                 usbd_free_xfer(sc->amrr_xfer);
  591                 sc->amrr_xfer = NULL;
  592         }
  593 
  594         if (sc->sc_rx_pipeh != NULL) {
  595                 usbd_abort_pipe(sc->sc_rx_pipeh);
  596                 usbd_close_pipe(sc->sc_rx_pipeh);
  597         }
  598         if (sc->sc_tx_pipeh != NULL) {
  599                 usbd_abort_pipe(sc->sc_tx_pipeh);
  600                 usbd_close_pipe(sc->sc_tx_pipeh);
  601         }
  602         
  603         rum_free_rx_list(sc);
  604         rum_free_tx_list(sc);
  605 
  606         bpfdetach(ifp);
  607         ieee80211_ifdetach(ic);
  608         if_free(ifp);
  609 
  610         mtx_destroy(&sc->sc_mtx);
  611 
  612         return 0;
  613 }
  614 
  615 static int
  616 rum_alloc_tx_list(struct rum_softc *sc)
  617 {
  618         struct rum_tx_data *data;
  619         int i, error;
  620 
  621         sc->tx_queued = 0;
  622 
  623         for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
  624                 data = &sc->tx_data[i];
  625 
  626                 data->sc = sc;
  627 
  628                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
  629                 if (data->xfer == NULL) {
  630                         printf("%s: could not allocate tx xfer\n",
  631                             device_get_nameunit(sc->sc_dev));
  632                         error = ENOMEM;
  633                         goto fail;
  634                 }
  635                 data->buf = usbd_alloc_buffer(data->xfer,
  636                     RT2573_TX_DESC_SIZE + MCLBYTES);
  637                 if (data->buf == NULL) {
  638                         printf("%s: could not allocate tx buffer\n",
  639                             device_get_nameunit(sc->sc_dev));
  640                         error = ENOMEM;
  641                         goto fail;
  642                 }
  643                 /* clean Tx descriptor */
  644                 bzero(data->buf, RT2573_TX_DESC_SIZE);
  645         }
  646 
  647         return 0;
  648 
  649 fail:   rum_free_tx_list(sc);
  650         return error;
  651 }
  652 
  653 static void
  654 rum_free_tx_list(struct rum_softc *sc)
  655 {
  656         struct rum_tx_data *data;
  657         int i;
  658 
  659         for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
  660                 data = &sc->tx_data[i];
  661 
  662                 if (data->xfer != NULL) {
  663                         usbd_free_xfer(data->xfer);
  664                         data->xfer = NULL;
  665                 }
  666 
  667                 if (data->ni != NULL) {
  668                         ieee80211_free_node(data->ni);
  669                         data->ni = NULL;
  670                 }
  671         }
  672 }
  673 
  674 static int
  675 rum_alloc_rx_list(struct rum_softc *sc)
  676 {
  677         struct rum_rx_data *data;
  678         int i, error;
  679 
  680         for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
  681                 data = &sc->rx_data[i];
  682 
  683                 data->sc = sc;
  684 
  685                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
  686                 if (data->xfer == NULL) {
  687                         printf("%s: could not allocate rx xfer\n",
  688                             device_get_nameunit(sc->sc_dev));
  689                         error = ENOMEM;
  690                         goto fail;
  691                 }
  692                 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
  693                         printf("%s: could not allocate rx buffer\n",
  694                             device_get_nameunit(sc->sc_dev));
  695                         error = ENOMEM;
  696                         goto fail;
  697                 }
  698 
  699                 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
  700                 if (data->m == NULL) {
  701                         printf("%s: could not allocate rx mbuf\n",
  702                             device_get_nameunit(sc->sc_dev));
  703                         error = ENOMEM;
  704                         goto fail;
  705                 }
  706 
  707                 data->buf = mtod(data->m, uint8_t *);
  708         }
  709 
  710         return 0;
  711 
  712 fail:   rum_free_tx_list(sc);
  713         return error;
  714 }
  715 
  716 static void
  717 rum_free_rx_list(struct rum_softc *sc)
  718 {
  719         struct rum_rx_data *data;
  720         int i;
  721 
  722         for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
  723                 data = &sc->rx_data[i];
  724 
  725                 if (data->xfer != NULL) {
  726                         usbd_free_xfer(data->xfer);
  727                         data->xfer = NULL;
  728                 }
  729                 if (data->m != NULL) {
  730                         m_freem(data->m);
  731                         data->m = NULL;
  732                 }
  733         }
  734 }
  735 
  736 static int
  737 rum_media_change(struct ifnet *ifp)
  738 {
  739         struct rum_softc *sc = ifp->if_softc;
  740         int error;
  741 
  742         RUM_LOCK(sc);
  743 
  744         error = ieee80211_media_change(ifp);
  745         if (error != ENETRESET) {
  746                 RUM_UNLOCK(sc);
  747                 return error;
  748         }
  749 
  750         if ((ifp->if_flags & IFF_UP) &&
  751             (ifp->if_drv_flags & IFF_DRV_RUNNING))
  752                 rum_init(sc);
  753 
  754         RUM_UNLOCK(sc);
  755 
  756         return 0;
  757 }
  758 
  759 static void
  760 rum_task(void *arg)
  761 {
  762         struct rum_softc *sc = arg;
  763         struct ieee80211com *ic = &sc->sc_ic;
  764         enum ieee80211_state ostate;
  765         struct ieee80211_node *ni;
  766         uint32_t tmp;
  767 
  768         ostate = ic->ic_state;
  769 
  770         RUM_LOCK(sc);
  771 
  772         switch (sc->sc_state) {
  773         case IEEE80211_S_INIT:
  774                 if (ostate == IEEE80211_S_RUN) {
  775                         /* abort TSF synchronization */
  776                         tmp = rum_read(sc, RT2573_TXRX_CSR9);
  777                         rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
  778                 }
  779                 break;
  780 
  781         case IEEE80211_S_RUN:
  782                 ni = ic->ic_bss;
  783 
  784                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
  785                         rum_update_slot(ic->ic_ifp);
  786                         rum_enable_mrr(sc);
  787                         rum_set_txpreamble(sc);
  788                         rum_set_basicrates(sc);
  789                         rum_set_bssid(sc, ni->ni_bssid);
  790                 }
  791 
  792                 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
  793                     ic->ic_opmode == IEEE80211_M_IBSS)
  794                         rum_prepare_beacon(sc);
  795 
  796                 if (ic->ic_opmode != IEEE80211_M_MONITOR)
  797                         rum_enable_tsf_sync(sc);
  798 
  799                 /* enable automatic rate adaptation in STA mode */
  800                 if (ic->ic_opmode == IEEE80211_M_STA &&
  801                     ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
  802                         rum_amrr_start(sc, ni);
  803                 break;
  804         default:
  805                 break;
  806         }
  807 
  808         RUM_UNLOCK(sc);
  809 
  810         sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
  811 }
  812 
  813 static int
  814 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
  815 {
  816         struct rum_softc *sc = ic->ic_ifp->if_softc;
  817 
  818         callout_stop(&sc->amrr_ch);
  819 
  820         /* do it in a process context */
  821         sc->sc_state = nstate;
  822         sc->sc_arg = arg;
  823 
  824         usb_rem_task(sc->sc_udev, &sc->sc_task);
  825         if (nstate == IEEE80211_S_INIT)
  826                 sc->sc_newstate(ic, nstate, arg);
  827         else
  828                 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
  829         return 0;
  830 }
  831 
  832 /* quickly determine if a given rate is CCK or OFDM */
  833 #define RUM_RATE_IS_OFDM(rate)  ((rate) >= 12 && (rate) != 22)
  834 
  835 #define RUM_ACK_SIZE    14      /* 10 + 4(FCS) */
  836 #define RUM_CTS_SIZE    14      /* 10 + 4(FCS) */
  837 
  838 static void
  839 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
  840 {
  841         struct rum_tx_data *data = priv;
  842         struct rum_softc *sc = data->sc;
  843         struct ifnet *ifp = sc->sc_ic.ic_ifp;
  844 
  845         if (data->m->m_flags & M_TXCB)
  846                 ieee80211_process_callback(data->ni, data->m,
  847                         status == USBD_NORMAL_COMPLETION ? 0 : ETIMEDOUT);
  848 
  849         if (status != USBD_NORMAL_COMPLETION) {
  850                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
  851                         return;
  852 
  853                 printf("%s: could not transmit buffer: %s\n",
  854                     device_get_nameunit(sc->sc_dev), usbd_errstr(status));
  855 
  856                 if (status == USBD_STALLED)
  857                         usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
  858 
  859                 ifp->if_oerrors++;
  860                 return;
  861         }
  862 
  863         m_freem(data->m);
  864         data->m = NULL;
  865         ieee80211_free_node(data->ni);
  866         data->ni = NULL;
  867 
  868         sc->tx_queued--;
  869         ifp->if_opackets++;
  870 
  871         DPRINTFN(10, ("tx done\n"));
  872 
  873         sc->sc_tx_timer = 0;
  874         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  875         rum_start(ifp);
  876 }
  877 
  878 static void
  879 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
  880 {
  881         struct rum_rx_data *data = priv;
  882         struct rum_softc *sc = data->sc;
  883         struct ieee80211com *ic = &sc->sc_ic;
  884         struct ifnet *ifp = ic->ic_ifp;
  885         struct rum_rx_desc *desc;
  886         struct ieee80211_frame *wh;
  887         struct ieee80211_node *ni;
  888         struct mbuf *mnew, *m;
  889         int len, rssi;
  890 
  891         if (status != USBD_NORMAL_COMPLETION) {
  892                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
  893                         return;
  894 
  895                 if (status == USBD_STALLED)
  896                         usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
  897                 goto skip;
  898         }
  899 
  900         usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
  901 
  902         if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
  903                 DPRINTF(("%s: xfer too short %d\n", 
  904                     device_get_nameunit(sc->sc_dev), len));
  905                 ifp->if_ierrors++;
  906                 goto skip;
  907         }
  908 
  909         desc = (struct rum_rx_desc *)data->buf;
  910 
  911         if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
  912                 /*
  913                  * This should not happen since we did not request to receive
  914                  * those frames when we filled RT2573_TXRX_CSR0.
  915                  */
  916                 DPRINTFN(5, ("CRC error\n"));
  917                 ifp->if_ierrors++;
  918                 goto skip;
  919         }
  920 
  921         mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
  922         if (mnew == NULL) {
  923                 ifp->if_ierrors++;
  924                 goto skip;
  925         }
  926 
  927         m = data->m;
  928         data->m = mnew;
  929         data->buf = mtod(data->m, uint8_t *);
  930 
  931         /* finalize mbuf */
  932         m->m_pkthdr.rcvif = ifp;
  933         m->m_data = (caddr_t)(desc + 1); 
  934         m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
  935 
  936         rssi = rum_get_rssi(sc, desc->rssi);
  937 
  938         wh = mtod(m, struct ieee80211_frame *);
  939         ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
  940 
  941         /* Error happened during RSSI conversion. */
  942         if (rssi < 0)
  943                 rssi = ni->ni_rssi;
  944 
  945         if (bpf_peers_present(sc->sc_drvbpf)) {
  946                 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
  947 
  948                 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
  949                 tap->wr_rate = rum_rxrate(desc);
  950                 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
  951                 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
  952                 tap->wr_antenna = sc->rx_ant;
  953                 tap->wr_antsignal = rssi;
  954 
  955                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
  956         }
  957 
  958         /* send the frame to the 802.11 layer */
  959         ieee80211_input(ic, m, ni, rssi, RT2573_NOISE_FLOOR, 0);
  960 
  961         /* node is no longer needed */
  962         ieee80211_free_node(ni);
  963 
  964         DPRINTFN(15, ("rx done\n"));
  965 
  966 skip:   /* setup a new transfer */
  967         usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
  968             USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
  969         usbd_transfer(xfer);
  970 }
  971 
  972 /*
  973  * This function is only used by the Rx radiotap code. 
  974  */
  975 static int
  976 rum_rxrate(struct rum_rx_desc *desc)
  977 {
  978         if (le32toh(desc->flags) & RT2573_RX_OFDM) {
  979                 /* reverse function of rum_plcp_signal */
  980                 switch (desc->rate) {
  981                 case 0xb:       return 12;
  982                 case 0xf:       return 18;
  983                 case 0xa:       return 24;
  984                 case 0xe:       return 36;
  985                 case 0x9:       return 48;
  986                 case 0xd:       return 72;
  987                 case 0x8:       return 96;
  988                 case 0xc:       return 108;
  989                 }
  990         } else {
  991                 if (desc->rate == 10)
  992                         return 2;
  993                 if (desc->rate == 20)
  994                         return 4;
  995                 if (desc->rate == 55)
  996                         return 11;
  997                 if (desc->rate == 110)
  998                         return 22;
  999         }
 1000         return 2;       /* should not get there */
 1001 }
 1002 
 1003 /*
 1004  * Return the expected ack rate for a frame transmitted at rate `rate'.
 1005  */
 1006 static int
 1007 rum_ack_rate(struct ieee80211com *ic, int rate)
 1008 {
 1009         switch (rate) {
 1010         /* CCK rates */
 1011         case 2:
 1012                 return 2;
 1013         case 4:
 1014         case 11:
 1015         case 22:
 1016                 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
 1017 
 1018         /* OFDM rates */
 1019         case 12:
 1020         case 18:
 1021                 return 12;
 1022         case 24:
 1023         case 36:
 1024                 return 24;
 1025         case 48:
 1026         case 72:
 1027         case 96:
 1028         case 108:
 1029                 return 48;
 1030         }
 1031 
 1032         /* default to 1Mbps */
 1033         return 2;
 1034 }
 1035 
 1036 /*
 1037  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
 1038  * The function automatically determines the operating mode depending on the
 1039  * given rate. `flags' indicates whether short preamble is in use or not.
 1040  */
 1041 static uint16_t
 1042 rum_txtime(int len, int rate, uint32_t flags)
 1043 {
 1044         uint16_t txtime;
 1045 
 1046         if (RUM_RATE_IS_OFDM(rate)) {
 1047                 /* IEEE Std 802.11a-1999, pp. 37 */
 1048                 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
 1049                 txtime = 16 + 4 + 4 * txtime + 6;
 1050         } else {
 1051                 /* IEEE Std 802.11b-1999, pp. 28 */
 1052                 txtime = (16 * len + rate - 1) / rate;
 1053                 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
 1054                         txtime +=  72 + 24;
 1055                 else
 1056                         txtime += 144 + 48;
 1057         }
 1058         return txtime;
 1059 }
 1060 
 1061 static uint8_t
 1062 rum_plcp_signal(int rate)
 1063 {
 1064         switch (rate) {
 1065         /* CCK rates (returned values are device-dependent) */
 1066         case 2:         return 0x0;
 1067         case 4:         return 0x1;
 1068         case 11:        return 0x2;
 1069         case 22:        return 0x3;
 1070 
 1071         /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
 1072         case 12:        return 0xb;
 1073         case 18:        return 0xf;
 1074         case 24:        return 0xa;
 1075         case 36:        return 0xe;
 1076         case 48:        return 0x9;
 1077         case 72:        return 0xd;
 1078         case 96:        return 0x8;
 1079         case 108:       return 0xc;
 1080 
 1081         /* unsupported rates (should not get there) */
 1082         default:        return 0xff;
 1083         }
 1084 }
 1085 
 1086 static void
 1087 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
 1088     uint32_t flags, uint16_t xflags, int len, int rate)
 1089 {
 1090         struct ieee80211com *ic = &sc->sc_ic;
 1091         uint16_t plcp_length;
 1092         int remainder;
 1093 
 1094         desc->flags = htole32(flags);
 1095         desc->flags |= htole32(RT2573_TX_VALID);
 1096         desc->flags |= htole32(len << 16);
 1097 
 1098         desc->xflags = htole16(xflags);
 1099 
 1100         desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) | 
 1101             RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
 1102 
 1103         /* setup PLCP fields */
 1104         desc->plcp_signal  = rum_plcp_signal(rate);
 1105         desc->plcp_service = 4;
 1106 
 1107         len += IEEE80211_CRC_LEN;
 1108         if (RUM_RATE_IS_OFDM(rate)) {
 1109                 desc->flags |= htole32(RT2573_TX_OFDM);
 1110 
 1111                 plcp_length = len & 0xfff;
 1112                 desc->plcp_length_hi = plcp_length >> 6;
 1113                 desc->plcp_length_lo = plcp_length & 0x3f;
 1114         } else {
 1115                 plcp_length = (16 * len + rate - 1) / rate;
 1116                 if (rate == 22) {
 1117                         remainder = (16 * len) % 22;
 1118                         if (remainder != 0 && remainder < 7)
 1119                                 desc->plcp_service |= RT2573_PLCP_LENGEXT;
 1120                 }
 1121                 desc->plcp_length_hi = plcp_length >> 8;
 1122                 desc->plcp_length_lo = plcp_length & 0xff;
 1123 
 1124                 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
 1125                         desc->plcp_signal |= 0x08;
 1126         }
 1127 }
 1128 
 1129 #define RUM_TX_TIMEOUT  5000
 1130 
 1131 static int
 1132 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
 1133 {
 1134         struct ieee80211com *ic = &sc->sc_ic;
 1135         struct rum_tx_desc *desc;
 1136         struct rum_tx_data *data;
 1137         struct ieee80211_frame *wh;
 1138         uint32_t flags = 0;
 1139         uint16_t dur;
 1140         usbd_status error;
 1141         int xferlen, rate;
 1142 
 1143         data = &sc->tx_data[0];
 1144         desc = (struct rum_tx_desc *)data->buf;
 1145 
 1146         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
 1147 
 1148         data->m = m0;
 1149         data->ni = ni;
 1150 
 1151         wh = mtod(m0, struct ieee80211_frame *);
 1152 
 1153         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
 1154                 flags |= RT2573_TX_NEED_ACK;
 1155 
 1156                 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate), 
 1157                     ic->ic_flags) + sc->sifs;
 1158                 *(uint16_t *)wh->i_dur = htole16(dur);
 1159 
 1160                 /* tell hardware to add timestamp for probe responses */
 1161                 if ((wh->i_fc[0] &
 1162                     (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
 1163                     (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
 1164                         flags |= RT2573_TX_TIMESTAMP;
 1165         }
 1166 
 1167         if (bpf_peers_present(sc->sc_drvbpf)) {
 1168                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
 1169 
 1170                 tap->wt_flags = 0;
 1171                 tap->wt_rate = rate;
 1172                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
 1173                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
 1174                 tap->wt_antenna = sc->tx_ant;
 1175 
 1176                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
 1177         }
 1178 
 1179         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
 1180         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
 1181 
 1182         /* align end on a 4-bytes boundary */
 1183         xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
 1184 
 1185         /*
 1186          * No space left in the last URB to store the extra 4 bytes, force
 1187          * sending of another URB.
 1188          */
 1189         if ((xferlen % 64) == 0)
 1190                 xferlen += 4;
 1191 
 1192         DPRINTFN(10, ("sending mgt frame len=%d rate=%d xfer len=%d\n",
 1193             m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
 1194         
 1195         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
 1196             USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
 1197 
 1198         error = usbd_transfer(data->xfer);
 1199         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
 1200                 m_freem(m0);
 1201                 data->m = NULL;
 1202                 data->ni = NULL;
 1203                 return error;
 1204         }
 1205 
 1206         sc->tx_queued++;
 1207 
 1208         return 0;
 1209 }
 1210 
 1211 static int
 1212 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
 1213     const struct ieee80211_bpf_params *params)
 1214 {
 1215         struct ieee80211com *ic = &sc->sc_ic;
 1216         struct rum_tx_desc *desc;
 1217         struct rum_tx_data *data;
 1218         uint32_t flags;
 1219         usbd_status error;
 1220         int xferlen, rate;
 1221 
 1222         data = &sc->tx_data[0];
 1223         desc = (struct rum_tx_desc *)data->buf;
 1224 
 1225         rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
 1226         /* XXX validate */
 1227         if (rate == 0) {
 1228                 m_freem(m0);
 1229                 return EINVAL;
 1230         }
 1231 
 1232         if (bpf_peers_present(sc->sc_drvbpf)) {
 1233                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
 1234 
 1235                 tap->wt_flags = 0;
 1236                 tap->wt_rate = rate;
 1237                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
 1238                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
 1239                 tap->wt_antenna = sc->tx_ant;
 1240 
 1241                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
 1242         }
 1243 
 1244         data->m = m0;
 1245         data->ni = ni;
 1246 
 1247         flags = 0;
 1248         if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
 1249                 flags |= RT2573_TX_NEED_ACK;
 1250 
 1251         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
 1252         /* XXX need to setup descriptor ourself */
 1253         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
 1254 
 1255         /* align end on a 4-bytes boundary */
 1256         xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
 1257 
 1258         /*
 1259          * No space left in the last URB to store the extra 4 bytes, force
 1260          * sending of another URB.
 1261          */
 1262         if ((xferlen % 64) == 0)
 1263                 xferlen += 4;
 1264 
 1265         DPRINTFN(10, ("sending raw frame len=%u rate=%u xfer len=%u\n",
 1266             m0->m_pkthdr.len, rate, xferlen));
 1267 
 1268         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
 1269             xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT,
 1270             rum_txeof);
 1271 
 1272         error = usbd_transfer(data->xfer);
 1273         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
 1274                 return error;
 1275 
 1276         sc->tx_queued++;
 1277 
 1278         return 0;
 1279 }
 1280 
 1281 static int
 1282 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
 1283 {
 1284         struct ieee80211com *ic = &sc->sc_ic;
 1285         struct rum_tx_desc *desc;
 1286         struct rum_tx_data *data;
 1287         struct ieee80211_frame *wh;
 1288         struct ieee80211_key *k;
 1289         uint32_t flags = 0;
 1290         uint16_t dur;
 1291         usbd_status error;
 1292         int rate, xferlen;
 1293 
 1294         wh = mtod(m0, struct ieee80211_frame *);
 1295 
 1296         if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
 1297                 rate = ic->ic_fixed_rate;
 1298         else
 1299                 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
 1300 
 1301         rate &= IEEE80211_RATE_VAL;
 1302 
 1303         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
 1304                 k = ieee80211_crypto_encap(ic, ni, m0);
 1305                 if (k == NULL) {
 1306                         m_freem(m0);
 1307                         return ENOBUFS;
 1308                 }
 1309 
 1310                 /* packet header may have moved, reset our local pointer */
 1311                 wh = mtod(m0, struct ieee80211_frame *);
 1312         }
 1313 
 1314         data = &sc->tx_data[0];
 1315         desc = (struct rum_tx_desc *)data->buf;
 1316 
 1317         data->m = m0;
 1318         data->ni = ni;
 1319 
 1320         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
 1321                 flags |= RT2573_TX_NEED_ACK;
 1322                 flags |= RT2573_TX_MORE_FRAG;
 1323 
 1324                 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
 1325                     ic->ic_flags) + sc->sifs;
 1326                 *(uint16_t *)wh->i_dur = htole16(dur);
 1327         }
 1328 
 1329         if (bpf_peers_present(sc->sc_drvbpf)) {
 1330                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
 1331 
 1332                 tap->wt_flags = 0;
 1333                 tap->wt_rate = rate;
 1334                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
 1335                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
 1336                 tap->wt_antenna = sc->tx_ant;
 1337 
 1338                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
 1339         }
 1340 
 1341         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
 1342         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
 1343 
 1344         /* align end on a 4-bytes boundary */
 1345         xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
 1346 
 1347         /*
 1348          * No space left in the last URB to store the extra 4 bytes, force
 1349          * sending of another URB.
 1350          */
 1351         if ((xferlen % 64) == 0)
 1352                 xferlen += 4;
 1353 
 1354         DPRINTFN(10, ("sending frame len=%d rate=%d xfer len=%d\n",
 1355             m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
 1356 
 1357         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
 1358             USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
 1359 
 1360         error = usbd_transfer(data->xfer);
 1361         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
 1362                 m_freem(m0);
 1363                 data->m = NULL;
 1364                 data->ni = NULL;
 1365                 return error;
 1366         }
 1367 
 1368         sc->tx_queued++;
 1369 
 1370         return 0;
 1371 }
 1372 
 1373 static void
 1374 rum_start(struct ifnet *ifp)
 1375 {
 1376         struct rum_softc *sc = ifp->if_softc;
 1377         struct ieee80211com *ic = &sc->sc_ic;
 1378         struct ieee80211_node *ni;
 1379         struct mbuf *m0;
 1380         struct ether_header *eh;
 1381 
 1382         for (;;) {
 1383                 IF_POLL(&ic->ic_mgtq, m0);
 1384                 if (m0 != NULL) {
 1385                         if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
 1386                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1387                                 break;
 1388                         }
 1389                         IF_DEQUEUE(&ic->ic_mgtq, m0);
 1390 
 1391                         ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
 1392                         m0->m_pkthdr.rcvif = NULL;
 1393 
 1394                         if (bpf_peers_present(ic->ic_rawbpf))
 1395                                 bpf_mtap(ic->ic_rawbpf, m0);
 1396 
 1397                         if (rum_tx_mgt(sc, m0, ni) != 0) {
 1398                                 ieee80211_free_node(ni);
 1399                                 break;
 1400                         }
 1401                 } else {
 1402                         if (ic->ic_state != IEEE80211_S_RUN)
 1403                                 break;
 1404                         IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
 1405                         if (m0 == NULL)
 1406                                 break;
 1407                         if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
 1408                                 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
 1409                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1410                                 break;
 1411                         }
 1412                         /*
 1413                          * Cancel any background scan.
 1414                          */
 1415                         if (ic->ic_flags & IEEE80211_F_SCAN)
 1416                                 ieee80211_cancel_scan(ic);
 1417 
 1418                         if (m0->m_len < sizeof (struct ether_header) &&
 1419                             !(m0 = m_pullup(m0, sizeof (struct ether_header))))
 1420                                 continue;
 1421 
 1422                         eh = mtod(m0, struct ether_header *);
 1423                         ni = ieee80211_find_txnode(ic, eh->ether_dhost);
 1424                         if (ni == NULL) {
 1425                                 m_freem(m0);
 1426                                 continue;
 1427                         }
 1428                         BPF_MTAP(ifp, m0);
 1429 
 1430                         m0 = ieee80211_encap(ic, m0, ni);
 1431                         if (m0 == NULL) {
 1432                                 ieee80211_free_node(ni);
 1433                                 continue;
 1434                         }
 1435 
 1436                         if (bpf_peers_present(ic->ic_rawbpf))
 1437                                 bpf_mtap(ic->ic_rawbpf, m0);
 1438 
 1439                         if (rum_tx_data(sc, m0, ni) != 0) {
 1440                                 ieee80211_free_node(ni);
 1441                                 ifp->if_oerrors++;
 1442                                 break;
 1443                         }
 1444                 }
 1445 
 1446                 sc->sc_tx_timer = 5;
 1447                 ic->ic_lastdata = ticks;
 1448                 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
 1449         }
 1450 }
 1451 
 1452 static void
 1453 rum_watchdog(void *arg)
 1454 {
 1455         struct rum_softc *sc = arg;
 1456 
 1457         RUM_LOCK(sc);
 1458 
 1459         if (sc->sc_tx_timer > 0) {
 1460                 if (--sc->sc_tx_timer == 0) {
 1461                         device_printf(sc->sc_dev, "device timeout\n");
 1462                         /*rum_init(ifp); XXX needs a process context! */
 1463                         sc->sc_ifp->if_oerrors++;
 1464                         RUM_UNLOCK(sc);
 1465                         return;
 1466                 }
 1467                 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
 1468         }
 1469 
 1470         RUM_UNLOCK(sc);
 1471 }
 1472 
 1473 static int
 1474 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 1475 {
 1476         struct rum_softc *sc = ifp->if_softc;
 1477         struct ieee80211com *ic = &sc->sc_ic;
 1478         int error = 0;
 1479 
 1480         RUM_LOCK(sc);
 1481 
 1482         switch (cmd) {
 1483         case SIOCSIFFLAGS:
 1484                 if (ifp->if_flags & IFF_UP) {
 1485                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1486                                 rum_update_promisc(sc);
 1487                         else
 1488                                 rum_init(sc);
 1489                 } else {
 1490                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1491                                 rum_stop(sc);
 1492                 }
 1493                 break;
 1494         default:
 1495                 error = ieee80211_ioctl(ic, cmd, data);
 1496         }
 1497 
 1498         if (error == ENETRESET) {
 1499                 if ((ifp->if_flags & IFF_UP) &&
 1500                     (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
 1501                     (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
 1502                         rum_init(sc);
 1503                 error = 0;
 1504         }
 1505 
 1506         RUM_UNLOCK(sc);
 1507 
 1508         return error;
 1509 }
 1510 
 1511 static void
 1512 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
 1513 {
 1514         usb_device_request_t req;
 1515         usbd_status error;
 1516 
 1517         req.bmRequestType = UT_READ_VENDOR_DEVICE;
 1518         req.bRequest = RT2573_READ_EEPROM;
 1519         USETW(req.wValue, 0);
 1520         USETW(req.wIndex, addr);
 1521         USETW(req.wLength, len);
 1522 
 1523         error = usbd_do_request(sc->sc_udev, &req, buf);
 1524         if (error != 0) {
 1525                 printf("%s: could not read EEPROM: %s\n",
 1526                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
 1527         }
 1528 }
 1529 
 1530 static uint32_t
 1531 rum_read(struct rum_softc *sc, uint16_t reg)
 1532 {
 1533         uint32_t val;
 1534 
 1535         rum_read_multi(sc, reg, &val, sizeof val);
 1536 
 1537         return le32toh(val);
 1538 }
 1539 
 1540 static void
 1541 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
 1542 {
 1543         usb_device_request_t req;
 1544         usbd_status error;
 1545 
 1546         req.bmRequestType = UT_READ_VENDOR_DEVICE;
 1547         req.bRequest = RT2573_READ_MULTI_MAC;
 1548         USETW(req.wValue, 0);
 1549         USETW(req.wIndex, reg);
 1550         USETW(req.wLength, len);
 1551 
 1552         error = usbd_do_request(sc->sc_udev, &req, buf);
 1553         if (error != 0) {
 1554                 printf("%s: could not multi read MAC register: %s\n",
 1555                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
 1556         }
 1557 }
 1558 
 1559 static void
 1560 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
 1561 {
 1562         uint32_t tmp = htole32(val);
 1563 
 1564         rum_write_multi(sc, reg, &tmp, sizeof tmp);
 1565 }
 1566 
 1567 static void
 1568 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
 1569 {
 1570         usb_device_request_t req;
 1571         usbd_status error;
 1572 
 1573         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
 1574         req.bRequest = RT2573_WRITE_MULTI_MAC;
 1575         USETW(req.wValue, 0);
 1576         USETW(req.wIndex, reg);
 1577         USETW(req.wLength, len);
 1578 
 1579         error = usbd_do_request(sc->sc_udev, &req, buf);
 1580         if (error != 0) {
 1581                 printf("%s: could not multi write MAC register: %s\n",
 1582                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
 1583         }
 1584 }
 1585 
 1586 static void
 1587 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
 1588 {
 1589         uint32_t tmp;
 1590         int ntries;
 1591 
 1592         for (ntries = 0; ntries < 5; ntries++) {
 1593                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
 1594                         break;
 1595         }
 1596         if (ntries == 5) {
 1597                 printf("%s: could not write to BBP\n", 
 1598                     device_get_nameunit(sc->sc_dev));
 1599                 return;
 1600         }
 1601 
 1602         tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
 1603         rum_write(sc, RT2573_PHY_CSR3, tmp);
 1604 }
 1605 
 1606 static uint8_t
 1607 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
 1608 {
 1609         uint32_t val;
 1610         int ntries;
 1611 
 1612         for (ntries = 0; ntries < 5; ntries++) {
 1613                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
 1614                         break;
 1615         }
 1616         if (ntries == 5) {
 1617                 printf("%s: could not read BBP\n", 
 1618                     device_get_nameunit(sc->sc_dev));
 1619                 return 0;
 1620         }
 1621 
 1622         val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
 1623         rum_write(sc, RT2573_PHY_CSR3, val);
 1624 
 1625         for (ntries = 0; ntries < 100; ntries++) {
 1626                 val = rum_read(sc, RT2573_PHY_CSR3);
 1627                 if (!(val & RT2573_BBP_BUSY))
 1628                         return val & 0xff;
 1629                 DELAY(1);
 1630         }
 1631 
 1632         printf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
 1633         return 0;
 1634 }
 1635 
 1636 static void
 1637 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
 1638 {
 1639         uint32_t tmp;
 1640         int ntries;
 1641 
 1642         for (ntries = 0; ntries < 5; ntries++) {
 1643                 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
 1644                         break;
 1645         }
 1646         if (ntries == 5) {
 1647                 printf("%s: could not write to RF\n", 
 1648                     device_get_nameunit(sc->sc_dev));
 1649                 return;
 1650         }
 1651 
 1652         tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
 1653             (reg & 3);
 1654         rum_write(sc, RT2573_PHY_CSR4, tmp);
 1655 
 1656         /* remember last written value in sc */
 1657         sc->rf_regs[reg] = val;
 1658 
 1659         DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
 1660 }
 1661 
 1662 static void
 1663 rum_select_antenna(struct rum_softc *sc)
 1664 {
 1665         uint8_t bbp4, bbp77;
 1666         uint32_t tmp;
 1667 
 1668         bbp4  = rum_bbp_read(sc, 4);
 1669         bbp77 = rum_bbp_read(sc, 77);
 1670 
 1671         /* TBD */
 1672 
 1673         /* make sure Rx is disabled before switching antenna */
 1674         tmp = rum_read(sc, RT2573_TXRX_CSR0);
 1675         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
 1676 
 1677         rum_bbp_write(sc,  4, bbp4);
 1678         rum_bbp_write(sc, 77, bbp77);
 1679 
 1680         rum_write(sc, RT2573_TXRX_CSR0, tmp);
 1681 }
 1682 
 1683 /*
 1684  * Enable multi-rate retries for frames sent at OFDM rates.
 1685  * In 802.11b/g mode, allow fallback to CCK rates.
 1686  */
 1687 static void
 1688 rum_enable_mrr(struct rum_softc *sc)
 1689 {
 1690         struct ieee80211com *ic = &sc->sc_ic;
 1691         uint32_t tmp;
 1692 
 1693         tmp = rum_read(sc, RT2573_TXRX_CSR4);
 1694 
 1695         tmp &= ~RT2573_MRR_CCK_FALLBACK;
 1696         if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
 1697                 tmp |= RT2573_MRR_CCK_FALLBACK;
 1698         tmp |= RT2573_MRR_ENABLED;
 1699 
 1700         rum_write(sc, RT2573_TXRX_CSR4, tmp);
 1701 }
 1702 
 1703 static void
 1704 rum_set_txpreamble(struct rum_softc *sc)
 1705 {
 1706         uint32_t tmp;
 1707 
 1708         tmp = rum_read(sc, RT2573_TXRX_CSR4);
 1709 
 1710         tmp &= ~RT2573_SHORT_PREAMBLE;
 1711         if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
 1712                 tmp |= RT2573_SHORT_PREAMBLE;
 1713 
 1714         rum_write(sc, RT2573_TXRX_CSR4, tmp);
 1715 }
 1716 
 1717 static void
 1718 rum_set_basicrates(struct rum_softc *sc)
 1719 {
 1720         struct ieee80211com *ic = &sc->sc_ic;
 1721 
 1722         /* update basic rate set */
 1723         if (ic->ic_curmode == IEEE80211_MODE_11B) {
 1724                 /* 11b basic rates: 1, 2Mbps */
 1725                 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
 1726         } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
 1727                 /* 11a basic rates: 6, 12, 24Mbps */
 1728                 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
 1729         } else {
 1730                 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
 1731                 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
 1732         }
 1733 }
 1734 
 1735 /*
 1736  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
 1737  * driver.
 1738  */
 1739 static void
 1740 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
 1741 {
 1742         uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
 1743         uint32_t tmp;
 1744 
 1745         /* update all BBP registers that depend on the band */
 1746         bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
 1747         bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
 1748         if (IEEE80211_IS_CHAN_5GHZ(c)) {
 1749                 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
 1750                 bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
 1751         }
 1752         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
 1753             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
 1754                 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
 1755         }
 1756 
 1757         sc->bbp17 = bbp17;
 1758         rum_bbp_write(sc,  17, bbp17);
 1759         rum_bbp_write(sc,  96, bbp96);
 1760         rum_bbp_write(sc, 104, bbp104);
 1761 
 1762         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
 1763             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
 1764                 rum_bbp_write(sc, 75, 0x80);
 1765                 rum_bbp_write(sc, 86, 0x80);
 1766                 rum_bbp_write(sc, 88, 0x80);
 1767         }
 1768 
 1769         rum_bbp_write(sc, 35, bbp35);
 1770         rum_bbp_write(sc, 97, bbp97);
 1771         rum_bbp_write(sc, 98, bbp98);
 1772 
 1773         tmp = rum_read(sc, RT2573_PHY_CSR0);
 1774         tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
 1775         if (IEEE80211_IS_CHAN_2GHZ(c))
 1776                 tmp |= RT2573_PA_PE_2GHZ;
 1777         else
 1778                 tmp |= RT2573_PA_PE_5GHZ;
 1779         rum_write(sc, RT2573_PHY_CSR0, tmp);
 1780 
 1781         /* 802.11a uses a 16 microseconds short interframe space */
 1782         sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
 1783 }
 1784 
 1785 static void
 1786 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
 1787 {
 1788         struct ieee80211com *ic = &sc->sc_ic;
 1789         const struct rfprog *rfprog;
 1790         uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
 1791         int8_t power;
 1792         u_int i, chan;
 1793 
 1794         chan = ieee80211_chan2ieee(ic, c);
 1795         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
 1796                 return;
 1797 
 1798         /* select the appropriate RF settings based on what EEPROM says */
 1799         rfprog = (sc->rf_rev == RT2573_RF_5225 ||
 1800                   sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
 1801 
 1802         /* find the settings for this channel (we know it exists) */
 1803         for (i = 0; rfprog[i].chan != chan; i++);
 1804 
 1805         power = sc->txpow[i];
 1806         if (power < 0) {
 1807                 bbp94 += power;
 1808                 power = 0;
 1809         } else if (power > 31) {
 1810                 bbp94 += power - 31;
 1811                 power = 31;
 1812         }
 1813 
 1814         /*
 1815          * If we are switching from the 2GHz band to the 5GHz band or
 1816          * vice-versa, BBP registers need to be reprogrammed.
 1817          */
 1818         if (c->ic_flags != ic->ic_curchan->ic_flags) {
 1819                 rum_select_band(sc, c);
 1820                 rum_select_antenna(sc);
 1821         }
 1822         ic->ic_curchan = c;
 1823 
 1824         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
 1825         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
 1826         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
 1827         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
 1828 
 1829         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
 1830         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
 1831         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
 1832         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
 1833 
 1834         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
 1835         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
 1836         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
 1837         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
 1838 
 1839         DELAY(10);
 1840 
 1841         /* enable smart mode for MIMO-capable RFs */
 1842         bbp3 = rum_bbp_read(sc, 3);
 1843 
 1844         bbp3 &= ~RT2573_SMART_MODE;
 1845         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
 1846                 bbp3 |= RT2573_SMART_MODE;
 1847 
 1848         rum_bbp_write(sc, 3, bbp3);
 1849 
 1850         if (bbp94 != RT2573_BBPR94_DEFAULT)
 1851                 rum_bbp_write(sc, 94, bbp94);
 1852 }
 1853 
 1854 /*
 1855  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
 1856  * and HostAP operating modes.
 1857  */
 1858 static void
 1859 rum_enable_tsf_sync(struct rum_softc *sc)
 1860 {
 1861         struct ieee80211com *ic = &sc->sc_ic;
 1862         uint32_t tmp;
 1863 
 1864         if (ic->ic_opmode != IEEE80211_M_STA) {
 1865                 /*
 1866                  * Change default 16ms TBTT adjustment to 8ms.
 1867                  * Must be done before enabling beacon generation.
 1868                  */
 1869                 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
 1870         }
 1871 
 1872         tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
 1873 
 1874         /* set beacon interval (in 1/16ms unit) */
 1875         tmp |= ic->ic_bss->ni_intval * 16;
 1876 
 1877         tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
 1878         if (ic->ic_opmode == IEEE80211_M_STA)
 1879                 tmp |= RT2573_TSF_MODE(1);
 1880         else
 1881                 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
 1882 
 1883         rum_write(sc, RT2573_TXRX_CSR9, tmp);
 1884 }
 1885 
 1886 static void
 1887 rum_update_slot(struct ifnet *ifp)
 1888 {
 1889         struct rum_softc *sc = ifp->if_softc;
 1890         struct ieee80211com *ic = &sc->sc_ic;
 1891         uint8_t slottime;
 1892         uint32_t tmp;
 1893 
 1894         slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
 1895 
 1896         tmp = rum_read(sc, RT2573_MAC_CSR9);
 1897         tmp = (tmp & ~0xff) | slottime;
 1898         rum_write(sc, RT2573_MAC_CSR9, tmp);
 1899 
 1900         DPRINTF(("setting slot time to %uus\n", slottime));
 1901 }
 1902 
 1903 static void
 1904 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
 1905 {
 1906         uint32_t tmp;
 1907 
 1908         tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
 1909         rum_write(sc, RT2573_MAC_CSR4, tmp);
 1910 
 1911         tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
 1912         rum_write(sc, RT2573_MAC_CSR5, tmp);
 1913 }
 1914 
 1915 static void
 1916 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
 1917 {
 1918         uint32_t tmp;
 1919 
 1920         tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
 1921         rum_write(sc, RT2573_MAC_CSR2, tmp);
 1922 
 1923         tmp = addr[4] | addr[5] << 8 | 0xff << 16;
 1924         rum_write(sc, RT2573_MAC_CSR3, tmp);
 1925 }
 1926 
 1927 static void
 1928 rum_update_promisc(struct rum_softc *sc)
 1929 {
 1930         struct ifnet *ifp = sc->sc_ic.ic_ifp;
 1931         uint32_t tmp;
 1932 
 1933         tmp = rum_read(sc, RT2573_TXRX_CSR0);
 1934 
 1935         tmp &= ~RT2573_DROP_NOT_TO_ME;
 1936         if (!(ifp->if_flags & IFF_PROMISC))
 1937                 tmp |= RT2573_DROP_NOT_TO_ME;
 1938 
 1939         rum_write(sc, RT2573_TXRX_CSR0, tmp);
 1940 
 1941         DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
 1942             "entering" : "leaving"));
 1943 }
 1944 
 1945 static const char *
 1946 rum_get_rf(int rev)
 1947 {
 1948         switch (rev) {
 1949         case RT2573_RF_2527:    return "RT2527 (MIMO XR)";
 1950         case RT2573_RF_2528:    return "RT2528";
 1951         case RT2573_RF_5225:    return "RT5225 (MIMO XR)";
 1952         case RT2573_RF_5226:    return "RT5226";
 1953         default:                return "unknown";
 1954         }
 1955 }
 1956 
 1957 static void
 1958 rum_read_eeprom(struct rum_softc *sc)
 1959 {
 1960         struct ieee80211com *ic = &sc->sc_ic;
 1961         uint16_t val;
 1962 #ifdef RUM_DEBUG
 1963         int i;
 1964 #endif
 1965 
 1966         /* read MAC address */
 1967         rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
 1968 
 1969         rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
 1970         val = le16toh(val);
 1971         sc->rf_rev =   (val >> 11) & 0x1f;
 1972         sc->hw_radio = (val >> 10) & 0x1;
 1973         sc->rx_ant =   (val >> 4)  & 0x3;
 1974         sc->tx_ant =   (val >> 2)  & 0x3;
 1975         sc->nb_ant =   val & 0x3;
 1976 
 1977         DPRINTF(("RF revision=%d\n", sc->rf_rev));
 1978 
 1979         rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
 1980         val = le16toh(val);
 1981         sc->ext_5ghz_lna = (val >> 6) & 0x1;
 1982         sc->ext_2ghz_lna = (val >> 4) & 0x1;
 1983 
 1984         DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
 1985             sc->ext_2ghz_lna, sc->ext_5ghz_lna));
 1986 
 1987         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
 1988         val = le16toh(val);
 1989         if ((val & 0xff) != 0xff)
 1990                 sc->rssi_2ghz_corr = (int8_t)(val & 0xff);      /* signed */
 1991 
 1992         /* Only [-10, 10] is valid */
 1993         if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
 1994                 sc->rssi_2ghz_corr = 0;
 1995 
 1996         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
 1997         val = le16toh(val);
 1998         if ((val & 0xff) != 0xff)
 1999                 sc->rssi_5ghz_corr = (int8_t)(val & 0xff);      /* signed */
 2000 
 2001         /* Only [-10, 10] is valid */
 2002         if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
 2003                 sc->rssi_5ghz_corr = 0;
 2004 
 2005         if (sc->ext_2ghz_lna)
 2006                 sc->rssi_2ghz_corr -= 14;
 2007         if (sc->ext_5ghz_lna)
 2008                 sc->rssi_5ghz_corr -= 14;
 2009 
 2010         DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
 2011             sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
 2012 
 2013         rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
 2014         val = le16toh(val);
 2015         if ((val & 0xff) != 0xff)
 2016                 sc->rffreq = val & 0xff;
 2017 
 2018         DPRINTF(("RF freq=%d\n", sc->rffreq));
 2019 
 2020         /* read Tx power for all a/b/g channels */
 2021         rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
 2022         /* XXX default Tx power for 802.11a channels */
 2023         memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
 2024 #ifdef RUM_DEBUG
 2025         for (i = 0; i < 14; i++)
 2026                 DPRINTF(("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]));
 2027 #endif
 2028 
 2029         /* read default values for BBP registers */
 2030         rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
 2031 #ifdef RUM_DEBUG
 2032         for (i = 0; i < 14; i++) {
 2033                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
 2034                         continue;
 2035                 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
 2036                     sc->bbp_prom[i].val));
 2037         }
 2038 #endif
 2039 }
 2040 
 2041 static int
 2042 rum_bbp_init(struct rum_softc *sc)
 2043 {
 2044 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
 2045         int i, ntries;
 2046 
 2047         /* wait for BBP to be ready */
 2048         for (ntries = 0; ntries < 100; ntries++) {
 2049                 const uint8_t val = rum_bbp_read(sc, 0);
 2050                 if (val != 0 && val != 0xff)
 2051                         break;
 2052                 DELAY(1000);
 2053         }
 2054         if (ntries == 100) {
 2055                 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
 2056                 return EIO;
 2057         }
 2058 
 2059         /* initialize BBP registers to default values */
 2060         for (i = 0; i < N(rum_def_bbp); i++)
 2061                 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
 2062 
 2063         /* write vendor-specific BBP values (from EEPROM) */
 2064         for (i = 0; i < 16; i++) {
 2065                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
 2066                         continue;
 2067                 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
 2068         }
 2069 
 2070         return 0;
 2071 #undef N
 2072 }
 2073 
 2074 static void
 2075 rum_init(void *priv)
 2076 {
 2077 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
 2078         struct rum_softc *sc = priv;
 2079         struct ieee80211com *ic = &sc->sc_ic;
 2080         struct ifnet *ifp = ic->ic_ifp;
 2081         struct rum_rx_data *data;
 2082         uint32_t tmp;
 2083         usbd_status error;
 2084         int i, ntries;
 2085 
 2086         rum_stop(sc);
 2087 
 2088         /* initialize MAC registers to default values */
 2089         for (i = 0; i < N(rum_def_mac); i++)
 2090                 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
 2091 
 2092         /* set host ready */
 2093         rum_write(sc, RT2573_MAC_CSR1, 3);
 2094         rum_write(sc, RT2573_MAC_CSR1, 0);
 2095 
 2096         /* wait for BBP/RF to wakeup */
 2097         for (ntries = 0; ntries < 1000; ntries++) {
 2098                 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
 2099                         break;
 2100                 rum_write(sc, RT2573_MAC_CSR12, 4);     /* force wakeup */
 2101                 DELAY(1000);
 2102         }
 2103         if (ntries == 1000) {
 2104                 printf("%s: timeout waiting for BBP/RF to wakeup\n",
 2105                     device_get_nameunit(sc->sc_dev));
 2106                 goto fail;
 2107         }
 2108 
 2109         if ((error = rum_bbp_init(sc)) != 0)
 2110                 goto fail;
 2111 
 2112         /* select default channel */
 2113         rum_select_band(sc, ic->ic_curchan);
 2114         rum_select_antenna(sc);
 2115         rum_set_chan(sc, ic->ic_curchan);
 2116 
 2117         /* clear STA registers */
 2118         rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
 2119 
 2120         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
 2121         rum_set_macaddr(sc, ic->ic_myaddr);
 2122 
 2123         /* initialize ASIC */
 2124         rum_write(sc, RT2573_MAC_CSR1, 4);
 2125 
 2126         /*
 2127          * Allocate xfer for AMRR statistics requests.
 2128          */
 2129         sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
 2130         if (sc->amrr_xfer == NULL) {
 2131                 printf("%s: could not allocate AMRR xfer\n",
 2132                     device_get_nameunit(sc->sc_dev));
 2133                 goto fail;
 2134         }
 2135 
 2136         /*
 2137          * Open Tx and Rx USB bulk pipes.
 2138          */
 2139         error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
 2140             &sc->sc_tx_pipeh);
 2141         if (error != 0) {
 2142                 printf("%s: could not open Tx pipe: %s\n",
 2143                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
 2144                 goto fail;
 2145         }
 2146         error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
 2147             &sc->sc_rx_pipeh);
 2148         if (error != 0) {
 2149                 printf("%s: could not open Rx pipe: %s\n",
 2150                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
 2151                 goto fail;
 2152         }
 2153 
 2154         /*
 2155          * Allocate Tx and Rx xfer queues.
 2156          */
 2157         error = rum_alloc_tx_list(sc);
 2158         if (error != 0) {
 2159                 printf("%s: could not allocate Tx list\n",
 2160                     device_get_nameunit(sc->sc_dev));
 2161                 goto fail;
 2162         }
 2163         error = rum_alloc_rx_list(sc);
 2164         if (error != 0) {
 2165                 printf("%s: could not allocate Rx list\n",
 2166                     device_get_nameunit(sc->sc_dev));
 2167                 goto fail;
 2168         }
 2169 
 2170         /*
 2171          * Start up the receive pipe.
 2172          */
 2173         for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
 2174                 data = &sc->rx_data[i];
 2175 
 2176                 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
 2177                     MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
 2178                 usbd_transfer(data->xfer);
 2179         }
 2180 
 2181         /* update Rx filter */
 2182         tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
 2183 
 2184         tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
 2185         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
 2186                 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
 2187                        RT2573_DROP_ACKCTS;
 2188                 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
 2189                         tmp |= RT2573_DROP_TODS;
 2190                 if (!(ifp->if_flags & IFF_PROMISC))
 2191                         tmp |= RT2573_DROP_NOT_TO_ME;
 2192         }
 2193         rum_write(sc, RT2573_TXRX_CSR0, tmp);
 2194 
 2195         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 2196         ifp->if_drv_flags |= IFF_DRV_RUNNING;
 2197 
 2198         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
 2199                 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
 2200                         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
 2201         } else
 2202                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
 2203 
 2204         return;
 2205 
 2206 fail:   rum_stop(sc);
 2207 #undef N
 2208 }
 2209 
 2210 static void
 2211 rum_stop(void *priv)
 2212 {
 2213         struct rum_softc *sc = priv;
 2214         struct ieee80211com *ic = &sc->sc_ic;
 2215         struct ifnet *ifp = ic->ic_ifp;
 2216         uint32_t tmp;
 2217 
 2218         sc->sc_tx_timer = 0;
 2219         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
 2220 
 2221         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
 2222 
 2223         /* disable Rx */
 2224         tmp = rum_read(sc, RT2573_TXRX_CSR0);
 2225         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
 2226 
 2227         /* reset ASIC */
 2228         rum_write(sc, RT2573_MAC_CSR1, 3);
 2229         rum_write(sc, RT2573_MAC_CSR1, 0);
 2230 
 2231         if (sc->amrr_xfer != NULL) {
 2232                 usbd_free_xfer(sc->amrr_xfer);
 2233                 sc->amrr_xfer = NULL;
 2234         }
 2235 
 2236         if (sc->sc_rx_pipeh != NULL) {
 2237                 usbd_abort_pipe(sc->sc_rx_pipeh);
 2238                 usbd_close_pipe(sc->sc_rx_pipeh);
 2239                 sc->sc_rx_pipeh = NULL;
 2240         }
 2241         if (sc->sc_tx_pipeh != NULL) {
 2242                 usbd_abort_pipe(sc->sc_tx_pipeh);
 2243                 usbd_close_pipe(sc->sc_tx_pipeh);
 2244                 sc->sc_tx_pipeh = NULL;
 2245         }
 2246 
 2247         rum_free_rx_list(sc);
 2248         rum_free_tx_list(sc);
 2249 }
 2250 
 2251 static int
 2252 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
 2253 {
 2254         usb_device_request_t req;
 2255         uint16_t reg = RT2573_MCU_CODE_BASE;
 2256         usbd_status error;
 2257 
 2258         /* copy firmware image into NIC */
 2259         for (; size >= 4; reg += 4, ucode += 4, size -= 4)
 2260                 rum_write(sc, reg, UGETDW(ucode));
 2261 
 2262         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
 2263         req.bRequest = RT2573_MCU_CNTL;
 2264         USETW(req.wValue, RT2573_MCU_RUN);
 2265         USETW(req.wIndex, 0);
 2266         USETW(req.wLength, 0);
 2267 
 2268         error = usbd_do_request(sc->sc_udev, &req, NULL);
 2269         if (error != 0) {
 2270                 printf("%s: could not run firmware: %s\n",
 2271                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
 2272         }
 2273         return error;
 2274 }
 2275 
 2276 static int
 2277 rum_prepare_beacon(struct rum_softc *sc)
 2278 {
 2279         struct ieee80211com *ic = &sc->sc_ic;
 2280         struct rum_tx_desc desc;
 2281         struct mbuf *m0;
 2282         int rate;
 2283 
 2284         m0 = ieee80211_beacon_alloc(ic->ic_bss, &sc->sc_bo);
 2285         if (m0 == NULL) {
 2286                 return ENOBUFS;
 2287         }
 2288 
 2289         /* send beacons at the lowest available rate */
 2290         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
 2291 
 2292         rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
 2293             m0->m_pkthdr.len, rate);
 2294 
 2295         /* copy the first 24 bytes of Tx descriptor into NIC memory */
 2296         rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
 2297 
 2298         /* copy beacon header and payload into NIC memory */
 2299         rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
 2300             m0->m_pkthdr.len);
 2301 
 2302         m_freem(m0);
 2303 
 2304         return 0;
 2305 }
 2306 
 2307 static int
 2308 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
 2309     const struct ieee80211_bpf_params *params)
 2310 {
 2311         struct ieee80211com *ic = ni->ni_ic;
 2312         struct ifnet *ifp = ic->ic_ifp;
 2313         struct rum_softc *sc = ifp->if_softc;
 2314 
 2315         /* prevent management frames from being sent if we're not ready */
 2316         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
 2317                 m_freem(m);
 2318                 ieee80211_free_node(ni);
 2319                 return ENETDOWN;
 2320         }
 2321         if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
 2322                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 2323                 m_freem(m);
 2324                 ieee80211_free_node(ni);
 2325                 return EIO;
 2326         }
 2327 
 2328         if (bpf_peers_present(ic->ic_rawbpf))
 2329                 bpf_mtap(ic->ic_rawbpf, m);
 2330 
 2331         ifp->if_opackets++;
 2332 
 2333         if (params == NULL) {
 2334                 /*
 2335                  * Legacy path; interpret frame contents to decide
 2336                  * precisely how to send the frame.
 2337                  */
 2338                 if (rum_tx_mgt(sc, m, ni) != 0)
 2339                         goto bad;
 2340         } else {
 2341                 /*
 2342                  * Caller supplied explicit parameters to use in
 2343                  * sending the frame.
 2344                  */
 2345                 if (rum_tx_raw(sc, m, ni, params) != 0)
 2346                         goto bad;
 2347         }
 2348         sc->sc_tx_timer = 5;
 2349         callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
 2350 
 2351         return 0;
 2352 bad:
 2353         ifp->if_oerrors++;
 2354         ieee80211_free_node(ni);
 2355         return EIO;
 2356 }
 2357 
 2358 static void
 2359 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
 2360 {
 2361         int i;
 2362 
 2363         /* clear statistic registers (STA_CSR0 to STA_CSR5) */
 2364         rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
 2365 
 2366         ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
 2367 
 2368         /* set rate to some reasonable initial value */
 2369         for (i = ni->ni_rates.rs_nrates - 1;
 2370              i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
 2371              i--);
 2372         ni->ni_txrate = i;
 2373 
 2374         callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
 2375 }
 2376 
 2377 static void
 2378 rum_amrr_timeout(void *arg)
 2379 {
 2380         struct rum_softc *sc = (struct rum_softc *)arg;
 2381         usb_device_request_t req;
 2382 
 2383         /*
 2384          * Asynchronously read statistic registers (cleared by read).
 2385          */
 2386         req.bmRequestType = UT_READ_VENDOR_DEVICE;
 2387         req.bRequest = RT2573_READ_MULTI_MAC;
 2388         USETW(req.wValue, 0);
 2389         USETW(req.wIndex, RT2573_STA_CSR0);
 2390         USETW(req.wLength, sizeof sc->sta);
 2391 
 2392         usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
 2393             USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
 2394             rum_amrr_update);
 2395         (void)usbd_transfer(sc->amrr_xfer);
 2396 }
 2397 
 2398 static void
 2399 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
 2400     usbd_status status)
 2401 {
 2402         struct rum_softc *sc = (struct rum_softc *)priv;
 2403         struct ifnet *ifp = sc->sc_ic.ic_ifp;
 2404 
 2405         if (status != USBD_NORMAL_COMPLETION) {
 2406                 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
 2407                     "cancelling automatic rate control\n");
 2408                 return;
 2409         }
 2410 
 2411         /* count TX retry-fail as Tx errors */
 2412         ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
 2413 
 2414         sc->amn.amn_retrycnt =
 2415             (le32toh(sc->sta[4]) >> 16) +       /* TX one-retry ok count */
 2416             (le32toh(sc->sta[5]) & 0xffff) +    /* TX more-retry ok count */
 2417             (le32toh(sc->sta[5]) >> 16);        /* TX retry-fail count */
 2418 
 2419         sc->amn.amn_txcnt =
 2420             sc->amn.amn_retrycnt +
 2421             (le32toh(sc->sta[4]) & 0xffff);     /* TX no-retry ok count */
 2422 
 2423         ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
 2424 
 2425         callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
 2426 }
 2427 
 2428 static void
 2429 rum_scan_start(struct ieee80211com *ic)
 2430 {
 2431         struct rum_softc *sc = ic->ic_ifp->if_softc;
 2432 
 2433         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
 2434 
 2435         /* do it in a process context */
 2436         sc->sc_scan_action = RUM_SCAN_START;
 2437         usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
 2438 }
 2439 
 2440 static void
 2441 rum_scan_end(struct ieee80211com *ic)
 2442 {
 2443         struct rum_softc *sc = ic->ic_ifp->if_softc;
 2444 
 2445         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
 2446 
 2447         /* do it in a process context */
 2448         sc->sc_scan_action = RUM_SCAN_END;
 2449         usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
 2450 }
 2451 
 2452 static void
 2453 rum_set_channel(struct ieee80211com *ic)
 2454 {
 2455         struct rum_softc *sc = ic->ic_ifp->if_softc;
 2456 
 2457         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
 2458 
 2459         /* do it in a process context */
 2460         sc->sc_scan_action = RUM_SET_CHANNEL;
 2461         usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
 2462 }
 2463 
 2464 static void
 2465 rum_scantask(void *arg)
 2466 {
 2467         struct rum_softc *sc = arg;
 2468         struct ieee80211com *ic = &sc->sc_ic;
 2469         struct ifnet *ifp = ic->ic_ifp;
 2470         uint32_t tmp;
 2471 
 2472         RUM_LOCK(sc);
 2473 
 2474         switch (sc->sc_scan_action) {
 2475         case RUM_SCAN_START:
 2476                 /* abort TSF synchronization */
 2477                 tmp = rum_read(sc, RT2573_TXRX_CSR9);
 2478                 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
 2479                 rum_set_bssid(sc, ifp->if_broadcastaddr);
 2480                 break;
 2481 
 2482         case RUM_SCAN_END:
 2483                 rum_enable_tsf_sync(sc);
 2484                 /* XXX keep local copy */
 2485                 rum_set_bssid(sc, ic->ic_bss->ni_bssid);
 2486                 break;
 2487 
 2488         case RUM_SET_CHANNEL:
 2489                 mtx_lock(&Giant);
 2490                 rum_set_chan(sc, ic->ic_curchan);
 2491                 mtx_unlock(&Giant);
 2492                 break;
 2493 
 2494         default:
 2495                 panic("unknown scan action %d\n", sc->sc_scan_action);
 2496                 /* NEVER REACHED */
 2497                 break;
 2498         }
 2499 
 2500         RUM_UNLOCK(sc);
 2501 }
 2502 
 2503 static int
 2504 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
 2505 {
 2506         int lna, agc, rssi;
 2507 
 2508         lna = (raw >> 5) & 0x3;
 2509         agc = raw & 0x1f;
 2510 
 2511         if (lna == 0) {
 2512                 /*
 2513                  * No RSSI mapping
 2514                  *
 2515                  * NB: Since RSSI is relative to noise floor, -1 is
 2516                  *     adequate for caller to know error happened.
 2517                  */
 2518                 return -1;
 2519         }
 2520 
 2521         rssi = (2 * agc) - RT2573_NOISE_FLOOR;
 2522 
 2523         if (IEEE80211_IS_CHAN_2GHZ(sc->sc_ic.ic_curchan)) {
 2524                 rssi += sc->rssi_2ghz_corr;
 2525 
 2526                 if (lna == 1)
 2527                         rssi -= 64;
 2528                 else if (lna == 2)
 2529                         rssi -= 74;
 2530                 else if (lna == 3)
 2531                         rssi -= 90;
 2532         } else {
 2533                 rssi += sc->rssi_5ghz_corr;
 2534 
 2535                 if (!sc->ext_5ghz_lna && lna != 1)
 2536                         rssi += 4;
 2537 
 2538                 if (lna == 1)
 2539                         rssi -= 64;
 2540                 else if (lna == 2)
 2541                         rssi -= 86;
 2542                 else if (lna == 3)
 2543                         rssi -= 100;
 2544         }
 2545         return rssi;
 2546 }
 2547 
 2548 static device_method_t rum_methods[] = {
 2549         /* Device interface */
 2550         DEVMETHOD(device_probe,         rum_match),
 2551         DEVMETHOD(device_attach,        rum_attach),
 2552         DEVMETHOD(device_detach,        rum_detach),
 2553 
 2554         { 0, 0 }
 2555 };
 2556 
 2557 static driver_t rum_driver = {
 2558         "rum",
 2559         rum_methods,
 2560         sizeof(struct rum_softc)
 2561 };
 2562 
 2563 static devclass_t rum_devclass;
 2564 
 2565 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);

Cache object: a634e591107334840e7525acfc594082


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.