1191983Sweongyo/* $OpenBSD: if_upgt.c,v 1.35 2008/04/16 18:32:15 damien Exp $ */ 2191983Sweongyo/* $FreeBSD: releng/11.0/sys/dev/usb/wlan/if_upgt.c 298818 2016-04-29 22:14:11Z avos $ */ 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 *); 131287197Sglebiusstatic void upgt_init(struct upgt_softc *); 132287197Sglebiusstatic void upgt_parent(struct ieee80211com *); 133287197Sglebiusstatic int upgt_transmit(struct ieee80211com *, struct mbuf *); 134287197Sglebiusstatic void upgt_start(struct upgt_softc *); 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 struct upgt_softc *sc = device_get_softc(dev); 244287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 245192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 246298818Savos uint8_t bands[IEEE80211_MODE_BYTES]; 247293339Savos uint8_t iface_index = UPGT_IFACE_INDEX; 248287197Sglebius int error; 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); 261287197Sglebius mbufq_init(&sc->sc_snd, ifqmaxlen); 262191983Sweongyo 263194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 264191983Sweongyo upgt_config, UPGT_N_XFERS, sc, &sc->sc_mtx); 265191983Sweongyo if (error) { 266191983Sweongyo device_printf(dev, "could not allocate USB transfers, " 267194228Sthompsa "err=%s\n", usbd_errstr(error)); 268246565Shselasky goto fail1; 269191983Sweongyo } 270191983Sweongyo 271244503Shselasky sc->sc_rx_dma_buf = usbd_xfer_get_frame_buffer( 272244503Shselasky sc->sc_xfer[UPGT_BULK_RX], 0); 273244503Shselasky sc->sc_tx_dma_buf = usbd_xfer_get_frame_buffer( 274244503Shselasky sc->sc_xfer[UPGT_BULK_TX], 0); 275244503Shselasky 276246565Shselasky /* Setup TX and RX buffers */ 277246565Shselasky error = upgt_alloc_tx(sc); 278246565Shselasky if (error) 279246565Shselasky goto fail2; 280246565Shselasky error = upgt_alloc_rx(sc); 281246565Shselasky if (error) 282246565Shselasky goto fail3; 283246565Shselasky 284191983Sweongyo /* Initialize the device. */ 285191983Sweongyo error = upgt_device_reset(sc); 286191983Sweongyo if (error) 287287197Sglebius goto fail4; 288191983Sweongyo /* Verify the firmware. */ 289191983Sweongyo error = upgt_fw_verify(sc); 290191983Sweongyo if (error) 291287197Sglebius goto fail4; 292191983Sweongyo /* Calculate device memory space. */ 293191983Sweongyo if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) { 294191983Sweongyo device_printf(dev, 295199816Sthompsa "could not find memory space addresses on FW\n"); 296191983Sweongyo error = EIO; 297287197Sglebius goto fail4; 298191983Sweongyo } 299191983Sweongyo sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1; 300191983Sweongyo sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1; 301191983Sweongyo 302191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n", 303191983Sweongyo sc->sc_memaddr_frame_start); 304191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n", 305191983Sweongyo sc->sc_memaddr_frame_end); 306191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n", 307191983Sweongyo sc->sc_memaddr_rx_start); 308191983Sweongyo 309191983Sweongyo upgt_mem_init(sc); 310191983Sweongyo 311191983Sweongyo /* Load the firmware. */ 312191983Sweongyo error = upgt_fw_load(sc); 313191983Sweongyo if (error) 314287197Sglebius goto fail4; 315191983Sweongyo 316191983Sweongyo /* Read the whole EEPROM content and parse it. */ 317191983Sweongyo error = upgt_eeprom_read(sc); 318191983Sweongyo if (error) 319287197Sglebius goto fail4; 320191983Sweongyo error = upgt_eeprom_parse(sc); 321191983Sweongyo if (error) 322287197Sglebius goto fail4; 323191983Sweongyo 324191983Sweongyo /* all works related with the device have done here. */ 325191983Sweongyo upgt_abort_xfers(sc); 326191983Sweongyo 327283537Sglebius ic->ic_softc = sc; 328283527Sglebius ic->ic_name = device_get_nameunit(dev); 329191983Sweongyo ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 330191983Sweongyo ic->ic_opmode = IEEE80211_M_STA; 331191983Sweongyo /* set device capabilities */ 332191983Sweongyo ic->ic_caps = 333191983Sweongyo IEEE80211_C_STA /* station mode */ 334191983Sweongyo | IEEE80211_C_MONITOR /* monitor mode */ 335191983Sweongyo | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 336191983Sweongyo | IEEE80211_C_SHSLOT /* short slot time supported */ 337191983Sweongyo | IEEE80211_C_BGSCAN /* capable of bg scanning */ 338191983Sweongyo | IEEE80211_C_WPA /* 802.11i */ 339191983Sweongyo ; 340191983Sweongyo 341293339Savos memset(bands, 0, sizeof(bands)); 342293339Savos setbit(bands, IEEE80211_MODE_11B); 343293339Savos setbit(bands, IEEE80211_MODE_11G); 344293339Savos ieee80211_init_channels(ic, NULL, bands); 345191983Sweongyo 346287197Sglebius ieee80211_ifattach(ic); 347191983Sweongyo ic->ic_raw_xmit = upgt_raw_xmit; 348191983Sweongyo ic->ic_scan_start = upgt_scan_start; 349191983Sweongyo ic->ic_scan_end = upgt_scan_end; 350191983Sweongyo ic->ic_set_channel = upgt_set_channel; 351191983Sweongyo ic->ic_vap_create = upgt_vap_create; 352191983Sweongyo ic->ic_vap_delete = upgt_vap_delete; 353191983Sweongyo ic->ic_update_mcast = upgt_update_mcast; 354287197Sglebius ic->ic_transmit = upgt_transmit; 355287197Sglebius ic->ic_parent = upgt_parent; 356191983Sweongyo 357192468Ssam ieee80211_radiotap_attach(ic, 358192468Ssam &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 359192468Ssam UPGT_TX_RADIOTAP_PRESENT, 360192468Ssam &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 361192468Ssam UPGT_RX_RADIOTAP_PRESENT); 362191983Sweongyo 363191983Sweongyo upgt_sysctl_node(sc); 364191983Sweongyo 365191983Sweongyo if (bootverbose) 366191983Sweongyo ieee80211_announce(ic); 367191983Sweongyo 368191983Sweongyo return (0); 369191983Sweongyo 370246565Shselaskyfail4: upgt_free_rx(sc); 371246565Shselaskyfail3: upgt_free_tx(sc); 372246565Shselaskyfail2: usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS); 373191983Sweongyofail1: mtx_destroy(&sc->sc_mtx); 374191983Sweongyo 375191983Sweongyo return (error); 376191983Sweongyo} 377191983Sweongyo 378191983Sweongyostatic void 379192984Sthompsaupgt_txeof(struct usb_xfer *xfer, struct upgt_data *data) 380191983Sweongyo{ 381191983Sweongyo 382191983Sweongyo if (data->m) { 383287197Sglebius /* XXX status? */ 384287197Sglebius ieee80211_tx_complete(data->ni, data->m, 0); 385191983Sweongyo data->m = NULL; 386191983Sweongyo data->ni = NULL; 387191983Sweongyo } 388191983Sweongyo} 389191983Sweongyo 390191983Sweongyostatic void 391191983Sweongyoupgt_get_stats(struct upgt_softc *sc) 392191983Sweongyo{ 393191983Sweongyo struct upgt_data *data_cmd; 394191983Sweongyo struct upgt_lmac_mem *mem; 395191983Sweongyo struct upgt_lmac_stats *stats; 396191983Sweongyo 397191983Sweongyo data_cmd = upgt_getbuf(sc); 398191983Sweongyo if (data_cmd == NULL) { 399269569Sn_hibma device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 400191983Sweongyo return; 401191983Sweongyo } 402191983Sweongyo 403191983Sweongyo /* 404191983Sweongyo * Transmit the URB containing the CMD data. 405191983Sweongyo */ 406227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 407191983Sweongyo 408191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 409191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 410191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 411191983Sweongyo 412191983Sweongyo stats = (struct upgt_lmac_stats *)(mem + 1); 413191983Sweongyo 414191983Sweongyo stats->header1.flags = 0; 415191983Sweongyo stats->header1.type = UPGT_H1_TYPE_CTRL; 416191983Sweongyo stats->header1.len = htole16( 417191983Sweongyo sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header)); 418191983Sweongyo 419191983Sweongyo stats->header2.reqid = htole32(sc->sc_memaddr_frame_start); 420191983Sweongyo stats->header2.type = htole16(UPGT_H2_TYPE_STATS); 421191983Sweongyo stats->header2.flags = 0; 422191983Sweongyo 423191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*stats); 424191983Sweongyo 425191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)stats, 426191983Sweongyo data_cmd->buflen - sizeof(*mem)); 427191983Sweongyo 428191983Sweongyo upgt_bulk_tx(sc, data_cmd); 429191983Sweongyo} 430191983Sweongyo 431287197Sglebiusstatic void 432287197Sglebiusupgt_parent(struct ieee80211com *ic) 433191983Sweongyo{ 434287197Sglebius struct upgt_softc *sc = ic->ic_softc; 435246614Shselasky int startall = 0; 436191983Sweongyo 437246614Shselasky UPGT_LOCK(sc); 438287197Sglebius if (sc->sc_flags & UPGT_FLAG_DETACHED) { 439287197Sglebius UPGT_UNLOCK(sc); 440287197Sglebius return; 441287197Sglebius } 442287197Sglebius if (ic->ic_nrunning > 0) { 443287197Sglebius if (sc->sc_flags & UPGT_FLAG_INITDONE) { 444287197Sglebius if (ic->ic_allmulti > 0 || ic->ic_promisc > 0) 445287197Sglebius upgt_set_multi(sc); 446191983Sweongyo } else { 447287197Sglebius upgt_init(sc); 448287197Sglebius startall = 1; 449191983Sweongyo } 450287197Sglebius } else if (sc->sc_flags & UPGT_FLAG_INITDONE) 451287197Sglebius upgt_stop(sc); 452287197Sglebius UPGT_UNLOCK(sc); 453287197Sglebius if (startall) 454287197Sglebius ieee80211_start_all(ic); 455191983Sweongyo} 456191983Sweongyo 457191983Sweongyostatic void 458287197Sglebiusupgt_stop(struct upgt_softc *sc) 459191983Sweongyo{ 460191983Sweongyo 461191983Sweongyo UPGT_ASSERT_LOCKED(sc); 462191983Sweongyo 463287197Sglebius if (sc->sc_flags & UPGT_FLAG_INITDONE) 464191983Sweongyo upgt_set_macfilter(sc, IEEE80211_S_INIT); 465191983Sweongyo upgt_abort_xfers_locked(sc); 466191983Sweongyo /* device down */ 467191983Sweongyo sc->sc_tx_timer = 0; 468191983Sweongyo sc->sc_flags &= ~UPGT_FLAG_INITDONE; 469191983Sweongyo} 470191983Sweongyo 471191983Sweongyostatic void 472191983Sweongyoupgt_set_led(struct upgt_softc *sc, int action) 473191983Sweongyo{ 474191983Sweongyo struct upgt_data *data_cmd; 475191983Sweongyo struct upgt_lmac_mem *mem; 476191983Sweongyo struct upgt_lmac_led *led; 477191983Sweongyo 478191983Sweongyo data_cmd = upgt_getbuf(sc); 479191983Sweongyo if (data_cmd == NULL) { 480191983Sweongyo device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 481191983Sweongyo return; 482191983Sweongyo } 483191983Sweongyo 484191983Sweongyo /* 485191983Sweongyo * Transmit the URB containing the CMD data. 486191983Sweongyo */ 487227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 488191983Sweongyo 489191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 490191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 491191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 492191983Sweongyo 493191983Sweongyo led = (struct upgt_lmac_led *)(mem + 1); 494191983Sweongyo 495191983Sweongyo led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 496191983Sweongyo led->header1.type = UPGT_H1_TYPE_CTRL; 497191983Sweongyo led->header1.len = htole16( 498191983Sweongyo sizeof(struct upgt_lmac_led) - 499191983Sweongyo sizeof(struct upgt_lmac_header)); 500191983Sweongyo 501191983Sweongyo led->header2.reqid = htole32(sc->sc_memaddr_frame_start); 502191983Sweongyo led->header2.type = htole16(UPGT_H2_TYPE_LED); 503191983Sweongyo led->header2.flags = 0; 504191983Sweongyo 505191983Sweongyo switch (action) { 506191983Sweongyo case UPGT_LED_OFF: 507191983Sweongyo led->mode = htole16(UPGT_LED_MODE_SET); 508191983Sweongyo led->action_fix = 0; 509191983Sweongyo led->action_tmp = htole16(UPGT_LED_ACTION_OFF); 510191983Sweongyo led->action_tmp_dur = 0; 511191983Sweongyo break; 512191983Sweongyo case UPGT_LED_ON: 513191983Sweongyo led->mode = htole16(UPGT_LED_MODE_SET); 514191983Sweongyo led->action_fix = 0; 515191983Sweongyo led->action_tmp = htole16(UPGT_LED_ACTION_ON); 516191983Sweongyo led->action_tmp_dur = 0; 517191983Sweongyo break; 518191983Sweongyo case UPGT_LED_BLINK: 519191983Sweongyo if (sc->sc_state != IEEE80211_S_RUN) { 520191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 521191983Sweongyo return; 522191983Sweongyo } 523191983Sweongyo if (sc->sc_led_blink) { 524191983Sweongyo /* previous blink was not finished */ 525191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 526191983Sweongyo return; 527191983Sweongyo } 528191983Sweongyo led->mode = htole16(UPGT_LED_MODE_SET); 529191983Sweongyo led->action_fix = htole16(UPGT_LED_ACTION_OFF); 530191983Sweongyo led->action_tmp = htole16(UPGT_LED_ACTION_ON); 531191983Sweongyo led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR); 532191983Sweongyo /* lock blink */ 533191983Sweongyo sc->sc_led_blink = 1; 534191983Sweongyo callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc); 535191983Sweongyo break; 536191983Sweongyo default: 537191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next); 538191983Sweongyo return; 539191983Sweongyo } 540191983Sweongyo 541191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*led); 542191983Sweongyo 543191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)led, 544191983Sweongyo data_cmd->buflen - sizeof(*mem)); 545191983Sweongyo 546191983Sweongyo upgt_bulk_tx(sc, data_cmd); 547191983Sweongyo} 548191983Sweongyo 549191983Sweongyostatic void 550191983Sweongyoupgt_set_led_blink(void *arg) 551191983Sweongyo{ 552191983Sweongyo struct upgt_softc *sc = arg; 553191983Sweongyo 554191983Sweongyo /* blink finished, we are ready for a next one */ 555191983Sweongyo sc->sc_led_blink = 0; 556191983Sweongyo} 557191983Sweongyo 558191983Sweongyostatic void 559287197Sglebiusupgt_init(struct upgt_softc *sc) 560191983Sweongyo{ 561191983Sweongyo 562191983Sweongyo UPGT_ASSERT_LOCKED(sc); 563191983Sweongyo 564287197Sglebius if (sc->sc_flags & UPGT_FLAG_INITDONE) 565287197Sglebius upgt_stop(sc); 566191983Sweongyo 567194228Sthompsa usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]); 568191983Sweongyo 569191983Sweongyo (void)upgt_set_macfilter(sc, IEEE80211_S_SCAN); 570191983Sweongyo 571191983Sweongyo sc->sc_flags |= UPGT_FLAG_INITDONE; 572191983Sweongyo 573191983Sweongyo callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc); 574191983Sweongyo} 575191983Sweongyo 576191983Sweongyostatic int 577191983Sweongyoupgt_set_macfilter(struct upgt_softc *sc, uint8_t state) 578191983Sweongyo{ 579287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 580191983Sweongyo struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 581212131Sthompsa struct ieee80211_node *ni; 582191983Sweongyo struct upgt_data *data_cmd; 583191983Sweongyo struct upgt_lmac_mem *mem; 584191983Sweongyo struct upgt_lmac_filter *filter; 585191983Sweongyo 586191983Sweongyo UPGT_ASSERT_LOCKED(sc); 587191983Sweongyo 588191983Sweongyo data_cmd = upgt_getbuf(sc); 589191983Sweongyo if (data_cmd == NULL) { 590191983Sweongyo device_printf(sc->sc_dev, "out of TX buffers.\n"); 591191983Sweongyo return (ENOBUFS); 592191983Sweongyo } 593191983Sweongyo 594191983Sweongyo /* 595191983Sweongyo * Transmit the URB containing the CMD data. 596191983Sweongyo */ 597227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 598191983Sweongyo 599191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 600191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 601191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 602191983Sweongyo 603191983Sweongyo filter = (struct upgt_lmac_filter *)(mem + 1); 604191983Sweongyo 605191983Sweongyo filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 606191983Sweongyo filter->header1.type = UPGT_H1_TYPE_CTRL; 607191983Sweongyo filter->header1.len = htole16( 608191983Sweongyo sizeof(struct upgt_lmac_filter) - 609191983Sweongyo sizeof(struct upgt_lmac_header)); 610191983Sweongyo 611191983Sweongyo filter->header2.reqid = htole32(sc->sc_memaddr_frame_start); 612191983Sweongyo filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER); 613191983Sweongyo filter->header2.flags = 0; 614191983Sweongyo 615191983Sweongyo switch (state) { 616191983Sweongyo case IEEE80211_S_INIT: 617191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n", 618191983Sweongyo __func__); 619191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_RESET); 620191983Sweongyo break; 621191983Sweongyo case IEEE80211_S_SCAN: 622191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, 623191983Sweongyo "set MAC filter to SCAN (bssid %s)\n", 624287197Sglebius ether_sprintf(ieee80211broadcastaddr)); 625191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_NONE); 626287197Sglebius IEEE80211_ADDR_COPY(filter->dst, 627287197Sglebius vap ? vap->iv_myaddr : ic->ic_macaddr); 628287197Sglebius IEEE80211_ADDR_COPY(filter->src, ieee80211broadcastaddr); 629191983Sweongyo filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 630191983Sweongyo filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 631191983Sweongyo filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 632191983Sweongyo filter->rxhw = htole32(sc->sc_eeprom_hwrx); 633191983Sweongyo filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 634191983Sweongyo break; 635191983Sweongyo case IEEE80211_S_RUN: 636212127Sthompsa ni = ieee80211_ref_node(vap->iv_bss); 637191983Sweongyo /* XXX monitor mode isn't tested yet. */ 638191983Sweongyo if (vap->iv_opmode == IEEE80211_M_MONITOR) { 639191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_MONITOR); 640287197Sglebius IEEE80211_ADDR_COPY(filter->dst, 641287197Sglebius vap ? vap->iv_myaddr : ic->ic_macaddr); 642191983Sweongyo IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid); 643191983Sweongyo filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1); 644191983Sweongyo filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 645191983Sweongyo filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2); 646191983Sweongyo filter->rxhw = htole32(sc->sc_eeprom_hwrx); 647191983Sweongyo filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3); 648191983Sweongyo } else { 649191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, 650191983Sweongyo "set MAC filter to RUN (bssid %s)\n", 651191983Sweongyo ether_sprintf(ni->ni_bssid)); 652191983Sweongyo filter->type = htole16(UPGT_FILTER_TYPE_STA); 653287197Sglebius IEEE80211_ADDR_COPY(filter->dst, 654287197Sglebius vap ? vap->iv_myaddr : ic->ic_macaddr); 655191983Sweongyo IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid); 656191983Sweongyo filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1); 657191983Sweongyo filter->rxaddr = htole32(sc->sc_memaddr_rx_start); 658191983Sweongyo filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2); 659191983Sweongyo filter->rxhw = htole32(sc->sc_eeprom_hwrx); 660191983Sweongyo filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3); 661191983Sweongyo } 662212127Sthompsa ieee80211_free_node(ni); 663191983Sweongyo break; 664191983Sweongyo default: 665191983Sweongyo device_printf(sc->sc_dev, 666199816Sthompsa "MAC filter does not know that state\n"); 667191983Sweongyo break; 668191983Sweongyo } 669191983Sweongyo 670191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*filter); 671191983Sweongyo 672191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)filter, 673191983Sweongyo data_cmd->buflen - sizeof(*mem)); 674191983Sweongyo 675191983Sweongyo upgt_bulk_tx(sc, data_cmd); 676191983Sweongyo 677191983Sweongyo return (0); 678191983Sweongyo} 679191983Sweongyo 680191983Sweongyostatic void 681191983Sweongyoupgt_setup_rates(struct ieee80211vap *vap, struct ieee80211com *ic) 682191983Sweongyo{ 683287197Sglebius struct upgt_softc *sc = ic->ic_softc; 684191983Sweongyo const struct ieee80211_txparam *tp; 685191983Sweongyo 686191983Sweongyo /* 687191983Sweongyo * 0x01 = OFMD6 0x10 = DS1 688191983Sweongyo * 0x04 = OFDM9 0x11 = DS2 689191983Sweongyo * 0x06 = OFDM12 0x12 = DS5 690191983Sweongyo * 0x07 = OFDM18 0x13 = DS11 691191983Sweongyo * 0x08 = OFDM24 692191983Sweongyo * 0x09 = OFDM36 693191983Sweongyo * 0x0a = OFDM48 694191983Sweongyo * 0x0b = OFDM54 695191983Sweongyo */ 696191983Sweongyo const uint8_t rateset_auto_11b[] = 697191983Sweongyo { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 }; 698191983Sweongyo const uint8_t rateset_auto_11g[] = 699191983Sweongyo { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 }; 700191983Sweongyo const uint8_t rateset_fix_11bg[] = 701191983Sweongyo { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07, 702191983Sweongyo 0x08, 0x09, 0x0a, 0x0b }; 703191983Sweongyo 704191983Sweongyo tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 705191983Sweongyo 706191983Sweongyo /* XXX */ 707191983Sweongyo if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 708191983Sweongyo /* 709191983Sweongyo * Automatic rate control is done by the device. 710191983Sweongyo * We just pass the rateset from which the device 711191983Sweongyo * will pickup a rate. 712191983Sweongyo */ 713191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11B) 714227461Shselasky memcpy(sc->sc_cur_rateset, rateset_auto_11b, 715191983Sweongyo sizeof(sc->sc_cur_rateset)); 716191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11G || 717191983Sweongyo ic->ic_curmode == IEEE80211_MODE_AUTO) 718227461Shselasky memcpy(sc->sc_cur_rateset, rateset_auto_11g, 719191983Sweongyo sizeof(sc->sc_cur_rateset)); 720191983Sweongyo } else { 721191983Sweongyo /* set a fixed rate */ 722191983Sweongyo memset(sc->sc_cur_rateset, rateset_fix_11bg[tp->ucastrate], 723191983Sweongyo sizeof(sc->sc_cur_rateset)); 724191983Sweongyo } 725191983Sweongyo} 726191983Sweongyo 727191983Sweongyostatic void 728191983Sweongyoupgt_set_multi(void *arg) 729191983Sweongyo{ 730191983Sweongyo 731287197Sglebius /* XXX don't know how to set a device. Lack of docs. */ 732287197Sglebius} 733191983Sweongyo 734287197Sglebiusstatic int 735287197Sglebiusupgt_transmit(struct ieee80211com *ic, struct mbuf *m) 736287197Sglebius{ 737287197Sglebius struct upgt_softc *sc = ic->ic_softc; 738287197Sglebius int error; 739287197Sglebius 740287197Sglebius UPGT_LOCK(sc); 741287197Sglebius if ((sc->sc_flags & UPGT_FLAG_INITDONE) == 0) { 742287197Sglebius UPGT_UNLOCK(sc); 743287197Sglebius return (ENXIO); 744287197Sglebius } 745287197Sglebius error = mbufq_enqueue(&sc->sc_snd, m); 746287197Sglebius if (error) { 747287197Sglebius UPGT_UNLOCK(sc); 748287197Sglebius return (error); 749287197Sglebius } 750287197Sglebius upgt_start(sc); 751287197Sglebius UPGT_UNLOCK(sc); 752287197Sglebius 753287197Sglebius return (0); 754191983Sweongyo} 755191983Sweongyo 756191983Sweongyostatic void 757287197Sglebiusupgt_start(struct upgt_softc *sc) 758191983Sweongyo{ 759191983Sweongyo struct upgt_data *data_tx; 760191983Sweongyo struct ieee80211_node *ni; 761191983Sweongyo struct mbuf *m; 762191983Sweongyo 763287197Sglebius UPGT_ASSERT_LOCKED(sc); 764287197Sglebius 765287197Sglebius if ((sc->sc_flags & UPGT_FLAG_INITDONE) == 0) 766191983Sweongyo return; 767191983Sweongyo 768287197Sglebius while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 769191983Sweongyo data_tx = upgt_gettxbuf(sc); 770191983Sweongyo if (data_tx == NULL) { 771287197Sglebius mbufq_prepend(&sc->sc_snd, m); 772191983Sweongyo break; 773191983Sweongyo } 774191983Sweongyo 775191983Sweongyo ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 776191983Sweongyo m->m_pkthdr.rcvif = NULL; 777191983Sweongyo 778191983Sweongyo if (upgt_tx_start(sc, m, ni, data_tx) != 0) { 779287197Sglebius if_inc_counter(ni->ni_vap->iv_ifp, 780287197Sglebius IFCOUNTER_OERRORS, 1); 781191983Sweongyo STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next); 782191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 783191983Sweongyo ieee80211_free_node(ni); 784191983Sweongyo continue; 785191983Sweongyo } 786191983Sweongyo sc->sc_tx_timer = 5; 787191983Sweongyo } 788191983Sweongyo} 789191983Sweongyo 790191983Sweongyostatic int 791191983Sweongyoupgt_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 792191983Sweongyo const struct ieee80211_bpf_params *params) 793191983Sweongyo{ 794191983Sweongyo struct ieee80211com *ic = ni->ni_ic; 795286950Sadrian struct upgt_softc *sc = ic->ic_softc; 796191983Sweongyo struct upgt_data *data_tx = NULL; 797191983Sweongyo 798287197Sglebius UPGT_LOCK(sc); 799191983Sweongyo /* prevent management frames from being sent if we're not ready */ 800287197Sglebius if (!(sc->sc_flags & UPGT_FLAG_INITDONE)) { 801191983Sweongyo m_freem(m); 802287197Sglebius UPGT_UNLOCK(sc); 803191983Sweongyo return ENETDOWN; 804191983Sweongyo } 805191983Sweongyo 806191983Sweongyo data_tx = upgt_gettxbuf(sc); 807191983Sweongyo if (data_tx == NULL) { 808191983Sweongyo m_freem(m); 809191983Sweongyo UPGT_UNLOCK(sc); 810191983Sweongyo return (ENOBUFS); 811191983Sweongyo } 812191983Sweongyo 813191983Sweongyo if (upgt_tx_start(sc, m, ni, data_tx) != 0) { 814191983Sweongyo STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next); 815191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 816191983Sweongyo UPGT_UNLOCK(sc); 817191983Sweongyo return (EIO); 818191983Sweongyo } 819191983Sweongyo UPGT_UNLOCK(sc); 820191983Sweongyo 821191983Sweongyo sc->sc_tx_timer = 5; 822191983Sweongyo return (0); 823191983Sweongyo} 824191983Sweongyo 825191983Sweongyostatic void 826191983Sweongyoupgt_watchdog(void *arg) 827191983Sweongyo{ 828191983Sweongyo struct upgt_softc *sc = arg; 829287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 830191983Sweongyo 831191983Sweongyo if (sc->sc_tx_timer > 0) { 832191983Sweongyo if (--sc->sc_tx_timer == 0) { 833191983Sweongyo device_printf(sc->sc_dev, "watchdog timeout\n"); 834287197Sglebius /* upgt_init(sc); XXX needs a process context ? */ 835287197Sglebius counter_u64_add(ic->ic_oerrors, 1); 836191983Sweongyo return; 837191983Sweongyo } 838191983Sweongyo callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc); 839191983Sweongyo } 840191983Sweongyo} 841191983Sweongyo 842191983Sweongyostatic uint32_t 843191983Sweongyoupgt_mem_alloc(struct upgt_softc *sc) 844191983Sweongyo{ 845191983Sweongyo int i; 846191983Sweongyo 847191983Sweongyo for (i = 0; i < sc->sc_memory.pages; i++) { 848191983Sweongyo if (sc->sc_memory.page[i].used == 0) { 849191983Sweongyo sc->sc_memory.page[i].used = 1; 850191983Sweongyo return (sc->sc_memory.page[i].addr); 851191983Sweongyo } 852191983Sweongyo } 853191983Sweongyo 854191983Sweongyo return (0); 855191983Sweongyo} 856191983Sweongyo 857191983Sweongyostatic void 858191983Sweongyoupgt_scan_start(struct ieee80211com *ic) 859191983Sweongyo{ 860191983Sweongyo /* do nothing. */ 861191983Sweongyo} 862191983Sweongyo 863191983Sweongyostatic void 864191983Sweongyoupgt_scan_end(struct ieee80211com *ic) 865191983Sweongyo{ 866191983Sweongyo /* do nothing. */ 867191983Sweongyo} 868191983Sweongyo 869191983Sweongyostatic void 870191983Sweongyoupgt_set_channel(struct ieee80211com *ic) 871191983Sweongyo{ 872286950Sadrian struct upgt_softc *sc = ic->ic_softc; 873191983Sweongyo 874191983Sweongyo UPGT_LOCK(sc); 875191983Sweongyo upgt_set_chan(sc, ic->ic_curchan); 876191983Sweongyo UPGT_UNLOCK(sc); 877191983Sweongyo} 878191983Sweongyo 879191983Sweongyostatic void 880191983Sweongyoupgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c) 881191983Sweongyo{ 882287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 883191983Sweongyo struct upgt_data *data_cmd; 884191983Sweongyo struct upgt_lmac_mem *mem; 885191983Sweongyo struct upgt_lmac_channel *chan; 886191983Sweongyo int channel; 887191983Sweongyo 888191983Sweongyo UPGT_ASSERT_LOCKED(sc); 889191983Sweongyo 890191983Sweongyo channel = ieee80211_chan2ieee(ic, c); 891191983Sweongyo if (channel == 0 || channel == IEEE80211_CHAN_ANY) { 892191983Sweongyo /* XXX should NEVER happen */ 893191983Sweongyo device_printf(sc->sc_dev, 894191983Sweongyo "%s: invalid channel %x\n", __func__, channel); 895191983Sweongyo return; 896191983Sweongyo } 897191983Sweongyo 898191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel); 899191983Sweongyo 900191983Sweongyo data_cmd = upgt_getbuf(sc); 901191983Sweongyo if (data_cmd == NULL) { 902191983Sweongyo device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__); 903191983Sweongyo return; 904191983Sweongyo } 905191983Sweongyo /* 906191983Sweongyo * Transmit the URB containing the CMD data. 907191983Sweongyo */ 908227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 909191983Sweongyo 910191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 911191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 912191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 913191983Sweongyo 914191983Sweongyo chan = (struct upgt_lmac_channel *)(mem + 1); 915191983Sweongyo 916191983Sweongyo chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK; 917191983Sweongyo chan->header1.type = UPGT_H1_TYPE_CTRL; 918191983Sweongyo chan->header1.len = htole16( 919191983Sweongyo sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header)); 920191983Sweongyo 921191983Sweongyo chan->header2.reqid = htole32(sc->sc_memaddr_frame_start); 922191983Sweongyo chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL); 923191983Sweongyo chan->header2.flags = 0; 924191983Sweongyo 925191983Sweongyo chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1); 926191983Sweongyo chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2); 927191983Sweongyo chan->freq6 = sc->sc_eeprom_freq6[channel]; 928191983Sweongyo chan->settings = sc->sc_eeprom_freq6_settings; 929191983Sweongyo chan->unknown3 = UPGT_CHANNEL_UNKNOWN3; 930191983Sweongyo 931227461Shselasky memcpy(chan->freq3_1, &sc->sc_eeprom_freq3[channel].data, 932191983Sweongyo sizeof(chan->freq3_1)); 933227461Shselasky memcpy(chan->freq4, &sc->sc_eeprom_freq4[channel], 934191983Sweongyo sizeof(sc->sc_eeprom_freq4[channel])); 935227461Shselasky memcpy(chan->freq3_2, &sc->sc_eeprom_freq3[channel].data, 936191983Sweongyo sizeof(chan->freq3_2)); 937191983Sweongyo 938191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*chan); 939191983Sweongyo 940191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)chan, 941191983Sweongyo data_cmd->buflen - sizeof(*mem)); 942191983Sweongyo 943191983Sweongyo upgt_bulk_tx(sc, data_cmd); 944191983Sweongyo} 945191983Sweongyo 946191983Sweongyostatic struct ieee80211vap * 947228621Sbschmidtupgt_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 948228621Sbschmidt enum ieee80211_opmode opmode, int flags, 949228621Sbschmidt const uint8_t bssid[IEEE80211_ADDR_LEN], 950228621Sbschmidt const uint8_t mac[IEEE80211_ADDR_LEN]) 951191983Sweongyo{ 952191983Sweongyo struct upgt_vap *uvp; 953191983Sweongyo struct ieee80211vap *vap; 954191983Sweongyo 955191983Sweongyo if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 956191983Sweongyo return NULL; 957288667Skevlo uvp = malloc(sizeof(struct upgt_vap), M_80211_VAP, M_WAITOK | M_ZERO); 958191983Sweongyo vap = &uvp->vap; 959191983Sweongyo /* enable s/w bmiss handling for sta mode */ 960191983Sweongyo 961257743Shselasky if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 962287197Sglebius flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) { 963257743Shselasky /* out of memory */ 964257743Shselasky free(uvp, M_80211_VAP); 965257743Shselasky return (NULL); 966257743Shselasky } 967257743Shselasky 968191983Sweongyo /* override state transition machine */ 969191983Sweongyo uvp->newstate = vap->iv_newstate; 970191983Sweongyo vap->iv_newstate = upgt_newstate; 971191983Sweongyo 972191983Sweongyo /* setup device rates */ 973191983Sweongyo upgt_setup_rates(vap, ic); 974191983Sweongyo 975191983Sweongyo /* complete setup */ 976191983Sweongyo ieee80211_vap_attach(vap, ieee80211_media_change, 977287197Sglebius ieee80211_media_status, mac); 978191983Sweongyo ic->ic_opmode = opmode; 979191983Sweongyo return vap; 980191983Sweongyo} 981191983Sweongyo 982191983Sweongyostatic int 983191983Sweongyoupgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 984191983Sweongyo{ 985191983Sweongyo struct upgt_vap *uvp = UPGT_VAP(vap); 986191983Sweongyo struct ieee80211com *ic = vap->iv_ic; 987286950Sadrian struct upgt_softc *sc = ic->ic_softc; 988191983Sweongyo 989191983Sweongyo /* do it in a process context */ 990191983Sweongyo sc->sc_state = nstate; 991191983Sweongyo 992191983Sweongyo IEEE80211_UNLOCK(ic); 993191983Sweongyo UPGT_LOCK(sc); 994191983Sweongyo callout_stop(&sc->sc_led_ch); 995191983Sweongyo callout_stop(&sc->sc_watchdog_ch); 996191983Sweongyo 997191983Sweongyo switch (nstate) { 998191983Sweongyo case IEEE80211_S_INIT: 999191983Sweongyo /* do not accept any frames if the device is down */ 1000191983Sweongyo (void)upgt_set_macfilter(sc, sc->sc_state); 1001191983Sweongyo upgt_set_led(sc, UPGT_LED_OFF); 1002191983Sweongyo break; 1003191983Sweongyo case IEEE80211_S_SCAN: 1004191983Sweongyo upgt_set_chan(sc, ic->ic_curchan); 1005191983Sweongyo break; 1006191983Sweongyo case IEEE80211_S_AUTH: 1007191983Sweongyo upgt_set_chan(sc, ic->ic_curchan); 1008191983Sweongyo break; 1009191983Sweongyo case IEEE80211_S_ASSOC: 1010191983Sweongyo break; 1011191983Sweongyo case IEEE80211_S_RUN: 1012191983Sweongyo upgt_set_macfilter(sc, sc->sc_state); 1013191983Sweongyo upgt_set_led(sc, UPGT_LED_ON); 1014191983Sweongyo break; 1015191983Sweongyo default: 1016191983Sweongyo break; 1017191983Sweongyo } 1018191983Sweongyo UPGT_UNLOCK(sc); 1019191983Sweongyo IEEE80211_LOCK(ic); 1020191983Sweongyo return (uvp->newstate(vap, nstate, arg)); 1021191983Sweongyo} 1022191983Sweongyo 1023191983Sweongyostatic void 1024191983Sweongyoupgt_vap_delete(struct ieee80211vap *vap) 1025191983Sweongyo{ 1026191983Sweongyo struct upgt_vap *uvp = UPGT_VAP(vap); 1027191983Sweongyo 1028191983Sweongyo ieee80211_vap_detach(vap); 1029191983Sweongyo free(uvp, M_80211_VAP); 1030191983Sweongyo} 1031191983Sweongyo 1032191983Sweongyostatic void 1033283540Sglebiusupgt_update_mcast(struct ieee80211com *ic) 1034191983Sweongyo{ 1035283540Sglebius struct upgt_softc *sc = ic->ic_softc; 1036191983Sweongyo 1037191983Sweongyo upgt_set_multi(sc); 1038191983Sweongyo} 1039191983Sweongyo 1040191983Sweongyostatic int 1041191983Sweongyoupgt_eeprom_parse(struct upgt_softc *sc) 1042191983Sweongyo{ 1043287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 1044191983Sweongyo struct upgt_eeprom_header *eeprom_header; 1045191983Sweongyo struct upgt_eeprom_option *eeprom_option; 1046191983Sweongyo uint16_t option_len; 1047191983Sweongyo uint16_t option_type; 1048191983Sweongyo uint16_t preamble_len; 1049191983Sweongyo int option_end = 0; 1050191983Sweongyo 1051191983Sweongyo /* calculate eeprom options start offset */ 1052191983Sweongyo eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom; 1053191983Sweongyo preamble_len = le16toh(eeprom_header->preamble_len); 1054191983Sweongyo eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom + 1055191983Sweongyo (sizeof(struct upgt_eeprom_header) + preamble_len)); 1056191983Sweongyo 1057191983Sweongyo while (!option_end) { 1058246944Shselasky 1059246944Shselasky /* sanity check */ 1060246944Shselasky if (eeprom_option >= (struct upgt_eeprom_option *) 1061246944Shselasky (sc->sc_eeprom + UPGT_EEPROM_SIZE)) { 1062246944Shselasky return (EINVAL); 1063246944Shselasky } 1064246944Shselasky 1065191983Sweongyo /* the eeprom option length is stored in words */ 1066191983Sweongyo option_len = 1067191983Sweongyo (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t); 1068191983Sweongyo option_type = 1069191983Sweongyo le16toh(eeprom_option->type); 1070191983Sweongyo 1071246944Shselasky /* sanity check */ 1072246944Shselasky if (option_len == 0 || option_len >= UPGT_EEPROM_SIZE) 1073246944Shselasky return (EINVAL); 1074246944Shselasky 1075191983Sweongyo switch (option_type) { 1076191983Sweongyo case UPGT_EEPROM_TYPE_NAME: 1077191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1078191983Sweongyo "EEPROM name len=%d\n", option_len); 1079191983Sweongyo break; 1080191983Sweongyo case UPGT_EEPROM_TYPE_SERIAL: 1081191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1082191983Sweongyo "EEPROM serial len=%d\n", option_len); 1083191983Sweongyo break; 1084191983Sweongyo case UPGT_EEPROM_TYPE_MAC: 1085191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1086191983Sweongyo "EEPROM mac len=%d\n", option_len); 1087191983Sweongyo 1088287197Sglebius IEEE80211_ADDR_COPY(ic->ic_macaddr, 1089287197Sglebius eeprom_option->data); 1090191983Sweongyo break; 1091191983Sweongyo case UPGT_EEPROM_TYPE_HWRX: 1092191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1093191983Sweongyo "EEPROM hwrx len=%d\n", option_len); 1094191983Sweongyo 1095191983Sweongyo upgt_eeprom_parse_hwrx(sc, eeprom_option->data); 1096191983Sweongyo break; 1097191983Sweongyo case UPGT_EEPROM_TYPE_CHIP: 1098191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1099191983Sweongyo "EEPROM chip len=%d\n", option_len); 1100191983Sweongyo break; 1101191983Sweongyo case UPGT_EEPROM_TYPE_FREQ3: 1102191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1103191983Sweongyo "EEPROM freq3 len=%d\n", option_len); 1104191983Sweongyo 1105191983Sweongyo upgt_eeprom_parse_freq3(sc, eeprom_option->data, 1106191983Sweongyo option_len); 1107191983Sweongyo break; 1108191983Sweongyo case UPGT_EEPROM_TYPE_FREQ4: 1109191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1110191983Sweongyo "EEPROM freq4 len=%d\n", option_len); 1111191983Sweongyo 1112191983Sweongyo upgt_eeprom_parse_freq4(sc, eeprom_option->data, 1113191983Sweongyo option_len); 1114191983Sweongyo break; 1115191983Sweongyo case UPGT_EEPROM_TYPE_FREQ5: 1116191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1117191983Sweongyo "EEPROM freq5 len=%d\n", option_len); 1118191983Sweongyo break; 1119191983Sweongyo case UPGT_EEPROM_TYPE_FREQ6: 1120191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1121191983Sweongyo "EEPROM freq6 len=%d\n", option_len); 1122191983Sweongyo 1123191983Sweongyo upgt_eeprom_parse_freq6(sc, eeprom_option->data, 1124191983Sweongyo option_len); 1125191983Sweongyo break; 1126191983Sweongyo case UPGT_EEPROM_TYPE_END: 1127191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1128191983Sweongyo "EEPROM end len=%d\n", option_len); 1129191983Sweongyo option_end = 1; 1130191983Sweongyo break; 1131191983Sweongyo case UPGT_EEPROM_TYPE_OFF: 1132191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1133199816Sthompsa "%s: EEPROM off without end option\n", __func__); 1134191983Sweongyo return (EIO); 1135191983Sweongyo default: 1136191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1137191983Sweongyo "EEPROM unknown type 0x%04x len=%d\n", 1138191983Sweongyo option_type, option_len); 1139191983Sweongyo break; 1140191983Sweongyo } 1141191983Sweongyo 1142191983Sweongyo /* jump to next EEPROM option */ 1143191983Sweongyo eeprom_option = (struct upgt_eeprom_option *) 1144191983Sweongyo (eeprom_option->data + option_len); 1145191983Sweongyo } 1146191983Sweongyo return (0); 1147191983Sweongyo} 1148191983Sweongyo 1149191983Sweongyostatic void 1150191983Sweongyoupgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len) 1151191983Sweongyo{ 1152191983Sweongyo struct upgt_eeprom_freq3_header *freq3_header; 1153191983Sweongyo struct upgt_lmac_freq3 *freq3; 1154246944Shselasky int i; 1155246944Shselasky int elements; 1156246944Shselasky int flags; 1157191983Sweongyo unsigned channel; 1158191983Sweongyo 1159191983Sweongyo freq3_header = (struct upgt_eeprom_freq3_header *)data; 1160191983Sweongyo freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1); 1161191983Sweongyo 1162191983Sweongyo flags = freq3_header->flags; 1163191983Sweongyo elements = freq3_header->elements; 1164191983Sweongyo 1165191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n", 1166191983Sweongyo flags, elements); 1167191983Sweongyo 1168246944Shselasky if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq3[0]))) 1169246944Shselasky return; 1170246944Shselasky 1171191983Sweongyo for (i = 0; i < elements; i++) { 1172191983Sweongyo channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0); 1173233774Shselasky if (channel >= IEEE80211_CHAN_MAX) 1174191983Sweongyo continue; 1175191983Sweongyo 1176191983Sweongyo sc->sc_eeprom_freq3[channel] = freq3[i]; 1177191983Sweongyo 1178191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1179191983Sweongyo le16toh(sc->sc_eeprom_freq3[channel].freq), channel); 1180191983Sweongyo } 1181191983Sweongyo} 1182191983Sweongyo 1183191983Sweongyovoid 1184191983Sweongyoupgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len) 1185191983Sweongyo{ 1186191983Sweongyo struct upgt_eeprom_freq4_header *freq4_header; 1187191983Sweongyo struct upgt_eeprom_freq4_1 *freq4_1; 1188191983Sweongyo struct upgt_eeprom_freq4_2 *freq4_2; 1189246944Shselasky int i; 1190246944Shselasky int j; 1191246944Shselasky int elements; 1192246944Shselasky int settings; 1193246944Shselasky int flags; 1194191983Sweongyo unsigned channel; 1195191983Sweongyo 1196191983Sweongyo freq4_header = (struct upgt_eeprom_freq4_header *)data; 1197191983Sweongyo freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1); 1198191983Sweongyo flags = freq4_header->flags; 1199191983Sweongyo elements = freq4_header->elements; 1200191983Sweongyo settings = freq4_header->settings; 1201191983Sweongyo 1202191983Sweongyo /* we need this value later */ 1203191983Sweongyo sc->sc_eeprom_freq6_settings = freq4_header->settings; 1204191983Sweongyo 1205191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n", 1206191983Sweongyo flags, elements, settings); 1207191983Sweongyo 1208246944Shselasky if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq4_1[0]))) 1209246944Shselasky return; 1210246944Shselasky 1211191983Sweongyo for (i = 0; i < elements; i++) { 1212191983Sweongyo channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0); 1213233774Shselasky if (channel >= IEEE80211_CHAN_MAX) 1214191983Sweongyo continue; 1215191983Sweongyo 1216191983Sweongyo freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data; 1217191983Sweongyo for (j = 0; j < settings; j++) { 1218191983Sweongyo sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j]; 1219191983Sweongyo sc->sc_eeprom_freq4[channel][j].pad = 0; 1220191983Sweongyo } 1221191983Sweongyo 1222191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1223191983Sweongyo le16toh(freq4_1[i].freq), channel); 1224191983Sweongyo } 1225191983Sweongyo} 1226191983Sweongyo 1227191983Sweongyovoid 1228191983Sweongyoupgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len) 1229191983Sweongyo{ 1230191983Sweongyo struct upgt_lmac_freq6 *freq6; 1231246944Shselasky int i; 1232246944Shselasky int elements; 1233191983Sweongyo unsigned channel; 1234191983Sweongyo 1235191983Sweongyo freq6 = (struct upgt_lmac_freq6 *)data; 1236191983Sweongyo elements = len / sizeof(struct upgt_lmac_freq6); 1237191983Sweongyo 1238191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements); 1239191983Sweongyo 1240246944Shselasky if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq6[0]))) 1241246944Shselasky return; 1242246944Shselasky 1243191983Sweongyo for (i = 0; i < elements; i++) { 1244191983Sweongyo channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0); 1245233774Shselasky if (channel >= IEEE80211_CHAN_MAX) 1246191983Sweongyo continue; 1247191983Sweongyo 1248191983Sweongyo sc->sc_eeprom_freq6[channel] = freq6[i]; 1249191983Sweongyo 1250191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n", 1251191983Sweongyo le16toh(sc->sc_eeprom_freq6[channel].freq), channel); 1252191983Sweongyo } 1253191983Sweongyo} 1254191983Sweongyo 1255191983Sweongyostatic void 1256191983Sweongyoupgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data) 1257191983Sweongyo{ 1258191983Sweongyo struct upgt_eeprom_option_hwrx *option_hwrx; 1259191983Sweongyo 1260191983Sweongyo option_hwrx = (struct upgt_eeprom_option_hwrx *)data; 1261191983Sweongyo 1262191983Sweongyo sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST; 1263191983Sweongyo 1264191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n", 1265191983Sweongyo sc->sc_eeprom_hwrx); 1266191983Sweongyo} 1267191983Sweongyo 1268191983Sweongyostatic int 1269191983Sweongyoupgt_eeprom_read(struct upgt_softc *sc) 1270191983Sweongyo{ 1271191983Sweongyo struct upgt_data *data_cmd; 1272191983Sweongyo struct upgt_lmac_mem *mem; 1273191983Sweongyo struct upgt_lmac_eeprom *eeprom; 1274191983Sweongyo int block, error, offset; 1275191983Sweongyo 1276191983Sweongyo UPGT_LOCK(sc); 1277194228Sthompsa usb_pause_mtx(&sc->sc_mtx, 100); 1278191983Sweongyo 1279191983Sweongyo offset = 0; 1280191983Sweongyo block = UPGT_EEPROM_BLOCK_SIZE; 1281191983Sweongyo while (offset < UPGT_EEPROM_SIZE) { 1282191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1283191983Sweongyo "request EEPROM block (offset=%d, len=%d)\n", offset, block); 1284191983Sweongyo 1285191983Sweongyo data_cmd = upgt_getbuf(sc); 1286191983Sweongyo if (data_cmd == NULL) { 1287191983Sweongyo UPGT_UNLOCK(sc); 1288191983Sweongyo return (ENOBUFS); 1289191983Sweongyo } 1290191983Sweongyo 1291191983Sweongyo /* 1292191983Sweongyo * Transmit the URB containing the CMD data. 1293191983Sweongyo */ 1294227461Shselasky memset(data_cmd->buf, 0, MCLBYTES); 1295191983Sweongyo 1296191983Sweongyo mem = (struct upgt_lmac_mem *)data_cmd->buf; 1297191983Sweongyo mem->addr = htole32(sc->sc_memaddr_frame_start + 1298191983Sweongyo UPGT_MEMSIZE_FRAME_HEAD); 1299191983Sweongyo 1300191983Sweongyo eeprom = (struct upgt_lmac_eeprom *)(mem + 1); 1301191983Sweongyo eeprom->header1.flags = 0; 1302191983Sweongyo eeprom->header1.type = UPGT_H1_TYPE_CTRL; 1303191983Sweongyo eeprom->header1.len = htole16(( 1304191983Sweongyo sizeof(struct upgt_lmac_eeprom) - 1305191983Sweongyo sizeof(struct upgt_lmac_header)) + block); 1306191983Sweongyo 1307191983Sweongyo eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start); 1308191983Sweongyo eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM); 1309191983Sweongyo eeprom->header2.flags = 0; 1310191983Sweongyo 1311191983Sweongyo eeprom->offset = htole16(offset); 1312191983Sweongyo eeprom->len = htole16(block); 1313191983Sweongyo 1314191983Sweongyo data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block; 1315191983Sweongyo 1316191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)eeprom, 1317191983Sweongyo data_cmd->buflen - sizeof(*mem)); 1318191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1319191983Sweongyo 1320191983Sweongyo error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz); 1321191983Sweongyo if (error != 0) { 1322191983Sweongyo device_printf(sc->sc_dev, 1323199816Sthompsa "timeout while waiting for EEPROM data\n"); 1324191983Sweongyo UPGT_UNLOCK(sc); 1325191983Sweongyo return (EIO); 1326191983Sweongyo } 1327191983Sweongyo 1328191983Sweongyo offset += block; 1329191983Sweongyo if (UPGT_EEPROM_SIZE - offset < block) 1330191983Sweongyo block = UPGT_EEPROM_SIZE - offset; 1331191983Sweongyo } 1332191983Sweongyo 1333191983Sweongyo UPGT_UNLOCK(sc); 1334191983Sweongyo return (0); 1335191983Sweongyo} 1336191983Sweongyo 1337191983Sweongyo/* 1338191983Sweongyo * When a rx data came in the function returns a mbuf and a rssi values. 1339191983Sweongyo */ 1340191983Sweongyostatic struct mbuf * 1341192984Sthompsaupgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi) 1342191983Sweongyo{ 1343191983Sweongyo struct mbuf *m = NULL; 1344194677Sthompsa struct upgt_softc *sc = usbd_xfer_softc(xfer); 1345191983Sweongyo struct upgt_lmac_header *header; 1346191983Sweongyo struct upgt_lmac_eeprom *eeprom; 1347191983Sweongyo uint8_t h1_type; 1348191983Sweongyo uint16_t h2_type; 1349194677Sthompsa int actlen, sumlen; 1350191983Sweongyo 1351194677Sthompsa usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1352194677Sthompsa 1353191983Sweongyo UPGT_ASSERT_LOCKED(sc); 1354191983Sweongyo 1355194677Sthompsa if (actlen < 1) 1356191983Sweongyo return (NULL); 1357191983Sweongyo 1358191983Sweongyo /* Check only at the very beginning. */ 1359191983Sweongyo if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) && 1360191983Sweongyo (memcmp(data->buf, "OK", 2) == 0)) { 1361191983Sweongyo sc->sc_flags |= UPGT_FLAG_FWLOADED; 1362191983Sweongyo wakeup_one(sc); 1363191983Sweongyo return (NULL); 1364191983Sweongyo } 1365191983Sweongyo 1366233774Shselasky if (actlen < (int)UPGT_RX_MINSZ) 1367191983Sweongyo return (NULL); 1368191983Sweongyo 1369191983Sweongyo /* 1370191983Sweongyo * Check what type of frame came in. 1371191983Sweongyo */ 1372191983Sweongyo header = (struct upgt_lmac_header *)(data->buf + 4); 1373191983Sweongyo 1374191983Sweongyo h1_type = header->header1.type; 1375191983Sweongyo h2_type = le16toh(header->header2.type); 1376191983Sweongyo 1377191983Sweongyo if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) { 1378191983Sweongyo eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4); 1379191983Sweongyo uint16_t eeprom_offset = le16toh(eeprom->offset); 1380191983Sweongyo uint16_t eeprom_len = le16toh(eeprom->len); 1381191983Sweongyo 1382191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1383191983Sweongyo "received EEPROM block (offset=%d, len=%d)\n", 1384191983Sweongyo eeprom_offset, eeprom_len); 1385191983Sweongyo 1386227461Shselasky memcpy(sc->sc_eeprom + eeprom_offset, 1387227461Shselasky data->buf + sizeof(struct upgt_lmac_eeprom) + 4, 1388227461Shselasky eeprom_len); 1389191983Sweongyo 1390191983Sweongyo /* EEPROM data has arrived in time, wakeup. */ 1391191983Sweongyo wakeup(sc); 1392191983Sweongyo } else if (h1_type == UPGT_H1_TYPE_CTRL && 1393191983Sweongyo h2_type == UPGT_H2_TYPE_TX_DONE) { 1394191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n", 1395191983Sweongyo __func__); 1396191983Sweongyo upgt_tx_done(sc, data->buf + 4); 1397191983Sweongyo } else if (h1_type == UPGT_H1_TYPE_RX_DATA || 1398191983Sweongyo h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) { 1399191983Sweongyo DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n", 1400191983Sweongyo __func__); 1401191983Sweongyo m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len), 1402191983Sweongyo rssi); 1403191983Sweongyo } else if (h1_type == UPGT_H1_TYPE_CTRL && 1404191983Sweongyo h2_type == UPGT_H2_TYPE_STATS) { 1405191983Sweongyo DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n", 1406191983Sweongyo __func__); 1407191983Sweongyo /* TODO: what could we do with the statistic data? */ 1408191983Sweongyo } else { 1409191983Sweongyo /* ignore unknown frame types */ 1410191983Sweongyo DPRINTF(sc, UPGT_DEBUG_INTR, 1411191983Sweongyo "received unknown frame type 0x%02x\n", 1412191983Sweongyo header->header1.type); 1413191983Sweongyo } 1414191983Sweongyo return (m); 1415191983Sweongyo} 1416191983Sweongyo 1417191983Sweongyo/* 1418191983Sweongyo * The firmware awaits a checksum for each frame we send to it. 1419191983Sweongyo * The algorithm used therefor is uncommon but somehow similar to CRC32. 1420191983Sweongyo */ 1421191983Sweongyostatic uint32_t 1422191983Sweongyoupgt_chksum_le(const uint32_t *buf, size_t size) 1423191983Sweongyo{ 1424233774Shselasky size_t i; 1425191983Sweongyo uint32_t crc = 0; 1426191983Sweongyo 1427191983Sweongyo for (i = 0; i < size; i += sizeof(uint32_t)) { 1428191983Sweongyo crc = htole32(crc ^ *buf++); 1429191983Sweongyo crc = htole32((crc >> 5) ^ (crc << 3)); 1430191983Sweongyo } 1431191983Sweongyo 1432191983Sweongyo return (crc); 1433191983Sweongyo} 1434191983Sweongyo 1435191983Sweongyostatic struct mbuf * 1436191983Sweongyoupgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi) 1437191983Sweongyo{ 1438287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 1439191983Sweongyo struct upgt_lmac_rx_desc *rxdesc; 1440191983Sweongyo struct mbuf *m; 1441191983Sweongyo 1442191983Sweongyo /* 1443191983Sweongyo * don't pass packets to the ieee80211 framework if the driver isn't 1444191983Sweongyo * RUNNING. 1445191983Sweongyo */ 1446287197Sglebius if (!(sc->sc_flags & UPGT_FLAG_INITDONE)) 1447191983Sweongyo return (NULL); 1448191983Sweongyo 1449191983Sweongyo /* access RX packet descriptor */ 1450191983Sweongyo rxdesc = (struct upgt_lmac_rx_desc *)data; 1451191983Sweongyo 1452191983Sweongyo /* create mbuf which is suitable for strict alignment archs */ 1453191983Sweongyo KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES, 1454191983Sweongyo ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN)); 1455243857Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1456191983Sweongyo if (m == NULL) { 1457199816Sthompsa device_printf(sc->sc_dev, "could not create RX mbuf\n"); 1458191983Sweongyo return (NULL); 1459191983Sweongyo } 1460191983Sweongyo m_adj(m, ETHER_ALIGN); 1461227461Shselasky memcpy(mtod(m, char *), rxdesc->data, pkglen); 1462191983Sweongyo /* trim FCS */ 1463191983Sweongyo m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN; 1464191983Sweongyo 1465192468Ssam if (ieee80211_radiotap_active(ic)) { 1466191983Sweongyo struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap; 1467191983Sweongyo 1468191983Sweongyo tap->wr_flags = 0; 1469191983Sweongyo tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate); 1470191983Sweongyo tap->wr_antsignal = rxdesc->rssi; 1471191983Sweongyo } 1472191983Sweongyo 1473191983Sweongyo DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__); 1474191983Sweongyo *rssi = rxdesc->rssi; 1475191983Sweongyo return (m); 1476191983Sweongyo} 1477191983Sweongyo 1478191983Sweongyostatic uint8_t 1479191983Sweongyoupgt_rx_rate(struct upgt_softc *sc, const int rate) 1480191983Sweongyo{ 1481287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 1482191983Sweongyo static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 }; 1483191983Sweongyo static const uint8_t ofdm_upgt2rate[12] = 1484191983Sweongyo { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 }; 1485191983Sweongyo 1486191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11B && 1487191983Sweongyo !(rate < 0 || rate > 3)) 1488191983Sweongyo return cck_upgt2rate[rate & 0xf]; 1489191983Sweongyo 1490191983Sweongyo if (ic->ic_curmode == IEEE80211_MODE_11G && 1491191983Sweongyo !(rate < 0 || rate > 11)) 1492191983Sweongyo return ofdm_upgt2rate[rate & 0xf]; 1493191983Sweongyo 1494191983Sweongyo return (0); 1495191983Sweongyo} 1496191983Sweongyo 1497191983Sweongyostatic void 1498191983Sweongyoupgt_tx_done(struct upgt_softc *sc, uint8_t *data) 1499191983Sweongyo{ 1500191983Sweongyo struct upgt_lmac_tx_done_desc *desc; 1501191983Sweongyo int i, freed = 0; 1502191983Sweongyo 1503191983Sweongyo UPGT_ASSERT_LOCKED(sc); 1504191983Sweongyo 1505191983Sweongyo desc = (struct upgt_lmac_tx_done_desc *)data; 1506191983Sweongyo 1507191983Sweongyo for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 1508191983Sweongyo struct upgt_data *data_tx = &sc->sc_tx_data[i]; 1509191983Sweongyo 1510191983Sweongyo if (data_tx->addr == le32toh(desc->header2.reqid)) { 1511191983Sweongyo upgt_mem_free(sc, data_tx->addr); 1512191983Sweongyo data_tx->ni = NULL; 1513191983Sweongyo data_tx->addr = 0; 1514191983Sweongyo data_tx->m = NULL; 1515191983Sweongyo 1516191983Sweongyo DPRINTF(sc, UPGT_DEBUG_TX_PROC, 1517191983Sweongyo "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ", 1518191983Sweongyo le32toh(desc->header2.reqid), 1519191983Sweongyo le16toh(desc->status), le16toh(desc->rssi)); 1520191983Sweongyo DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n", 1521191983Sweongyo le16toh(desc->seq)); 1522191983Sweongyo 1523191983Sweongyo freed++; 1524191983Sweongyo } 1525191983Sweongyo } 1526191983Sweongyo 1527191983Sweongyo if (freed != 0) { 1528287197Sglebius UPGT_UNLOCK(sc); 1529286437Sadrian sc->sc_tx_timer = 0; 1530287197Sglebius upgt_start(sc); 1531191983Sweongyo UPGT_LOCK(sc); 1532191983Sweongyo } 1533191983Sweongyo} 1534191983Sweongyo 1535191983Sweongyostatic void 1536191983Sweongyoupgt_mem_free(struct upgt_softc *sc, uint32_t addr) 1537191983Sweongyo{ 1538191983Sweongyo int i; 1539191983Sweongyo 1540191983Sweongyo for (i = 0; i < sc->sc_memory.pages; i++) { 1541191983Sweongyo if (sc->sc_memory.page[i].addr == addr) { 1542191983Sweongyo sc->sc_memory.page[i].used = 0; 1543191983Sweongyo return; 1544191983Sweongyo } 1545191983Sweongyo } 1546191983Sweongyo 1547191983Sweongyo device_printf(sc->sc_dev, 1548199816Sthompsa "could not free memory address 0x%08x\n", addr); 1549191983Sweongyo} 1550191983Sweongyo 1551191983Sweongyostatic int 1552191983Sweongyoupgt_fw_load(struct upgt_softc *sc) 1553191983Sweongyo{ 1554191983Sweongyo const struct firmware *fw; 1555191983Sweongyo struct upgt_data *data_cmd; 1556191983Sweongyo struct upgt_fw_x2_header *x2; 1557191983Sweongyo char start_fwload_cmd[] = { 0x3c, 0x0d }; 1558233774Shselasky int error = 0; 1559233774Shselasky size_t offset; 1560233774Shselasky int bsize; 1561233774Shselasky int n; 1562191983Sweongyo uint32_t crc32; 1563191983Sweongyo 1564191983Sweongyo fw = firmware_get(upgt_fwname); 1565191983Sweongyo if (fw == NULL) { 1566199816Sthompsa device_printf(sc->sc_dev, "could not read microcode %s\n", 1567191983Sweongyo upgt_fwname); 1568191983Sweongyo return (EIO); 1569191983Sweongyo } 1570191983Sweongyo 1571191983Sweongyo UPGT_LOCK(sc); 1572191983Sweongyo 1573191983Sweongyo /* send firmware start load command */ 1574191983Sweongyo data_cmd = upgt_getbuf(sc); 1575191983Sweongyo if (data_cmd == NULL) { 1576191983Sweongyo error = ENOBUFS; 1577191983Sweongyo goto fail; 1578191983Sweongyo } 1579191983Sweongyo data_cmd->buflen = sizeof(start_fwload_cmd); 1580227461Shselasky memcpy(data_cmd->buf, start_fwload_cmd, data_cmd->buflen); 1581191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1582191983Sweongyo 1583191983Sweongyo /* send X2 header */ 1584191983Sweongyo data_cmd = upgt_getbuf(sc); 1585191983Sweongyo if (data_cmd == NULL) { 1586191983Sweongyo error = ENOBUFS; 1587191983Sweongyo goto fail; 1588191983Sweongyo } 1589191983Sweongyo data_cmd->buflen = sizeof(struct upgt_fw_x2_header); 1590191983Sweongyo x2 = (struct upgt_fw_x2_header *)data_cmd->buf; 1591227461Shselasky memcpy(x2->signature, UPGT_X2_SIGNATURE, UPGT_X2_SIGNATURE_SIZE); 1592191983Sweongyo x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START); 1593191983Sweongyo x2->len = htole32(fw->datasize); 1594191983Sweongyo x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf + 1595191983Sweongyo UPGT_X2_SIGNATURE_SIZE, 1596191983Sweongyo sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE - 1597191983Sweongyo sizeof(uint32_t)); 1598191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1599191983Sweongyo 1600191983Sweongyo /* download firmware */ 1601191983Sweongyo for (offset = 0; offset < fw->datasize; offset += bsize) { 1602191983Sweongyo if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE) 1603191983Sweongyo bsize = UPGT_FW_BLOCK_SIZE; 1604191983Sweongyo else 1605191983Sweongyo bsize = fw->datasize - offset; 1606191983Sweongyo 1607191983Sweongyo data_cmd = upgt_getbuf(sc); 1608191983Sweongyo if (data_cmd == NULL) { 1609191983Sweongyo error = ENOBUFS; 1610191983Sweongyo goto fail; 1611191983Sweongyo } 1612191983Sweongyo n = upgt_fw_copy((const uint8_t *)fw->data + offset, 1613191983Sweongyo data_cmd->buf, bsize); 1614191983Sweongyo data_cmd->buflen = bsize; 1615191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1616191983Sweongyo 1617191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n", 1618191983Sweongyo offset, n, bsize); 1619191983Sweongyo bsize = n; 1620191983Sweongyo } 1621191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__); 1622191983Sweongyo 1623191983Sweongyo /* load firmware */ 1624191983Sweongyo data_cmd = upgt_getbuf(sc); 1625191983Sweongyo if (data_cmd == NULL) { 1626191983Sweongyo error = ENOBUFS; 1627191983Sweongyo goto fail; 1628191983Sweongyo } 1629191983Sweongyo crc32 = upgt_crc32_le(fw->data, fw->datasize); 1630191983Sweongyo *((uint32_t *)(data_cmd->buf) ) = crc32; 1631191983Sweongyo *((uint8_t *)(data_cmd->buf) + 4) = 'g'; 1632191983Sweongyo *((uint8_t *)(data_cmd->buf) + 5) = '\r'; 1633191983Sweongyo data_cmd->buflen = 6; 1634191983Sweongyo upgt_bulk_tx(sc, data_cmd); 1635191983Sweongyo 1636191983Sweongyo /* waiting 'OK' response. */ 1637194228Sthompsa usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]); 1638191983Sweongyo error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz); 1639191983Sweongyo if (error != 0) { 1640199816Sthompsa device_printf(sc->sc_dev, "firmware load failed\n"); 1641191983Sweongyo error = EIO; 1642191983Sweongyo } 1643191983Sweongyo 1644191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__); 1645191983Sweongyofail: 1646191983Sweongyo UPGT_UNLOCK(sc); 1647191983Sweongyo firmware_put(fw, FIRMWARE_UNLOAD); 1648191983Sweongyo return (error); 1649191983Sweongyo} 1650191983Sweongyo 1651191983Sweongyostatic uint32_t 1652191983Sweongyoupgt_crc32_le(const void *buf, size_t size) 1653191983Sweongyo{ 1654191983Sweongyo uint32_t crc; 1655191983Sweongyo 1656191983Sweongyo crc = ether_crc32_le(buf, size); 1657191983Sweongyo 1658191983Sweongyo /* apply final XOR value as common for CRC-32 */ 1659191983Sweongyo crc = htole32(crc ^ 0xffffffffU); 1660191983Sweongyo 1661191983Sweongyo return (crc); 1662191983Sweongyo} 1663191983Sweongyo 1664191983Sweongyo/* 1665191983Sweongyo * While copying the version 2 firmware, we need to replace two characters: 1666191983Sweongyo * 1667191983Sweongyo * 0x7e -> 0x7d 0x5e 1668191983Sweongyo * 0x7d -> 0x7d 0x5d 1669191983Sweongyo */ 1670191983Sweongyostatic int 1671191983Sweongyoupgt_fw_copy(const uint8_t *src, char *dst, int size) 1672191983Sweongyo{ 1673191983Sweongyo int i, j; 1674191983Sweongyo 1675191983Sweongyo for (i = 0, j = 0; i < size && j < size; i++) { 1676191983Sweongyo switch (src[i]) { 1677191983Sweongyo case 0x7e: 1678191983Sweongyo dst[j] = 0x7d; 1679191983Sweongyo j++; 1680191983Sweongyo dst[j] = 0x5e; 1681191983Sweongyo j++; 1682191983Sweongyo break; 1683191983Sweongyo case 0x7d: 1684191983Sweongyo dst[j] = 0x7d; 1685191983Sweongyo j++; 1686191983Sweongyo dst[j] = 0x5d; 1687191983Sweongyo j++; 1688191983Sweongyo break; 1689191983Sweongyo default: 1690191983Sweongyo dst[j] = src[i]; 1691191983Sweongyo j++; 1692191983Sweongyo break; 1693191983Sweongyo } 1694191983Sweongyo } 1695191983Sweongyo 1696191983Sweongyo return (i); 1697191983Sweongyo} 1698191983Sweongyo 1699191983Sweongyostatic int 1700191983Sweongyoupgt_mem_init(struct upgt_softc *sc) 1701191983Sweongyo{ 1702191983Sweongyo int i; 1703191983Sweongyo 1704191983Sweongyo for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) { 1705191983Sweongyo sc->sc_memory.page[i].used = 0; 1706191983Sweongyo 1707191983Sweongyo if (i == 0) { 1708191983Sweongyo /* 1709191983Sweongyo * The first memory page is always reserved for 1710191983Sweongyo * command data. 1711191983Sweongyo */ 1712191983Sweongyo sc->sc_memory.page[i].addr = 1713191983Sweongyo sc->sc_memaddr_frame_start + MCLBYTES; 1714191983Sweongyo } else { 1715191983Sweongyo sc->sc_memory.page[i].addr = 1716191983Sweongyo sc->sc_memory.page[i - 1].addr + MCLBYTES; 1717191983Sweongyo } 1718191983Sweongyo 1719191983Sweongyo if (sc->sc_memory.page[i].addr + MCLBYTES >= 1720191983Sweongyo sc->sc_memaddr_frame_end) 1721191983Sweongyo break; 1722191983Sweongyo 1723191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n", 1724191983Sweongyo i, sc->sc_memory.page[i].addr); 1725191983Sweongyo } 1726191983Sweongyo 1727191983Sweongyo sc->sc_memory.pages = i; 1728191983Sweongyo 1729191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages); 1730191983Sweongyo return (0); 1731191983Sweongyo} 1732191983Sweongyo 1733191983Sweongyostatic int 1734191983Sweongyoupgt_fw_verify(struct upgt_softc *sc) 1735191983Sweongyo{ 1736191983Sweongyo const struct firmware *fw; 1737191983Sweongyo const struct upgt_fw_bra_option *bra_opt; 1738191983Sweongyo const struct upgt_fw_bra_descr *descr; 1739191983Sweongyo const uint8_t *p; 1740191983Sweongyo const uint32_t *uc; 1741191983Sweongyo uint32_t bra_option_type, bra_option_len; 1742233774Shselasky size_t offset; 1743233774Shselasky int bra_end = 0; 1744233774Shselasky int error = 0; 1745191983Sweongyo 1746191983Sweongyo fw = firmware_get(upgt_fwname); 1747191983Sweongyo if (fw == NULL) { 1748199816Sthompsa device_printf(sc->sc_dev, "could not read microcode %s\n", 1749191983Sweongyo upgt_fwname); 1750191983Sweongyo return EIO; 1751191983Sweongyo } 1752191983Sweongyo 1753191983Sweongyo /* 1754191983Sweongyo * Seek to beginning of Boot Record Area (BRA). 1755191983Sweongyo */ 1756191983Sweongyo for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) { 1757191983Sweongyo uc = (const uint32_t *)((const uint8_t *)fw->data + offset); 1758191983Sweongyo if (*uc == 0) 1759191983Sweongyo break; 1760191983Sweongyo } 1761191983Sweongyo for (; offset < fw->datasize; offset += sizeof(*uc)) { 1762191983Sweongyo uc = (const uint32_t *)((const uint8_t *)fw->data + offset); 1763191983Sweongyo if (*uc != 0) 1764191983Sweongyo break; 1765191983Sweongyo } 1766191983Sweongyo if (offset == fw->datasize) { 1767191983Sweongyo device_printf(sc->sc_dev, 1768199816Sthompsa "firmware Boot Record Area not found\n"); 1769191983Sweongyo error = EIO; 1770191983Sweongyo goto fail; 1771191983Sweongyo } 1772191983Sweongyo 1773191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1774191983Sweongyo "firmware Boot Record Area found at offset %d\n", offset); 1775191983Sweongyo 1776191983Sweongyo /* 1777191983Sweongyo * Parse Boot Record Area (BRA) options. 1778191983Sweongyo */ 1779191983Sweongyo while (offset < fw->datasize && bra_end == 0) { 1780191983Sweongyo /* get current BRA option */ 1781191983Sweongyo p = (const uint8_t *)fw->data + offset; 1782191983Sweongyo bra_opt = (const struct upgt_fw_bra_option *)p; 1783191983Sweongyo bra_option_type = le32toh(bra_opt->type); 1784191983Sweongyo bra_option_len = le32toh(bra_opt->len) * sizeof(*uc); 1785191983Sweongyo 1786191983Sweongyo switch (bra_option_type) { 1787191983Sweongyo case UPGT_BRA_TYPE_FW: 1788191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n", 1789191983Sweongyo bra_option_len); 1790191983Sweongyo 1791191983Sweongyo if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) { 1792191983Sweongyo device_printf(sc->sc_dev, 1793199816Sthompsa "wrong UPGT_BRA_TYPE_FW len\n"); 1794191983Sweongyo error = EIO; 1795191983Sweongyo goto fail; 1796191983Sweongyo } 1797191983Sweongyo if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data, 1798191983Sweongyo bra_option_len) == 0) { 1799191983Sweongyo sc->sc_fw_type = UPGT_FWTYPE_LM86; 1800191983Sweongyo break; 1801191983Sweongyo } 1802191983Sweongyo if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data, 1803191983Sweongyo bra_option_len) == 0) { 1804191983Sweongyo sc->sc_fw_type = UPGT_FWTYPE_LM87; 1805191983Sweongyo break; 1806191983Sweongyo } 1807191983Sweongyo device_printf(sc->sc_dev, 1808199816Sthompsa "unsupported firmware type\n"); 1809191983Sweongyo error = EIO; 1810191983Sweongyo goto fail; 1811191983Sweongyo case UPGT_BRA_TYPE_VERSION: 1812191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1813191983Sweongyo "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len); 1814191983Sweongyo break; 1815191983Sweongyo case UPGT_BRA_TYPE_DEPIF: 1816191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1817191983Sweongyo "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len); 1818191983Sweongyo break; 1819191983Sweongyo case UPGT_BRA_TYPE_EXPIF: 1820191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1821191983Sweongyo "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len); 1822191983Sweongyo break; 1823191983Sweongyo case UPGT_BRA_TYPE_DESCR: 1824191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1825191983Sweongyo "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len); 1826191983Sweongyo 1827191983Sweongyo descr = (const struct upgt_fw_bra_descr *)bra_opt->data; 1828191983Sweongyo 1829191983Sweongyo sc->sc_memaddr_frame_start = 1830191983Sweongyo le32toh(descr->memaddr_space_start); 1831191983Sweongyo sc->sc_memaddr_frame_end = 1832191983Sweongyo le32toh(descr->memaddr_space_end); 1833191983Sweongyo 1834191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1835191983Sweongyo "memory address space start=0x%08x\n", 1836191983Sweongyo sc->sc_memaddr_frame_start); 1837191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, 1838191983Sweongyo "memory address space end=0x%08x\n", 1839191983Sweongyo sc->sc_memaddr_frame_end); 1840191983Sweongyo break; 1841191983Sweongyo case UPGT_BRA_TYPE_END: 1842191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n", 1843191983Sweongyo bra_option_len); 1844191983Sweongyo bra_end = 1; 1845191983Sweongyo break; 1846191983Sweongyo default: 1847191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n", 1848191983Sweongyo bra_option_len); 1849191983Sweongyo error = EIO; 1850191983Sweongyo goto fail; 1851191983Sweongyo } 1852191983Sweongyo 1853191983Sweongyo /* jump to next BRA option */ 1854191983Sweongyo offset += sizeof(struct upgt_fw_bra_option) + bra_option_len; 1855191983Sweongyo } 1856191983Sweongyo 1857191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__); 1858191983Sweongyofail: 1859191983Sweongyo firmware_put(fw, FIRMWARE_UNLOAD); 1860191983Sweongyo return (error); 1861191983Sweongyo} 1862191983Sweongyo 1863191983Sweongyostatic void 1864191983Sweongyoupgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data) 1865191983Sweongyo{ 1866191983Sweongyo 1867191983Sweongyo UPGT_ASSERT_LOCKED(sc); 1868191983Sweongyo 1869191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next); 1870191983Sweongyo UPGT_STAT_INC(sc, st_tx_pending); 1871194228Sthompsa usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]); 1872191983Sweongyo} 1873191983Sweongyo 1874191983Sweongyostatic int 1875191983Sweongyoupgt_device_reset(struct upgt_softc *sc) 1876191983Sweongyo{ 1877191983Sweongyo struct upgt_data *data; 1878191983Sweongyo char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e }; 1879191983Sweongyo 1880191983Sweongyo UPGT_LOCK(sc); 1881191983Sweongyo 1882191983Sweongyo data = upgt_getbuf(sc); 1883191983Sweongyo if (data == NULL) { 1884191983Sweongyo UPGT_UNLOCK(sc); 1885191983Sweongyo return (ENOBUFS); 1886191983Sweongyo } 1887227461Shselasky memcpy(data->buf, init_cmd, sizeof(init_cmd)); 1888191983Sweongyo data->buflen = sizeof(init_cmd); 1889191983Sweongyo upgt_bulk_tx(sc, data); 1890194228Sthompsa usb_pause_mtx(&sc->sc_mtx, 100); 1891191983Sweongyo 1892191983Sweongyo UPGT_UNLOCK(sc); 1893191983Sweongyo DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__); 1894191983Sweongyo return (0); 1895191983Sweongyo} 1896191983Sweongyo 1897191983Sweongyostatic int 1898191983Sweongyoupgt_alloc_tx(struct upgt_softc *sc) 1899191983Sweongyo{ 1900191983Sweongyo int i; 1901191983Sweongyo 1902191983Sweongyo STAILQ_INIT(&sc->sc_tx_active); 1903191983Sweongyo STAILQ_INIT(&sc->sc_tx_inactive); 1904191983Sweongyo STAILQ_INIT(&sc->sc_tx_pending); 1905191983Sweongyo 1906191983Sweongyo for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 1907191983Sweongyo struct upgt_data *data = &sc->sc_tx_data[i]; 1908244503Shselasky data->buf = ((uint8_t *)sc->sc_tx_dma_buf) + (i * MCLBYTES); 1909191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next); 1910191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 1911191983Sweongyo } 1912191983Sweongyo 1913191983Sweongyo return (0); 1914191983Sweongyo} 1915191983Sweongyo 1916191983Sweongyostatic int 1917191983Sweongyoupgt_alloc_rx(struct upgt_softc *sc) 1918191983Sweongyo{ 1919191983Sweongyo int i; 1920191983Sweongyo 1921191983Sweongyo STAILQ_INIT(&sc->sc_rx_active); 1922191983Sweongyo STAILQ_INIT(&sc->sc_rx_inactive); 1923191983Sweongyo 1924191983Sweongyo for (i = 0; i < UPGT_RX_MAXCOUNT; i++) { 1925191983Sweongyo struct upgt_data *data = &sc->sc_rx_data[i]; 1926244503Shselasky data->buf = ((uint8_t *)sc->sc_rx_dma_buf) + (i * MCLBYTES); 1927191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 1928191983Sweongyo } 1929191983Sweongyo return (0); 1930191983Sweongyo} 1931191983Sweongyo 1932191983Sweongyostatic int 1933191983Sweongyoupgt_detach(device_t dev) 1934191983Sweongyo{ 1935191983Sweongyo struct upgt_softc *sc = device_get_softc(dev); 1936287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 1937246614Shselasky unsigned int x; 1938191983Sweongyo 1939246614Shselasky /* 1940246614Shselasky * Prevent further allocations from RX/TX/CMD 1941246614Shselasky * data lists and ioctls 1942246614Shselasky */ 1943246614Shselasky UPGT_LOCK(sc); 1944246614Shselasky sc->sc_flags |= UPGT_FLAG_DETACHED; 1945191983Sweongyo 1946246614Shselasky STAILQ_INIT(&sc->sc_tx_active); 1947246614Shselasky STAILQ_INIT(&sc->sc_tx_inactive); 1948246614Shselasky STAILQ_INIT(&sc->sc_tx_pending); 1949246614Shselasky 1950246614Shselasky STAILQ_INIT(&sc->sc_rx_active); 1951246614Shselasky STAILQ_INIT(&sc->sc_rx_inactive); 1952246614Shselasky 1953191983Sweongyo upgt_stop(sc); 1954287197Sglebius UPGT_UNLOCK(sc); 1955191983Sweongyo 1956191983Sweongyo callout_drain(&sc->sc_led_ch); 1957191983Sweongyo callout_drain(&sc->sc_watchdog_ch); 1958191983Sweongyo 1959246614Shselasky /* drain USB transfers */ 1960246614Shselasky for (x = 0; x != UPGT_N_XFERS; x++) 1961246614Shselasky usbd_transfer_drain(sc->sc_xfer[x]); 1962244503Shselasky 1963246614Shselasky /* free data buffers */ 1964246614Shselasky UPGT_LOCK(sc); 1965191983Sweongyo upgt_free_rx(sc); 1966191983Sweongyo upgt_free_tx(sc); 1967246614Shselasky UPGT_UNLOCK(sc); 1968191983Sweongyo 1969246614Shselasky /* free USB transfers and some data buffers */ 1970246614Shselasky usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS); 1971246614Shselasky 1972246614Shselasky ieee80211_ifdetach(ic); 1973287197Sglebius mbufq_drain(&sc->sc_snd); 1974191983Sweongyo mtx_destroy(&sc->sc_mtx); 1975191983Sweongyo 1976191983Sweongyo return (0); 1977191983Sweongyo} 1978191983Sweongyo 1979191983Sweongyostatic void 1980191983Sweongyoupgt_free_rx(struct upgt_softc *sc) 1981191983Sweongyo{ 1982191983Sweongyo int i; 1983191983Sweongyo 1984191983Sweongyo for (i = 0; i < UPGT_RX_MAXCOUNT; i++) { 1985191983Sweongyo struct upgt_data *data = &sc->sc_rx_data[i]; 1986191983Sweongyo 1987244503Shselasky data->buf = NULL; 1988191983Sweongyo data->ni = NULL; 1989191983Sweongyo } 1990191983Sweongyo} 1991191983Sweongyo 1992191983Sweongyostatic void 1993191983Sweongyoupgt_free_tx(struct upgt_softc *sc) 1994191983Sweongyo{ 1995191983Sweongyo int i; 1996191983Sweongyo 1997191983Sweongyo for (i = 0; i < UPGT_TX_MAXCOUNT; i++) { 1998191983Sweongyo struct upgt_data *data = &sc->sc_tx_data[i]; 1999191983Sweongyo 2000246614Shselasky if (data->ni != NULL) 2001246614Shselasky ieee80211_free_node(data->ni); 2002246614Shselasky 2003244503Shselasky data->buf = NULL; 2004191983Sweongyo data->ni = NULL; 2005191983Sweongyo } 2006191983Sweongyo} 2007191983Sweongyo 2008191983Sweongyostatic void 2009191983Sweongyoupgt_abort_xfers_locked(struct upgt_softc *sc) 2010191983Sweongyo{ 2011191983Sweongyo int i; 2012191983Sweongyo 2013191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2014191983Sweongyo /* abort any pending transfers */ 2015191983Sweongyo for (i = 0; i < UPGT_N_XFERS; i++) 2016194228Sthompsa usbd_transfer_stop(sc->sc_xfer[i]); 2017191983Sweongyo} 2018191983Sweongyo 2019191983Sweongyostatic void 2020191983Sweongyoupgt_abort_xfers(struct upgt_softc *sc) 2021191983Sweongyo{ 2022191983Sweongyo 2023191983Sweongyo UPGT_LOCK(sc); 2024191983Sweongyo upgt_abort_xfers_locked(sc); 2025191983Sweongyo UPGT_UNLOCK(sc); 2026191983Sweongyo} 2027191983Sweongyo 2028191983Sweongyo#define UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d) \ 2029191983Sweongyo SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d) 2030191983Sweongyo 2031191983Sweongyostatic void 2032191983Sweongyoupgt_sysctl_node(struct upgt_softc *sc) 2033191983Sweongyo{ 2034191983Sweongyo struct sysctl_ctx_list *ctx; 2035191983Sweongyo struct sysctl_oid_list *child; 2036191983Sweongyo struct sysctl_oid *tree; 2037191983Sweongyo struct upgt_stat *stats; 2038191983Sweongyo 2039191983Sweongyo stats = &sc->sc_stat; 2040191983Sweongyo ctx = device_get_sysctl_ctx(sc->sc_dev); 2041191983Sweongyo child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev)); 2042191983Sweongyo 2043191983Sweongyo tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD, 2044191983Sweongyo NULL, "UPGT statistics"); 2045191983Sweongyo child = SYSCTL_CHILDREN(tree); 2046191983Sweongyo UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active", 2047191983Sweongyo &stats->st_tx_active, "Active numbers in TX queue"); 2048191983Sweongyo UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive", 2049191983Sweongyo &stats->st_tx_inactive, "Inactive numbers in TX queue"); 2050191983Sweongyo UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending", 2051191983Sweongyo &stats->st_tx_pending, "Pending numbers in TX queue"); 2052191983Sweongyo} 2053191983Sweongyo 2054191983Sweongyo#undef UPGT_SYSCTL_STAT_ADD32 2055191983Sweongyo 2056191983Sweongyostatic struct upgt_data * 2057191983Sweongyo_upgt_getbuf(struct upgt_softc *sc) 2058191983Sweongyo{ 2059191983Sweongyo struct upgt_data *bf; 2060191983Sweongyo 2061191983Sweongyo bf = STAILQ_FIRST(&sc->sc_tx_inactive); 2062191983Sweongyo if (bf != NULL) { 2063191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next); 2064191983Sweongyo UPGT_STAT_DEC(sc, st_tx_inactive); 2065191983Sweongyo } else 2066191983Sweongyo bf = NULL; 2067191983Sweongyo if (bf == NULL) 2068191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__, 2069191983Sweongyo "out of xmit buffers"); 2070191983Sweongyo return (bf); 2071191983Sweongyo} 2072191983Sweongyo 2073191983Sweongyostatic struct upgt_data * 2074191983Sweongyoupgt_getbuf(struct upgt_softc *sc) 2075191983Sweongyo{ 2076191983Sweongyo struct upgt_data *bf; 2077191983Sweongyo 2078191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2079191983Sweongyo 2080191983Sweongyo bf = _upgt_getbuf(sc); 2081287197Sglebius if (bf == NULL) 2082191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__); 2083191983Sweongyo 2084191983Sweongyo return (bf); 2085191983Sweongyo} 2086191983Sweongyo 2087191983Sweongyostatic struct upgt_data * 2088191983Sweongyoupgt_gettxbuf(struct upgt_softc *sc) 2089191983Sweongyo{ 2090191983Sweongyo struct upgt_data *bf; 2091191983Sweongyo 2092191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2093191983Sweongyo 2094191983Sweongyo bf = upgt_getbuf(sc); 2095191983Sweongyo if (bf == NULL) 2096191983Sweongyo return (NULL); 2097191983Sweongyo 2098191983Sweongyo bf->addr = upgt_mem_alloc(sc); 2099191983Sweongyo if (bf->addr == 0) { 2100191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n", 2101191983Sweongyo __func__); 2102191983Sweongyo STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next); 2103191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 2104191983Sweongyo return (NULL); 2105191983Sweongyo } 2106191983Sweongyo return (bf); 2107191983Sweongyo} 2108191983Sweongyo 2109191983Sweongyostatic int 2110191983Sweongyoupgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni, 2111191983Sweongyo struct upgt_data *data) 2112191983Sweongyo{ 2113192468Ssam struct ieee80211vap *vap = ni->ni_vap; 2114191983Sweongyo int error = 0, len; 2115191983Sweongyo struct ieee80211_frame *wh; 2116191983Sweongyo struct ieee80211_key *k; 2117191983Sweongyo struct upgt_lmac_mem *mem; 2118191983Sweongyo struct upgt_lmac_tx_desc *txdesc; 2119191983Sweongyo 2120191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2121191983Sweongyo 2122191983Sweongyo upgt_set_led(sc, UPGT_LED_BLINK); 2123191983Sweongyo 2124191983Sweongyo /* 2125191983Sweongyo * Software crypto. 2126191983Sweongyo */ 2127191983Sweongyo wh = mtod(m, struct ieee80211_frame *); 2128260444Skevlo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2129191983Sweongyo k = ieee80211_crypto_encap(ni, m); 2130191983Sweongyo if (k == NULL) { 2131191983Sweongyo device_printf(sc->sc_dev, 2132191983Sweongyo "ieee80211_crypto_encap returns NULL.\n"); 2133191983Sweongyo error = EIO; 2134191983Sweongyo goto done; 2135191983Sweongyo } 2136191983Sweongyo 2137191983Sweongyo /* in case packet header moved, reset pointer */ 2138191983Sweongyo wh = mtod(m, struct ieee80211_frame *); 2139191983Sweongyo } 2140191983Sweongyo 2141191983Sweongyo /* Transmit the URB containing the TX data. */ 2142227461Shselasky memset(data->buf, 0, MCLBYTES); 2143191983Sweongyo mem = (struct upgt_lmac_mem *)data->buf; 2144191983Sweongyo mem->addr = htole32(data->addr); 2145191983Sweongyo txdesc = (struct upgt_lmac_tx_desc *)(mem + 1); 2146191983Sweongyo 2147191983Sweongyo if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2148191983Sweongyo IEEE80211_FC0_TYPE_MGT) { 2149191983Sweongyo /* mgmt frames */ 2150191983Sweongyo txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT; 2151191983Sweongyo /* always send mgmt frames at lowest rate (DS1) */ 2152191983Sweongyo memset(txdesc->rates, 0x10, sizeof(txdesc->rates)); 2153191983Sweongyo } else { 2154191983Sweongyo /* data frames */ 2155191983Sweongyo txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA; 2156227461Shselasky memcpy(txdesc->rates, sc->sc_cur_rateset, sizeof(txdesc->rates)); 2157191983Sweongyo } 2158191983Sweongyo txdesc->header1.type = UPGT_H1_TYPE_TX_DATA; 2159191983Sweongyo txdesc->header1.len = htole16(m->m_pkthdr.len); 2160191983Sweongyo txdesc->header2.reqid = htole32(data->addr); 2161191983Sweongyo txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES); 2162191983Sweongyo txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES); 2163191983Sweongyo txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA); 2164191983Sweongyo txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE; 2165191983Sweongyo 2166192468Ssam if (ieee80211_radiotap_active_vap(vap)) { 2167191983Sweongyo struct upgt_tx_radiotap_header *tap = &sc->sc_txtap; 2168191983Sweongyo 2169191983Sweongyo tap->wt_flags = 0; 2170191983Sweongyo tap->wt_rate = 0; /* XXX where to get from? */ 2171191983Sweongyo 2172192468Ssam ieee80211_radiotap_tx(vap, m); 2173191983Sweongyo } 2174191983Sweongyo 2175191983Sweongyo /* copy frame below our TX descriptor header */ 2176191983Sweongyo m_copydata(m, 0, m->m_pkthdr.len, 2177191983Sweongyo data->buf + (sizeof(*mem) + sizeof(*txdesc))); 2178191983Sweongyo /* calculate frame size */ 2179191983Sweongyo len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len; 2180191983Sweongyo /* we need to align the frame to a 4 byte boundary */ 2181191983Sweongyo len = (len + 3) & ~3; 2182191983Sweongyo /* calculate frame checksum */ 2183191983Sweongyo mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem)); 2184191983Sweongyo data->ni = ni; 2185191983Sweongyo data->m = m; 2186191983Sweongyo data->buflen = len; 2187191983Sweongyo 2188191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n", 2189191983Sweongyo __func__, len); 2190191983Sweongyo KASSERT(len <= MCLBYTES, ("mbuf is small for saving data")); 2191191983Sweongyo 2192191983Sweongyo upgt_bulk_tx(sc, data); 2193191983Sweongyodone: 2194191983Sweongyo /* 2195191983Sweongyo * If we don't regulary read the device statistics, the RX queue 2196191983Sweongyo * will stall. It's strange, but it works, so we keep reading 2197191983Sweongyo * the statistics here. *shrug* 2198191983Sweongyo */ 2199287197Sglebius if (!(vap->iv_ifp->if_get_counter(vap->iv_ifp, IFCOUNTER_OPACKETS) % 2200271866Sglebius UPGT_TX_STAT_INTERVAL)) 2201191983Sweongyo upgt_get_stats(sc); 2202191983Sweongyo 2203191983Sweongyo return (error); 2204191983Sweongyo} 2205191983Sweongyo 2206191983Sweongyostatic void 2207194677Sthompsaupgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2208191983Sweongyo{ 2209194677Sthompsa struct upgt_softc *sc = usbd_xfer_softc(xfer); 2210287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 2211191983Sweongyo struct ieee80211_frame *wh; 2212191983Sweongyo struct ieee80211_node *ni; 2213191983Sweongyo struct mbuf *m = NULL; 2214191983Sweongyo struct upgt_data *data; 2215191983Sweongyo int8_t nf; 2216191983Sweongyo int rssi = -1; 2217191983Sweongyo 2218191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2219191983Sweongyo 2220191983Sweongyo switch (USB_GET_STATE(xfer)) { 2221191983Sweongyo case USB_ST_TRANSFERRED: 2222191983Sweongyo data = STAILQ_FIRST(&sc->sc_rx_active); 2223191983Sweongyo if (data == NULL) 2224191983Sweongyo goto setup; 2225191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2226191983Sweongyo m = upgt_rxeof(xfer, data, &rssi); 2227191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2228191983Sweongyo /* FALLTHROUGH */ 2229191983Sweongyo case USB_ST_SETUP: 2230191983Sweongyosetup: 2231191983Sweongyo data = STAILQ_FIRST(&sc->sc_rx_inactive); 2232191983Sweongyo if (data == NULL) 2233191983Sweongyo return; 2234191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next); 2235191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next); 2236244503Shselasky usbd_xfer_set_frame_data(xfer, 0, data->buf, MCLBYTES); 2237194228Sthompsa usbd_transfer_submit(xfer); 2238191983Sweongyo 2239191983Sweongyo /* 2240191983Sweongyo * To avoid LOR we should unlock our private mutex here to call 2241191983Sweongyo * ieee80211_input() because here is at the end of a USB 2242191983Sweongyo * callback and safe to unlock. 2243191983Sweongyo */ 2244191983Sweongyo UPGT_UNLOCK(sc); 2245191983Sweongyo if (m != NULL) { 2246191983Sweongyo wh = mtod(m, struct ieee80211_frame *); 2247191983Sweongyo ni = ieee80211_find_rxnode(ic, 2248191983Sweongyo (struct ieee80211_frame_min *)wh); 2249191983Sweongyo nf = -95; /* XXX */ 2250191983Sweongyo if (ni != NULL) { 2251192468Ssam (void) ieee80211_input(ni, m, rssi, nf); 2252191983Sweongyo /* node is no longer needed */ 2253191983Sweongyo ieee80211_free_node(ni); 2254191983Sweongyo } else 2255192468Ssam (void) ieee80211_input_all(ic, m, rssi, nf); 2256191983Sweongyo m = NULL; 2257191983Sweongyo } 2258191983Sweongyo UPGT_LOCK(sc); 2259287197Sglebius upgt_start(sc); 2260191983Sweongyo break; 2261191983Sweongyo default: 2262191983Sweongyo /* needs it to the inactive queue due to a error. */ 2263191983Sweongyo data = STAILQ_FIRST(&sc->sc_rx_active); 2264191983Sweongyo if (data != NULL) { 2265191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2266191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2267191983Sweongyo } 2268194677Sthompsa if (error != USB_ERR_CANCELLED) { 2269194677Sthompsa usbd_xfer_set_stall(xfer); 2270287197Sglebius counter_u64_add(ic->ic_ierrors, 1); 2271191983Sweongyo goto setup; 2272191983Sweongyo } 2273191983Sweongyo break; 2274191983Sweongyo } 2275191983Sweongyo} 2276191983Sweongyo 2277191983Sweongyostatic void 2278194677Sthompsaupgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error) 2279191983Sweongyo{ 2280194677Sthompsa struct upgt_softc *sc = usbd_xfer_softc(xfer); 2281191983Sweongyo struct upgt_data *data; 2282191983Sweongyo 2283191983Sweongyo UPGT_ASSERT_LOCKED(sc); 2284191983Sweongyo switch (USB_GET_STATE(xfer)) { 2285191983Sweongyo case USB_ST_TRANSFERRED: 2286191983Sweongyo data = STAILQ_FIRST(&sc->sc_tx_active); 2287191983Sweongyo if (data == NULL) 2288191983Sweongyo goto setup; 2289191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next); 2290191983Sweongyo UPGT_STAT_DEC(sc, st_tx_active); 2291191983Sweongyo upgt_txeof(xfer, data); 2292191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next); 2293191983Sweongyo UPGT_STAT_INC(sc, st_tx_inactive); 2294191983Sweongyo /* FALLTHROUGH */ 2295191983Sweongyo case USB_ST_SETUP: 2296191983Sweongyosetup: 2297191983Sweongyo data = STAILQ_FIRST(&sc->sc_tx_pending); 2298191983Sweongyo if (data == NULL) { 2299191983Sweongyo DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n", 2300191983Sweongyo __func__); 2301191983Sweongyo return; 2302191983Sweongyo } 2303191983Sweongyo STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next); 2304191983Sweongyo UPGT_STAT_DEC(sc, st_tx_pending); 2305191983Sweongyo STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next); 2306191983Sweongyo UPGT_STAT_INC(sc, st_tx_active); 2307191983Sweongyo 2308194677Sthompsa usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen); 2309194228Sthompsa usbd_transfer_submit(xfer); 2310287197Sglebius upgt_start(sc); 2311191983Sweongyo break; 2312191983Sweongyo default: 2313191983Sweongyo data = STAILQ_FIRST(&sc->sc_tx_active); 2314191983Sweongyo if (data == NULL) 2315191983Sweongyo goto setup; 2316191983Sweongyo if (data->ni != NULL) { 2317287197Sglebius if_inc_counter(data->ni->ni_vap->iv_ifp, 2318287197Sglebius IFCOUNTER_OERRORS, 1); 2319191983Sweongyo ieee80211_free_node(data->ni); 2320191983Sweongyo data->ni = NULL; 2321191983Sweongyo } 2322194677Sthompsa if (error != USB_ERR_CANCELLED) { 2323194677Sthompsa usbd_xfer_set_stall(xfer); 2324191983Sweongyo goto setup; 2325191983Sweongyo } 2326191983Sweongyo break; 2327191983Sweongyo } 2328191983Sweongyo} 2329191983Sweongyo 2330191983Sweongyostatic device_method_t upgt_methods[] = { 2331191983Sweongyo /* Device interface */ 2332191983Sweongyo DEVMETHOD(device_probe, upgt_match), 2333191983Sweongyo DEVMETHOD(device_attach, upgt_attach), 2334191983Sweongyo DEVMETHOD(device_detach, upgt_detach), 2335244503Shselasky DEVMETHOD_END 2336191983Sweongyo}; 2337191983Sweongyo 2338191983Sweongyostatic driver_t upgt_driver = { 2339233774Shselasky .name = "upgt", 2340233774Shselasky .methods = upgt_methods, 2341233774Shselasky .size = sizeof(struct upgt_softc) 2342191983Sweongyo}; 2343191983Sweongyo 2344191983Sweongyostatic devclass_t upgt_devclass; 2345191983Sweongyo 2346191983SweongyoDRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0); 2347191983SweongyoMODULE_VERSION(if_upgt, 1); 2348191983SweongyoMODULE_DEPEND(if_upgt, usb, 1, 1, 1); 2349191983SweongyoMODULE_DEPEND(if_upgt, wlan, 1, 1, 1); 2350191983SweongyoMODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1); 2351292080SimpUSB_PNP_HOST_INFO(upgt_devs); 2352