if_upgt.c revision 283540
1191983Sweongyo/* $OpenBSD: if_upgt.c,v 1.35 2008/04/16 18:32:15 damien Exp $ */ 2191983Sweongyo/* $FreeBSD: head/sys/dev/usb/wlan/if_upgt.c 283540 2015-05-25 19:53:29Z glebius $ */ 3191983Sweongyo 4191983Sweongyo/* 5191983Sweongyo * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org> 6191983Sweongyo * 7191983Sweongyo * Permission to use, copy, modify, and distribute this software for any 8191983Sweongyo * purpose with or without fee is hereby granted, provided that the above 9191983Sweongyo * copyright notice and this permission notice appear in all copies. 10191983Sweongyo * 11191983Sweongyo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12191983Sweongyo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13191983Sweongyo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14191983Sweongyo * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15191983Sweongyo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16191983Sweongyo * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17191983Sweongyo * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18191983Sweongyo */ 19191983Sweongyo 20191983Sweongyo#include <sys/param.h> 21191983Sweongyo#include <sys/systm.h> 22191983Sweongyo#include <sys/kernel.h> 23191983Sweongyo#include <sys/endian.h> 24191983Sweongyo#include <sys/firmware.h> 25191983Sweongyo#include <sys/linker.h> 26191983Sweongyo#include <sys/mbuf.h> 27191983Sweongyo#include <sys/malloc.h> 28191983Sweongyo#include <sys/module.h> 29191983Sweongyo#include <sys/socket.h> 30191983Sweongyo#include <sys/sockio.h> 31191983Sweongyo#include <sys/sysctl.h> 32191983Sweongyo 33191983Sweongyo#include <net/if.h> 34257176Sglebius#include <net/if_var.h> 35191983Sweongyo#include <net/if_arp.h> 36191983Sweongyo#include <net/ethernet.h> 37191983Sweongyo#include <net/if_dl.h> 38191983Sweongyo#include <net/if_media.h> 39191983Sweongyo#include <net/if_types.h> 40191983Sweongyo 41191983Sweongyo#include <sys/bus.h> 42191983Sweongyo#include <machine/bus.h> 43191983Sweongyo 44191983Sweongyo#include <net80211/ieee80211_var.h> 45191983Sweongyo#include <net80211/ieee80211_phy.h> 46191983Sweongyo#include <net80211/ieee80211_radiotap.h> 47191983Sweongyo#include <net80211/ieee80211_regdomain.h> 48191983Sweongyo 49191983Sweongyo#include <net/bpf.h> 50191983Sweongyo 51191983Sweongyo#include <dev/usb/usb.h> 52194677Sthompsa#include <dev/usb/usbdi.h> 53191983Sweongyo#include "usbdevs.h" 54191983Sweongyo 55191983Sweongyo#include <dev/usb/wlan/if_upgtvar.h> 56191983Sweongyo 57191983Sweongyo/* 58191983Sweongyo * Driver for the USB PrismGT devices. 59191983Sweongyo * 60191983Sweongyo * For now just USB 2.0 devices with the GW3887 chipset are supported. 61191983Sweongyo * The driver has been written based on the firmware version 2.13.1.0_LM87. 62191983Sweongyo * 63191983Sweongyo * TODO's: 64191983Sweongyo * - MONITOR mode test. 65191983Sweongyo * - Add HOSTAP mode. 66191983Sweongyo * - Add IBSS mode. 67191983Sweongyo * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets). 68191983Sweongyo * 69191983Sweongyo * Parts of this driver has been influenced by reading the p54u driver 70191983Sweongyo * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and 71191983Sweongyo * Sebastien Bourdeauducq <lekernel@prism54.org>. 72191983Sweongyo */ 73191983Sweongyo 74227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, upgt, CTLFLAG_RD, 0, 75191983Sweongyo "USB PrismGT GW3887 driver parameters"); 76191983Sweongyo 77191983Sweongyo#ifdef UPGT_DEBUG 78191983Sweongyoint upgt_debug = 0; 79267992ShselaskySYSCTL_INT(_hw_upgt, OID_AUTO, debug, CTLFLAG_RWTUN, &upgt_debug, 80191983Sweongyo 0, "control debugging printfs"); 81191983Sweongyoenum { 82191983Sweongyo UPGT_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 83191983Sweongyo UPGT_DEBUG_RECV = 0x00000002, /* basic recv operation */ 84191983Sweongyo UPGT_DEBUG_RESET = 0x00000004, /* reset processing */ 85191983Sweongyo UPGT_DEBUG_INTR = 0x00000008, /* INTR */ 86191983Sweongyo UPGT_DEBUG_TX_PROC = 0x00000010, /* tx ISR proc */ 87191983Sweongyo UPGT_DEBUG_RX_PROC = 0x00000020, /* rx ISR proc */ 88191983Sweongyo UPGT_DEBUG_STATE = 0x00000040, /* 802.11 state transitions */ 89191983Sweongyo UPGT_DEBUG_STAT = 0x00000080, /* statistic */ 90191983Sweongyo UPGT_DEBUG_FW = 0x00000100, /* firmware */ 91191983Sweongyo UPGT_DEBUG_ANY = 0xffffffff 92191983Sweongyo}; 93191983Sweongyo#define DPRINTF(sc, m, fmt, ...) do { \ 94191983Sweongyo if (sc->sc_debug & (m)) \ 95191983Sweongyo printf(fmt, __VA_ARGS__); \ 96191983Sweongyo} while (0) 97191983Sweongyo#else 98191983Sweongyo#define DPRINTF(sc, m, fmt, ...) do { \ 99191983Sweongyo (void) sc; \ 100191983Sweongyo} while (0) 101191983Sweongyo#endif 102191983Sweongyo 103191983Sweongyo/* 104191983Sweongyo * Prototypes. 105191983Sweongyo */ 106191983Sweongyostatic device_probe_t upgt_match; 107191983Sweongyostatic device_attach_t upgt_attach; 108191983Sweongyostatic device_detach_t upgt_detach; 109191983Sweongyostatic int upgt_alloc_tx(struct upgt_softc *); 110191983Sweongyostatic int upgt_alloc_rx(struct upgt_softc *); 111191983Sweongyostatic int upgt_device_reset(struct upgt_softc *); 112191983Sweongyostatic void upgt_bulk_tx(struct upgt_softc *, struct upgt_data *); 113191983Sweongyostatic int upgt_fw_verify(struct upgt_softc *); 114191983Sweongyostatic int upgt_mem_init(struct upgt_softc *); 115191983Sweongyostatic int upgt_fw_load(struct upgt_softc *); 116191983Sweongyostatic int upgt_fw_copy(const uint8_t *, char *, int); 117191983Sweongyostatic uint32_t upgt_crc32_le(const void *, size_t); 118191983Sweongyostatic struct mbuf * 119192984Sthompsa upgt_rxeof(struct usb_xfer *, struct upgt_data *, int *); 120191983Sweongyostatic struct mbuf * 121191983Sweongyo upgt_rx(struct upgt_softc *, uint8_t *, int, int *); 122192984Sthompsastatic void upgt_txeof(struct usb_xfer *, struct upgt_data *); 123191983Sweongyostatic int upgt_eeprom_read(struct upgt_softc *); 124191983Sweongyostatic int upgt_eeprom_parse(struct upgt_softc *); 125191983Sweongyostatic void upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *); 126191983Sweongyostatic void upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int); 127191983Sweongyostatic void upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int); 128191983Sweongyostatic void upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int); 129191983Sweongyostatic uint32_t upgt_chksum_le(const uint32_t *, size_t); 130191983Sweongyostatic void upgt_tx_done(struct upgt_softc *, uint8_t *); 131191983Sweongyostatic void upgt_init(void *); 132191983Sweongyostatic void upgt_init_locked(struct upgt_softc *); 133191983Sweongyostatic int upgt_ioctl(struct ifnet *, u_long, caddr_t); 134191983Sweongyostatic void upgt_start(struct ifnet *); 135191983Sweongyostatic int upgt_raw_xmit(struct ieee80211_node *, struct mbuf *, 136191983Sweongyo const struct ieee80211_bpf_params *); 137191983Sweongyostatic void upgt_scan_start(struct ieee80211com *); 138191983Sweongyostatic void upgt_scan_end(struct ieee80211com *); 139191983Sweongyostatic void upgt_set_channel(struct ieee80211com *); 140191983Sweongyostatic struct ieee80211vap *upgt_vap_create(struct ieee80211com *, 141228621Sbschmidt const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 142228621Sbschmidt const uint8_t [IEEE80211_ADDR_LEN], 143228621Sbschmidt const uint8_t [IEEE80211_ADDR_LEN]); 144191983Sweongyostatic void upgt_vap_delete(struct ieee80211vap *); 145283540Sglebiusstatic void upgt_update_mcast(struct ieee80211com *); 146191983Sweongyostatic uint8_t upgt_rx_rate(struct upgt_softc *, const int); 147191983Sweongyostatic void upgt_set_multi(void *); 148191983Sweongyostatic void upgt_stop(struct upgt_softc *); 149191983Sweongyostatic void upgt_setup_rates(struct ieee80211vap *, struct ieee80211com *); 150191983Sweongyostatic int upgt_set_macfilter(struct upgt_softc *, uint8_t); 151191983Sweongyostatic int upgt_newstate(struct ieee80211vap *, enum ieee80211_state, int); 152191983Sweongyostatic void upgt_set_chan(struct upgt_softc *, struct ieee80211_channel *); 153191983Sweongyostatic void upgt_set_led(struct upgt_softc *, int); 154191983Sweongyostatic void upgt_set_led_blink(void *); 155191983Sweongyostatic void upgt_get_stats(struct upgt_softc *); 156191983Sweongyostatic void upgt_mem_free(struct upgt_softc *, uint32_t); 157191983Sweongyostatic uint32_t upgt_mem_alloc(struct upgt_softc *); 158191983Sweongyostatic void upgt_free_tx(struct upgt_softc *); 159191983Sweongyostatic void upgt_free_rx(struct upgt_softc *); 160191983Sweongyostatic void upgt_watchdog(void *); 161191983Sweongyostatic void upgt_abort_xfers(struct upgt_softc *); 162191983Sweongyostatic void upgt_abort_xfers_locked(struct upgt_softc *); 163191983Sweongyostatic void upgt_sysctl_node(struct upgt_softc *); 164191983Sweongyostatic struct upgt_data * 165191983Sweongyo upgt_getbuf(struct upgt_softc *); 166191983Sweongyostatic struct upgt_data * 167191983Sweongyo upgt_gettxbuf(struct upgt_softc *); 168191983Sweongyostatic int upgt_tx_start(struct upgt_softc *, struct mbuf *, 169191983Sweongyo struct ieee80211_node *, struct upgt_data *); 170191983Sweongyo 171191983Sweongyostatic const char *upgt_fwname = "upgt-gw3887"; 172191983Sweongyo 173223486Shselaskystatic const STRUCT_USB_HOST_ID upgt_devs[] = { 174191983Sweongyo#define UPGT_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 175191983Sweongyo /* version 2 devices */ 176191983Sweongyo UPGT_DEV(ACCTON, PRISM_GT), 177191983Sweongyo UPGT_DEV(BELKIN, F5D7050), 178191983Sweongyo UPGT_DEV(CISCOLINKSYS, WUSB54AG), 179191983Sweongyo UPGT_DEV(CONCEPTRONIC, PRISM_GT), 180191983Sweongyo UPGT_DEV(DELL, PRISM_GT_1), 181191983Sweongyo UPGT_DEV(DELL, PRISM_GT_2), 182191983Sweongyo UPGT_DEV(FSC, E5400), 183191983Sweongyo UPGT_DEV(GLOBESPAN, PRISM_GT_1), 184191983Sweongyo UPGT_DEV(GLOBESPAN, PRISM_GT_2), 185275646Smp UPGT_DEV(NETGEAR, WG111V1_2), 186191983Sweongyo UPGT_DEV(INTERSIL, PRISM_GT), 187191983Sweongyo UPGT_DEV(SMC, 2862WG), 188209447Sthompsa UPGT_DEV(USR, USR5422), 189191983Sweongyo UPGT_DEV(WISTRONNEWEB, UR045G), 190191983Sweongyo UPGT_DEV(XYRATEX, PRISM_GT_1), 191191983Sweongyo UPGT_DEV(XYRATEX, PRISM_GT_2), 192191983Sweongyo UPGT_DEV(ZCOM, XG703A), 193191983Sweongyo UPGT_DEV(ZCOM, XM142) 194191983Sweongyo}; 195191983Sweongyo 196193045Sthompsastatic usb_callback_t upgt_bulk_rx_callback; 197193045Sthompsastatic usb_callback_t upgt_bulk_tx_callback; 198191983Sweongyo 199192984Sthompsastatic const struct usb_config upgt_config[UPGT_N_XFERS] = { 200191983Sweongyo [UPGT_BULK_TX] = { 201191983Sweongyo .type = UE_BULK, 202191983Sweongyo .endpoint = UE_ADDR_ANY, 203191983Sweongyo .direction = UE_DIR_OUT, 204244503Shselasky .bufsize = MCLBYTES * UPGT_TX_MAXCOUNT, 205191983Sweongyo .flags = { 206191983Sweongyo .force_short_xfer = 1, 207191983Sweongyo .pipe_bof = 1 208191983Sweongyo }, 209191983Sweongyo .callback = upgt_bulk_tx_callback, 210191983Sweongyo .timeout = UPGT_USB_TIMEOUT, /* ms */ 211191983Sweongyo }, 212191983Sweongyo [UPGT_BULK_RX] = { 213191983Sweongyo .type = UE_BULK, 214191983Sweongyo .endpoint = UE_ADDR_ANY, 215191983Sweongyo .direction = UE_DIR_IN, 216244503Shselasky .bufsize = MCLBYTES * UPGT_RX_MAXCOUNT, 217191983Sweongyo .flags = { 218191983Sweongyo .pipe_bof = 1, 219191983Sweongyo .short_xfer_ok = 1 220191983Sweongyo }, 221191983Sweongyo .callback = upgt_bulk_rx_callback, 222191983Sweongyo }, 223191983Sweongyo}; 224191983Sweongyo 225191983Sweongyostatic int 226191983Sweongyoupgt_match(device_t dev) 227191983Sweongyo{ 228192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 229191983Sweongyo 230192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 231191983Sweongyo return (ENXIO); 232191983Sweongyo if (uaa->info.bConfigIndex != UPGT_CONFIG_INDEX) 233191983Sweongyo return (ENXIO); 234191983Sweongyo if (uaa->info.bIfaceIndex != UPGT_IFACE_INDEX) 235191983Sweongyo return (ENXIO); 236191983Sweongyo 237223486Shselasky return (usbd_lookup_id_by_uaa(upgt_devs, sizeof(upgt_devs), uaa)); 238191983Sweongyo} 239191983Sweongyo 240191983Sweongyostatic int 241191983Sweongyoupgt_attach(device_t dev) 242191983Sweongyo{ 243191983Sweongyo int error; 244191983Sweongyo struct ieee80211com *ic; 245191983Sweongyo struct ifnet *ifp; 246191983Sweongyo struct upgt_softc *sc = device_get_softc(dev); 247192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 248191983Sweongyo uint8_t bands, iface_index = UPGT_IFACE_INDEX; 249191983Sweongyo 250191983Sweongyo sc->sc_dev = dev; 251191983Sweongyo sc->sc_udev = uaa->device; 252191983Sweongyo#ifdef UPGT_DEBUG 253191983Sweongyo sc->sc_debug = upgt_debug; 254191983Sweongyo#endif 255194228Sthompsa device_set_usb_desc(dev); 256191983Sweongyo 257191983Sweongyo mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK, 258191983Sweongyo MTX_DEF); 259191983Sweongyo callout_init(&sc->sc_led_ch, 0); 260191983Sweongyo callout_init(&sc->sc_watchdog_ch, 0); 261191983Sweongyo 262194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 263191983Sweongyo upgt_config, UPGT_N_XFERS, sc, &sc->sc_mtx); 264191983Sweongyo if (error) { 265191983Sweongyo device_printf(dev, "could not allocate USB transfers, " 266194228Sthompsa "err=%s\n", usbd_errstr(error)); 267246565Shselasky goto fail1; 268191983Sweongyo } 269191983Sweongyo 270244503Shselasky sc->sc_rx_dma_buf = usbd_xfer_get_frame_buffer( 271244503Shselasky sc->sc_xfer[UPGT_BULK_RX], 0); 272244503Shselasky sc->sc_tx_dma_buf = usbd_xfer_get_frame_buffer( 273244503Shselasky sc->sc_xfer[UPGT_BULK_TX], 0); 274244503Shselasky 275246565Shselasky /* Setup TX and RX buffers */ 276246565Shselasky error = upgt_alloc_tx(sc); 277246565Shselasky if (error) 278246565Shselasky goto fail2; 279246565Shselasky error = upgt_alloc_rx(sc); 280246565Shselasky if (error) 281246565Shselasky goto fail3; 282246565Shselasky 283191983Sweongyo ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 284191983Sweongyo if (ifp == NULL) { 285191983Sweongyo device_printf(dev, "can not if_alloc()\n"); 286191983Sweongyo goto fail4; 287191983Sweongyo } 288191983Sweongyo 289191983Sweongyo /* Initialize the device. */ 290191983Sweongyo error = upgt_device_reset(sc); 291191983Sweongyo if (error) 292191983Sweongyo goto fail5; 293191983Sweongyo /* Verify the firmware. */ 294191983Sweongyo error = upgt_fw_verify(sc); 295191983Sweongyo if (error) 296191983Sweongyo goto fail5; 297191983Sweongyo /* Calculate device memory space. */ 298191983Sweongyo if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) { 299191983Sweongyo device_printf(dev, 300199816Sthompsa "could not find memory space addresses on FW\n"); 301191983Sweongyo error = EIO; 302191983Sweongyo goto fail5; 303191983Sweongyo } 304191983Sweongyo sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1; 305191983Sweongyo sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1; 306191983Sweongyo 307191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n", 308191983Sweongyo sc->sc_memaddr_frame_start); 309191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n", 310191983Sweongyo sc->sc_memaddr_frame_end); 311191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n", 312191983Sweongyo sc->sc_memaddr_rx_start); 313191983Sweongyo 314191983Sweongyo upgt_mem_init(sc); 315191983Sweongyo 316191983Sweongyo /* Load the firmware. */ 317191983Sweongyo error = upgt_fw_load(sc); 318191983Sweongyo if (error) 319191983Sweongyo goto fail5; 320191983Sweongyo 321191983Sweongyo /* Read the whole EEPROM content and parse it. */ 322191983Sweongyo error = upgt_eeprom_read(sc); 323191983Sweongyo if (error) 324191983Sweongyo goto fail5; 325191983Sweongyo error = upgt_eeprom_parse(sc); 326191983Sweongyo if (error) 327191983Sweongyo goto fail5; 328191983Sweongyo 329191983Sweongyo /* all works related with the device have done here. */ 330191983Sweongyo upgt_abort_xfers(sc); 331191983Sweongyo 332191983Sweongyo /* Setup the 802.11 device. */ 333191983Sweongyo ifp->if_softc = sc; 334191983Sweongyo if_initname(ifp, "upgt", device_get_unit(sc->sc_dev)); 335191983Sweongyo ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 336191983Sweongyo ifp->if_init = upgt_init; 337191983Sweongyo ifp->if_ioctl = upgt_ioctl; 338191983Sweongyo ifp->if_start = upgt_start; 339207554Ssobomax IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 340191983Sweongyo IFQ_SET_READY(&ifp->if_snd); 341191983Sweongyo 342191983Sweongyo ic = ifp->if_l2com; 343191983Sweongyo ic->ic_ifp = ifp; 344283537Sglebius ic->ic_softc = sc; 345283527Sglebius ic->ic_name = device_get_nameunit(dev); 346191983Sweongyo ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 347191983Sweongyo ic->ic_opmode = IEEE80211_M_STA; 348191983Sweongyo /* set device capabilities */ 349191983Sweongyo ic->ic_caps = 350191983Sweongyo IEEE80211_C_STA /* station mode */ 351191983Sweongyo | IEEE80211_C_MONITOR /* monitor mode */ 352191983Sweongyo | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 353191983Sweongyo | IEEE80211_C_SHSLOT /* short slot time supported */ 354191983Sweongyo | IEEE80211_C_BGSCAN /* capable of bg scanning */ 355191983Sweongyo | IEEE80211_C_WPA /* 802.11i */ 356191983Sweongyo ; 357191983Sweongyo 358191983Sweongyo bands = 0; 359191983Sweongyo setbit(&bands, IEEE80211_MODE_11B); 360191983Sweongyo setbit(&bands, IEEE80211_MODE_11G); 361191983Sweongyo ieee80211_init_channels(ic, NULL, &bands); 362191983Sweongyo 363191983Sweongyo ieee80211_ifattach(ic, sc->sc_myaddr); 364191983Sweongyo ic->ic_raw_xmit = upgt_raw_xmit; 365191983Sweongyo ic->ic_scan_start = upgt_scan_start; 366191983Sweongyo ic->ic_scan_end = upgt_scan_end; 367191983Sweongyo ic->ic_set_channel = upgt_set_channel; 368191983Sweongyo 369191983Sweongyo ic->ic_vap_create = upgt_vap_create; 370191983Sweongyo ic->ic_vap_delete = upgt_vap_delete; 371191983Sweongyo ic->ic_update_mcast = upgt_update_mcast; 372191983Sweongyo 373192468Ssam ieee80211_radiotap_attach(ic, 374192468Ssam &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 375192468Ssam UPGT_TX_RADIOTAP_PRESENT, 376192468Ssam &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 377192468Ssam UPGT_RX_RADIOTAP_PRESENT); 378191983Sweongyo 379191983Sweongyo upgt_sysctl_node(sc); 380191983Sweongyo 381191983Sweongyo if (bootverbose) 382191983Sweongyo ieee80211_announce(ic); 383191983Sweongyo 384191983Sweongyo return (0); 385191983Sweongyo 386191983Sweongyofail5: if_free(ifp); 387246565Shselaskyfail4: upgt_free_rx(sc); 388246565Shselaskyfail3: upgt_free_tx(sc); 389246565Shselaskyfail2: usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS); 390191983Sweongyofail1: mtx_destroy(&sc->sc_mtx); 391191983Sweongyo 392191983Sweongyo return (error); 393191983Sweongyo} 394191983Sweongyo 395191983Sweongyostatic void 396192984Sthompsaupgt_txeof(struct usb_xfer *xfer, struct upgt_data *data) 397191983Sweongyo{ 398194677Sthompsa struct upgt_softc *sc = usbd_xfer_softc(xfer); 399191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 400191983Sweongyo struct mbuf *m; 401191983Sweongyo 402191983Sweongyo UPGT_ASSERT_LOCKED(sc); 403191983Sweongyo 404191983Sweongyo /* 405191983Sweongyo * Do any tx complete callback. Note this must be done before releasing 406191983Sweongyo * the node reference. 407191983Sweongyo */ 408191983Sweongyo if (data->m) { 409191983Sweongyo m = data->m; 410191983Sweongyo if (m->m_flags & M_TXCB) { 411191983Sweongyo /* XXX status? */ 412191983Sweongyo ieee80211_process_callback(data->ni, m, 0); 413191983Sweongyo } 414191983Sweongyo m_freem(m); 415191983Sweongyo data->m = NULL; 416191983Sweongyo } 417191983Sweongyo if (data->ni) { 418191983Sweongyo ieee80211_free_node(data->ni); 419191983Sweongyo data->ni = NULL; 420191983Sweongyo } 421271866Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 422191983Sweongyo} 423191983Sweongyo 424191983Sweongyostatic void 425191983Sweongyoupgt_get_stats(struct upgt_softc *sc) 426191983Sweongyo{ 427191983Sweongyo struct upgt_data *data_cmd; 428191983Sweongyo struct upgt_lmac_mem *mem; 429191983Sweongyo struct upgt_lmac_stats *stats; 430191983Sweongyo 431191983Sweongyo data_cmd = upgt_getbuf(sc); 432191983Sweongyo if (data_cmd == NULL) { 433269569Sn_hibma device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 434191983Sweongyo return; 435191983Sweongyo } 436191983Sweongyo 437191983Sweongyo /* 438191983Sweongyo * Transmit the URB containing the CMD data. 439191983Sweongyo */ 440227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 441191983Sweongyo 442191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 443191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 444191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 445191983Sweongyo 446191983Sweongyo stats = (struct upgt_lmac_stats *)(mem + 1); 447191983Sweongyo 448191983Sweongyo stats->header1.flags = 0; 449191983Sweongyo stats->header1.type = UPGT_H1_TYPE_CTRL; 450191983Sweongyo stats->header1.len = htole16( 451191983Sweongyo sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header)); 452191983Sweongyo 453191983Sweongyo stats->header2.reqid = htole32(sc->sc_memaddr_frame_start); 454191983Sweongyo stats->header2.type = htole16(UPGT_H2_TYPE_STATS); 455191983Sweongyo stats->header2.flags = 0; 456191983Sweongyo 457191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*stats); 458191983Sweongyo 459191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)stats, 460191983Sweongyo data_cmd->buflen - sizeof(*mem)); 461191983Sweongyo 462191983Sweongyo upgt_bulk_tx(sc, data_cmd); 463191983Sweongyo} 464191983Sweongyo 465191983Sweongyostatic int 466191983Sweongyoupgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 467191983Sweongyo{ 468191983Sweongyo struct upgt_softc *sc = ifp->if_softc; 469191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 470191983Sweongyo struct ifreq *ifr = (struct ifreq *) data; 471246614Shselasky int error; 472246614Shselasky int startall = 0; 473191983Sweongyo 474246614Shselasky UPGT_LOCK(sc); 475246614Shselasky error = (sc->sc_flags & UPGT_FLAG_DETACHED) ? ENXIO : 0; 476246614Shselasky UPGT_UNLOCK(sc); 477246614Shselasky if (error) 478246614Shselasky return (error); 479246614Shselasky 480191983Sweongyo switch (cmd) { 481191983Sweongyo case SIOCSIFFLAGS: 482191983Sweongyo if (ifp->if_flags & IFF_UP) { 483191983Sweongyo if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 484191983Sweongyo if ((ifp->if_flags ^ sc->sc_if_flags) & 485191983Sweongyo (IFF_ALLMULTI | IFF_PROMISC)) 486191983Sweongyo upgt_set_multi(sc); 487191983Sweongyo } else { 488191983Sweongyo upgt_init(sc); 489191983Sweongyo startall = 1; 490191983Sweongyo } 491191983Sweongyo } else { 492191983Sweongyo if (ifp->if_drv_flags & IFF_DRV_RUNNING) 493191983Sweongyo upgt_stop(sc); 494191983Sweongyo } 495191983Sweongyo sc->sc_if_flags = ifp->if_flags; 496191983Sweongyo if (startall) 497191983Sweongyo ieee80211_start_all(ic); 498191983Sweongyo break; 499191983Sweongyo case SIOCGIFMEDIA: 500191983Sweongyo error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 501191983Sweongyo break; 502191983Sweongyo case SIOCGIFADDR: 503191983Sweongyo error = ether_ioctl(ifp, cmd, data); 504191983Sweongyo break; 505191983Sweongyo default: 506191983Sweongyo error = EINVAL; 507191983Sweongyo break; 508191983Sweongyo } 509191983Sweongyo return error; 510191983Sweongyo} 511191983Sweongyo 512191983Sweongyostatic void 513191983Sweongyoupgt_stop_locked(struct upgt_softc *sc) 514191983Sweongyo{ 515191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 516191983Sweongyo 517191983Sweongyo UPGT_ASSERT_LOCKED(sc); 518191983Sweongyo 519191983Sweongyo if (ifp->if_drv_flags & IFF_DRV_RUNNING) 520191983Sweongyo upgt_set_macfilter(sc, IEEE80211_S_INIT); 521191983Sweongyo upgt_abort_xfers_locked(sc); 522191983Sweongyo} 523191983Sweongyo 524191983Sweongyostatic void 525191983Sweongyoupgt_stop(struct upgt_softc *sc) 526191983Sweongyo{ 527191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 528191983Sweongyo 529191983Sweongyo UPGT_LOCK(sc); 530191983Sweongyo upgt_stop_locked(sc); 531191983Sweongyo UPGT_UNLOCK(sc); 532191983Sweongyo 533191983Sweongyo /* device down */ 534191983Sweongyo sc->sc_tx_timer = 0; 535191983Sweongyo ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 536191983Sweongyo sc->sc_flags &= ~UPGT_FLAG_INITDONE; 537191983Sweongyo} 538191983Sweongyo 539191983Sweongyostatic void 540191983Sweongyoupgt_set_led(struct upgt_softc *sc, int action) 541191983Sweongyo{ 542191983Sweongyo struct upgt_data *data_cmd; 543191983Sweongyo struct upgt_lmac_mem *mem; 544191983Sweongyo struct upgt_lmac_led *led; 545191983Sweongyo 546191983Sweongyo data_cmd = upgt_getbuf(sc); 547191983Sweongyo if (data_cmd == NULL) { 548191983Sweongyo device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 549191983Sweongyo return; 550191983Sweongyo } 551191983Sweongyo 552191983Sweongyo /* 553191983Sweongyo * Transmit the URB containing the CMD data. 554191983Sweongyo */ 555227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 556191983Sweongyo 557191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 558191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 559191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 560191983Sweongyo 561191983Sweongyo led = (struct upgt_lmac_led *)(mem + 1); 562191983Sweongyo 563191983Sweongyo led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 564191983Sweongyo led->header1.type = UPGT_H1_TYPE_CTRL; 565191983Sweongyo led->header1.len = htole16( 566191983Sweongyo sizeof(struct upgt_lmac_led) - 567191983Sweongyo sizeof(struct upgt_lmac_header)); 568191983Sweongyo 569191983Sweongyo led->header2.reqid = htole32(sc->sc_memaddr_frame_start); 570191983Sweongyo led->header2.type = htole16(UPGT_H2_TYPE_LED); 571191983Sweongyo led->header2.flags = 0; 572191983Sweongyo 573191983Sweongyo switch (action) { 574191983Sweongyo case UPGT_LED_OFF: 575191983Sweongyo led->mode = htole16(UPGT_LED_MODE_SET); 576191983Sweongyo led->action_fix = 0; 577191983Sweongyo led->action_tmp = htole16(UPGT_LED_ACTION_OFF); 578191983Sweongyo led->action_tmp_dur = 0; 579191983Sweongyo break; 580191983Sweongyo case UPGT_LED_ON: 581191983Sweongyo led->mode = htole16(UPGT_LED_MODE_SET); 582191983Sweongyo led->action_fix = 0; 583191983Sweongyo led->action_tmp = htole16(UPGT_LED_ACTION_ON); 584191983Sweongyo led->action_tmp_dur = 0; 585191983Sweongyo break; 586191983Sweongyo case UPGT_LED_BLINK: 587191983Sweongyo if (sc->sc_state != IEEE80211_S_RUN) { 588191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 589191983Sweongyo return; 590191983Sweongyo } 591191983Sweongyo if (sc->sc_led_blink) { 592191983Sweongyo /* previous blink was not finished */ 593191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 594191983Sweongyo return; 595191983Sweongyo } 596191983Sweongyo led->mode = htole16(UPGT_LED_MODE_SET); 597191983Sweongyo led->action_fix = htole16(UPGT_LED_ACTION_OFF); 598191983Sweongyo led->action_tmp = htole16(UPGT_LED_ACTION_ON); 599191983Sweongyo led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR); 600191983Sweongyo /* lock blink */ 601191983Sweongyo sc->sc_led_blink = 1; 602191983Sweongyo callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc); 603191983Sweongyo break; 604191983Sweongyo default: 605191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 606191983Sweongyo return; 607191983Sweongyo } 608191983Sweongyo 609191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*led); 610191983Sweongyo 611191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)led, 612191983Sweongyo data_cmd->buflen - sizeof(*mem)); 613191983Sweongyo 614191983Sweongyo upgt_bulk_tx(sc, data_cmd); 615191983Sweongyo} 616191983Sweongyo 617191983Sweongyostatic void 618191983Sweongyoupgt_set_led_blink(void *arg) 619191983Sweongyo{ 620191983Sweongyo struct upgt_softc *sc = arg; 621191983Sweongyo 622191983Sweongyo /* blink finished, we are ready for a next one */ 623191983Sweongyo sc->sc_led_blink = 0; 624191983Sweongyo} 625191983Sweongyo 626191983Sweongyostatic void 627191983Sweongyoupgt_init(void *priv) 628191983Sweongyo{ 629191983Sweongyo struct upgt_softc *sc = priv; 630191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 631191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 632191983Sweongyo 633191983Sweongyo UPGT_LOCK(sc); 634191983Sweongyo upgt_init_locked(sc); 635191983Sweongyo UPGT_UNLOCK(sc); 636191983Sweongyo 637191983Sweongyo if (ifp->if_drv_flags & IFF_DRV_RUNNING) 638191983Sweongyo ieee80211_start_all(ic); /* start all vap's */ 639191983Sweongyo} 640191983Sweongyo 641191983Sweongyostatic void 642191983Sweongyoupgt_init_locked(struct upgt_softc *sc) 643191983Sweongyo{ 644191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 645191983Sweongyo 646191983Sweongyo UPGT_ASSERT_LOCKED(sc); 647191983Sweongyo 648191983Sweongyo if (ifp->if_drv_flags & IFF_DRV_RUNNING) 649191983Sweongyo upgt_stop_locked(sc); 650191983Sweongyo 651194228Sthompsa usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]); 652191983Sweongyo 653191983Sweongyo (void)upgt_set_macfilter(sc, IEEE80211_S_SCAN); 654191983Sweongyo 655191983Sweongyo ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 656191983Sweongyo ifp->if_drv_flags |= IFF_DRV_RUNNING; 657191983Sweongyo sc->sc_flags |= UPGT_FLAG_INITDONE; 658191983Sweongyo 659191983Sweongyo callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc); 660191983Sweongyo} 661191983Sweongyo 662191983Sweongyostatic int 663191983Sweongyoupgt_set_macfilter(struct upgt_softc *sc, uint8_t state) 664191983Sweongyo{ 665191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 666191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 667191983Sweongyo struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 668212131Sthompsa struct ieee80211_node *ni; 669191983Sweongyo struct upgt_data *data_cmd; 670191983Sweongyo struct upgt_lmac_mem *mem; 671191983Sweongyo struct upgt_lmac_filter *filter; 672191983Sweongyo uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 673191983Sweongyo 674191983Sweongyo UPGT_ASSERT_LOCKED(sc); 675191983Sweongyo 676191983Sweongyo data_cmd = upgt_getbuf(sc); 677191983Sweongyo if (data_cmd == NULL) { 678191983Sweongyo device_printf(sc->sc_dev, "out of TX buffers.\n"); 679191983Sweongyo return (ENOBUFS); 680191983Sweongyo } 681191983Sweongyo 682191983Sweongyo /* 683191983Sweongyo * Transmit the URB containing the CMD data. 684191983Sweongyo */ 685227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 686191983Sweongyo 687191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 688191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 689191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 690191983Sweongyo 691191983Sweongyo filter = (struct upgt_lmac_filter *)(mem + 1); 692191983Sweongyo 693191983Sweongyo filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 694191983Sweongyo filter->header1.type = UPGT_H1_TYPE_CTRL; 695191983Sweongyo filter->header1.len = htole16( 696191983Sweongyo sizeof(struct upgt_lmac_filter) - 697191983Sweongyo sizeof(struct upgt_lmac_header)); 698191983Sweongyo 699191983Sweongyo filter->header2.reqid = htole32(sc->sc_memaddr_frame_start); 700191983Sweongyo filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER); 701191983Sweongyo filter->header2.flags = 0; 702191983Sweongyo 703191983Sweongyo switch (state) { 704191983Sweongyo case IEEE80211_S_INIT: 705191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n", 706191983Sweongyo __func__); 707191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_RESET); 708191983Sweongyo break; 709191983Sweongyo case IEEE80211_S_SCAN: 710191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, 711191983Sweongyo "set MAC filter to SCAN (bssid %s)\n", 712191983Sweongyo ether_sprintf(broadcast)); 713191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_NONE); 714191983Sweongyo IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr); 715191983Sweongyo IEEE80211_ADDR_COPY(filter->src, broadcast); 716191983Sweongyo filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 717191983Sweongyo filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 718191983Sweongyo filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 719191983Sweongyo filter->rxhw = htole32(sc->sc_eeprom_hwrx); 720191983Sweongyo filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 721191983Sweongyo break; 722191983Sweongyo case IEEE80211_S_RUN: 723212127Sthompsa ni = ieee80211_ref_node(vap->iv_bss); 724191983Sweongyo /* XXX monitor mode isn't tested yet. */ 725191983Sweongyo if (vap->iv_opmode == IEEE80211_M_MONITOR) { 726191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_MONITOR); 727191983Sweongyo IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr); 728191983Sweongyo IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid); 729191983Sweongyo filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1); 730191983Sweongyo filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 731191983Sweongyo filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2); 732191983Sweongyo filter->rxhw = htole32(sc->sc_eeprom_hwrx); 733191983Sweongyo filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3); 734191983Sweongyo } else { 735191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, 736191983Sweongyo "set MAC filter to RUN (bssid %s)\n", 737191983Sweongyo ether_sprintf(ni->ni_bssid)); 738191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_STA); 739191983Sweongyo IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr); 740191983Sweongyo IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid); 741191983Sweongyo filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 742191983Sweongyo filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 743191983Sweongyo filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 744191983Sweongyo filter->rxhw = htole32(sc->sc_eeprom_hwrx); 745191983Sweongyo filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 746191983Sweongyo } 747212127Sthompsa ieee80211_free_node(ni); 748191983Sweongyo break; 749191983Sweongyo default: 750191983Sweongyo device_printf(sc->sc_dev, 751199816Sthompsa "MAC filter does not know that state\n"); 752191983Sweongyo break; 753191983Sweongyo } 754191983Sweongyo 755191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*filter); 756191983Sweongyo 757191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)filter, 758191983Sweongyo data_cmd->buflen - sizeof(*mem)); 759191983Sweongyo 760191983Sweongyo upgt_bulk_tx(sc, data_cmd); 761191983Sweongyo 762191983Sweongyo return (0); 763191983Sweongyo} 764191983Sweongyo 765191983Sweongyostatic void 766191983Sweongyoupgt_setup_rates(struct ieee80211vap *vap, struct ieee80211com *ic) 767191983Sweongyo{ 768191983Sweongyo struct ifnet *ifp = ic->ic_ifp; 769191983Sweongyo struct upgt_softc *sc = ifp->if_softc; 770191983Sweongyo const struct ieee80211_txparam *tp; 771191983Sweongyo 772191983Sweongyo /* 773191983Sweongyo * 0x01 = OFMD6 0x10 = DS1 774191983Sweongyo * 0x04 = OFDM9 0x11 = DS2 775191983Sweongyo * 0x06 = OFDM12 0x12 = DS5 776191983Sweongyo * 0x07 = OFDM18 0x13 = DS11 777191983Sweongyo * 0x08 = OFDM24 778191983Sweongyo * 0x09 = OFDM36 779191983Sweongyo * 0x0a = OFDM48 780191983Sweongyo * 0x0b = OFDM54 781191983Sweongyo */ 782191983Sweongyo const uint8_t rateset_auto_11b[] = 783191983Sweongyo { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 }; 784191983Sweongyo const uint8_t rateset_auto_11g[] = 785191983Sweongyo { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 }; 786191983Sweongyo const uint8_t rateset_fix_11bg[] = 787191983Sweongyo { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07, 788191983Sweongyo 0x08, 0x09, 0x0a, 0x0b }; 789191983Sweongyo 790191983Sweongyo tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 791191983Sweongyo 792191983Sweongyo /* XXX */ 793191983Sweongyo if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 794191983Sweongyo /* 795191983Sweongyo * Automatic rate control is done by the device. 796191983Sweongyo * We just pass the rateset from which the device 797191983Sweongyo * will pickup a rate. 798191983Sweongyo */ 799191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11B) 800227461Shselasky memcpy(sc->sc_cur_rateset, rateset_auto_11b, 801191983Sweongyo sizeof(sc->sc_cur_rateset)); 802191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11G || 803191983Sweongyo ic->ic_curmode == IEEE80211_MODE_AUTO) 804227461Shselasky memcpy(sc->sc_cur_rateset, rateset_auto_11g, 805191983Sweongyo sizeof(sc->sc_cur_rateset)); 806191983Sweongyo } else { 807191983Sweongyo /* set a fixed rate */ 808191983Sweongyo memset(sc->sc_cur_rateset, rateset_fix_11bg[tp->ucastrate], 809191983Sweongyo sizeof(sc->sc_cur_rateset)); 810191983Sweongyo } 811191983Sweongyo} 812191983Sweongyo 813191983Sweongyostatic void 814191983Sweongyoupgt_set_multi(void *arg) 815191983Sweongyo{ 816191983Sweongyo struct upgt_softc *sc = arg; 817191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 818191983Sweongyo 819191983Sweongyo if (!(ifp->if_flags & IFF_UP)) 820191983Sweongyo return; 821191983Sweongyo 822191983Sweongyo /* 823191983Sweongyo * XXX don't know how to set a device. Lack of docs. Just try to set 824191983Sweongyo * IFF_ALLMULTI flag here. 825191983Sweongyo */ 826191983Sweongyo ifp->if_flags |= IFF_ALLMULTI; 827191983Sweongyo} 828191983Sweongyo 829191983Sweongyostatic void 830191983Sweongyoupgt_start(struct ifnet *ifp) 831191983Sweongyo{ 832191983Sweongyo struct upgt_softc *sc = ifp->if_softc; 833191983Sweongyo struct upgt_data *data_tx; 834191983Sweongyo struct ieee80211_node *ni; 835191983Sweongyo struct mbuf *m; 836191983Sweongyo 837191983Sweongyo if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 838191983Sweongyo return; 839191983Sweongyo 840191983Sweongyo UPGT_LOCK(sc); 841191983Sweongyo for (;;) { 842191983Sweongyo IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 843191983Sweongyo if (m == NULL) 844191983Sweongyo break; 845191983Sweongyo 846191983Sweongyo data_tx = upgt_gettxbuf(sc); 847191983Sweongyo if (data_tx == NULL) { 848191983Sweongyo IFQ_DRV_PREPEND(&ifp->if_snd, m); 849191983Sweongyo break; 850191983Sweongyo } 851191983Sweongyo 852191983Sweongyo ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 853191983Sweongyo m->m_pkthdr.rcvif = NULL; 854191983Sweongyo 855191983Sweongyo if (upgt_tx_start(sc, m, ni, data_tx) != 0) { 856191983Sweongyo STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next); 857191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 858191983Sweongyo ieee80211_free_node(ni); 859271866Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 860191983Sweongyo continue; 861191983Sweongyo } 862191983Sweongyo sc->sc_tx_timer = 5; 863191983Sweongyo } 864191983Sweongyo UPGT_UNLOCK(sc); 865191983Sweongyo} 866191983Sweongyo 867191983Sweongyostatic int 868191983Sweongyoupgt_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 869191983Sweongyo const struct ieee80211_bpf_params *params) 870191983Sweongyo{ 871191983Sweongyo struct ieee80211com *ic = ni->ni_ic; 872191983Sweongyo struct ifnet *ifp = ic->ic_ifp; 873191983Sweongyo struct upgt_softc *sc = ifp->if_softc; 874191983Sweongyo struct upgt_data *data_tx = NULL; 875191983Sweongyo 876191983Sweongyo /* prevent management frames from being sent if we're not ready */ 877191983Sweongyo if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 878191983Sweongyo m_freem(m); 879191983Sweongyo ieee80211_free_node(ni); 880191983Sweongyo return ENETDOWN; 881191983Sweongyo } 882191983Sweongyo 883191983Sweongyo UPGT_LOCK(sc); 884191983Sweongyo data_tx = upgt_gettxbuf(sc); 885191983Sweongyo if (data_tx == NULL) { 886191983Sweongyo ieee80211_free_node(ni); 887191983Sweongyo m_freem(m); 888191983Sweongyo UPGT_UNLOCK(sc); 889191983Sweongyo return (ENOBUFS); 890191983Sweongyo } 891191983Sweongyo 892191983Sweongyo if (upgt_tx_start(sc, m, ni, data_tx) != 0) { 893191983Sweongyo STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next); 894191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 895191983Sweongyo ieee80211_free_node(ni); 896271866Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 897191983Sweongyo UPGT_UNLOCK(sc); 898191983Sweongyo return (EIO); 899191983Sweongyo } 900191983Sweongyo UPGT_UNLOCK(sc); 901191983Sweongyo 902191983Sweongyo sc->sc_tx_timer = 5; 903191983Sweongyo return (0); 904191983Sweongyo} 905191983Sweongyo 906191983Sweongyostatic void 907191983Sweongyoupgt_watchdog(void *arg) 908191983Sweongyo{ 909191983Sweongyo struct upgt_softc *sc = arg; 910191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 911191983Sweongyo 912191983Sweongyo if (sc->sc_tx_timer > 0) { 913191983Sweongyo if (--sc->sc_tx_timer == 0) { 914191983Sweongyo device_printf(sc->sc_dev, "watchdog timeout\n"); 915191983Sweongyo /* upgt_init(ifp); XXX needs a process context ? */ 916271866Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 917191983Sweongyo return; 918191983Sweongyo } 919191983Sweongyo callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc); 920191983Sweongyo } 921191983Sweongyo} 922191983Sweongyo 923191983Sweongyostatic uint32_t 924191983Sweongyoupgt_mem_alloc(struct upgt_softc *sc) 925191983Sweongyo{ 926191983Sweongyo int i; 927191983Sweongyo 928191983Sweongyo for (i = 0; i < sc->sc_memory.pages; i++) { 929191983Sweongyo if (sc->sc_memory.page[i].used == 0) { 930191983Sweongyo sc->sc_memory.page[i].used = 1; 931191983Sweongyo return (sc->sc_memory.page[i].addr); 932191983Sweongyo } 933191983Sweongyo } 934191983Sweongyo 935191983Sweongyo return (0); 936191983Sweongyo} 937191983Sweongyo 938191983Sweongyostatic void 939191983Sweongyoupgt_scan_start(struct ieee80211com *ic) 940191983Sweongyo{ 941191983Sweongyo /* do nothing. */ 942191983Sweongyo} 943191983Sweongyo 944191983Sweongyostatic void 945191983Sweongyoupgt_scan_end(struct ieee80211com *ic) 946191983Sweongyo{ 947191983Sweongyo /* do nothing. */ 948191983Sweongyo} 949191983Sweongyo 950191983Sweongyostatic void 951191983Sweongyoupgt_set_channel(struct ieee80211com *ic) 952191983Sweongyo{ 953191983Sweongyo struct upgt_softc *sc = ic->ic_ifp->if_softc; 954191983Sweongyo 955191983Sweongyo UPGT_LOCK(sc); 956191983Sweongyo upgt_set_chan(sc, ic->ic_curchan); 957191983Sweongyo UPGT_UNLOCK(sc); 958191983Sweongyo} 959191983Sweongyo 960191983Sweongyostatic void 961191983Sweongyoupgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c) 962191983Sweongyo{ 963191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 964191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 965191983Sweongyo struct upgt_data *data_cmd; 966191983Sweongyo struct upgt_lmac_mem *mem; 967191983Sweongyo struct upgt_lmac_channel *chan; 968191983Sweongyo int channel; 969191983Sweongyo 970191983Sweongyo UPGT_ASSERT_LOCKED(sc); 971191983Sweongyo 972191983Sweongyo channel = ieee80211_chan2ieee(ic, c); 973191983Sweongyo if (channel == 0 || channel == IEEE80211_CHAN_ANY) { 974191983Sweongyo /* XXX should NEVER happen */ 975191983Sweongyo device_printf(sc->sc_dev, 976191983Sweongyo "%s: invalid channel %x\n", __func__, channel); 977191983Sweongyo return; 978191983Sweongyo } 979191983Sweongyo 980191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel); 981191983Sweongyo 982191983Sweongyo data_cmd = upgt_getbuf(sc); 983191983Sweongyo if (data_cmd == NULL) { 984191983Sweongyo device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 985191983Sweongyo return; 986191983Sweongyo } 987191983Sweongyo /* 988191983Sweongyo * Transmit the URB containing the CMD data. 989191983Sweongyo */ 990227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 991191983Sweongyo 992191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 993191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 994191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 995191983Sweongyo 996191983Sweongyo chan = (struct upgt_lmac_channel *)(mem + 1); 997191983Sweongyo 998191983Sweongyo chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 999191983Sweongyo chan->header1.type = UPGT_H1_TYPE_CTRL; 1000191983Sweongyo chan->header1.len = htole16( 1001191983Sweongyo sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header)); 1002191983Sweongyo 1003191983Sweongyo chan->header2.reqid = htole32(sc->sc_memaddr_frame_start); 1004191983Sweongyo chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL); 1005191983Sweongyo chan->header2.flags = 0; 1006191983Sweongyo 1007191983Sweongyo chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1); 1008191983Sweongyo chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2); 1009191983Sweongyo chan->freq6 = sc->sc_eeprom_freq6[channel]; 1010191983Sweongyo chan->settings = sc->sc_eeprom_freq6_settings; 1011191983Sweongyo chan->unknown3 = UPGT_CHANNEL_UNKNOWN3; 1012191983Sweongyo 1013227461Shselasky memcpy(chan->freq3_1, &sc->sc_eeprom_freq3[channel].data, 1014191983Sweongyo sizeof(chan->freq3_1)); 1015227461Shselasky memcpy(chan->freq4, &sc->sc_eeprom_freq4[channel], 1016191983Sweongyo sizeof(sc->sc_eeprom_freq4[channel])); 1017227461Shselasky memcpy(chan->freq3_2, &sc->sc_eeprom_freq3[channel].data, 1018191983Sweongyo sizeof(chan->freq3_2)); 1019191983Sweongyo 1020191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*chan); 1021191983Sweongyo 1022191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)chan, 1023191983Sweongyo data_cmd->buflen - sizeof(*mem)); 1024191983Sweongyo 1025191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1026191983Sweongyo} 1027191983Sweongyo 1028191983Sweongyostatic struct ieee80211vap * 1029228621Sbschmidtupgt_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 1030228621Sbschmidt enum ieee80211_opmode opmode, int flags, 1031228621Sbschmidt const uint8_t bssid[IEEE80211_ADDR_LEN], 1032228621Sbschmidt const uint8_t mac[IEEE80211_ADDR_LEN]) 1033191983Sweongyo{ 1034191983Sweongyo struct upgt_vap *uvp; 1035191983Sweongyo struct ieee80211vap *vap; 1036191983Sweongyo 1037191983Sweongyo if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 1038191983Sweongyo return NULL; 1039191983Sweongyo uvp = (struct upgt_vap *) malloc(sizeof(struct upgt_vap), 1040191983Sweongyo M_80211_VAP, M_NOWAIT | M_ZERO); 1041191983Sweongyo if (uvp == NULL) 1042191983Sweongyo return NULL; 1043191983Sweongyo vap = &uvp->vap; 1044191983Sweongyo /* enable s/w bmiss handling for sta mode */ 1045191983Sweongyo 1046257743Shselasky if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 1047257743Shselasky flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) { 1048257743Shselasky /* out of memory */ 1049257743Shselasky free(uvp, M_80211_VAP); 1050257743Shselasky return (NULL); 1051257743Shselasky } 1052257743Shselasky 1053191983Sweongyo /* override state transition machine */ 1054191983Sweongyo uvp->newstate = vap->iv_newstate; 1055191983Sweongyo vap->iv_newstate = upgt_newstate; 1056191983Sweongyo 1057191983Sweongyo /* setup device rates */ 1058191983Sweongyo upgt_setup_rates(vap, ic); 1059191983Sweongyo 1060191983Sweongyo /* complete setup */ 1061191983Sweongyo ieee80211_vap_attach(vap, ieee80211_media_change, 1062191983Sweongyo ieee80211_media_status); 1063191983Sweongyo ic->ic_opmode = opmode; 1064191983Sweongyo return vap; 1065191983Sweongyo} 1066191983Sweongyo 1067191983Sweongyostatic int 1068191983Sweongyoupgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1069191983Sweongyo{ 1070191983Sweongyo struct upgt_vap *uvp = UPGT_VAP(vap); 1071191983Sweongyo struct ieee80211com *ic = vap->iv_ic; 1072191983Sweongyo struct upgt_softc *sc = ic->ic_ifp->if_softc; 1073191983Sweongyo 1074191983Sweongyo /* do it in a process context */ 1075191983Sweongyo sc->sc_state = nstate; 1076191983Sweongyo 1077191983Sweongyo IEEE80211_UNLOCK(ic); 1078191983Sweongyo UPGT_LOCK(sc); 1079191983Sweongyo callout_stop(&sc->sc_led_ch); 1080191983Sweongyo callout_stop(&sc->sc_watchdog_ch); 1081191983Sweongyo 1082191983Sweongyo switch (nstate) { 1083191983Sweongyo case IEEE80211_S_INIT: 1084191983Sweongyo /* do not accept any frames if the device is down */ 1085191983Sweongyo (void)upgt_set_macfilter(sc, sc->sc_state); 1086191983Sweongyo upgt_set_led(sc, UPGT_LED_OFF); 1087191983Sweongyo break; 1088191983Sweongyo case IEEE80211_S_SCAN: 1089191983Sweongyo upgt_set_chan(sc, ic->ic_curchan); 1090191983Sweongyo break; 1091191983Sweongyo case IEEE80211_S_AUTH: 1092191983Sweongyo upgt_set_chan(sc, ic->ic_curchan); 1093191983Sweongyo break; 1094191983Sweongyo case IEEE80211_S_ASSOC: 1095191983Sweongyo break; 1096191983Sweongyo case IEEE80211_S_RUN: 1097191983Sweongyo upgt_set_macfilter(sc, sc->sc_state); 1098191983Sweongyo upgt_set_led(sc, UPGT_LED_ON); 1099191983Sweongyo break; 1100191983Sweongyo default: 1101191983Sweongyo break; 1102191983Sweongyo } 1103191983Sweongyo UPGT_UNLOCK(sc); 1104191983Sweongyo IEEE80211_LOCK(ic); 1105191983Sweongyo return (uvp->newstate(vap, nstate, arg)); 1106191983Sweongyo} 1107191983Sweongyo 1108191983Sweongyostatic void 1109191983Sweongyoupgt_vap_delete(struct ieee80211vap *vap) 1110191983Sweongyo{ 1111191983Sweongyo struct upgt_vap *uvp = UPGT_VAP(vap); 1112191983Sweongyo 1113191983Sweongyo ieee80211_vap_detach(vap); 1114191983Sweongyo free(uvp, M_80211_VAP); 1115191983Sweongyo} 1116191983Sweongyo 1117191983Sweongyostatic void 1118283540Sglebiusupgt_update_mcast(struct ieee80211com *ic) 1119191983Sweongyo{ 1120283540Sglebius struct upgt_softc *sc = ic->ic_softc; 1121191983Sweongyo 1122191983Sweongyo upgt_set_multi(sc); 1123191983Sweongyo} 1124191983Sweongyo 1125191983Sweongyostatic int 1126191983Sweongyoupgt_eeprom_parse(struct upgt_softc *sc) 1127191983Sweongyo{ 1128191983Sweongyo struct upgt_eeprom_header *eeprom_header; 1129191983Sweongyo struct upgt_eeprom_option *eeprom_option; 1130191983Sweongyo uint16_t option_len; 1131191983Sweongyo uint16_t option_type; 1132191983Sweongyo uint16_t preamble_len; 1133191983Sweongyo int option_end = 0; 1134191983Sweongyo 1135191983Sweongyo /* calculate eeprom options start offset */ 1136191983Sweongyo eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom; 1137191983Sweongyo preamble_len = le16toh(eeprom_header->preamble_len); 1138191983Sweongyo eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom + 1139191983Sweongyo (sizeof(struct upgt_eeprom_header) + preamble_len)); 1140191983Sweongyo 1141191983Sweongyo while (!option_end) { 1142246944Shselasky 1143246944Shselasky /* sanity check */ 1144246944Shselasky if (eeprom_option >= (struct upgt_eeprom_option *) 1145246944Shselasky (sc->sc_eeprom + UPGT_EEPROM_SIZE)) { 1146246944Shselasky return (EINVAL); 1147246944Shselasky } 1148246944Shselasky 1149191983Sweongyo /* the eeprom option length is stored in words */ 1150191983Sweongyo option_len = 1151191983Sweongyo (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t); 1152191983Sweongyo option_type = 1153191983Sweongyo le16toh(eeprom_option->type); 1154191983Sweongyo 1155246944Shselasky /* sanity check */ 1156246944Shselasky if (option_len == 0 || option_len >= UPGT_EEPROM_SIZE) 1157246944Shselasky return (EINVAL); 1158246944Shselasky 1159191983Sweongyo switch (option_type) { 1160191983Sweongyo case UPGT_EEPROM_TYPE_NAME: 1161191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1162191983Sweongyo "EEPROM name len=%d\n", option_len); 1163191983Sweongyo break; 1164191983Sweongyo case UPGT_EEPROM_TYPE_SERIAL: 1165191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1166191983Sweongyo "EEPROM serial len=%d\n", option_len); 1167191983Sweongyo break; 1168191983Sweongyo case UPGT_EEPROM_TYPE_MAC: 1169191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1170191983Sweongyo "EEPROM mac len=%d\n", option_len); 1171191983Sweongyo 1172191983Sweongyo IEEE80211_ADDR_COPY(sc->sc_myaddr, eeprom_option->data); 1173191983Sweongyo break; 1174191983Sweongyo case UPGT_EEPROM_TYPE_HWRX: 1175191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1176191983Sweongyo "EEPROM hwrx len=%d\n", option_len); 1177191983Sweongyo 1178191983Sweongyo upgt_eeprom_parse_hwrx(sc, eeprom_option->data); 1179191983Sweongyo break; 1180191983Sweongyo case UPGT_EEPROM_TYPE_CHIP: 1181191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1182191983Sweongyo "EEPROM chip len=%d\n", option_len); 1183191983Sweongyo break; 1184191983Sweongyo case UPGT_EEPROM_TYPE_FREQ3: 1185191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1186191983Sweongyo "EEPROM freq3 len=%d\n", option_len); 1187191983Sweongyo 1188191983Sweongyo upgt_eeprom_parse_freq3(sc, eeprom_option->data, 1189191983Sweongyo option_len); 1190191983Sweongyo break; 1191191983Sweongyo case UPGT_EEPROM_TYPE_FREQ4: 1192191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1193191983Sweongyo "EEPROM freq4 len=%d\n", option_len); 1194191983Sweongyo 1195191983Sweongyo upgt_eeprom_parse_freq4(sc, eeprom_option->data, 1196191983Sweongyo option_len); 1197191983Sweongyo break; 1198191983Sweongyo case UPGT_EEPROM_TYPE_FREQ5: 1199191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1200191983Sweongyo "EEPROM freq5 len=%d\n", option_len); 1201191983Sweongyo break; 1202191983Sweongyo case UPGT_EEPROM_TYPE_FREQ6: 1203191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1204191983Sweongyo "EEPROM freq6 len=%d\n", option_len); 1205191983Sweongyo 1206191983Sweongyo upgt_eeprom_parse_freq6(sc, eeprom_option->data, 1207191983Sweongyo option_len); 1208191983Sweongyo break; 1209191983Sweongyo case UPGT_EEPROM_TYPE_END: 1210191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1211191983Sweongyo "EEPROM end len=%d\n", option_len); 1212191983Sweongyo option_end = 1; 1213191983Sweongyo break; 1214191983Sweongyo case UPGT_EEPROM_TYPE_OFF: 1215191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1216199816Sthompsa "%s: EEPROM off without end option\n", __func__); 1217191983Sweongyo return (EIO); 1218191983Sweongyo default: 1219191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1220191983Sweongyo "EEPROM unknown type 0x%04x len=%d\n", 1221191983Sweongyo option_type, option_len); 1222191983Sweongyo break; 1223191983Sweongyo } 1224191983Sweongyo 1225191983Sweongyo /* jump to next EEPROM option */ 1226191983Sweongyo eeprom_option = (struct upgt_eeprom_option *) 1227191983Sweongyo (eeprom_option->data + option_len); 1228191983Sweongyo } 1229191983Sweongyo return (0); 1230191983Sweongyo} 1231191983Sweongyo 1232191983Sweongyostatic void 1233191983Sweongyoupgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len) 1234191983Sweongyo{ 1235191983Sweongyo struct upgt_eeprom_freq3_header *freq3_header; 1236191983Sweongyo struct upgt_lmac_freq3 *freq3; 1237246944Shselasky int i; 1238246944Shselasky int elements; 1239246944Shselasky int flags; 1240191983Sweongyo unsigned channel; 1241191983Sweongyo 1242191983Sweongyo freq3_header = (struct upgt_eeprom_freq3_header *)data; 1243191983Sweongyo freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1); 1244191983Sweongyo 1245191983Sweongyo flags = freq3_header->flags; 1246191983Sweongyo elements = freq3_header->elements; 1247191983Sweongyo 1248191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n", 1249191983Sweongyo flags, elements); 1250191983Sweongyo 1251246944Shselasky if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq3[0]))) 1252246944Shselasky return; 1253246944Shselasky 1254191983Sweongyo for (i = 0; i < elements; i++) { 1255191983Sweongyo channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0); 1256233774Shselasky if (channel >= IEEE80211_CHAN_MAX) 1257191983Sweongyo continue; 1258191983Sweongyo 1259191983Sweongyo sc->sc_eeprom_freq3[channel] = freq3[i]; 1260191983Sweongyo 1261191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1262191983Sweongyo le16toh(sc->sc_eeprom_freq3[channel].freq), channel); 1263191983Sweongyo } 1264191983Sweongyo} 1265191983Sweongyo 1266191983Sweongyovoid 1267191983Sweongyoupgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len) 1268191983Sweongyo{ 1269191983Sweongyo struct upgt_eeprom_freq4_header *freq4_header; 1270191983Sweongyo struct upgt_eeprom_freq4_1 *freq4_1; 1271191983Sweongyo struct upgt_eeprom_freq4_2 *freq4_2; 1272246944Shselasky int i; 1273246944Shselasky int j; 1274246944Shselasky int elements; 1275246944Shselasky int settings; 1276246944Shselasky int flags; 1277191983Sweongyo unsigned channel; 1278191983Sweongyo 1279191983Sweongyo freq4_header = (struct upgt_eeprom_freq4_header *)data; 1280191983Sweongyo freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1); 1281191983Sweongyo flags = freq4_header->flags; 1282191983Sweongyo elements = freq4_header->elements; 1283191983Sweongyo settings = freq4_header->settings; 1284191983Sweongyo 1285191983Sweongyo /* we need this value later */ 1286191983Sweongyo sc->sc_eeprom_freq6_settings = freq4_header->settings; 1287191983Sweongyo 1288191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n", 1289191983Sweongyo flags, elements, settings); 1290191983Sweongyo 1291246944Shselasky if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq4_1[0]))) 1292246944Shselasky return; 1293246944Shselasky 1294191983Sweongyo for (i = 0; i < elements; i++) { 1295191983Sweongyo channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0); 1296233774Shselasky if (channel >= IEEE80211_CHAN_MAX) 1297191983Sweongyo continue; 1298191983Sweongyo 1299191983Sweongyo freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data; 1300191983Sweongyo for (j = 0; j < settings; j++) { 1301191983Sweongyo sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j]; 1302191983Sweongyo sc->sc_eeprom_freq4[channel][j].pad = 0; 1303191983Sweongyo } 1304191983Sweongyo 1305191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1306191983Sweongyo le16toh(freq4_1[i].freq), channel); 1307191983Sweongyo } 1308191983Sweongyo} 1309191983Sweongyo 1310191983Sweongyovoid 1311191983Sweongyoupgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len) 1312191983Sweongyo{ 1313191983Sweongyo struct upgt_lmac_freq6 *freq6; 1314246944Shselasky int i; 1315246944Shselasky int elements; 1316191983Sweongyo unsigned channel; 1317191983Sweongyo 1318191983Sweongyo freq6 = (struct upgt_lmac_freq6 *)data; 1319191983Sweongyo elements = len / sizeof(struct upgt_lmac_freq6); 1320191983Sweongyo 1321191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements); 1322191983Sweongyo 1323246944Shselasky if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq6[0]))) 1324246944Shselasky return; 1325246944Shselasky 1326191983Sweongyo for (i = 0; i < elements; i++) { 1327191983Sweongyo channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0); 1328233774Shselasky if (channel >= IEEE80211_CHAN_MAX) 1329191983Sweongyo continue; 1330191983Sweongyo 1331191983Sweongyo sc->sc_eeprom_freq6[channel] = freq6[i]; 1332191983Sweongyo 1333191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1334191983Sweongyo le16toh(sc->sc_eeprom_freq6[channel].freq), channel); 1335191983Sweongyo } 1336191983Sweongyo} 1337191983Sweongyo 1338191983Sweongyostatic void 1339191983Sweongyoupgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data) 1340191983Sweongyo{ 1341191983Sweongyo struct upgt_eeprom_option_hwrx *option_hwrx; 1342191983Sweongyo 1343191983Sweongyo option_hwrx = (struct upgt_eeprom_option_hwrx *)data; 1344191983Sweongyo 1345191983Sweongyo sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST; 1346191983Sweongyo 1347191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n", 1348191983Sweongyo sc->sc_eeprom_hwrx); 1349191983Sweongyo} 1350191983Sweongyo 1351191983Sweongyostatic int 1352191983Sweongyoupgt_eeprom_read(struct upgt_softc *sc) 1353191983Sweongyo{ 1354191983Sweongyo struct upgt_data *data_cmd; 1355191983Sweongyo struct upgt_lmac_mem *mem; 1356191983Sweongyo struct upgt_lmac_eeprom *eeprom; 1357191983Sweongyo int block, error, offset; 1358191983Sweongyo 1359191983Sweongyo UPGT_LOCK(sc); 1360194228Sthompsa usb_pause_mtx(&sc->sc_mtx, 100); 1361191983Sweongyo 1362191983Sweongyo offset = 0; 1363191983Sweongyo block = UPGT_EEPROM_BLOCK_SIZE; 1364191983Sweongyo while (offset < UPGT_EEPROM_SIZE) { 1365191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1366191983Sweongyo "request EEPROM block (offset=%d, len=%d)\n", offset, block); 1367191983Sweongyo 1368191983Sweongyo data_cmd = upgt_getbuf(sc); 1369191983Sweongyo if (data_cmd == NULL) { 1370191983Sweongyo UPGT_UNLOCK(sc); 1371191983Sweongyo return (ENOBUFS); 1372191983Sweongyo } 1373191983Sweongyo 1374191983Sweongyo /* 1375191983Sweongyo * Transmit the URB containing the CMD data. 1376191983Sweongyo */ 1377227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 1378191983Sweongyo 1379191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 1380191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 1381191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 1382191983Sweongyo 1383191983Sweongyo eeprom = (struct upgt_lmac_eeprom *)(mem + 1); 1384191983Sweongyo eeprom->header1.flags = 0; 1385191983Sweongyo eeprom->header1.type = UPGT_H1_TYPE_CTRL; 1386191983Sweongyo eeprom->header1.len = htole16(( 1387191983Sweongyo sizeof(struct upgt_lmac_eeprom) - 1388191983Sweongyo sizeof(struct upgt_lmac_header)) + block); 1389191983Sweongyo 1390191983Sweongyo eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start); 1391191983Sweongyo eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM); 1392191983Sweongyo eeprom->header2.flags = 0; 1393191983Sweongyo 1394191983Sweongyo eeprom->offset = htole16(offset); 1395191983Sweongyo eeprom->len = htole16(block); 1396191983Sweongyo 1397191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block; 1398191983Sweongyo 1399191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)eeprom, 1400191983Sweongyo data_cmd->buflen - sizeof(*mem)); 1401191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1402191983Sweongyo 1403191983Sweongyo error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz); 1404191983Sweongyo if (error != 0) { 1405191983Sweongyo device_printf(sc->sc_dev, 1406199816Sthompsa "timeout while waiting for EEPROM data\n"); 1407191983Sweongyo UPGT_UNLOCK(sc); 1408191983Sweongyo return (EIO); 1409191983Sweongyo } 1410191983Sweongyo 1411191983Sweongyo offset += block; 1412191983Sweongyo if (UPGT_EEPROM_SIZE - offset < block) 1413191983Sweongyo block = UPGT_EEPROM_SIZE - offset; 1414191983Sweongyo } 1415191983Sweongyo 1416191983Sweongyo UPGT_UNLOCK(sc); 1417191983Sweongyo return (0); 1418191983Sweongyo} 1419191983Sweongyo 1420191983Sweongyo/* 1421191983Sweongyo * When a rx data came in the function returns a mbuf and a rssi values. 1422191983Sweongyo */ 1423191983Sweongyostatic struct mbuf * 1424192984Sthompsaupgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi) 1425191983Sweongyo{ 1426191983Sweongyo struct mbuf *m = NULL; 1427194677Sthompsa struct upgt_softc *sc = usbd_xfer_softc(xfer); 1428191983Sweongyo struct upgt_lmac_header *header; 1429191983Sweongyo struct upgt_lmac_eeprom *eeprom; 1430191983Sweongyo uint8_t h1_type; 1431191983Sweongyo uint16_t h2_type; 1432194677Sthompsa int actlen, sumlen; 1433191983Sweongyo 1434194677Sthompsa usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1435194677Sthompsa 1436191983Sweongyo UPGT_ASSERT_LOCKED(sc); 1437191983Sweongyo 1438194677Sthompsa if (actlen < 1) 1439191983Sweongyo return (NULL); 1440191983Sweongyo 1441191983Sweongyo /* Check only at the very beginning. */ 1442191983Sweongyo if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) && 1443191983Sweongyo (memcmp(data->buf, "OK", 2) == 0)) { 1444191983Sweongyo sc->sc_flags |= UPGT_FLAG_FWLOADED; 1445191983Sweongyo wakeup_one(sc); 1446191983Sweongyo return (NULL); 1447191983Sweongyo } 1448191983Sweongyo 1449233774Shselasky if (actlen < (int)UPGT_RX_MINSZ) 1450191983Sweongyo return (NULL); 1451191983Sweongyo 1452191983Sweongyo /* 1453191983Sweongyo * Check what type of frame came in. 1454191983Sweongyo */ 1455191983Sweongyo header = (struct upgt_lmac_header *)(data->buf + 4); 1456191983Sweongyo 1457191983Sweongyo h1_type = header->header1.type; 1458191983Sweongyo h2_type = le16toh(header->header2.type); 1459191983Sweongyo 1460191983Sweongyo if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) { 1461191983Sweongyo eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4); 1462191983Sweongyo uint16_t eeprom_offset = le16toh(eeprom->offset); 1463191983Sweongyo uint16_t eeprom_len = le16toh(eeprom->len); 1464191983Sweongyo 1465191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1466191983Sweongyo "received EEPROM block (offset=%d, len=%d)\n", 1467191983Sweongyo eeprom_offset, eeprom_len); 1468191983Sweongyo 1469227461Shselasky memcpy(sc->sc_eeprom + eeprom_offset, 1470227461Shselasky data->buf + sizeof(struct upgt_lmac_eeprom) + 4, 1471227461Shselasky eeprom_len); 1472191983Sweongyo 1473191983Sweongyo /* EEPROM data has arrived in time, wakeup. */ 1474191983Sweongyo wakeup(sc); 1475191983Sweongyo } else if (h1_type == UPGT_H1_TYPE_CTRL && 1476191983Sweongyo h2_type == UPGT_H2_TYPE_TX_DONE) { 1477191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n", 1478191983Sweongyo __func__); 1479191983Sweongyo upgt_tx_done(sc, data->buf + 4); 1480191983Sweongyo } else if (h1_type == UPGT_H1_TYPE_RX_DATA || 1481191983Sweongyo h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) { 1482191983Sweongyo DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n", 1483191983Sweongyo __func__); 1484191983Sweongyo m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len), 1485191983Sweongyo rssi); 1486191983Sweongyo } else if (h1_type == UPGT_H1_TYPE_CTRL && 1487191983Sweongyo h2_type == UPGT_H2_TYPE_STATS) { 1488191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n", 1489191983Sweongyo __func__); 1490191983Sweongyo /* TODO: what could we do with the statistic data? */ 1491191983Sweongyo } else { 1492191983Sweongyo /* ignore unknown frame types */ 1493191983Sweongyo DPRINTF(sc, UPGT_DEBUG_INTR, 1494191983Sweongyo "received unknown frame type 0x%02x\n", 1495191983Sweongyo header->header1.type); 1496191983Sweongyo } 1497191983Sweongyo return (m); 1498191983Sweongyo} 1499191983Sweongyo 1500191983Sweongyo/* 1501191983Sweongyo * The firmware awaits a checksum for each frame we send to it. 1502191983Sweongyo * The algorithm used therefor is uncommon but somehow similar to CRC32. 1503191983Sweongyo */ 1504191983Sweongyostatic uint32_t 1505191983Sweongyoupgt_chksum_le(const uint32_t *buf, size_t size) 1506191983Sweongyo{ 1507233774Shselasky size_t i; 1508191983Sweongyo uint32_t crc = 0; 1509191983Sweongyo 1510191983Sweongyo for (i = 0; i < size; i += sizeof(uint32_t)) { 1511191983Sweongyo crc = htole32(crc ^ *buf++); 1512191983Sweongyo crc = htole32((crc >> 5) ^ (crc << 3)); 1513191983Sweongyo } 1514191983Sweongyo 1515191983Sweongyo return (crc); 1516191983Sweongyo} 1517191983Sweongyo 1518191983Sweongyostatic struct mbuf * 1519191983Sweongyoupgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi) 1520191983Sweongyo{ 1521191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 1522191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 1523191983Sweongyo struct upgt_lmac_rx_desc *rxdesc; 1524191983Sweongyo struct mbuf *m; 1525191983Sweongyo 1526191983Sweongyo /* 1527191983Sweongyo * don't pass packets to the ieee80211 framework if the driver isn't 1528191983Sweongyo * RUNNING. 1529191983Sweongyo */ 1530191983Sweongyo if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 1531191983Sweongyo return (NULL); 1532191983Sweongyo 1533191983Sweongyo /* access RX packet descriptor */ 1534191983Sweongyo rxdesc = (struct upgt_lmac_rx_desc *)data; 1535191983Sweongyo 1536191983Sweongyo /* create mbuf which is suitable for strict alignment archs */ 1537191983Sweongyo KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES, 1538191983Sweongyo ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN)); 1539243857Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1540191983Sweongyo if (m == NULL) { 1541199816Sthompsa device_printf(sc->sc_dev, "could not create RX mbuf\n"); 1542191983Sweongyo return (NULL); 1543191983Sweongyo } 1544191983Sweongyo m_adj(m, ETHER_ALIGN); 1545227461Shselasky memcpy(mtod(m, char *), rxdesc->data, pkglen); 1546191983Sweongyo /* trim FCS */ 1547191983Sweongyo m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN; 1548191983Sweongyo m->m_pkthdr.rcvif = ifp; 1549191983Sweongyo 1550192468Ssam if (ieee80211_radiotap_active(ic)) { 1551191983Sweongyo struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap; 1552191983Sweongyo 1553191983Sweongyo tap->wr_flags = 0; 1554191983Sweongyo tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate); 1555191983Sweongyo tap->wr_antsignal = rxdesc->rssi; 1556191983Sweongyo } 1557271866Sglebius if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 1558191983Sweongyo 1559191983Sweongyo DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__); 1560191983Sweongyo *rssi = rxdesc->rssi; 1561191983Sweongyo return (m); 1562191983Sweongyo} 1563191983Sweongyo 1564191983Sweongyostatic uint8_t 1565191983Sweongyoupgt_rx_rate(struct upgt_softc *sc, const int rate) 1566191983Sweongyo{ 1567191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 1568191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 1569191983Sweongyo static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 }; 1570191983Sweongyo static const uint8_t ofdm_upgt2rate[12] = 1571191983Sweongyo { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 }; 1572191983Sweongyo 1573191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11B && 1574191983Sweongyo !(rate < 0 || rate > 3)) 1575191983Sweongyo return cck_upgt2rate[rate & 0xf]; 1576191983Sweongyo 1577191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11G && 1578191983Sweongyo !(rate < 0 || rate > 11)) 1579191983Sweongyo return ofdm_upgt2rate[rate & 0xf]; 1580191983Sweongyo 1581191983Sweongyo return (0); 1582191983Sweongyo} 1583191983Sweongyo 1584191983Sweongyostatic void 1585191983Sweongyoupgt_tx_done(struct upgt_softc *sc, uint8_t *data) 1586191983Sweongyo{ 1587191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 1588191983Sweongyo struct upgt_lmac_tx_done_desc *desc; 1589191983Sweongyo int i, freed = 0; 1590191983Sweongyo 1591191983Sweongyo UPGT_ASSERT_LOCKED(sc); 1592191983Sweongyo 1593191983Sweongyo desc = (struct upgt_lmac_tx_done_desc *)data; 1594191983Sweongyo 1595191983Sweongyo for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 1596191983Sweongyo struct upgt_data *data_tx = &sc->sc_tx_data[i]; 1597191983Sweongyo 1598191983Sweongyo if (data_tx->addr == le32toh(desc->header2.reqid)) { 1599191983Sweongyo upgt_mem_free(sc, data_tx->addr); 1600191983Sweongyo data_tx->ni = NULL; 1601191983Sweongyo data_tx->addr = 0; 1602191983Sweongyo data_tx->m = NULL; 1603191983Sweongyo 1604191983Sweongyo DPRINTF(sc, UPGT_DEBUG_TX_PROC, 1605191983Sweongyo "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ", 1606191983Sweongyo le32toh(desc->header2.reqid), 1607191983Sweongyo le16toh(desc->status), le16toh(desc->rssi)); 1608191983Sweongyo DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n", 1609191983Sweongyo le16toh(desc->seq)); 1610191983Sweongyo 1611191983Sweongyo freed++; 1612191983Sweongyo } 1613191983Sweongyo } 1614191983Sweongyo 1615191983Sweongyo if (freed != 0) { 1616191983Sweongyo sc->sc_tx_timer = 0; 1617191983Sweongyo ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1618191983Sweongyo UPGT_UNLOCK(sc); 1619191983Sweongyo upgt_start(ifp); 1620191983Sweongyo UPGT_LOCK(sc); 1621191983Sweongyo } 1622191983Sweongyo} 1623191983Sweongyo 1624191983Sweongyostatic void 1625191983Sweongyoupgt_mem_free(struct upgt_softc *sc, uint32_t addr) 1626191983Sweongyo{ 1627191983Sweongyo int i; 1628191983Sweongyo 1629191983Sweongyo for (i = 0; i < sc->sc_memory.pages; i++) { 1630191983Sweongyo if (sc->sc_memory.page[i].addr == addr) { 1631191983Sweongyo sc->sc_memory.page[i].used = 0; 1632191983Sweongyo return; 1633191983Sweongyo } 1634191983Sweongyo } 1635191983Sweongyo 1636191983Sweongyo device_printf(sc->sc_dev, 1637199816Sthompsa "could not free memory address 0x%08x\n", addr); 1638191983Sweongyo} 1639191983Sweongyo 1640191983Sweongyostatic int 1641191983Sweongyoupgt_fw_load(struct upgt_softc *sc) 1642191983Sweongyo{ 1643191983Sweongyo const struct firmware *fw; 1644191983Sweongyo struct upgt_data *data_cmd; 1645191983Sweongyo struct upgt_fw_x2_header *x2; 1646191983Sweongyo char start_fwload_cmd[] = { 0x3c, 0x0d }; 1647233774Shselasky int error = 0; 1648233774Shselasky size_t offset; 1649233774Shselasky int bsize; 1650233774Shselasky int n; 1651191983Sweongyo uint32_t crc32; 1652191983Sweongyo 1653191983Sweongyo fw = firmware_get(upgt_fwname); 1654191983Sweongyo if (fw == NULL) { 1655199816Sthompsa device_printf(sc->sc_dev, "could not read microcode %s\n", 1656191983Sweongyo upgt_fwname); 1657191983Sweongyo return (EIO); 1658191983Sweongyo } 1659191983Sweongyo 1660191983Sweongyo UPGT_LOCK(sc); 1661191983Sweongyo 1662191983Sweongyo /* send firmware start load command */ 1663191983Sweongyo data_cmd = upgt_getbuf(sc); 1664191983Sweongyo if (data_cmd == NULL) { 1665191983Sweongyo error = ENOBUFS; 1666191983Sweongyo goto fail; 1667191983Sweongyo } 1668191983Sweongyo data_cmd->buflen = sizeof(start_fwload_cmd); 1669227461Shselasky memcpy(data_cmd->buf, start_fwload_cmd, data_cmd->buflen); 1670191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1671191983Sweongyo 1672191983Sweongyo /* send X2 header */ 1673191983Sweongyo data_cmd = upgt_getbuf(sc); 1674191983Sweongyo if (data_cmd == NULL) { 1675191983Sweongyo error = ENOBUFS; 1676191983Sweongyo goto fail; 1677191983Sweongyo } 1678191983Sweongyo data_cmd->buflen = sizeof(struct upgt_fw_x2_header); 1679191983Sweongyo x2 = (struct upgt_fw_x2_header *)data_cmd->buf; 1680227461Shselasky memcpy(x2->signature, UPGT_X2_SIGNATURE, UPGT_X2_SIGNATURE_SIZE); 1681191983Sweongyo x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START); 1682191983Sweongyo x2->len = htole32(fw->datasize); 1683191983Sweongyo x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf + 1684191983Sweongyo UPGT_X2_SIGNATURE_SIZE, 1685191983Sweongyo sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE - 1686191983Sweongyo sizeof(uint32_t)); 1687191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1688191983Sweongyo 1689191983Sweongyo /* download firmware */ 1690191983Sweongyo for (offset = 0; offset < fw->datasize; offset += bsize) { 1691191983Sweongyo if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE) 1692191983Sweongyo bsize = UPGT_FW_BLOCK_SIZE; 1693191983Sweongyo else 1694191983Sweongyo bsize = fw->datasize - offset; 1695191983Sweongyo 1696191983Sweongyo data_cmd = upgt_getbuf(sc); 1697191983Sweongyo if (data_cmd == NULL) { 1698191983Sweongyo error = ENOBUFS; 1699191983Sweongyo goto fail; 1700191983Sweongyo } 1701191983Sweongyo n = upgt_fw_copy((const uint8_t *)fw->data + offset, 1702191983Sweongyo data_cmd->buf, bsize); 1703191983Sweongyo data_cmd->buflen = bsize; 1704191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1705191983Sweongyo 1706191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n", 1707191983Sweongyo offset, n, bsize); 1708191983Sweongyo bsize = n; 1709191983Sweongyo } 1710191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__); 1711191983Sweongyo 1712191983Sweongyo /* load firmware */ 1713191983Sweongyo data_cmd = upgt_getbuf(sc); 1714191983Sweongyo if (data_cmd == NULL) { 1715191983Sweongyo error = ENOBUFS; 1716191983Sweongyo goto fail; 1717191983Sweongyo } 1718191983Sweongyo crc32 = upgt_crc32_le(fw->data, fw->datasize); 1719191983Sweongyo *((uint32_t *)(data_cmd->buf) ) = crc32; 1720191983Sweongyo *((uint8_t *)(data_cmd->buf) + 4) = 'g'; 1721191983Sweongyo *((uint8_t *)(data_cmd->buf) + 5) = '\r'; 1722191983Sweongyo data_cmd->buflen = 6; 1723191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1724191983Sweongyo 1725191983Sweongyo /* waiting 'OK' response. */ 1726194228Sthompsa usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]); 1727191983Sweongyo error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz); 1728191983Sweongyo if (error != 0) { 1729199816Sthompsa device_printf(sc->sc_dev, "firmware load failed\n"); 1730191983Sweongyo error = EIO; 1731191983Sweongyo } 1732191983Sweongyo 1733191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__); 1734191983Sweongyofail: 1735191983Sweongyo UPGT_UNLOCK(sc); 1736191983Sweongyo firmware_put(fw, FIRMWARE_UNLOAD); 1737191983Sweongyo return (error); 1738191983Sweongyo} 1739191983Sweongyo 1740191983Sweongyostatic uint32_t 1741191983Sweongyoupgt_crc32_le(const void *buf, size_t size) 1742191983Sweongyo{ 1743191983Sweongyo uint32_t crc; 1744191983Sweongyo 1745191983Sweongyo crc = ether_crc32_le(buf, size); 1746191983Sweongyo 1747191983Sweongyo /* apply final XOR value as common for CRC-32 */ 1748191983Sweongyo crc = htole32(crc ^ 0xffffffffU); 1749191983Sweongyo 1750191983Sweongyo return (crc); 1751191983Sweongyo} 1752191983Sweongyo 1753191983Sweongyo/* 1754191983Sweongyo * While copying the version 2 firmware, we need to replace two characters: 1755191983Sweongyo * 1756191983Sweongyo * 0x7e -> 0x7d 0x5e 1757191983Sweongyo * 0x7d -> 0x7d 0x5d 1758191983Sweongyo */ 1759191983Sweongyostatic int 1760191983Sweongyoupgt_fw_copy(const uint8_t *src, char *dst, int size) 1761191983Sweongyo{ 1762191983Sweongyo int i, j; 1763191983Sweongyo 1764191983Sweongyo for (i = 0, j = 0; i < size && j < size; i++) { 1765191983Sweongyo switch (src[i]) { 1766191983Sweongyo case 0x7e: 1767191983Sweongyo dst[j] = 0x7d; 1768191983Sweongyo j++; 1769191983Sweongyo dst[j] = 0x5e; 1770191983Sweongyo j++; 1771191983Sweongyo break; 1772191983Sweongyo case 0x7d: 1773191983Sweongyo dst[j] = 0x7d; 1774191983Sweongyo j++; 1775191983Sweongyo dst[j] = 0x5d; 1776191983Sweongyo j++; 1777191983Sweongyo break; 1778191983Sweongyo default: 1779191983Sweongyo dst[j] = src[i]; 1780191983Sweongyo j++; 1781191983Sweongyo break; 1782191983Sweongyo } 1783191983Sweongyo } 1784191983Sweongyo 1785191983Sweongyo return (i); 1786191983Sweongyo} 1787191983Sweongyo 1788191983Sweongyostatic int 1789191983Sweongyoupgt_mem_init(struct upgt_softc *sc) 1790191983Sweongyo{ 1791191983Sweongyo int i; 1792191983Sweongyo 1793191983Sweongyo for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) { 1794191983Sweongyo sc->sc_memory.page[i].used = 0; 1795191983Sweongyo 1796191983Sweongyo if (i == 0) { 1797191983Sweongyo /* 1798191983Sweongyo * The first memory page is always reserved for 1799191983Sweongyo * command data. 1800191983Sweongyo */ 1801191983Sweongyo sc->sc_memory.page[i].addr = 1802191983Sweongyo sc->sc_memaddr_frame_start + MCLBYTES; 1803191983Sweongyo } else { 1804191983Sweongyo sc->sc_memory.page[i].addr = 1805191983Sweongyo sc->sc_memory.page[i - 1].addr + MCLBYTES; 1806191983Sweongyo } 1807191983Sweongyo 1808191983Sweongyo if (sc->sc_memory.page[i].addr + MCLBYTES >= 1809191983Sweongyo sc->sc_memaddr_frame_end) 1810191983Sweongyo break; 1811191983Sweongyo 1812191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n", 1813191983Sweongyo i, sc->sc_memory.page[i].addr); 1814191983Sweongyo } 1815191983Sweongyo 1816191983Sweongyo sc->sc_memory.pages = i; 1817191983Sweongyo 1818191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages); 1819191983Sweongyo return (0); 1820191983Sweongyo} 1821191983Sweongyo 1822191983Sweongyostatic int 1823191983Sweongyoupgt_fw_verify(struct upgt_softc *sc) 1824191983Sweongyo{ 1825191983Sweongyo const struct firmware *fw; 1826191983Sweongyo const struct upgt_fw_bra_option *bra_opt; 1827191983Sweongyo const struct upgt_fw_bra_descr *descr; 1828191983Sweongyo const uint8_t *p; 1829191983Sweongyo const uint32_t *uc; 1830191983Sweongyo uint32_t bra_option_type, bra_option_len; 1831233774Shselasky size_t offset; 1832233774Shselasky int bra_end = 0; 1833233774Shselasky int error = 0; 1834191983Sweongyo 1835191983Sweongyo fw = firmware_get(upgt_fwname); 1836191983Sweongyo if (fw == NULL) { 1837199816Sthompsa device_printf(sc->sc_dev, "could not read microcode %s\n", 1838191983Sweongyo upgt_fwname); 1839191983Sweongyo return EIO; 1840191983Sweongyo } 1841191983Sweongyo 1842191983Sweongyo /* 1843191983Sweongyo * Seek to beginning of Boot Record Area (BRA). 1844191983Sweongyo */ 1845191983Sweongyo for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) { 1846191983Sweongyo uc = (const uint32_t *)((const uint8_t *)fw->data + offset); 1847191983Sweongyo if (*uc == 0) 1848191983Sweongyo break; 1849191983Sweongyo } 1850191983Sweongyo for (; offset < fw->datasize; offset += sizeof(*uc)) { 1851191983Sweongyo uc = (const uint32_t *)((const uint8_t *)fw->data + offset); 1852191983Sweongyo if (*uc != 0) 1853191983Sweongyo break; 1854191983Sweongyo } 1855191983Sweongyo if (offset == fw->datasize) { 1856191983Sweongyo device_printf(sc->sc_dev, 1857199816Sthompsa "firmware Boot Record Area not found\n"); 1858191983Sweongyo error = EIO; 1859191983Sweongyo goto fail; 1860191983Sweongyo } 1861191983Sweongyo 1862191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1863191983Sweongyo "firmware Boot Record Area found at offset %d\n", offset); 1864191983Sweongyo 1865191983Sweongyo /* 1866191983Sweongyo * Parse Boot Record Area (BRA) options. 1867191983Sweongyo */ 1868191983Sweongyo while (offset < fw->datasize && bra_end == 0) { 1869191983Sweongyo /* get current BRA option */ 1870191983Sweongyo p = (const uint8_t *)fw->data + offset; 1871191983Sweongyo bra_opt = (const struct upgt_fw_bra_option *)p; 1872191983Sweongyo bra_option_type = le32toh(bra_opt->type); 1873191983Sweongyo bra_option_len = le32toh(bra_opt->len) * sizeof(*uc); 1874191983Sweongyo 1875191983Sweongyo switch (bra_option_type) { 1876191983Sweongyo case UPGT_BRA_TYPE_FW: 1877191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n", 1878191983Sweongyo bra_option_len); 1879191983Sweongyo 1880191983Sweongyo if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) { 1881191983Sweongyo device_printf(sc->sc_dev, 1882199816Sthompsa "wrong UPGT_BRA_TYPE_FW len\n"); 1883191983Sweongyo error = EIO; 1884191983Sweongyo goto fail; 1885191983Sweongyo } 1886191983Sweongyo if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data, 1887191983Sweongyo bra_option_len) == 0) { 1888191983Sweongyo sc->sc_fw_type = UPGT_FWTYPE_LM86; 1889191983Sweongyo break; 1890191983Sweongyo } 1891191983Sweongyo if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data, 1892191983Sweongyo bra_option_len) == 0) { 1893191983Sweongyo sc->sc_fw_type = UPGT_FWTYPE_LM87; 1894191983Sweongyo break; 1895191983Sweongyo } 1896191983Sweongyo device_printf(sc->sc_dev, 1897199816Sthompsa "unsupported firmware type\n"); 1898191983Sweongyo error = EIO; 1899191983Sweongyo goto fail; 1900191983Sweongyo case UPGT_BRA_TYPE_VERSION: 1901191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1902191983Sweongyo "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len); 1903191983Sweongyo break; 1904191983Sweongyo case UPGT_BRA_TYPE_DEPIF: 1905191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1906191983Sweongyo "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len); 1907191983Sweongyo break; 1908191983Sweongyo case UPGT_BRA_TYPE_EXPIF: 1909191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1910191983Sweongyo "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len); 1911191983Sweongyo break; 1912191983Sweongyo case UPGT_BRA_TYPE_DESCR: 1913191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1914191983Sweongyo "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len); 1915191983Sweongyo 1916191983Sweongyo descr = (const struct upgt_fw_bra_descr *)bra_opt->data; 1917191983Sweongyo 1918191983Sweongyo sc->sc_memaddr_frame_start = 1919191983Sweongyo le32toh(descr->memaddr_space_start); 1920191983Sweongyo sc->sc_memaddr_frame_end = 1921191983Sweongyo le32toh(descr->memaddr_space_end); 1922191983Sweongyo 1923191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1924191983Sweongyo "memory address space start=0x%08x\n", 1925191983Sweongyo sc->sc_memaddr_frame_start); 1926191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1927191983Sweongyo "memory address space end=0x%08x\n", 1928191983Sweongyo sc->sc_memaddr_frame_end); 1929191983Sweongyo break; 1930191983Sweongyo case UPGT_BRA_TYPE_END: 1931191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n", 1932191983Sweongyo bra_option_len); 1933191983Sweongyo bra_end = 1; 1934191983Sweongyo break; 1935191983Sweongyo default: 1936191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n", 1937191983Sweongyo bra_option_len); 1938191983Sweongyo error = EIO; 1939191983Sweongyo goto fail; 1940191983Sweongyo } 1941191983Sweongyo 1942191983Sweongyo /* jump to next BRA option */ 1943191983Sweongyo offset += sizeof(struct upgt_fw_bra_option) + bra_option_len; 1944191983Sweongyo } 1945191983Sweongyo 1946191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__); 1947191983Sweongyofail: 1948191983Sweongyo firmware_put(fw, FIRMWARE_UNLOAD); 1949191983Sweongyo return (error); 1950191983Sweongyo} 1951191983Sweongyo 1952191983Sweongyostatic void 1953191983Sweongyoupgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data) 1954191983Sweongyo{ 1955191983Sweongyo 1956191983Sweongyo UPGT_ASSERT_LOCKED(sc); 1957191983Sweongyo 1958191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next); 1959191983Sweongyo UPGT_STAT_INC(sc, st_tx_pending); 1960194228Sthompsa usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]); 1961191983Sweongyo} 1962191983Sweongyo 1963191983Sweongyostatic int 1964191983Sweongyoupgt_device_reset(struct upgt_softc *sc) 1965191983Sweongyo{ 1966191983Sweongyo struct upgt_data *data; 1967191983Sweongyo char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e }; 1968191983Sweongyo 1969191983Sweongyo UPGT_LOCK(sc); 1970191983Sweongyo 1971191983Sweongyo data = upgt_getbuf(sc); 1972191983Sweongyo if (data == NULL) { 1973191983Sweongyo UPGT_UNLOCK(sc); 1974191983Sweongyo return (ENOBUFS); 1975191983Sweongyo } 1976227461Shselasky memcpy(data->buf, init_cmd, sizeof(init_cmd)); 1977191983Sweongyo data->buflen = sizeof(init_cmd); 1978191983Sweongyo upgt_bulk_tx(sc, data); 1979194228Sthompsa usb_pause_mtx(&sc->sc_mtx, 100); 1980191983Sweongyo 1981191983Sweongyo UPGT_UNLOCK(sc); 1982191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__); 1983191983Sweongyo return (0); 1984191983Sweongyo} 1985191983Sweongyo 1986191983Sweongyostatic int 1987191983Sweongyoupgt_alloc_tx(struct upgt_softc *sc) 1988191983Sweongyo{ 1989191983Sweongyo int i; 1990191983Sweongyo 1991191983Sweongyo STAILQ_INIT(&sc->sc_tx_active); 1992191983Sweongyo STAILQ_INIT(&sc->sc_tx_inactive); 1993191983Sweongyo STAILQ_INIT(&sc->sc_tx_pending); 1994191983Sweongyo 1995191983Sweongyo for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 1996191983Sweongyo struct upgt_data *data = &sc->sc_tx_data[i]; 1997244503Shselasky data->buf = ((uint8_t *)sc->sc_tx_dma_buf) + (i * MCLBYTES); 1998191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next); 1999191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 2000191983Sweongyo } 2001191983Sweongyo 2002191983Sweongyo return (0); 2003191983Sweongyo} 2004191983Sweongyo 2005191983Sweongyostatic int 2006191983Sweongyoupgt_alloc_rx(struct upgt_softc *sc) 2007191983Sweongyo{ 2008191983Sweongyo int i; 2009191983Sweongyo 2010191983Sweongyo STAILQ_INIT(&sc->sc_rx_active); 2011191983Sweongyo STAILQ_INIT(&sc->sc_rx_inactive); 2012191983Sweongyo 2013191983Sweongyo for (i = 0; i < UPGT_RX_MAXCOUNT; i++) { 2014191983Sweongyo struct upgt_data *data = &sc->sc_rx_data[i]; 2015244503Shselasky data->buf = ((uint8_t *)sc->sc_rx_dma_buf) + (i * MCLBYTES); 2016191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2017191983Sweongyo } 2018191983Sweongyo return (0); 2019191983Sweongyo} 2020191983Sweongyo 2021191983Sweongyostatic int 2022191983Sweongyoupgt_detach(device_t dev) 2023191983Sweongyo{ 2024191983Sweongyo struct upgt_softc *sc = device_get_softc(dev); 2025191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 2026191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 2027246614Shselasky unsigned int x; 2028191983Sweongyo 2029246614Shselasky /* 2030246614Shselasky * Prevent further allocations from RX/TX/CMD 2031246614Shselasky * data lists and ioctls 2032246614Shselasky */ 2033246614Shselasky UPGT_LOCK(sc); 2034246614Shselasky sc->sc_flags |= UPGT_FLAG_DETACHED; 2035191983Sweongyo 2036246614Shselasky STAILQ_INIT(&sc->sc_tx_active); 2037246614Shselasky STAILQ_INIT(&sc->sc_tx_inactive); 2038246614Shselasky STAILQ_INIT(&sc->sc_tx_pending); 2039246614Shselasky 2040246614Shselasky STAILQ_INIT(&sc->sc_rx_active); 2041246614Shselasky STAILQ_INIT(&sc->sc_rx_inactive); 2042246614Shselasky UPGT_UNLOCK(sc); 2043246614Shselasky 2044191983Sweongyo upgt_stop(sc); 2045191983Sweongyo 2046191983Sweongyo callout_drain(&sc->sc_led_ch); 2047191983Sweongyo callout_drain(&sc->sc_watchdog_ch); 2048191983Sweongyo 2049246614Shselasky /* drain USB transfers */ 2050246614Shselasky for (x = 0; x != UPGT_N_XFERS; x++) 2051246614Shselasky usbd_transfer_drain(sc->sc_xfer[x]); 2052244503Shselasky 2053246614Shselasky /* free data buffers */ 2054246614Shselasky UPGT_LOCK(sc); 2055191983Sweongyo upgt_free_rx(sc); 2056191983Sweongyo upgt_free_tx(sc); 2057246614Shselasky UPGT_UNLOCK(sc); 2058191983Sweongyo 2059246614Shselasky /* free USB transfers and some data buffers */ 2060246614Shselasky usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS); 2061246614Shselasky 2062246614Shselasky ieee80211_ifdetach(ic); 2063191983Sweongyo if_free(ifp); 2064191983Sweongyo mtx_destroy(&sc->sc_mtx); 2065191983Sweongyo 2066191983Sweongyo return (0); 2067191983Sweongyo} 2068191983Sweongyo 2069191983Sweongyostatic void 2070191983Sweongyoupgt_free_rx(struct upgt_softc *sc) 2071191983Sweongyo{ 2072191983Sweongyo int i; 2073191983Sweongyo 2074191983Sweongyo for (i = 0; i < UPGT_RX_MAXCOUNT; i++) { 2075191983Sweongyo struct upgt_data *data = &sc->sc_rx_data[i]; 2076191983Sweongyo 2077244503Shselasky data->buf = NULL; 2078191983Sweongyo data->ni = NULL; 2079191983Sweongyo } 2080191983Sweongyo} 2081191983Sweongyo 2082191983Sweongyostatic void 2083191983Sweongyoupgt_free_tx(struct upgt_softc *sc) 2084191983Sweongyo{ 2085191983Sweongyo int i; 2086191983Sweongyo 2087191983Sweongyo for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 2088191983Sweongyo struct upgt_data *data = &sc->sc_tx_data[i]; 2089191983Sweongyo 2090246614Shselasky if (data->ni != NULL) 2091246614Shselasky ieee80211_free_node(data->ni); 2092246614Shselasky 2093244503Shselasky data->buf = NULL; 2094191983Sweongyo data->ni = NULL; 2095191983Sweongyo } 2096191983Sweongyo} 2097191983Sweongyo 2098191983Sweongyostatic void 2099191983Sweongyoupgt_abort_xfers_locked(struct upgt_softc *sc) 2100191983Sweongyo{ 2101191983Sweongyo int i; 2102191983Sweongyo 2103191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2104191983Sweongyo /* abort any pending transfers */ 2105191983Sweongyo for (i = 0; i < UPGT_N_XFERS; i++) 2106194228Sthompsa usbd_transfer_stop(sc->sc_xfer[i]); 2107191983Sweongyo} 2108191983Sweongyo 2109191983Sweongyostatic void 2110191983Sweongyoupgt_abort_xfers(struct upgt_softc *sc) 2111191983Sweongyo{ 2112191983Sweongyo 2113191983Sweongyo UPGT_LOCK(sc); 2114191983Sweongyo upgt_abort_xfers_locked(sc); 2115191983Sweongyo UPGT_UNLOCK(sc); 2116191983Sweongyo} 2117191983Sweongyo 2118191983Sweongyo#define UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d) \ 2119191983Sweongyo SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d) 2120191983Sweongyo 2121191983Sweongyostatic void 2122191983Sweongyoupgt_sysctl_node(struct upgt_softc *sc) 2123191983Sweongyo{ 2124191983Sweongyo struct sysctl_ctx_list *ctx; 2125191983Sweongyo struct sysctl_oid_list *child; 2126191983Sweongyo struct sysctl_oid *tree; 2127191983Sweongyo struct upgt_stat *stats; 2128191983Sweongyo 2129191983Sweongyo stats = &sc->sc_stat; 2130191983Sweongyo ctx = device_get_sysctl_ctx(sc->sc_dev); 2131191983Sweongyo child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev)); 2132191983Sweongyo 2133191983Sweongyo tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD, 2134191983Sweongyo NULL, "UPGT statistics"); 2135191983Sweongyo child = SYSCTL_CHILDREN(tree); 2136191983Sweongyo UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active", 2137191983Sweongyo &stats->st_tx_active, "Active numbers in TX queue"); 2138191983Sweongyo UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive", 2139191983Sweongyo &stats->st_tx_inactive, "Inactive numbers in TX queue"); 2140191983Sweongyo UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending", 2141191983Sweongyo &stats->st_tx_pending, "Pending numbers in TX queue"); 2142191983Sweongyo} 2143191983Sweongyo 2144191983Sweongyo#undef UPGT_SYSCTL_STAT_ADD32 2145191983Sweongyo 2146191983Sweongyostatic struct upgt_data * 2147191983Sweongyo_upgt_getbuf(struct upgt_softc *sc) 2148191983Sweongyo{ 2149191983Sweongyo struct upgt_data *bf; 2150191983Sweongyo 2151191983Sweongyo bf = STAILQ_FIRST(&sc->sc_tx_inactive); 2152191983Sweongyo if (bf != NULL) { 2153191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next); 2154191983Sweongyo UPGT_STAT_DEC(sc, st_tx_inactive); 2155191983Sweongyo } else 2156191983Sweongyo bf = NULL; 2157191983Sweongyo if (bf == NULL) 2158191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__, 2159191983Sweongyo "out of xmit buffers"); 2160191983Sweongyo return (bf); 2161191983Sweongyo} 2162191983Sweongyo 2163191983Sweongyostatic struct upgt_data * 2164191983Sweongyoupgt_getbuf(struct upgt_softc *sc) 2165191983Sweongyo{ 2166191983Sweongyo struct upgt_data *bf; 2167191983Sweongyo 2168191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2169191983Sweongyo 2170191983Sweongyo bf = _upgt_getbuf(sc); 2171191983Sweongyo if (bf == NULL) { 2172191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 2173191983Sweongyo 2174191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__); 2175191983Sweongyo ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2176191983Sweongyo } 2177191983Sweongyo 2178191983Sweongyo return (bf); 2179191983Sweongyo} 2180191983Sweongyo 2181191983Sweongyostatic struct upgt_data * 2182191983Sweongyoupgt_gettxbuf(struct upgt_softc *sc) 2183191983Sweongyo{ 2184191983Sweongyo struct upgt_data *bf; 2185191983Sweongyo 2186191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2187191983Sweongyo 2188191983Sweongyo bf = upgt_getbuf(sc); 2189191983Sweongyo if (bf == NULL) 2190191983Sweongyo return (NULL); 2191191983Sweongyo 2192191983Sweongyo bf->addr = upgt_mem_alloc(sc); 2193191983Sweongyo if (bf->addr == 0) { 2194191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 2195191983Sweongyo 2196191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n", 2197191983Sweongyo __func__); 2198191983Sweongyo STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next); 2199191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 2200191983Sweongyo if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) 2201191983Sweongyo ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2202191983Sweongyo return (NULL); 2203191983Sweongyo } 2204191983Sweongyo return (bf); 2205191983Sweongyo} 2206191983Sweongyo 2207191983Sweongyostatic int 2208191983Sweongyoupgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni, 2209191983Sweongyo struct upgt_data *data) 2210191983Sweongyo{ 2211192468Ssam struct ieee80211vap *vap = ni->ni_vap; 2212191983Sweongyo int error = 0, len; 2213191983Sweongyo struct ieee80211_frame *wh; 2214191983Sweongyo struct ieee80211_key *k; 2215191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 2216191983Sweongyo struct upgt_lmac_mem *mem; 2217191983Sweongyo struct upgt_lmac_tx_desc *txdesc; 2218191983Sweongyo 2219191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2220191983Sweongyo 2221191983Sweongyo upgt_set_led(sc, UPGT_LED_BLINK); 2222191983Sweongyo 2223191983Sweongyo /* 2224191983Sweongyo * Software crypto. 2225191983Sweongyo */ 2226191983Sweongyo wh = mtod(m, struct ieee80211_frame *); 2227260444Skevlo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2228191983Sweongyo k = ieee80211_crypto_encap(ni, m); 2229191983Sweongyo if (k == NULL) { 2230191983Sweongyo device_printf(sc->sc_dev, 2231191983Sweongyo "ieee80211_crypto_encap returns NULL.\n"); 2232191983Sweongyo error = EIO; 2233191983Sweongyo goto done; 2234191983Sweongyo } 2235191983Sweongyo 2236191983Sweongyo /* in case packet header moved, reset pointer */ 2237191983Sweongyo wh = mtod(m, struct ieee80211_frame *); 2238191983Sweongyo } 2239191983Sweongyo 2240191983Sweongyo /* Transmit the URB containing the TX data. */ 2241227461Shselasky memset(data->buf, 0, MCLBYTES); 2242191983Sweongyo mem = (struct upgt_lmac_mem *)data->buf; 2243191983Sweongyo mem->addr = htole32(data->addr); 2244191983Sweongyo txdesc = (struct upgt_lmac_tx_desc *)(mem + 1); 2245191983Sweongyo 2246191983Sweongyo if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2247191983Sweongyo IEEE80211_FC0_TYPE_MGT) { 2248191983Sweongyo /* mgmt frames */ 2249191983Sweongyo txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT; 2250191983Sweongyo /* always send mgmt frames at lowest rate (DS1) */ 2251191983Sweongyo memset(txdesc->rates, 0x10, sizeof(txdesc->rates)); 2252191983Sweongyo } else { 2253191983Sweongyo /* data frames */ 2254191983Sweongyo txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA; 2255227461Shselasky memcpy(txdesc->rates, sc->sc_cur_rateset, sizeof(txdesc->rates)); 2256191983Sweongyo } 2257191983Sweongyo txdesc->header1.type = UPGT_H1_TYPE_TX_DATA; 2258191983Sweongyo txdesc->header1.len = htole16(m->m_pkthdr.len); 2259191983Sweongyo txdesc->header2.reqid = htole32(data->addr); 2260191983Sweongyo txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES); 2261191983Sweongyo txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES); 2262191983Sweongyo txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA); 2263191983Sweongyo txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE; 2264191983Sweongyo 2265192468Ssam if (ieee80211_radiotap_active_vap(vap)) { 2266191983Sweongyo struct upgt_tx_radiotap_header *tap = &sc->sc_txtap; 2267191983Sweongyo 2268191983Sweongyo tap->wt_flags = 0; 2269191983Sweongyo tap->wt_rate = 0; /* XXX where to get from? */ 2270191983Sweongyo 2271192468Ssam ieee80211_radiotap_tx(vap, m); 2272191983Sweongyo } 2273191983Sweongyo 2274191983Sweongyo /* copy frame below our TX descriptor header */ 2275191983Sweongyo m_copydata(m, 0, m->m_pkthdr.len, 2276191983Sweongyo data->buf + (sizeof(*mem) + sizeof(*txdesc))); 2277191983Sweongyo /* calculate frame size */ 2278191983Sweongyo len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len; 2279191983Sweongyo /* we need to align the frame to a 4 byte boundary */ 2280191983Sweongyo len = (len + 3) & ~3; 2281191983Sweongyo /* calculate frame checksum */ 2282191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem)); 2283191983Sweongyo data->ni = ni; 2284191983Sweongyo data->m = m; 2285191983Sweongyo data->buflen = len; 2286191983Sweongyo 2287191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n", 2288191983Sweongyo __func__, len); 2289191983Sweongyo KASSERT(len <= MCLBYTES, ("mbuf is small for saving data")); 2290191983Sweongyo 2291191983Sweongyo upgt_bulk_tx(sc, data); 2292191983Sweongyodone: 2293191983Sweongyo /* 2294191983Sweongyo * If we don't regulary read the device statistics, the RX queue 2295191983Sweongyo * will stall. It's strange, but it works, so we keep reading 2296191983Sweongyo * the statistics here. *shrug* 2297191983Sweongyo */ 2298271866Sglebius if (!(ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS) % 2299271866Sglebius UPGT_TX_STAT_INTERVAL)) 2300191983Sweongyo upgt_get_stats(sc); 2301191983Sweongyo 2302191983Sweongyo return (error); 2303191983Sweongyo} 2304191983Sweongyo 2305191983Sweongyostatic void 2306194677Sthompsaupgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2307191983Sweongyo{ 2308194677Sthompsa struct upgt_softc *sc = usbd_xfer_softc(xfer); 2309191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 2310191983Sweongyo struct ieee80211com *ic = ifp->if_l2com; 2311191983Sweongyo struct ieee80211_frame *wh; 2312191983Sweongyo struct ieee80211_node *ni; 2313191983Sweongyo struct mbuf *m = NULL; 2314191983Sweongyo struct upgt_data *data; 2315191983Sweongyo int8_t nf; 2316191983Sweongyo int rssi = -1; 2317191983Sweongyo 2318191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2319191983Sweongyo 2320191983Sweongyo switch (USB_GET_STATE(xfer)) { 2321191983Sweongyo case USB_ST_TRANSFERRED: 2322191983Sweongyo data = STAILQ_FIRST(&sc->sc_rx_active); 2323191983Sweongyo if (data == NULL) 2324191983Sweongyo goto setup; 2325191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2326191983Sweongyo m = upgt_rxeof(xfer, data, &rssi); 2327191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2328191983Sweongyo /* FALLTHROUGH */ 2329191983Sweongyo case USB_ST_SETUP: 2330191983Sweongyosetup: 2331191983Sweongyo data = STAILQ_FIRST(&sc->sc_rx_inactive); 2332191983Sweongyo if (data == NULL) 2333191983Sweongyo return; 2334191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next); 2335191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next); 2336244503Shselasky usbd_xfer_set_frame_data(xfer, 0, data->buf, MCLBYTES); 2337194228Sthompsa usbd_transfer_submit(xfer); 2338191983Sweongyo 2339191983Sweongyo /* 2340191983Sweongyo * To avoid LOR we should unlock our private mutex here to call 2341191983Sweongyo * ieee80211_input() because here is at the end of a USB 2342191983Sweongyo * callback and safe to unlock. 2343191983Sweongyo */ 2344191983Sweongyo UPGT_UNLOCK(sc); 2345191983Sweongyo if (m != NULL) { 2346191983Sweongyo wh = mtod(m, struct ieee80211_frame *); 2347191983Sweongyo ni = ieee80211_find_rxnode(ic, 2348191983Sweongyo (struct ieee80211_frame_min *)wh); 2349191983Sweongyo nf = -95; /* XXX */ 2350191983Sweongyo if (ni != NULL) { 2351192468Ssam (void) ieee80211_input(ni, m, rssi, nf); 2352191983Sweongyo /* node is no longer needed */ 2353191983Sweongyo ieee80211_free_node(ni); 2354191983Sweongyo } else 2355192468Ssam (void) ieee80211_input_all(ic, m, rssi, nf); 2356191983Sweongyo m = NULL; 2357191983Sweongyo } 2358198099Sweongyo if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 2359198099Sweongyo !IFQ_IS_EMPTY(&ifp->if_snd)) 2360198099Sweongyo upgt_start(ifp); 2361191983Sweongyo UPGT_LOCK(sc); 2362191983Sweongyo break; 2363191983Sweongyo default: 2364191983Sweongyo /* needs it to the inactive queue due to a error. */ 2365191983Sweongyo data = STAILQ_FIRST(&sc->sc_rx_active); 2366191983Sweongyo if (data != NULL) { 2367191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2368191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2369191983Sweongyo } 2370194677Sthompsa if (error != USB_ERR_CANCELLED) { 2371194677Sthompsa usbd_xfer_set_stall(xfer); 2372271866Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 2373191983Sweongyo goto setup; 2374191983Sweongyo } 2375191983Sweongyo break; 2376191983Sweongyo } 2377191983Sweongyo} 2378191983Sweongyo 2379191983Sweongyostatic void 2380194677Sthompsaupgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error) 2381191983Sweongyo{ 2382194677Sthompsa struct upgt_softc *sc = usbd_xfer_softc(xfer); 2383191983Sweongyo struct ifnet *ifp = sc->sc_ifp; 2384191983Sweongyo struct upgt_data *data; 2385191983Sweongyo 2386191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2387191983Sweongyo switch (USB_GET_STATE(xfer)) { 2388191983Sweongyo case USB_ST_TRANSFERRED: 2389191983Sweongyo data = STAILQ_FIRST(&sc->sc_tx_active); 2390191983Sweongyo if (data == NULL) 2391191983Sweongyo goto setup; 2392191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next); 2393191983Sweongyo UPGT_STAT_DEC(sc, st_tx_active); 2394191983Sweongyo upgt_txeof(xfer, data); 2395191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next); 2396191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 2397191983Sweongyo /* FALLTHROUGH */ 2398191983Sweongyo case USB_ST_SETUP: 2399191983Sweongyosetup: 2400191983Sweongyo data = STAILQ_FIRST(&sc->sc_tx_pending); 2401191983Sweongyo if (data == NULL) { 2402191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n", 2403191983Sweongyo __func__); 2404191983Sweongyo return; 2405191983Sweongyo } 2406191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next); 2407191983Sweongyo UPGT_STAT_DEC(sc, st_tx_pending); 2408191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next); 2409191983Sweongyo UPGT_STAT_INC(sc, st_tx_active); 2410191983Sweongyo 2411194677Sthompsa usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen); 2412194228Sthompsa usbd_transfer_submit(xfer); 2413191983Sweongyo UPGT_UNLOCK(sc); 2414191983Sweongyo upgt_start(ifp); 2415191983Sweongyo UPGT_LOCK(sc); 2416191983Sweongyo break; 2417191983Sweongyo default: 2418191983Sweongyo data = STAILQ_FIRST(&sc->sc_tx_active); 2419191983Sweongyo if (data == NULL) 2420191983Sweongyo goto setup; 2421191983Sweongyo if (data->ni != NULL) { 2422191983Sweongyo ieee80211_free_node(data->ni); 2423191983Sweongyo data->ni = NULL; 2424271866Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2425191983Sweongyo } 2426194677Sthompsa if (error != USB_ERR_CANCELLED) { 2427194677Sthompsa usbd_xfer_set_stall(xfer); 2428191983Sweongyo goto setup; 2429191983Sweongyo } 2430191983Sweongyo break; 2431191983Sweongyo } 2432191983Sweongyo} 2433191983Sweongyo 2434191983Sweongyostatic device_method_t upgt_methods[] = { 2435191983Sweongyo /* Device interface */ 2436191983Sweongyo DEVMETHOD(device_probe, upgt_match), 2437191983Sweongyo DEVMETHOD(device_attach, upgt_attach), 2438191983Sweongyo DEVMETHOD(device_detach, upgt_detach), 2439244503Shselasky DEVMETHOD_END 2440191983Sweongyo}; 2441191983Sweongyo 2442191983Sweongyostatic driver_t upgt_driver = { 2443233774Shselasky .name = "upgt", 2444233774Shselasky .methods = upgt_methods, 2445233774Shselasky .size = sizeof(struct upgt_softc) 2446191983Sweongyo}; 2447191983Sweongyo 2448191983Sweongyostatic devclass_t upgt_devclass; 2449191983Sweongyo 2450191983SweongyoDRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0); 2451191983SweongyoMODULE_VERSION(if_upgt, 1); 2452191983SweongyoMODULE_DEPEND(if_upgt, usb, 1, 1, 1); 2453191983SweongyoMODULE_DEPEND(if_upgt, wlan, 1, 1, 1); 2454191983SweongyoMODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1); 2455